blob: d6e31dae9d13669f555b0806c18b3c320a183cdf [file] [log] [blame]
Evan Cheng09e8ca82008-10-20 21:44:59 +00001//===-- PreAllocSplitting.cpp - Pre-allocation Interval Spltting Pass. ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the machine instruction level pre-register allocation
11// live interval splitting pass. It finds live interval barriers, i.e.
12// instructions which will kill all physical registers in certain register
13// classes, and split all live intervals which cross the barrier.
14//
15//===----------------------------------------------------------------------===//
16
17#define DEBUG_TYPE "pre-alloc-split"
Owen Anderson420dd372009-03-14 21:40:05 +000018#include "VirtRegMap.h"
Lang Hamesa937f222009-12-14 06:49:42 +000019#include "llvm/CodeGen/CalcSpillWeights.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000020#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000021#include "llvm/CodeGen/LiveStackAnalysis.h"
Owen Andersonf1f75b12008-11-04 22:22:41 +000022#include "llvm/CodeGen/MachineDominators.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineLoopInfo.h"
26#include "llvm/CodeGen/MachineRegisterInfo.h"
27#include "llvm/CodeGen/Passes.h"
28#include "llvm/CodeGen/RegisterCoalescer.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000029#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000030#include "llvm/Target/TargetMachine.h"
31#include "llvm/Target/TargetOptions.h"
32#include "llvm/Target/TargetRegisterInfo.h"
33#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000035#include "llvm/Support/ErrorHandling.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000036#include "llvm/ADT/DenseMap.h"
Evan Cheng54898932008-10-29 08:39:34 +000037#include "llvm/ADT/DepthFirstIterator.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000038#include "llvm/ADT/SmallPtrSet.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000039#include "llvm/ADT/Statistic.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000040using namespace llvm;
41
Evan Chengae7fa5b2008-10-28 01:48:24 +000042static cl::opt<int> PreSplitLimit("pre-split-limit", cl::init(-1), cl::Hidden);
Evan Cheng63040ce2009-11-09 06:49:22 +000043static cl::opt<int> DeadSplitLimit("dead-split-limit", cl::init(-1),
44 cl::Hidden);
45static cl::opt<int> RestoreFoldLimit("restore-fold-limit", cl::init(-1),
46 cl::Hidden);
Evan Chengae7fa5b2008-10-28 01:48:24 +000047
Owen Anderson45e68552009-01-29 05:28:55 +000048STATISTIC(NumSplits, "Number of intervals split");
Owen Anderson75fa96b2008-11-19 04:28:29 +000049STATISTIC(NumRemats, "Number of intervals split by rematerialization");
Owen Anderson7b9d67c2008-12-02 18:53:47 +000050STATISTIC(NumFolds, "Number of intervals split with spill folding");
Owen Anderson323c58d2009-03-05 07:19:18 +000051STATISTIC(NumRestoreFolds, "Number of intervals split with restore folding");
Owen Anderson2ebf63f2008-12-18 01:27:19 +000052STATISTIC(NumRenumbers, "Number of intervals renumbered into new registers");
Owen Anderson956ec272009-01-23 00:23:32 +000053STATISTIC(NumDeadSpills, "Number of dead spills removed");
Evan Chengf5cd4f02008-10-23 20:43:13 +000054
Evan Cheng09e8ca82008-10-20 21:44:59 +000055namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000056 class PreAllocSplitting : public MachineFunctionPass {
Evan Chengd0e32c52008-10-29 05:06:14 +000057 MachineFunction *CurrMF;
Evan Chengf5cd4f02008-10-23 20:43:13 +000058 const TargetMachine *TM;
59 const TargetInstrInfo *TII;
Owen Anderson3ef45492009-01-29 22:13:06 +000060 const TargetRegisterInfo* TRI;
Evan Chengf5cd4f02008-10-23 20:43:13 +000061 MachineFrameInfo *MFI;
62 MachineRegisterInfo *MRI;
Lang Hames233a60e2009-11-03 23:52:08 +000063 SlotIndexes *SIs;
Evan Chengf5cd4f02008-10-23 20:43:13 +000064 LiveIntervals *LIs;
Evan Chengd0e32c52008-10-29 05:06:14 +000065 LiveStacks *LSs;
Owen Anderson420dd372009-03-14 21:40:05 +000066 VirtRegMap *VRM;
Evan Cheng09e8ca82008-10-20 21:44:59 +000067
Evan Chengf5cd4f02008-10-23 20:43:13 +000068 // Barrier - Current barrier being processed.
69 MachineInstr *Barrier;
70
71 // BarrierMBB - Basic block where the barrier resides in.
72 MachineBasicBlock *BarrierMBB;
73
74 // Barrier - Current barrier index.
Lang Hames233a60e2009-11-03 23:52:08 +000075 SlotIndex BarrierIdx;
Evan Chengf5cd4f02008-10-23 20:43:13 +000076
77 // CurrLI - Current live interval being split.
78 LiveInterval *CurrLI;
79
Evan Chengd0e32c52008-10-29 05:06:14 +000080 // CurrSLI - Current stack slot live interval.
81 LiveInterval *CurrSLI;
82
83 // CurrSValNo - Current val# for the stack slot live interval.
84 VNInfo *CurrSValNo;
85
86 // IntervalSSMap - A map from live interval to spill slots.
87 DenseMap<unsigned, int> IntervalSSMap;
Evan Chengf5cd4f02008-10-23 20:43:13 +000088
Evan Cheng54898932008-10-29 08:39:34 +000089 // Def2SpillMap - A map from a def instruction index to spill index.
Lang Hames233a60e2009-11-03 23:52:08 +000090 DenseMap<SlotIndex, SlotIndex> Def2SpillMap;
Evan Cheng06587492008-10-24 02:05:00 +000091
Evan Cheng09e8ca82008-10-20 21:44:59 +000092 public:
93 static char ID;
Owen Anderson081c34b2010-10-19 17:21:58 +000094 PreAllocSplitting() : MachineFunctionPass(ID) {
95 initializePreAllocSplittingPass(*PassRegistry::getPassRegistry());
96 }
Evan Cheng09e8ca82008-10-20 21:44:59 +000097
98 virtual bool runOnMachineFunction(MachineFunction &MF);
99
100 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +0000101 AU.setPreservesCFG();
Lang Hames233a60e2009-11-03 23:52:08 +0000102 AU.addRequired<SlotIndexes>();
103 AU.addPreserved<SlotIndexes>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000104 AU.addRequired<LiveIntervals>();
105 AU.addPreserved<LiveIntervals>();
Evan Chengd0e32c52008-10-29 05:06:14 +0000106 AU.addRequired<LiveStacks>();
107 AU.addPreserved<LiveStacks>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000108 AU.addPreserved<RegisterCoalescer>();
Lang Hamesa937f222009-12-14 06:49:42 +0000109 AU.addPreserved<CalculateSpillWeights>();
Cameron Zwarichd959da92010-12-19 18:03:27 +0000110 AU.addPreservedID(StrongPHIEliminationID);
111 AU.addPreservedID(PHIEliminationID);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000112 AU.addRequired<MachineDominatorTree>();
113 AU.addRequired<MachineLoopInfo>();
Owen Anderson420dd372009-03-14 21:40:05 +0000114 AU.addRequired<VirtRegMap>();
Owen Andersonf1f75b12008-11-04 22:22:41 +0000115 AU.addPreserved<MachineDominatorTree>();
116 AU.addPreserved<MachineLoopInfo>();
Owen Anderson420dd372009-03-14 21:40:05 +0000117 AU.addPreserved<VirtRegMap>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000118 MachineFunctionPass::getAnalysisUsage(AU);
119 }
120
121 virtual void releaseMemory() {
Evan Chengd0e32c52008-10-29 05:06:14 +0000122 IntervalSSMap.clear();
Evan Cheng54898932008-10-29 08:39:34 +0000123 Def2SpillMap.clear();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000124 }
125
126 virtual const char *getPassName() const {
127 return "Pre-Register Allocaton Live Interval Splitting";
128 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000129
130 /// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000131 virtual void print(raw_ostream &O, const Module* M = 0) const {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000132 LIs->print(O, M);
133 }
134
Evan Chengf5cd4f02008-10-23 20:43:13 +0000135
136 private:
Evan Chengf5cd4f02008-10-23 20:43:13 +0000137
138 MachineBasicBlock::iterator
Evan Cheng1f08cc22008-10-28 05:28:21 +0000139 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
Lang Hamesb3661582009-11-14 00:02:51 +0000140 SmallPtrSet<MachineInstr*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000141
142 MachineBasicBlock::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000143 findRestorePoint(MachineBasicBlock*, MachineInstr*, SlotIndex,
Lang Hamesb3661582009-11-14 00:02:51 +0000144 SmallPtrSet<MachineInstr*, 4>&);
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,
Owen Anderson75fa96b2008-11-19 04:28:29 +0000164 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000165 MachineInstr* FoldSpill(unsigned vreg, const TargetRegisterClass* RC,
166 MachineInstr* DefMI,
167 MachineInstr* Barrier,
168 MachineBasicBlock* MBB,
169 int& SS,
170 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersonc93023a2009-03-04 08:52:31 +0000171 MachineInstr* FoldRestore(unsigned vreg,
172 const TargetRegisterClass* RC,
173 MachineInstr* Barrier,
174 MachineBasicBlock* MBB,
175 int SS,
176 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000177 void RenumberValno(VNInfo* VN);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000178 void ReconstructLiveInterval(LiveInterval* LI);
Owen Anderson956ec272009-01-23 00:23:32 +0000179 bool removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split);
Owen Anderson45e68552009-01-29 05:28:55 +0000180 unsigned getNumberOfNonSpills(SmallPtrSet<MachineInstr*, 4>& MIs,
181 unsigned Reg, int FrameIndex, bool& TwoAddr);
Evan Cheng19a72582009-02-02 18:33:18 +0000182 VNInfo* PerformPHIConstruction(MachineBasicBlock::iterator Use,
183 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000184 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000185 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
186 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
187 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000188 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
189 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000190 bool IsTopLevel, bool IsIntraBlock);
191 VNInfo* PerformPHIConstructionFallBack(MachineBasicBlock::iterator Use,
192 MachineBasicBlock* MBB, LiveInterval* LI,
193 SmallPtrSet<MachineInstr*, 4>& Visited,
194 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
195 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
196 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
197 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
198 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
199 bool IsTopLevel, bool IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000200};
Evan Cheng09e8ca82008-10-20 21:44:59 +0000201} // end anonymous namespace
202
203char PreAllocSplitting::ID = 0;
204
Owen Anderson2ab36d32010-10-12 19:48:12 +0000205INITIALIZE_PASS_BEGIN(PreAllocSplitting, "pre-alloc-splitting",
206 "Pre-Register Allocation Live Interval Splitting",
207 false, false)
208INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
209INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
210INITIALIZE_PASS_DEPENDENCY(LiveStacks)
211INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
212INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
213INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
214INITIALIZE_PASS_END(PreAllocSplitting, "pre-alloc-splitting",
Owen Anderson02dd53e2010-08-23 17:52:01 +0000215 "Pre-Register Allocation Live Interval Splitting",
Owen Andersonce665bd2010-10-07 22:25:06 +0000216 false, false)
Evan Cheng09e8ca82008-10-20 21:44:59 +0000217
Owen Anderson90c579d2010-08-06 18:33:48 +0000218char &llvm::PreAllocSplittingID = PreAllocSplitting::ID;
Evan Cheng09e8ca82008-10-20 21:44:59 +0000219
Evan Chengf5cd4f02008-10-23 20:43:13 +0000220/// findSpillPoint - Find a gap as far away from the given MI that's suitable
221/// for spilling the current live interval. The index must be before any
222/// defs and uses of the live interval register in the mbb. Return begin() if
223/// none is found.
224MachineBasicBlock::iterator
225PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000226 MachineInstr *DefMI,
Lang Hamesb3661582009-11-14 00:02:51 +0000227 SmallPtrSet<MachineInstr*, 4> &RefsInMBB) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000228 MachineBasicBlock::iterator Pt = MBB->begin();
229
Owen Anderson696a1302009-03-31 08:27:09 +0000230 MachineBasicBlock::iterator MII = MI;
231 MachineBasicBlock::iterator EndPt = DefMI
232 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
Owen Anderson4cafbb52009-02-20 10:02:23 +0000233
Owen Anderson696a1302009-03-31 08:27:09 +0000234 while (MII != EndPt && !RefsInMBB.count(MII) &&
235 MII->getOpcode() != TRI->getCallFrameSetupOpcode())
236 --MII;
237 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson4cafbb52009-02-20 10:02:23 +0000238
Owen Anderson696a1302009-03-31 08:27:09 +0000239 while (MII != EndPt && !RefsInMBB.count(MII)) {
Owen Anderson696a1302009-03-31 08:27:09 +0000240 // We can't insert the spill between the barrier (a call), and its
241 // corresponding call frame setup.
242 if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode()) {
243 while (MII->getOpcode() != TRI->getCallFrameSetupOpcode()) {
Owen Anderson5734c942009-02-05 05:58:41 +0000244 --MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000245 if (MII == EndPt) {
246 return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000247 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000248 }
Owen Anderson696a1302009-03-31 08:27:09 +0000249 continue;
Lang Hamesb3661582009-11-14 00:02:51 +0000250 } else {
Owen Anderson696a1302009-03-31 08:27:09 +0000251 Pt = MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000252 }
Owen Anderson696a1302009-03-31 08:27:09 +0000253
254 if (RefsInMBB.count(MII))
255 return Pt;
256
257
258 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000259 }
260
261 return Pt;
262}
263
264/// findRestorePoint - Find a gap in the instruction index map that's suitable
265/// for restoring the current live interval value. The index must be before any
266/// uses of the live interval register in the mbb. Return end() if none is
267/// found.
268MachineBasicBlock::iterator
269PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000270 SlotIndex LastIdx,
Lang Hamesb3661582009-11-14 00:02:51 +0000271 SmallPtrSet<MachineInstr*, 4> &RefsInMBB) {
Evan Cheng54898932008-10-29 08:39:34 +0000272 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
273 // begin index accordingly.
Owen Anderson5a92d4e2008-11-18 20:53:59 +0000274 MachineBasicBlock::iterator Pt = MBB->end();
Owen Anderson696a1302009-03-31 08:27:09 +0000275 MachineBasicBlock::iterator EndPt = MBB->getFirstTerminator();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000276
Owen Anderson696a1302009-03-31 08:27:09 +0000277 // We start at the call, so walk forward until we find the call frame teardown
278 // since we can't insert restores before that. Bail if we encounter a use
279 // during this time.
280 MachineBasicBlock::iterator MII = MI;
281 if (MII == EndPt) return Pt;
282
283 while (MII != EndPt && !RefsInMBB.count(MII) &&
284 MII->getOpcode() != TRI->getCallFrameDestroyOpcode())
285 ++MII;
286 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
287 ++MII;
288
289 // FIXME: Limit the number of instructions to examine to reduce
290 // compile time?
291 while (MII != EndPt) {
Lang Hames233a60e2009-11-03 23:52:08 +0000292 SlotIndex Index = LIs->getInstructionIndex(MII);
Owen Anderson696a1302009-03-31 08:27:09 +0000293 if (Index > LastIdx)
294 break;
Owen Anderson3ef45492009-01-29 22:13:06 +0000295
Owen Anderson696a1302009-03-31 08:27:09 +0000296 // We can't insert a restore between the barrier (a call) and its
297 // corresponding call frame teardown.
298 if (MII->getOpcode() == TRI->getCallFrameSetupOpcode()) {
299 do {
300 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000301 ++MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000302 } while (MII->getOpcode() != TRI->getCallFrameDestroyOpcode());
Lang Hamesb3661582009-11-14 00:02:51 +0000303 } else {
Owen Anderson696a1302009-03-31 08:27:09 +0000304 Pt = MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000305 }
Owen Anderson696a1302009-03-31 08:27:09 +0000306
307 if (RefsInMBB.count(MII))
308 return Pt;
309
310 ++MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000311 }
312
313 return Pt;
314}
315
Evan Chengd0e32c52008-10-29 05:06:14 +0000316/// CreateSpillStackSlot - Create a stack slot for the live interval being
317/// split. If the live interval was previously split, just reuse the same
318/// slot.
319int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
320 const TargetRegisterClass *RC) {
321 int SS;
322 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
323 if (I != IntervalSSMap.end()) {
324 SS = I->second;
325 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000326 SS = MFI->CreateSpillStackObject(RC->getSize(), RC->getAlignment());
Evan Chengd0e32c52008-10-29 05:06:14 +0000327 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000328 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000329
330 // Create live interval for stack slot.
Evan Chengc781a242009-05-03 18:32:42 +0000331 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Evan Cheng54898932008-10-29 08:39:34 +0000332 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000333 CurrSValNo = CurrSLI->getValNumInfo(0);
334 else
Lang Hames6e2968c2010-09-25 12:04:16 +0000335 CurrSValNo = CurrSLI->getNextValue(SlotIndex(), 0,
Lang Hames86511252009-09-04 20:41:11 +0000336 LSs->getVNInfoAllocator());
Evan Chengd0e32c52008-10-29 05:06:14 +0000337 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000338}
339
Evan Chengd0e32c52008-10-29 05:06:14 +0000340/// IsAvailableInStack - Return true if register is available in a split stack
341/// slot at the specified index.
342bool
Evan Cheng54898932008-10-29 08:39:34 +0000343PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
Lang Hames233a60e2009-11-03 23:52:08 +0000344 unsigned Reg, SlotIndex DefIndex,
345 SlotIndex RestoreIndex,
346 SlotIndex &SpillIndex,
Evan Cheng54898932008-10-29 08:39:34 +0000347 int& SS) const {
348 if (!DefMBB)
349 return false;
350
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000351 DenseMap<unsigned, int>::const_iterator I = IntervalSSMap.find(Reg);
Evan Chengd0e32c52008-10-29 05:06:14 +0000352 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000353 return false;
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000354 DenseMap<SlotIndex, SlotIndex>::const_iterator
Lang Hames86511252009-09-04 20:41:11 +0000355 II = Def2SpillMap.find(DefIndex);
Evan Cheng54898932008-10-29 08:39:34 +0000356 if (II == Def2SpillMap.end())
357 return false;
358
359 // If last spill of def is in the same mbb as barrier mbb (where restore will
360 // be), make sure it's not below the intended restore index.
361 // FIXME: Undo the previous spill?
362 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
363 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
364 return false;
365
366 SS = I->second;
367 SpillIndex = II->second;
368 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000369}
370
Evan Chengd0e32c52008-10-29 05:06:14 +0000371/// UpdateSpillSlotInterval - Given the specified val# of the register live
372/// interval being split, and the spill and restore indicies, update the live
373/// interval of the spill stack slot.
374void
Lang Hames233a60e2009-11-03 23:52:08 +0000375PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, SlotIndex SpillIndex,
376 SlotIndex RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000377 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
378 "Expect restore in the barrier mbb");
379
380 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
381 if (MBB == BarrierMBB) {
382 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000383 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
384 CurrSLI->addRange(SLR);
385 return;
386 }
387
Evan Cheng54898932008-10-29 08:39:34 +0000388 SmallPtrSet<MachineBasicBlock*, 4> Processed;
Lang Hames233a60e2009-11-03 23:52:08 +0000389 SlotIndex EndIdx = LIs->getMBBEndIdx(MBB);
Lang Hames74ab5ee2009-12-22 00:11:50 +0000390 LiveRange SLR(SpillIndex, EndIdx, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000391 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000392 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000393
394 // Start from the spill mbb, figure out the extend of the spill slot's
395 // live interval.
396 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000397 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
398 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000399 // If live range extend beyond end of mbb, add successors to work list.
400 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
401 SE = MBB->succ_end(); SI != SE; ++SI)
402 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000403
404 while (!WorkList.empty()) {
405 MachineBasicBlock *MBB = WorkList.back();
406 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000407 if (Processed.count(MBB))
408 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000409 SlotIndex Idx = LIs->getMBBStartIdx(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000410 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000411 if (LR && LR->valno == ValNo) {
412 EndIdx = LIs->getMBBEndIdx(MBB);
413 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000414 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000415 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000416 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000417 } else if (LR->end > EndIdx) {
418 // Live range extends beyond end of mbb, process successors.
Lang Hames233a60e2009-11-03 23:52:08 +0000419 LiveRange SLR(Idx, EndIdx.getNextIndex(), CurrSValNo);
Evan Cheng54898932008-10-29 08:39:34 +0000420 CurrSLI->addRange(SLR);
421 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
422 SE = MBB->succ_end(); SI != SE; ++SI)
423 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000424 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000425 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000426 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000427 }
Evan Cheng54898932008-10-29 08:39:34 +0000428 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000429 }
430 }
431}
432
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000433/// PerformPHIConstruction - From properly set up use and def lists, use a PHI
434/// construction algorithm to compute the ranges and valnos for an interval.
Evan Cheng19a72582009-02-02 18:33:18 +0000435VNInfo*
436PreAllocSplitting::PerformPHIConstruction(MachineBasicBlock::iterator UseI,
437 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000438 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000439 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
440 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
441 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000442 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
443 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000444 bool IsTopLevel, bool IsIntraBlock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000445 // Return memoized result if it's available.
Evan Cheng19a72582009-02-02 18:33:18 +0000446 if (IsTopLevel && Visited.count(UseI) && NewVNs.count(UseI))
447 return NewVNs[UseI];
448 else if (!IsTopLevel && IsIntraBlock && NewVNs.count(UseI))
449 return NewVNs[UseI];
450 else if (!IsIntraBlock && LiveOut.count(MBB))
Owen Anderson7d211e22008-12-31 02:00:25 +0000451 return LiveOut[MBB];
452
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000453 // Check if our block contains any uses or defs.
Owen Anderson7d211e22008-12-31 02:00:25 +0000454 bool ContainsDefs = Defs.count(MBB);
455 bool ContainsUses = Uses.count(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000456
Evan Cheng19a72582009-02-02 18:33:18 +0000457 VNInfo* RetVNI = 0;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000458
459 // Enumerate the cases of use/def contaning blocks.
460 if (!ContainsDefs && !ContainsUses) {
Evan Cheng19a72582009-02-02 18:33:18 +0000461 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
462 NewVNs, LiveOut, Phis,
463 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000464 } else if (ContainsDefs && !ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000465 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000466
467 // Search for the def in this block. If we don't find it before the
468 // instruction we care about, go to the fallback case. Note that that
Owen Anderson7d211e22008-12-31 02:00:25 +0000469 // should never happen: this cannot be intrablock, so use should
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000470 // always be an end() iterator.
Evan Cheng19a72582009-02-02 18:33:18 +0000471 assert(UseI == MBB->end() && "No use marked in intrablock");
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000472
Evan Cheng19a72582009-02-02 18:33:18 +0000473 MachineBasicBlock::iterator Walker = UseI;
474 --Walker;
475 while (Walker != MBB->begin()) {
476 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000477 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000478 --Walker;
479 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000480
481 // Once we've found it, extend its VNInfo to our instruction.
Lang Hames233a60e2009-11-03 23:52:08 +0000482 SlotIndex DefIndex = LIs->getInstructionIndex(Walker);
483 DefIndex = DefIndex.getDefIndex();
484 SlotIndex EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000485
Evan Cheng19a72582009-02-02 18:33:18 +0000486 RetVNI = NewVNs[Walker];
Lang Hames74ab5ee2009-12-22 00:11:50 +0000487 LI->addRange(LiveRange(DefIndex, EndIndex, RetVNI));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000488 } else if (!ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000489 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000490
491 // Search for the use in this block that precedes the instruction we care
Evan Cheng19a72582009-02-02 18:33:18 +0000492 // about, going to the fallback case if we don't find it.
Evan Cheng19a72582009-02-02 18:33:18 +0000493 MachineBasicBlock::iterator Walker = UseI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000494 bool found = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000495 while (Walker != MBB->begin()) {
Benjamin Kramer2e0de6f2010-01-07 19:46:15 +0000496 --Walker;
Evan Cheng19a72582009-02-02 18:33:18 +0000497 if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000498 found = true;
499 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000500 }
Evan Cheng19a72582009-02-02 18:33:18 +0000501 }
Benjamin Kramer2e0de6f2010-01-07 19:46:15 +0000502
503 if (!found)
504 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
505 Uses, NewVNs, LiveOut, Phis,
506 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000507
Lang Hames233a60e2009-11-03 23:52:08 +0000508 SlotIndex UseIndex = LIs->getInstructionIndex(Walker);
509 UseIndex = UseIndex.getUseIndex();
510 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000511 if (IsIntraBlock) {
Lang Hames74ab5ee2009-12-22 00:11:50 +0000512 EndIndex = LIs->getInstructionIndex(UseI).getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000513 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000514 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000515
516 // Now, recursively phi construct the VNInfo for the use we found,
517 // and then extend it to include the instruction we care about
Evan Cheng19a72582009-02-02 18:33:18 +0000518 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
519 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000520
Lang Hames74ab5ee2009-12-22 00:11:50 +0000521 LI->addRange(LiveRange(UseIndex, EndIndex, RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000522
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000523 // FIXME: Need to set kills properly for inter-block stuff.
Evan Cheng19a72582009-02-02 18:33:18 +0000524 } else if (ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000525 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
526 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000527
528 // This case is basically a merging of the two preceding case, with the
529 // special note that checking for defs must take precedence over checking
530 // for uses, because of two-address instructions.
Evan Cheng19a72582009-02-02 18:33:18 +0000531 MachineBasicBlock::iterator Walker = UseI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000532 bool foundDef = false;
533 bool foundUse = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000534 while (Walker != MBB->begin()) {
Benjamin Kramer2e0de6f2010-01-07 19:46:15 +0000535 --Walker;
Evan Cheng19a72582009-02-02 18:33:18 +0000536 if (BlockDefs.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000537 foundDef = true;
538 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000539 } else if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000540 foundUse = true;
541 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000542 }
Evan Cheng19a72582009-02-02 18:33:18 +0000543 }
Benjamin Kramer2e0de6f2010-01-07 19:46:15 +0000544
545 if (!foundDef && !foundUse)
546 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
547 Uses, NewVNs, LiveOut, Phis,
548 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000549
Lang Hames233a60e2009-11-03 23:52:08 +0000550 SlotIndex StartIndex = LIs->getInstructionIndex(Walker);
551 StartIndex = foundDef ? StartIndex.getDefIndex() : StartIndex.getUseIndex();
552 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000553 if (IsIntraBlock) {
Lang Hames74ab5ee2009-12-22 00:11:50 +0000554 EndIndex = LIs->getInstructionIndex(UseI).getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000555 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000556 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000557
558 if (foundDef)
Evan Cheng19a72582009-02-02 18:33:18 +0000559 RetVNI = NewVNs[Walker];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000560 else
Evan Cheng19a72582009-02-02 18:33:18 +0000561 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
562 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000563
Lang Hames74ab5ee2009-12-22 00:11:50 +0000564 LI->addRange(LiveRange(StartIndex, EndIndex, RetVNI));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000565 }
566
567 // Memoize results so we don't have to recompute them.
Evan Cheng19a72582009-02-02 18:33:18 +0000568 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000569 else {
Evan Cheng19a72582009-02-02 18:33:18 +0000570 if (!NewVNs.count(UseI))
571 NewVNs[UseI] = RetVNI;
572 Visited.insert(UseI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000573 }
574
Evan Cheng19a72582009-02-02 18:33:18 +0000575 return RetVNI;
576}
577
578/// PerformPHIConstructionFallBack - PerformPHIConstruction fall back path.
579///
580VNInfo*
581PreAllocSplitting::PerformPHIConstructionFallBack(MachineBasicBlock::iterator UseI,
582 MachineBasicBlock* MBB, LiveInterval* LI,
583 SmallPtrSet<MachineInstr*, 4>& Visited,
584 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
585 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
586 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
587 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
588 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
589 bool IsTopLevel, bool IsIntraBlock) {
590 // NOTE: Because this is the fallback case from other cases, we do NOT
591 // assume that we are not intrablock here.
592 if (Phis.count(MBB)) return Phis[MBB];
593
Lang Hames233a60e2009-11-03 23:52:08 +0000594 SlotIndex StartIndex = LIs->getMBBStartIdx(MBB);
Lang Hames857c4e02009-06-17 21:01:20 +0000595 VNInfo *RetVNI = Phis[MBB] =
Lang Hames6e2968c2010-09-25 12:04:16 +0000596 LI->getNextValue(SlotIndex(), /*FIXME*/ 0,
Lang Hames86511252009-09-04 20:41:11 +0000597 LIs->getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +0000598
Evan Cheng19a72582009-02-02 18:33:18 +0000599 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
600
601 // If there are no uses or defs between our starting point and the
602 // beginning of the block, then recursive perform phi construction
603 // on our predecessors.
604 DenseMap<MachineBasicBlock*, VNInfo*> IncomingVNs;
605 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
606 PE = MBB->pred_end(); PI != PE; ++PI) {
607 VNInfo* Incoming = PerformPHIConstruction((*PI)->end(), *PI, LI,
608 Visited, Defs, Uses, NewVNs,
609 LiveOut, Phis, false, false);
610 if (Incoming != 0)
611 IncomingVNs[*PI] = Incoming;
612 }
613
Lang Hames857c4e02009-06-17 21:01:20 +0000614 if (MBB->pred_size() == 1 && !RetVNI->hasPHIKill()) {
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000615 VNInfo* OldVN = RetVNI;
616 VNInfo* NewVN = IncomingVNs.begin()->second;
617 VNInfo* MergedVN = LI->MergeValueNumberInto(OldVN, NewVN);
618 if (MergedVN == OldVN) std::swap(OldVN, NewVN);
619
620 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator LOI = LiveOut.begin(),
621 LOE = LiveOut.end(); LOI != LOE; ++LOI)
622 if (LOI->second == OldVN)
623 LOI->second = MergedVN;
624 for (DenseMap<MachineInstr*, VNInfo*>::iterator NVI = NewVNs.begin(),
625 NVE = NewVNs.end(); NVI != NVE; ++NVI)
626 if (NVI->second == OldVN)
627 NVI->second = MergedVN;
628 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator PI = Phis.begin(),
629 PE = Phis.end(); PI != PE; ++PI)
630 if (PI->second == OldVN)
631 PI->second = MergedVN;
632 RetVNI = MergedVN;
Evan Cheng19a72582009-02-02 18:33:18 +0000633 } else {
634 // Otherwise, merge the incoming VNInfos with a phi join. Create a new
635 // VNInfo to represent the joined value.
636 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator I =
637 IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) {
Lang Hames857c4e02009-06-17 21:01:20 +0000638 I->second->setHasPHIKill(true);
Evan Cheng19a72582009-02-02 18:33:18 +0000639 }
640 }
641
Lang Hames233a60e2009-11-03 23:52:08 +0000642 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000643 if (IsIntraBlock) {
Lang Hames74ab5ee2009-12-22 00:11:50 +0000644 EndIndex = LIs->getInstructionIndex(UseI).getDefIndex();
Evan Cheng19a72582009-02-02 18:33:18 +0000645 } else
646 EndIndex = LIs->getMBBEndIdx(MBB);
Lang Hames74ab5ee2009-12-22 00:11:50 +0000647 LI->addRange(LiveRange(StartIndex, EndIndex, RetVNI));
Evan Cheng19a72582009-02-02 18:33:18 +0000648
649 // Memoize results so we don't have to recompute them.
650 if (!IsIntraBlock)
651 LiveOut[MBB] = RetVNI;
652 else {
653 if (!NewVNs.count(UseI))
654 NewVNs[UseI] = RetVNI;
655 Visited.insert(UseI);
656 }
657
658 return RetVNI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000659}
660
661/// ReconstructLiveInterval - Recompute a live interval from scratch.
662void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) {
Benjamin Kramer991de142010-03-30 20:16:45 +0000663 VNInfo::Allocator& Alloc = LIs->getVNInfoAllocator();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000664
665 // Clear the old ranges and valnos;
666 LI->clear();
667
668 // Cache the uses and defs of the register
669 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
670 RegMap Defs, Uses;
671
672 // Keep track of the new VNs we're creating.
673 DenseMap<MachineInstr*, VNInfo*> NewVNs;
674 SmallPtrSet<VNInfo*, 2> PhiVNs;
675
676 // Cache defs, and create a new VNInfo for each def.
677 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
678 DE = MRI->def_end(); DI != DE; ++DI) {
679 Defs[(*DI).getParent()].insert(&*DI);
680
Lang Hames233a60e2009-11-03 23:52:08 +0000681 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI);
682 DefIdx = DefIdx.getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000683
Chris Lattner518bb532010-02-09 19:54:29 +0000684 assert(!DI->isPHI() && "PHI instr in code during pre-alloc splitting.");
Lang Hames6e2968c2010-09-25 12:04:16 +0000685 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, Alloc);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000686
687 // If the def is a move, set the copy field.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000688 if (DI->isCopyLike() && DI->getOperand(0).getReg() == LI->reg)
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000689 NewVN->setCopy(&*DI);
690
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000691 NewVNs[&*DI] = NewVN;
692 }
693
694 // Cache uses as a separate pass from actually processing them.
695 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
696 UE = MRI->use_end(); UI != UE; ++UI)
697 Uses[(*UI).getParent()].insert(&*UI);
698
699 // Now, actually process every use and use a phi construction algorithm
700 // to walk from it to its reaching definitions, building VNInfos along
701 // the way.
Owen Anderson7d211e22008-12-31 02:00:25 +0000702 DenseMap<MachineBasicBlock*, VNInfo*> LiveOut;
703 DenseMap<MachineBasicBlock*, VNInfo*> Phis;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000704 SmallPtrSet<MachineInstr*, 4> Visited;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000705 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
706 UE = MRI->use_end(); UI != UE; ++UI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000707 PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000708 Uses, NewVNs, LiveOut, Phis, true, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000709 }
Owen Andersond4f6fe52008-12-28 23:35:13 +0000710
711 // Add ranges for dead defs
712 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
713 DE = MRI->def_end(); DI != DE; ++DI) {
Lang Hames233a60e2009-11-03 23:52:08 +0000714 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI);
715 DefIdx = DefIdx.getDefIndex();
Owen Andersond4f6fe52008-12-28 23:35:13 +0000716
717 if (LI->liveAt(DefIdx)) continue;
718
719 VNInfo* DeadVN = NewVNs[&*DI];
Lang Hames233a60e2009-11-03 23:52:08 +0000720 LI->addRange(LiveRange(DefIdx, DefIdx.getNextSlot(), DeadVN));
Evan Cheng35ca9202009-10-09 01:17:11 +0000721 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000722}
723
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000724/// RenumberValno - Split the given valno out into a new vreg, allowing it to
725/// be allocated to a different register. This function creates a new vreg,
726/// copies the valno and its live ranges over to the new vreg's interval,
727/// removes them from the old interval, and rewrites all uses and defs of
728/// the original reg to the new vreg within those ranges.
729void PreAllocSplitting::RenumberValno(VNInfo* VN) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000730 SmallVector<VNInfo*, 4> Stack;
731 SmallVector<VNInfo*, 4> VNsToCopy;
732 Stack.push_back(VN);
733
734 // Walk through and copy the valno we care about, and any other valnos
735 // that are two-address redefinitions of the one we care about. These
736 // will need to be rewritten as well. We also check for safety of the
737 // renumbering here, by making sure that none of the valno involved has
738 // phi kills.
739 while (!Stack.empty()) {
740 VNInfo* OldVN = Stack.back();
741 Stack.pop_back();
742
743 // Bail out if we ever encounter a valno that has a PHI kill. We can't
744 // renumber these.
Lang Hames857c4e02009-06-17 21:01:20 +0000745 if (OldVN->hasPHIKill()) return;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000746
747 VNsToCopy.push_back(OldVN);
748
749 // Locate two-address redefinitions
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000750 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(CurrLI->reg),
751 DE = MRI->def_end(); DI != DE; ++DI) {
752 if (!DI->isRegTiedToUseOperand(DI.getOperandNo())) continue;
753 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI).getDefIndex();
754 VNInfo* NextVN = CurrLI->findDefinedVNInfoForRegInt(DefIdx);
755 if (std::find(VNsToCopy.begin(), VNsToCopy.end(), NextVN) !=
756 VNsToCopy.end())
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000757 Stack.push_back(NextVN);
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000758 }
759 }
760
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000761 // Create the new vreg
762 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
763
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000764 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000765 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000766
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000767 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
768 VNsToCopy.end(); OI != OE; ++OI) {
769 VNInfo* OldVN = *OI;
770
771 // Copy the valno over
Lang Hames857c4e02009-06-17 21:01:20 +0000772 VNInfo* NewVN = NewLI.createValueCopy(OldVN, LIs->getVNInfoAllocator());
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000773 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
774
775 // Remove the valno from the old interval
776 CurrLI->removeValNo(OldVN);
777 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000778
779 // Rewrite defs and uses. This is done in two stages to avoid invalidating
780 // the reg_iterator.
781 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
782
783 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
784 E = MRI->reg_end(); I != E; ++I) {
785 MachineOperand& MO = I.getOperand();
Lang Hames233a60e2009-11-03 23:52:08 +0000786 SlotIndex InstrIdx = LIs->getInstructionIndex(&*I);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000787
Lang Hames233a60e2009-11-03 23:52:08 +0000788 if ((MO.isUse() && NewLI.liveAt(InstrIdx.getUseIndex())) ||
789 (MO.isDef() && NewLI.liveAt(InstrIdx.getDefIndex())))
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000790 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
791 }
792
793 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
794 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
795 MachineInstr* Inst = I->first;
796 unsigned OpIdx = I->second;
797 MachineOperand& MO = Inst->getOperand(OpIdx);
798 MO.setReg(NewVReg);
799 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000800
Owen Anderson420dd372009-03-14 21:40:05 +0000801 // Grow the VirtRegMap, since we've created a new vreg.
802 VRM->grow();
803
Owen Anderson45e68552009-01-29 05:28:55 +0000804 // The renumbered vreg shares a stack slot with the old register.
805 if (IntervalSSMap.count(CurrLI->reg))
806 IntervalSSMap[NewVReg] = IntervalSSMap[CurrLI->reg];
807
Dan Gohmanfe601042010-06-22 15:08:57 +0000808 ++NumRenumbers;
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000809}
810
Evan Cheng37844532009-07-16 09:20:10 +0000811bool PreAllocSplitting::Rematerialize(unsigned VReg, VNInfo* ValNo,
Owen Anderson6002e992008-12-04 21:20:30 +0000812 MachineInstr* DefMI,
813 MachineBasicBlock::iterator RestorePt,
Owen Anderson6002e992008-12-04 21:20:30 +0000814 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
815 MachineBasicBlock& MBB = *RestorePt->getParent();
816
817 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
Lang Hamescec29452010-09-26 03:37:09 +0000818 if (!DefMI || DefMI->getParent() == BarrierMBB)
Lang Hamesb3661582009-11-14 00:02:51 +0000819 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB);
Owen Anderson6002e992008-12-04 21:20:30 +0000820 else
Chris Lattner7896c9f2009-12-03 00:50:42 +0000821 KillPt = llvm::next(MachineBasicBlock::iterator(DefMI));
Owen Anderson6002e992008-12-04 21:20:30 +0000822
823 if (KillPt == DefMI->getParent()->end())
824 return false;
825
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000826 TII->reMaterialize(MBB, RestorePt, VReg, 0, DefMI, *TRI);
Lang Hamesb3661582009-11-14 00:02:51 +0000827 SlotIndex RematIdx = LIs->InsertMachineInstrInMaps(prior(RestorePt));
Owen Anderson6002e992008-12-04 21:20:30 +0000828
Owen Andersonb4b84362009-01-26 21:57:31 +0000829 ReconstructLiveInterval(CurrLI);
Lang Hames233a60e2009-11-03 23:52:08 +0000830 RematIdx = RematIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +0000831 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RematIdx));
Owen Andersone1762c92009-01-12 03:10:40 +0000832
Owen Anderson75fa96b2008-11-19 04:28:29 +0000833 ++NumSplits;
834 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000835 return true;
836}
837
838MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
839 const TargetRegisterClass* RC,
840 MachineInstr* DefMI,
841 MachineInstr* Barrier,
842 MachineBasicBlock* MBB,
843 int& SS,
844 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000845 // Go top down if RefsInMBB is empty.
846 if (RefsInMBB.empty())
847 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000848
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000849 MachineBasicBlock::iterator FoldPt = Barrier;
850 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
851 !RefsInMBB.count(FoldPt))
852 --FoldPt;
853
Evan Cheng1015ba72010-05-21 20:53:24 +0000854 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000855 if (OpIdx == -1)
856 return 0;
857
858 SmallVector<unsigned, 1> Ops;
859 Ops.push_back(OpIdx);
860
861 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
862 return 0;
863
864 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
865 if (I != IntervalSSMap.end()) {
866 SS = I->second;
867 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000868 SS = MFI->CreateSpillStackObject(RC->getSize(), RC->getAlignment());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000869 }
870
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000871 MachineInstr* FMI = TII->foldMemoryOperand(FoldPt, Ops, SS);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000872
873 if (FMI) {
874 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000875 FoldPt->eraseFromParent();
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000876 ++NumFolds;
877
878 IntervalSSMap[vreg] = SS;
Evan Chengc781a242009-05-03 18:32:42 +0000879 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000880 if (CurrSLI->hasAtLeastOneValue())
881 CurrSValNo = CurrSLI->getValNumInfo(0);
882 else
Lang Hames6e2968c2010-09-25 12:04:16 +0000883 CurrSValNo = CurrSLI->getNextValue(SlotIndex(), 0,
Lang Hames86511252009-09-04 20:41:11 +0000884 LSs->getVNInfoAllocator());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000885 }
886
887 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000888}
889
Owen Andersonc93023a2009-03-04 08:52:31 +0000890MachineInstr* PreAllocSplitting::FoldRestore(unsigned vreg,
891 const TargetRegisterClass* RC,
892 MachineInstr* Barrier,
893 MachineBasicBlock* MBB,
894 int SS,
895 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
Owen Andersona2bfb542009-03-05 08:23:20 +0000896 if ((int)RestoreFoldLimit != -1 && RestoreFoldLimit == (int)NumRestoreFolds)
Owen Anderson323c58d2009-03-05 07:19:18 +0000897 return 0;
898
Owen Andersonc93023a2009-03-04 08:52:31 +0000899 // Go top down if RefsInMBB is empty.
900 if (RefsInMBB.empty())
901 return 0;
902
903 // Can't fold a restore between a call stack setup and teardown.
904 MachineBasicBlock::iterator FoldPt = Barrier;
Owen Anderson323c58d2009-03-05 07:19:18 +0000905
906 // Advance from barrier to call frame teardown.
907 while (FoldPt != MBB->getFirstTerminator() &&
908 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
909 if (RefsInMBB.count(FoldPt))
910 return 0;
Owen Andersonc93023a2009-03-04 08:52:31 +0000911
Owen Anderson323c58d2009-03-05 07:19:18 +0000912 ++FoldPt;
913 }
914
915 if (FoldPt == MBB->getFirstTerminator())
916 return 0;
917 else
918 ++FoldPt;
919
920 // Now find the restore point.
921 while (FoldPt != MBB->getFirstTerminator() && !RefsInMBB.count(FoldPt)) {
Owen Andersonc93023a2009-03-04 08:52:31 +0000922 if (FoldPt->getOpcode() == TRI->getCallFrameSetupOpcode()) {
923 while (FoldPt != MBB->getFirstTerminator() &&
924 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
925 if (RefsInMBB.count(FoldPt))
926 return 0;
927
928 ++FoldPt;
929 }
930
Owen Anderson323c58d2009-03-05 07:19:18 +0000931 if (FoldPt == MBB->getFirstTerminator())
932 return 0;
933 }
934
935 ++FoldPt;
Owen Andersonc93023a2009-03-04 08:52:31 +0000936 }
937
938 if (FoldPt == MBB->getFirstTerminator())
939 return 0;
940
941 int OpIdx = FoldPt->findRegisterUseOperandIdx(vreg, true);
942 if (OpIdx == -1)
943 return 0;
944
945 SmallVector<unsigned, 1> Ops;
946 Ops.push_back(OpIdx);
947
948 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
949 return 0;
950
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000951 MachineInstr* FMI = TII->foldMemoryOperand(FoldPt, Ops, SS);
Owen Andersonc93023a2009-03-04 08:52:31 +0000952
953 if (FMI) {
954 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000955 FoldPt->eraseFromParent();
Owen Anderson323c58d2009-03-05 07:19:18 +0000956 ++NumRestoreFolds;
Owen Andersonc93023a2009-03-04 08:52:31 +0000957 }
958
959 return FMI;
960}
961
Evan Chengf5cd4f02008-10-23 20:43:13 +0000962/// SplitRegLiveInterval - Split (spill and restore) the given live interval
963/// so it would not cross the barrier that's being processed. Shrink wrap
964/// (minimize) the live interval to the last uses.
965bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
David Greene4175f582010-01-05 01:25:47 +0000966 DEBUG(dbgs() << "Pre-alloc splitting " << LI->reg << " for " << *Barrier
Lang Hames233a60e2009-11-03 23:52:08 +0000967 << " result: ");
968
Evan Chengf5cd4f02008-10-23 20:43:13 +0000969 CurrLI = LI;
970
971 // Find live range where current interval cross the barrier.
972 LiveInterval::iterator LR =
Lang Hames233a60e2009-11-03 23:52:08 +0000973 CurrLI->FindLiveRangeContaining(BarrierIdx.getUseIndex());
Evan Chengf5cd4f02008-10-23 20:43:13 +0000974 VNInfo *ValNo = LR->valno;
975
Torok Edwinf3689232009-07-12 20:07:01 +0000976 assert(!ValNo->isUnused() && "Val# is defined by a dead def?");
Evan Chengf5cd4f02008-10-23 20:43:13 +0000977
Lang Hames6e2968c2010-09-25 12:04:16 +0000978 MachineInstr *DefMI = LIs->getInstructionFromIndex(ValNo->def);
Evan Cheng06587492008-10-24 02:05:00 +0000979
Owen Andersond3be4622009-01-21 08:18:03 +0000980 // If this would create a new join point, do not split.
Lang Hames233a60e2009-11-03 23:52:08 +0000981 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent())) {
David Greene4175f582010-01-05 01:25:47 +0000982 DEBUG(dbgs() << "FAILED (would create a new join point).\n");
Owen Andersond3be4622009-01-21 08:18:03 +0000983 return false;
Lang Hames233a60e2009-11-03 23:52:08 +0000984 }
Owen Andersond3be4622009-01-21 08:18:03 +0000985
Evan Chengf5cd4f02008-10-23 20:43:13 +0000986 // Find all references in the barrier mbb.
987 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
988 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
989 E = MRI->reg_end(); I != E; ++I) {
990 MachineInstr *RefMI = &*I;
991 if (RefMI->getParent() == BarrierMBB)
992 RefsInMBB.insert(RefMI);
993 }
994
995 // Find a point to restore the value after the barrier.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000996 MachineBasicBlock::iterator RestorePt =
Lang Hamesb3661582009-11-14 00:02:51 +0000997 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +0000998 if (RestorePt == BarrierMBB->end()) {
David Greene4175f582010-01-05 01:25:47 +0000999 DEBUG(dbgs() << "FAILED (could not find a suitable restore point).\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001000 return false;
Lang Hames233a60e2009-11-03 23:52:08 +00001001 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001002
Owen Anderson75fa96b2008-11-19 04:28:29 +00001003 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
Lang Hamesb3661582009-11-14 00:02:51 +00001004 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt, RefsInMBB)) {
David Greene4175f582010-01-05 01:25:47 +00001005 DEBUG(dbgs() << "success (remat).\n");
Lang Hames233a60e2009-11-03 23:52:08 +00001006 return true;
1007 }
Owen Anderson75fa96b2008-11-19 04:28:29 +00001008
Evan Chengf5cd4f02008-10-23 20:43:13 +00001009 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +00001010 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001011 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Lang Hames233a60e2009-11-03 23:52:08 +00001012 SlotIndex SpillIndex;
Evan Cheng06587492008-10-24 02:05:00 +00001013 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +00001014 int SS = -1;
Lang Hamescec29452010-09-26 03:37:09 +00001015 if (!DefMI) {
Lang Hames857c4e02009-06-17 21:01:20 +00001016 // If we don't know where the def is we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001017 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
1018 BarrierMBB, SS, RefsInMBB))) {
1019 SpillIndex = LIs->getInstructionIndex(SpillMI);
1020 } else {
1021 MachineBasicBlock::iterator SpillPt =
Lang Hamesb3661582009-11-14 00:02:51 +00001022 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +00001023 if (SpillPt == BarrierMBB->begin()) {
David Greene4175f582010-01-05 01:25:47 +00001024 DEBUG(dbgs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001025 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001026 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001027 // Add spill.
1028
1029 SS = CreateSpillStackSlot(CurrLI->reg, RC);
Evan Cheng746ad692010-05-06 19:06:44 +00001030 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC,
1031 TRI);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001032 SpillMI = prior(SpillPt);
Lang Hamesb3661582009-11-14 00:02:51 +00001033 SpillIndex = LIs->InsertMachineInstrInMaps(SpillMI);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001034 }
Evan Cheng54898932008-10-29 08:39:34 +00001035 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
Lang Hamesb3661582009-11-14 00:02:51 +00001036 LIs->getZeroIndex(), SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +00001037 // If it's already split, just restore the value. There is no need to spill
1038 // the def again.
Lang Hames233a60e2009-11-03 23:52:08 +00001039 if (!DefMI) {
David Greene4175f582010-01-05 01:25:47 +00001040 DEBUG(dbgs() << "FAILED (def is dead).\n");
Evan Chengd0e32c52008-10-29 05:06:14 +00001041 return false; // Def is dead. Do nothing.
Lang Hames233a60e2009-11-03 23:52:08 +00001042 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001043
1044 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
Evan Cheng35ca9202009-10-09 01:17:11 +00001045 BarrierMBB, SS, RefsInMBB))) {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001046 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001047 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001048 // Check if it's possible to insert a spill after the def MI.
1049 MachineBasicBlock::iterator SpillPt;
1050 if (DefMBB == BarrierMBB) {
1051 // Add spill after the def and the last use before the barrier.
1052 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
Lang Hamesb3661582009-11-14 00:02:51 +00001053 RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +00001054 if (SpillPt == DefMBB->begin()) {
David Greene4175f582010-01-05 01:25:47 +00001055 DEBUG(dbgs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001056 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001057 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001058 } else {
Chris Lattner7896c9f2009-12-03 00:50:42 +00001059 SpillPt = llvm::next(MachineBasicBlock::iterator(DefMI));
Lang Hames233a60e2009-11-03 23:52:08 +00001060 if (SpillPt == DefMBB->end()) {
David Greene4175f582010-01-05 01:25:47 +00001061 DEBUG(dbgs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001062 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001063 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001064 }
Evan Cheng35ca9202009-10-09 01:17:11 +00001065 // Add spill.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001066 SS = CreateSpillStackSlot(CurrLI->reg, RC);
Evan Cheng746ad692010-05-06 19:06:44 +00001067 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg, false, SS, RC,
1068 TRI);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001069 SpillMI = prior(SpillPt);
Lang Hamesb3661582009-11-14 00:02:51 +00001070 SpillIndex = LIs->InsertMachineInstrInMaps(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001071 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001072 }
1073
Evan Cheng54898932008-10-29 08:39:34 +00001074 // Remember def instruction index to spill index mapping.
1075 if (DefMI && SpillMI)
1076 Def2SpillMap[ValNo->def] = SpillIndex;
1077
Evan Chengf5cd4f02008-10-23 20:43:13 +00001078 // Add restore.
Owen Andersonc93023a2009-03-04 08:52:31 +00001079 bool FoldedRestore = false;
Lang Hamesb3661582009-11-14 00:02:51 +00001080 SlotIndex RestoreIndex;
Owen Andersonc93023a2009-03-04 08:52:31 +00001081 if (MachineInstr* LMI = FoldRestore(CurrLI->reg, RC, Barrier,
1082 BarrierMBB, SS, RefsInMBB)) {
1083 RestorePt = LMI;
Owen Anderson323c58d2009-03-05 07:19:18 +00001084 RestoreIndex = LIs->getInstructionIndex(RestorePt);
Owen Andersonc93023a2009-03-04 08:52:31 +00001085 FoldedRestore = true;
1086 } else {
Evan Cheng746ad692010-05-06 19:06:44 +00001087 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC, TRI);
Owen Andersonc93023a2009-03-04 08:52:31 +00001088 MachineInstr *LoadMI = prior(RestorePt);
Lang Hamesb3661582009-11-14 00:02:51 +00001089 RestoreIndex = LIs->InsertMachineInstrInMaps(LoadMI);
Owen Andersonc93023a2009-03-04 08:52:31 +00001090 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001091
Evan Chengd0e32c52008-10-29 05:06:14 +00001092 // Update spill stack slot live interval.
Lang Hames233a60e2009-11-03 23:52:08 +00001093 UpdateSpillSlotInterval(ValNo, SpillIndex.getUseIndex().getNextSlot(),
1094 RestoreIndex.getDefIndex());
Evan Chengd0e32c52008-10-29 05:06:14 +00001095
Owen Andersonb4b84362009-01-26 21:57:31 +00001096 ReconstructLiveInterval(CurrLI);
Evan Cheng35ca9202009-10-09 01:17:11 +00001097
Owen Andersonc93023a2009-03-04 08:52:31 +00001098 if (!FoldedRestore) {
Lang Hames233a60e2009-11-03 23:52:08 +00001099 SlotIndex RestoreIdx = LIs->getInstructionIndex(prior(RestorePt));
1100 RestoreIdx = RestoreIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +00001101 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RestoreIdx));
Owen Andersonc93023a2009-03-04 08:52:31 +00001102 }
Owen Anderson7d211e22008-12-31 02:00:25 +00001103
Evan Chengae7fa5b2008-10-28 01:48:24 +00001104 ++NumSplits;
David Greene4175f582010-01-05 01:25:47 +00001105 DEBUG(dbgs() << "success.\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001106 return true;
1107}
1108
1109/// SplitRegLiveIntervals - Split all register live intervals that cross the
1110/// barrier that's being processed.
1111bool
Owen Anderson956ec272009-01-23 00:23:32 +00001112PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs,
1113 SmallPtrSet<LiveInterval*, 8>& Split) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001114 // First find all the virtual registers whose live intervals are intercepted
1115 // by the current barrier.
1116 SmallVector<LiveInterval*, 8> Intervals;
1117 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng4350eb82009-02-06 17:17:30 +00001118 // FIXME: If it's not safe to move any instruction that defines the barrier
1119 // register class, then it means there are some special dependencies which
1120 // codegen is not modelling. Ignore these barriers for now.
1121 if (!TII->isSafeToMoveRegClassDefs(*RC))
Evan Cheng23066282008-10-27 07:14:50 +00001122 continue;
Chris Lattner65569b82010-05-21 17:47:50 +00001123 const std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
Evan Chengf5cd4f02008-10-23 20:43:13 +00001124 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1125 unsigned Reg = VRs[i];
1126 if (!LIs->hasInterval(Reg))
1127 continue;
1128 LiveInterval *LI = &LIs->getInterval(Reg);
1129 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1130 // Virtual register live interval is intercepted by the barrier. We
1131 // should split and shrink wrap its interval if possible.
1132 Intervals.push_back(LI);
1133 }
1134 }
1135
1136 // Process the affected live intervals.
1137 bool Change = false;
1138 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001139 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1140 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001141 LiveInterval *LI = Intervals.back();
1142 Intervals.pop_back();
Owen Anderson956ec272009-01-23 00:23:32 +00001143 bool result = SplitRegLiveInterval(LI);
1144 if (result) Split.insert(LI);
1145 Change |= result;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001146 }
1147
1148 return Change;
1149}
1150
Owen Anderson45e68552009-01-29 05:28:55 +00001151unsigned PreAllocSplitting::getNumberOfNonSpills(
Owen Anderson32ca8652009-01-24 10:07:43 +00001152 SmallPtrSet<MachineInstr*, 4>& MIs,
Owen Anderson45e68552009-01-29 05:28:55 +00001153 unsigned Reg, int FrameIndex,
1154 bool& FeedsTwoAddr) {
1155 unsigned NonSpills = 0;
Owen Anderson32ca8652009-01-24 10:07:43 +00001156 for (SmallPtrSet<MachineInstr*, 4>::iterator UI = MIs.begin(), UE = MIs.end();
Owen Anderson45e68552009-01-29 05:28:55 +00001157 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001158 int StoreFrameIndex;
1159 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001160 if (StoreVReg != Reg || StoreFrameIndex != FrameIndex)
Dan Gohmanfe601042010-06-22 15:08:57 +00001161 ++NonSpills;
Owen Anderson45e68552009-01-29 05:28:55 +00001162
1163 int DefIdx = (*UI)->findRegisterDefOperandIdx(Reg);
Bob Wilsond9df5012009-04-09 17:16:43 +00001164 if (DefIdx != -1 && (*UI)->isRegTiedToUseOperand(DefIdx))
Owen Anderson45e68552009-01-29 05:28:55 +00001165 FeedsTwoAddr = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001166 }
1167
Owen Anderson45e68552009-01-29 05:28:55 +00001168 return NonSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001169}
1170
Owen Anderson956ec272009-01-23 00:23:32 +00001171/// removeDeadSpills - After doing splitting, filter through all intervals we've
1172/// split, and see if any of the spills are unnecessary. If so, remove them.
1173bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) {
1174 bool changed = false;
1175
Owen Anderson4bfc2092009-01-29 05:41:02 +00001176 // Walk over all of the live intervals that were touched by the splitter,
1177 // and see if we can do any DCE and/or folding.
Owen Anderson956ec272009-01-23 00:23:32 +00001178 for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(),
1179 LE = split.end(); LI != LE; ++LI) {
Owen Anderson9ce499a2009-01-23 03:28:53 +00001180 DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> > VNUseCount;
Owen Anderson956ec272009-01-23 00:23:32 +00001181
Owen Anderson4bfc2092009-01-29 05:41:02 +00001182 // First, collect all the uses of the vreg, and sort them by their
1183 // reaching definition (VNInfo).
Owen Anderson956ec272009-01-23 00:23:32 +00001184 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg),
1185 UE = MRI->use_end(); UI != UE; ++UI) {
Lang Hames233a60e2009-11-03 23:52:08 +00001186 SlotIndex index = LIs->getInstructionIndex(&*UI);
1187 index = index.getUseIndex();
Owen Anderson956ec272009-01-23 00:23:32 +00001188
1189 const LiveRange* LR = (*LI)->getLiveRangeContaining(index);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001190 VNUseCount[LR->valno].insert(&*UI);
Owen Anderson956ec272009-01-23 00:23:32 +00001191 }
1192
Owen Anderson4bfc2092009-01-29 05:41:02 +00001193 // Now, take the definitions (VNInfo's) one at a time and try to DCE
1194 // and/or fold them away.
Owen Anderson956ec272009-01-23 00:23:32 +00001195 for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(),
1196 VE = (*LI)->vni_end(); VI != VE; ++VI) {
Owen Anderson45e68552009-01-29 05:28:55 +00001197
1198 if (DeadSplitLimit != -1 && (int)NumDeadSpills == DeadSplitLimit)
1199 return changed;
1200
Owen Anderson956ec272009-01-23 00:23:32 +00001201 VNInfo* CurrVN = *VI;
Owen Anderson4bfc2092009-01-29 05:41:02 +00001202
1203 // We don't currently try to handle definitions with PHI kills, because
1204 // it would involve processing more than one VNInfo at once.
Lang Hames857c4e02009-06-17 21:01:20 +00001205 if (CurrVN->hasPHIKill()) continue;
Owen Anderson956ec272009-01-23 00:23:32 +00001206
Owen Anderson4bfc2092009-01-29 05:41:02 +00001207 // We also don't try to handle the results of PHI joins, since there's
1208 // no defining instruction to analyze.
Lang Hames6e2968c2010-09-25 12:04:16 +00001209 MachineInstr* DefMI = LIs->getInstructionFromIndex(CurrVN->def);
1210 if (!DefMI || CurrVN->isUnused()) continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001211
Owen Anderson4bfc2092009-01-29 05:41:02 +00001212 // We're only interested in eliminating cruft introduced by the splitter,
1213 // is of the form load-use or load-use-store. First, check that the
1214 // definition is a load, and remember what stack slot we loaded it from.
Owen Anderson956ec272009-01-23 00:23:32 +00001215 int FrameIndex;
1216 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
1217
Owen Anderson4bfc2092009-01-29 05:41:02 +00001218 // If the definition has no uses at all, just DCE it.
Owen Anderson9ce499a2009-01-23 03:28:53 +00001219 if (VNUseCount[CurrVN].size() == 0) {
1220 LIs->RemoveMachineInstrFromMaps(DefMI);
1221 (*LI)->removeValNo(CurrVN);
1222 DefMI->eraseFromParent();
Owen Andersonc0f3a032009-01-29 08:22:06 +00001223 VNUseCount.erase(CurrVN);
Dan Gohmanfe601042010-06-22 15:08:57 +00001224 ++NumDeadSpills;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001225 changed = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001226 continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001227 }
Owen Anderson32ca8652009-01-24 10:07:43 +00001228
Owen Anderson4bfc2092009-01-29 05:41:02 +00001229 // Second, get the number of non-store uses of the definition, as well as
1230 // a flag indicating whether it feeds into a later two-address definition.
Owen Anderson45e68552009-01-29 05:28:55 +00001231 bool FeedsTwoAddr = false;
1232 unsigned NonSpillCount = getNumberOfNonSpills(VNUseCount[CurrVN],
1233 (*LI)->reg, FrameIndex,
1234 FeedsTwoAddr);
1235
Owen Anderson4bfc2092009-01-29 05:41:02 +00001236 // If there's one non-store use and it doesn't feed a two-addr, then
1237 // this is a load-use-store case that we can try to fold.
Owen Anderson45e68552009-01-29 05:28:55 +00001238 if (NonSpillCount == 1 && !FeedsTwoAddr) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001239 // Start by finding the non-store use MachineInstr.
Owen Anderson45e68552009-01-29 05:28:55 +00001240 SmallPtrSet<MachineInstr*, 4>::iterator UI = VNUseCount[CurrVN].begin();
1241 int StoreFrameIndex;
1242 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1243 while (UI != VNUseCount[CurrVN].end() &&
1244 (StoreVReg == (*LI)->reg && StoreFrameIndex == FrameIndex)) {
1245 ++UI;
1246 if (UI != VNUseCount[CurrVN].end())
1247 StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1248 }
Owen Anderson45e68552009-01-29 05:28:55 +00001249 if (UI == VNUseCount[CurrVN].end()) continue;
1250
1251 MachineInstr* use = *UI;
1252
Owen Anderson4bfc2092009-01-29 05:41:02 +00001253 // Attempt to fold it away!
Owen Anderson45e68552009-01-29 05:28:55 +00001254 int OpIdx = use->findRegisterUseOperandIdx((*LI)->reg, false);
1255 if (OpIdx == -1) continue;
Owen Anderson45e68552009-01-29 05:28:55 +00001256 SmallVector<unsigned, 1> Ops;
1257 Ops.push_back(OpIdx);
Owen Anderson45e68552009-01-29 05:28:55 +00001258 if (!TII->canFoldMemoryOperand(use, Ops)) continue;
1259
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +00001260 MachineInstr* NewMI = TII->foldMemoryOperand(use, Ops, FrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001261
1262 if (!NewMI) continue;
1263
Owen Anderson4bfc2092009-01-29 05:41:02 +00001264 // Update relevant analyses.
Owen Anderson45e68552009-01-29 05:28:55 +00001265 LIs->RemoveMachineInstrFromMaps(DefMI);
1266 LIs->ReplaceMachineInstrInMaps(use, NewMI);
1267 (*LI)->removeValNo(CurrVN);
1268
1269 DefMI->eraseFromParent();
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +00001270 use->eraseFromParent();
Owen Anderson45e68552009-01-29 05:28:55 +00001271 VNUseCount[CurrVN].erase(use);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +00001272
Owen Anderson4bfc2092009-01-29 05:41:02 +00001273 // Remove deleted instructions. Note that we need to remove them from
1274 // the VNInfo->use map as well, just to be safe.
Owen Anderson45e68552009-01-29 05:28:55 +00001275 for (SmallPtrSet<MachineInstr*, 4>::iterator II =
1276 VNUseCount[CurrVN].begin(), IE = VNUseCount[CurrVN].end();
1277 II != IE; ++II) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001278 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
Owen Andersonc0f3a032009-01-29 08:22:06 +00001279 VNI = VNUseCount.begin(), VNE = VNUseCount.end(); VNI != VNE;
1280 ++VNI)
1281 if (VNI->first != CurrVN)
1282 VNI->second.erase(*II);
Owen Anderson45e68552009-01-29 05:28:55 +00001283 LIs->RemoveMachineInstrFromMaps(*II);
1284 (*II)->eraseFromParent();
1285 }
Owen Andersonc0f3a032009-01-29 08:22:06 +00001286
1287 VNUseCount.erase(CurrVN);
Owen Anderson45e68552009-01-29 05:28:55 +00001288
1289 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
1290 VI = VNUseCount.begin(), VE = VNUseCount.end(); VI != VE; ++VI)
1291 if (VI->second.erase(use))
1292 VI->second.insert(NewMI);
1293
Dan Gohmanfe601042010-06-22 15:08:57 +00001294 ++NumDeadSpills;
Owen Anderson45e68552009-01-29 05:28:55 +00001295 changed = true;
1296 continue;
1297 }
1298
Owen Anderson4bfc2092009-01-29 05:41:02 +00001299 // If there's more than one non-store instruction, we can't profitably
1300 // fold it, so bail.
Owen Anderson45e68552009-01-29 05:28:55 +00001301 if (NonSpillCount) continue;
Owen Anderson32ca8652009-01-24 10:07:43 +00001302
Owen Anderson4bfc2092009-01-29 05:41:02 +00001303 // Otherwise, this is a load-store case, so DCE them.
Owen Anderson32ca8652009-01-24 10:07:43 +00001304 for (SmallPtrSet<MachineInstr*, 4>::iterator UI =
1305 VNUseCount[CurrVN].begin(), UE = VNUseCount[CurrVN].end();
Lang Hamesf035ce52009-11-12 01:24:08 +00001306 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001307 LIs->RemoveMachineInstrFromMaps(*UI);
1308 (*UI)->eraseFromParent();
1309 }
1310
Owen Andersonc0f3a032009-01-29 08:22:06 +00001311 VNUseCount.erase(CurrVN);
1312
Owen Anderson32ca8652009-01-24 10:07:43 +00001313 LIs->RemoveMachineInstrFromMaps(DefMI);
1314 (*LI)->removeValNo(CurrVN);
1315 DefMI->eraseFromParent();
Dan Gohmanfe601042010-06-22 15:08:57 +00001316 ++NumDeadSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001317 changed = true;
Owen Anderson956ec272009-01-23 00:23:32 +00001318 }
1319 }
1320
1321 return changed;
1322}
1323
Owen Andersonf1f75b12008-11-04 22:22:41 +00001324bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1325 MachineBasicBlock* DefMBB,
1326 MachineBasicBlock* BarrierMBB) {
1327 if (DefMBB == BarrierMBB)
1328 return false;
1329
Lang Hames857c4e02009-06-17 21:01:20 +00001330 if (LR->valno->hasPHIKill())
Owen Andersonf1f75b12008-11-04 22:22:41 +00001331 return false;
1332
Lang Hames233a60e2009-11-03 23:52:08 +00001333 SlotIndex MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
Owen Andersonf1f75b12008-11-04 22:22:41 +00001334 if (LR->end < MBBEnd)
1335 return false;
1336
1337 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1338 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1339 return true;
1340
1341 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1342 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1343 typedef std::pair<MachineBasicBlock*,
1344 MachineBasicBlock::succ_iterator> ItPair;
1345 SmallVector<ItPair, 4> Stack;
1346 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1347
1348 while (!Stack.empty()) {
1349 ItPair P = Stack.back();
1350 Stack.pop_back();
1351
1352 MachineBasicBlock* PredMBB = P.first;
1353 MachineBasicBlock::succ_iterator S = P.second;
1354
1355 if (S == PredMBB->succ_end())
1356 continue;
1357 else if (Visited.count(*S)) {
1358 Stack.push_back(std::make_pair(PredMBB, ++S));
1359 continue;
1360 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001361 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001362
1363 MachineBasicBlock* MBB = *S;
1364 Visited.insert(MBB);
1365
1366 if (MBB == BarrierMBB)
1367 return true;
1368
1369 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1370 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1371 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1372 while (MDTN) {
1373 if (MDTN == DefMDTN)
1374 return true;
1375 else if (MDTN == BarrierMDTN)
1376 break;
1377 MDTN = MDTN->getIDom();
1378 }
1379
1380 MBBEnd = LIs->getMBBEndIdx(MBB);
1381 if (LR->end > MBBEnd)
1382 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1383 }
1384
1385 return false;
1386}
1387
1388
Evan Cheng09e8ca82008-10-20 21:44:59 +00001389bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001390 CurrMF = &MF;
1391 TM = &MF.getTarget();
Owen Anderson3ef45492009-01-29 22:13:06 +00001392 TRI = TM->getRegisterInfo();
Evan Chengd0e32c52008-10-29 05:06:14 +00001393 TII = TM->getInstrInfo();
1394 MFI = MF.getFrameInfo();
1395 MRI = &MF.getRegInfo();
Lang Hames233a60e2009-11-03 23:52:08 +00001396 SIs = &getAnalysis<SlotIndexes>();
Evan Chengd0e32c52008-10-29 05:06:14 +00001397 LIs = &getAnalysis<LiveIntervals>();
1398 LSs = &getAnalysis<LiveStacks>();
Owen Anderson420dd372009-03-14 21:40:05 +00001399 VRM = &getAnalysis<VirtRegMap>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001400
1401 bool MadeChange = false;
1402
1403 // Make sure blocks are numbered in order.
1404 MF.RenumberBlocks();
1405
Evan Cheng54898932008-10-29 08:39:34 +00001406 MachineBasicBlock *Entry = MF.begin();
1407 SmallPtrSet<MachineBasicBlock*,16> Visited;
1408
Owen Anderson956ec272009-01-23 00:23:32 +00001409 SmallPtrSet<LiveInterval*, 8> Split;
1410
Evan Cheng54898932008-10-29 08:39:34 +00001411 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1412 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1413 DFI != E; ++DFI) {
1414 BarrierMBB = *DFI;
1415 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1416 E = BarrierMBB->end(); I != E; ++I) {
1417 Barrier = &*I;
1418 const TargetRegisterClass **BarrierRCs =
1419 Barrier->getDesc().getRegClassBarriers();
1420 if (!BarrierRCs)
1421 continue;
1422 BarrierIdx = LIs->getInstructionIndex(Barrier);
Owen Anderson956ec272009-01-23 00:23:32 +00001423 MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split);
Evan Cheng54898932008-10-29 08:39:34 +00001424 }
1425 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001426
Owen Anderson956ec272009-01-23 00:23:32 +00001427 MadeChange |= removeDeadSpills(Split);
1428
Evan Chengf5cd4f02008-10-23 20:43:13 +00001429 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001430}