blob: 73d1b670a3d2a9827ee08b04c921cdaf253c22bd [file] [log] [blame]
Evan Cheng09e8ca82008-10-20 21:44:59 +00001//===-- PreAllocSplitting.cpp - Pre-allocation Interval Spltting Pass. ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the machine instruction level pre-register allocation
11// live interval splitting pass. It finds live interval barriers, i.e.
12// instructions which will kill all physical registers in certain register
13// classes, and split all live intervals which cross the barrier.
14//
15//===----------------------------------------------------------------------===//
16
17#define DEBUG_TYPE "pre-alloc-split"
Owen Anderson420dd372009-03-14 21:40:05 +000018#include "VirtRegMap.h"
Lang Hamesa937f222009-12-14 06:49:42 +000019#include "llvm/CodeGen/CalcSpillWeights.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000020#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000021#include "llvm/CodeGen/LiveStackAnalysis.h"
Owen Andersonf1f75b12008-11-04 22:22:41 +000022#include "llvm/CodeGen/MachineDominators.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineLoopInfo.h"
26#include "llvm/CodeGen/MachineRegisterInfo.h"
27#include "llvm/CodeGen/Passes.h"
28#include "llvm/CodeGen/RegisterCoalescer.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000029#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000030#include "llvm/Target/TargetMachine.h"
31#include "llvm/Target/TargetOptions.h"
32#include "llvm/Target/TargetRegisterInfo.h"
33#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000035#include "llvm/Support/ErrorHandling.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000036#include "llvm/ADT/DenseMap.h"
Evan Cheng54898932008-10-29 08:39:34 +000037#include "llvm/ADT/DepthFirstIterator.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000038#include "llvm/ADT/SmallPtrSet.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000039#include "llvm/ADT/Statistic.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000040using namespace llvm;
41
Evan Chengae7fa5b2008-10-28 01:48:24 +000042static cl::opt<int> PreSplitLimit("pre-split-limit", cl::init(-1), cl::Hidden);
Evan Cheng63040ce2009-11-09 06:49:22 +000043static cl::opt<int> DeadSplitLimit("dead-split-limit", cl::init(-1),
44 cl::Hidden);
45static cl::opt<int> RestoreFoldLimit("restore-fold-limit", cl::init(-1),
46 cl::Hidden);
Evan Chengae7fa5b2008-10-28 01:48:24 +000047
Owen Anderson45e68552009-01-29 05:28:55 +000048STATISTIC(NumSplits, "Number of intervals split");
Owen Anderson75fa96b2008-11-19 04:28:29 +000049STATISTIC(NumRemats, "Number of intervals split by rematerialization");
Owen Anderson7b9d67c2008-12-02 18:53:47 +000050STATISTIC(NumFolds, "Number of intervals split with spill folding");
Owen Anderson323c58d2009-03-05 07:19:18 +000051STATISTIC(NumRestoreFolds, "Number of intervals split with restore folding");
Owen Anderson2ebf63f2008-12-18 01:27:19 +000052STATISTIC(NumRenumbers, "Number of intervals renumbered into new registers");
Owen Anderson956ec272009-01-23 00:23:32 +000053STATISTIC(NumDeadSpills, "Number of dead spills removed");
Evan Chengf5cd4f02008-10-23 20:43:13 +000054
Evan Cheng09e8ca82008-10-20 21:44:59 +000055namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000056 class PreAllocSplitting : public MachineFunctionPass {
Evan Chengd0e32c52008-10-29 05:06:14 +000057 MachineFunction *CurrMF;
Evan Chengf5cd4f02008-10-23 20:43:13 +000058 const TargetMachine *TM;
59 const TargetInstrInfo *TII;
Owen Anderson3ef45492009-01-29 22:13:06 +000060 const TargetRegisterInfo* TRI;
Evan Chengf5cd4f02008-10-23 20:43:13 +000061 MachineFrameInfo *MFI;
62 MachineRegisterInfo *MRI;
Lang Hames233a60e2009-11-03 23:52:08 +000063 SlotIndexes *SIs;
Evan Chengf5cd4f02008-10-23 20:43:13 +000064 LiveIntervals *LIs;
Evan Chengd0e32c52008-10-29 05:06:14 +000065 LiveStacks *LSs;
Owen Anderson420dd372009-03-14 21:40:05 +000066 VirtRegMap *VRM;
Evan Cheng09e8ca82008-10-20 21:44:59 +000067
Evan Chengf5cd4f02008-10-23 20:43:13 +000068 // Barrier - Current barrier being processed.
69 MachineInstr *Barrier;
70
71 // BarrierMBB - Basic block where the barrier resides in.
72 MachineBasicBlock *BarrierMBB;
73
74 // Barrier - Current barrier index.
Lang Hames233a60e2009-11-03 23:52:08 +000075 SlotIndex BarrierIdx;
Evan Chengf5cd4f02008-10-23 20:43:13 +000076
77 // CurrLI - Current live interval being split.
78 LiveInterval *CurrLI;
79
Evan Chengd0e32c52008-10-29 05:06:14 +000080 // CurrSLI - Current stack slot live interval.
81 LiveInterval *CurrSLI;
82
83 // CurrSValNo - Current val# for the stack slot live interval.
84 VNInfo *CurrSValNo;
85
86 // IntervalSSMap - A map from live interval to spill slots.
87 DenseMap<unsigned, int> IntervalSSMap;
Evan Chengf5cd4f02008-10-23 20:43:13 +000088
Evan Cheng54898932008-10-29 08:39:34 +000089 // Def2SpillMap - A map from a def instruction index to spill index.
Lang Hames233a60e2009-11-03 23:52:08 +000090 DenseMap<SlotIndex, SlotIndex> Def2SpillMap;
Evan Cheng06587492008-10-24 02:05:00 +000091
Evan Cheng09e8ca82008-10-20 21:44:59 +000092 public:
93 static char ID;
Lang Hames233a60e2009-11-03 23:52:08 +000094 PreAllocSplitting()
Owen Anderson90c579d2010-08-06 18:33:48 +000095 : MachineFunctionPass(ID) {}
Evan Cheng09e8ca82008-10-20 21:44:59 +000096
97 virtual bool runOnMachineFunction(MachineFunction &MF);
98
99 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +0000100 AU.setPreservesCFG();
Lang Hames233a60e2009-11-03 23:52:08 +0000101 AU.addRequired<SlotIndexes>();
102 AU.addPreserved<SlotIndexes>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000103 AU.addRequired<LiveIntervals>();
104 AU.addPreserved<LiveIntervals>();
Evan Chengd0e32c52008-10-29 05:06:14 +0000105 AU.addRequired<LiveStacks>();
106 AU.addPreserved<LiveStacks>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000107 AU.addPreserved<RegisterCoalescer>();
Lang Hamesa937f222009-12-14 06:49:42 +0000108 AU.addPreserved<CalculateSpillWeights>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000109 if (StrongPHIElim)
110 AU.addPreservedID(StrongPHIEliminationID);
111 else
112 AU.addPreservedID(PHIEliminationID);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000113 AU.addRequired<MachineDominatorTree>();
114 AU.addRequired<MachineLoopInfo>();
Owen Anderson420dd372009-03-14 21:40:05 +0000115 AU.addRequired<VirtRegMap>();
Owen Andersonf1f75b12008-11-04 22:22:41 +0000116 AU.addPreserved<MachineDominatorTree>();
117 AU.addPreserved<MachineLoopInfo>();
Owen Anderson420dd372009-03-14 21:40:05 +0000118 AU.addPreserved<VirtRegMap>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000119 MachineFunctionPass::getAnalysisUsage(AU);
120 }
121
122 virtual void releaseMemory() {
Evan Chengd0e32c52008-10-29 05:06:14 +0000123 IntervalSSMap.clear();
Evan Cheng54898932008-10-29 08:39:34 +0000124 Def2SpillMap.clear();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000125 }
126
127 virtual const char *getPassName() const {
128 return "Pre-Register Allocaton Live Interval Splitting";
129 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000130
131 /// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000132 virtual void print(raw_ostream &O, const Module* M = 0) const {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000133 LIs->print(O, M);
134 }
135
Evan Chengf5cd4f02008-10-23 20:43:13 +0000136
137 private:
Evan Chengf5cd4f02008-10-23 20:43:13 +0000138
139 MachineBasicBlock::iterator
Evan Cheng1f08cc22008-10-28 05:28:21 +0000140 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
Lang Hamesb3661582009-11-14 00:02:51 +0000141 SmallPtrSet<MachineInstr*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000142
143 MachineBasicBlock::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000144 findRestorePoint(MachineBasicBlock*, MachineInstr*, SlotIndex,
Lang Hamesb3661582009-11-14 00:02:51 +0000145 SmallPtrSet<MachineInstr*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000146
Evan Chengd0e32c52008-10-29 05:06:14 +0000147 int CreateSpillStackSlot(unsigned, const TargetRegisterClass *);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000148
Lang Hames86511252009-09-04 20:41:11 +0000149 bool IsAvailableInStack(MachineBasicBlock*, unsigned,
Lang Hames233a60e2009-11-03 23:52:08 +0000150 SlotIndex, SlotIndex,
151 SlotIndex&, int&) const;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000152
Lang Hames233a60e2009-11-03 23:52:08 +0000153 void UpdateSpillSlotInterval(VNInfo*, SlotIndex, SlotIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000154
Evan Chengf5cd4f02008-10-23 20:43:13 +0000155 bool SplitRegLiveInterval(LiveInterval*);
156
Owen Anderson956ec272009-01-23 00:23:32 +0000157 bool SplitRegLiveIntervals(const TargetRegisterClass **,
158 SmallPtrSet<LiveInterval*, 8>&);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000159
160 bool createsNewJoin(LiveRange* LR, MachineBasicBlock* DefMBB,
161 MachineBasicBlock* BarrierMBB);
Owen Anderson75fa96b2008-11-19 04:28:29 +0000162 bool Rematerialize(unsigned vreg, VNInfo* ValNo,
163 MachineInstr* DefMI,
164 MachineBasicBlock::iterator RestorePt,
Owen Anderson75fa96b2008-11-19 04:28:29 +0000165 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000166 MachineInstr* FoldSpill(unsigned vreg, const TargetRegisterClass* RC,
167 MachineInstr* DefMI,
168 MachineInstr* Barrier,
169 MachineBasicBlock* MBB,
170 int& SS,
171 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersonc93023a2009-03-04 08:52:31 +0000172 MachineInstr* FoldRestore(unsigned vreg,
173 const TargetRegisterClass* RC,
174 MachineInstr* Barrier,
175 MachineBasicBlock* MBB,
176 int SS,
177 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000178 void RenumberValno(VNInfo* VN);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000179 void ReconstructLiveInterval(LiveInterval* LI);
Owen Anderson956ec272009-01-23 00:23:32 +0000180 bool removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split);
Owen Anderson45e68552009-01-29 05:28:55 +0000181 unsigned getNumberOfNonSpills(SmallPtrSet<MachineInstr*, 4>& MIs,
182 unsigned Reg, int FrameIndex, bool& TwoAddr);
Evan Cheng19a72582009-02-02 18:33:18 +0000183 VNInfo* PerformPHIConstruction(MachineBasicBlock::iterator Use,
184 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000185 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000186 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
187 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
188 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000189 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
190 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000191 bool IsTopLevel, bool IsIntraBlock);
192 VNInfo* PerformPHIConstructionFallBack(MachineBasicBlock::iterator Use,
193 MachineBasicBlock* MBB, LiveInterval* LI,
194 SmallPtrSet<MachineInstr*, 4>& Visited,
195 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
196 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
197 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
198 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
199 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
200 bool IsTopLevel, bool IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000201};
Evan Cheng09e8ca82008-10-20 21:44:59 +0000202} // end anonymous namespace
203
204char PreAllocSplitting::ID = 0;
205
Owen Anderson2ab36d32010-10-12 19:48:12 +0000206INITIALIZE_PASS_BEGIN(PreAllocSplitting, "pre-alloc-splitting",
207 "Pre-Register Allocation Live Interval Splitting",
208 false, false)
209INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
210INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
211INITIALIZE_PASS_DEPENDENCY(LiveStacks)
212INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
213INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
214INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
215INITIALIZE_PASS_END(PreAllocSplitting, "pre-alloc-splitting",
Owen Anderson02dd53e2010-08-23 17:52:01 +0000216 "Pre-Register Allocation Live Interval Splitting",
Owen Andersonce665bd2010-10-07 22:25:06 +0000217 false, false)
Evan Cheng09e8ca82008-10-20 21:44:59 +0000218
Owen Anderson90c579d2010-08-06 18:33:48 +0000219char &llvm::PreAllocSplittingID = PreAllocSplitting::ID;
Evan Cheng09e8ca82008-10-20 21:44:59 +0000220
Evan Chengf5cd4f02008-10-23 20:43:13 +0000221/// findSpillPoint - Find a gap as far away from the given MI that's suitable
222/// for spilling the current live interval. The index must be before any
223/// defs and uses of the live interval register in the mbb. Return begin() if
224/// none is found.
225MachineBasicBlock::iterator
226PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000227 MachineInstr *DefMI,
Lang Hamesb3661582009-11-14 00:02:51 +0000228 SmallPtrSet<MachineInstr*, 4> &RefsInMBB) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000229 MachineBasicBlock::iterator Pt = MBB->begin();
230
Owen Anderson696a1302009-03-31 08:27:09 +0000231 MachineBasicBlock::iterator MII = MI;
232 MachineBasicBlock::iterator EndPt = DefMI
233 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
Owen Anderson4cafbb52009-02-20 10:02:23 +0000234
Owen Anderson696a1302009-03-31 08:27:09 +0000235 while (MII != EndPt && !RefsInMBB.count(MII) &&
236 MII->getOpcode() != TRI->getCallFrameSetupOpcode())
237 --MII;
238 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson4cafbb52009-02-20 10:02:23 +0000239
Owen Anderson696a1302009-03-31 08:27:09 +0000240 while (MII != EndPt && !RefsInMBB.count(MII)) {
Owen Anderson696a1302009-03-31 08:27:09 +0000241 // We can't insert the spill between the barrier (a call), and its
242 // corresponding call frame setup.
243 if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode()) {
244 while (MII->getOpcode() != TRI->getCallFrameSetupOpcode()) {
Owen Anderson5734c942009-02-05 05:58:41 +0000245 --MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000246 if (MII == EndPt) {
247 return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000248 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000249 }
Owen Anderson696a1302009-03-31 08:27:09 +0000250 continue;
Lang Hamesb3661582009-11-14 00:02:51 +0000251 } else {
Owen Anderson696a1302009-03-31 08:27:09 +0000252 Pt = MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000253 }
Owen Anderson696a1302009-03-31 08:27:09 +0000254
255 if (RefsInMBB.count(MII))
256 return Pt;
257
258
259 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000260 }
261
262 return Pt;
263}
264
265/// findRestorePoint - Find a gap in the instruction index map that's suitable
266/// for restoring the current live interval value. The index must be before any
267/// uses of the live interval register in the mbb. Return end() if none is
268/// found.
269MachineBasicBlock::iterator
270PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000271 SlotIndex LastIdx,
Lang Hamesb3661582009-11-14 00:02:51 +0000272 SmallPtrSet<MachineInstr*, 4> &RefsInMBB) {
Evan Cheng54898932008-10-29 08:39:34 +0000273 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
274 // begin index accordingly.
Owen Anderson5a92d4e2008-11-18 20:53:59 +0000275 MachineBasicBlock::iterator Pt = MBB->end();
Owen Anderson696a1302009-03-31 08:27:09 +0000276 MachineBasicBlock::iterator EndPt = MBB->getFirstTerminator();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000277
Owen Anderson696a1302009-03-31 08:27:09 +0000278 // We start at the call, so walk forward until we find the call frame teardown
279 // since we can't insert restores before that. Bail if we encounter a use
280 // during this time.
281 MachineBasicBlock::iterator MII = MI;
282 if (MII == EndPt) return Pt;
283
284 while (MII != EndPt && !RefsInMBB.count(MII) &&
285 MII->getOpcode() != TRI->getCallFrameDestroyOpcode())
286 ++MII;
287 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
288 ++MII;
289
290 // FIXME: Limit the number of instructions to examine to reduce
291 // compile time?
292 while (MII != EndPt) {
Lang Hames233a60e2009-11-03 23:52:08 +0000293 SlotIndex Index = LIs->getInstructionIndex(MII);
Owen Anderson696a1302009-03-31 08:27:09 +0000294 if (Index > LastIdx)
295 break;
Owen Anderson3ef45492009-01-29 22:13:06 +0000296
Owen Anderson696a1302009-03-31 08:27:09 +0000297 // We can't insert a restore between the barrier (a call) and its
298 // corresponding call frame teardown.
299 if (MII->getOpcode() == TRI->getCallFrameSetupOpcode()) {
300 do {
301 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000302 ++MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000303 } while (MII->getOpcode() != TRI->getCallFrameDestroyOpcode());
Lang Hamesb3661582009-11-14 00:02:51 +0000304 } else {
Owen Anderson696a1302009-03-31 08:27:09 +0000305 Pt = MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000306 }
Owen Anderson696a1302009-03-31 08:27:09 +0000307
308 if (RefsInMBB.count(MII))
309 return Pt;
310
311 ++MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000312 }
313
314 return Pt;
315}
316
Evan Chengd0e32c52008-10-29 05:06:14 +0000317/// CreateSpillStackSlot - Create a stack slot for the live interval being
318/// split. If the live interval was previously split, just reuse the same
319/// slot.
320int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
321 const TargetRegisterClass *RC) {
322 int SS;
323 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
324 if (I != IntervalSSMap.end()) {
325 SS = I->second;
326 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000327 SS = MFI->CreateSpillStackObject(RC->getSize(), RC->getAlignment());
Evan Chengd0e32c52008-10-29 05:06:14 +0000328 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000329 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000330
331 // Create live interval for stack slot.
Evan Chengc781a242009-05-03 18:32:42 +0000332 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Evan Cheng54898932008-10-29 08:39:34 +0000333 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000334 CurrSValNo = CurrSLI->getValNumInfo(0);
335 else
Lang Hames6e2968c2010-09-25 12:04:16 +0000336 CurrSValNo = CurrSLI->getNextValue(SlotIndex(), 0,
Lang Hames86511252009-09-04 20:41:11 +0000337 LSs->getVNInfoAllocator());
Evan Chengd0e32c52008-10-29 05:06:14 +0000338 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000339}
340
Evan Chengd0e32c52008-10-29 05:06:14 +0000341/// IsAvailableInStack - Return true if register is available in a split stack
342/// slot at the specified index.
343bool
Evan Cheng54898932008-10-29 08:39:34 +0000344PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
Lang Hames233a60e2009-11-03 23:52:08 +0000345 unsigned Reg, SlotIndex DefIndex,
346 SlotIndex RestoreIndex,
347 SlotIndex &SpillIndex,
Evan Cheng54898932008-10-29 08:39:34 +0000348 int& SS) const {
349 if (!DefMBB)
350 return false;
351
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000352 DenseMap<unsigned, int>::const_iterator I = IntervalSSMap.find(Reg);
Evan Chengd0e32c52008-10-29 05:06:14 +0000353 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000354 return false;
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000355 DenseMap<SlotIndex, SlotIndex>::const_iterator
Lang Hames86511252009-09-04 20:41:11 +0000356 II = Def2SpillMap.find(DefIndex);
Evan Cheng54898932008-10-29 08:39:34 +0000357 if (II == Def2SpillMap.end())
358 return false;
359
360 // If last spill of def is in the same mbb as barrier mbb (where restore will
361 // be), make sure it's not below the intended restore index.
362 // FIXME: Undo the previous spill?
363 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
364 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
365 return false;
366
367 SS = I->second;
368 SpillIndex = II->second;
369 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000370}
371
Evan Chengd0e32c52008-10-29 05:06:14 +0000372/// UpdateSpillSlotInterval - Given the specified val# of the register live
373/// interval being split, and the spill and restore indicies, update the live
374/// interval of the spill stack slot.
375void
Lang Hames233a60e2009-11-03 23:52:08 +0000376PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, SlotIndex SpillIndex,
377 SlotIndex RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000378 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
379 "Expect restore in the barrier mbb");
380
381 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
382 if (MBB == BarrierMBB) {
383 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000384 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
385 CurrSLI->addRange(SLR);
386 return;
387 }
388
Evan Cheng54898932008-10-29 08:39:34 +0000389 SmallPtrSet<MachineBasicBlock*, 4> Processed;
Lang Hames233a60e2009-11-03 23:52:08 +0000390 SlotIndex EndIdx = LIs->getMBBEndIdx(MBB);
Lang Hames74ab5ee2009-12-22 00:11:50 +0000391 LiveRange SLR(SpillIndex, EndIdx, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000392 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000393 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000394
395 // Start from the spill mbb, figure out the extend of the spill slot's
396 // live interval.
397 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000398 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
399 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000400 // If live range extend beyond end of mbb, add successors to work list.
401 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
402 SE = MBB->succ_end(); SI != SE; ++SI)
403 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000404
405 while (!WorkList.empty()) {
406 MachineBasicBlock *MBB = WorkList.back();
407 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000408 if (Processed.count(MBB))
409 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000410 SlotIndex Idx = LIs->getMBBStartIdx(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000411 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000412 if (LR && LR->valno == ValNo) {
413 EndIdx = LIs->getMBBEndIdx(MBB);
414 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000415 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000416 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000417 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000418 } else if (LR->end > EndIdx) {
419 // Live range extends beyond end of mbb, process successors.
Lang Hames233a60e2009-11-03 23:52:08 +0000420 LiveRange SLR(Idx, EndIdx.getNextIndex(), CurrSValNo);
Evan Cheng54898932008-10-29 08:39:34 +0000421 CurrSLI->addRange(SLR);
422 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
423 SE = MBB->succ_end(); SI != SE; ++SI)
424 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000425 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000426 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000427 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000428 }
Evan Cheng54898932008-10-29 08:39:34 +0000429 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000430 }
431 }
432}
433
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000434/// PerformPHIConstruction - From properly set up use and def lists, use a PHI
435/// construction algorithm to compute the ranges and valnos for an interval.
Evan Cheng19a72582009-02-02 18:33:18 +0000436VNInfo*
437PreAllocSplitting::PerformPHIConstruction(MachineBasicBlock::iterator UseI,
438 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000439 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000440 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
441 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
442 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000443 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
444 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000445 bool IsTopLevel, bool IsIntraBlock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000446 // Return memoized result if it's available.
Evan Cheng19a72582009-02-02 18:33:18 +0000447 if (IsTopLevel && Visited.count(UseI) && NewVNs.count(UseI))
448 return NewVNs[UseI];
449 else if (!IsTopLevel && IsIntraBlock && NewVNs.count(UseI))
450 return NewVNs[UseI];
451 else if (!IsIntraBlock && LiveOut.count(MBB))
Owen Anderson7d211e22008-12-31 02:00:25 +0000452 return LiveOut[MBB];
453
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000454 // Check if our block contains any uses or defs.
Owen Anderson7d211e22008-12-31 02:00:25 +0000455 bool ContainsDefs = Defs.count(MBB);
456 bool ContainsUses = Uses.count(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000457
Evan Cheng19a72582009-02-02 18:33:18 +0000458 VNInfo* RetVNI = 0;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000459
460 // Enumerate the cases of use/def contaning blocks.
461 if (!ContainsDefs && !ContainsUses) {
Evan Cheng19a72582009-02-02 18:33:18 +0000462 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
463 NewVNs, LiveOut, Phis,
464 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000465 } else if (ContainsDefs && !ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000466 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000467
468 // Search for the def in this block. If we don't find it before the
469 // instruction we care about, go to the fallback case. Note that that
Owen Anderson7d211e22008-12-31 02:00:25 +0000470 // should never happen: this cannot be intrablock, so use should
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000471 // always be an end() iterator.
Evan Cheng19a72582009-02-02 18:33:18 +0000472 assert(UseI == MBB->end() && "No use marked in intrablock");
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000473
Evan Cheng19a72582009-02-02 18:33:18 +0000474 MachineBasicBlock::iterator Walker = UseI;
475 --Walker;
476 while (Walker != MBB->begin()) {
477 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000478 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000479 --Walker;
480 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000481
482 // Once we've found it, extend its VNInfo to our instruction.
Lang Hames233a60e2009-11-03 23:52:08 +0000483 SlotIndex DefIndex = LIs->getInstructionIndex(Walker);
484 DefIndex = DefIndex.getDefIndex();
485 SlotIndex EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000486
Evan Cheng19a72582009-02-02 18:33:18 +0000487 RetVNI = NewVNs[Walker];
Lang Hames74ab5ee2009-12-22 00:11:50 +0000488 LI->addRange(LiveRange(DefIndex, EndIndex, RetVNI));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000489 } else if (!ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000490 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000491
492 // Search for the use in this block that precedes the instruction we care
Evan Cheng19a72582009-02-02 18:33:18 +0000493 // about, going to the fallback case if we don't find it.
Evan Cheng19a72582009-02-02 18:33:18 +0000494 MachineBasicBlock::iterator Walker = UseI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000495 bool found = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000496 while (Walker != MBB->begin()) {
Benjamin Kramer2e0de6f2010-01-07 19:46:15 +0000497 --Walker;
Evan Cheng19a72582009-02-02 18:33:18 +0000498 if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000499 found = true;
500 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000501 }
Evan Cheng19a72582009-02-02 18:33:18 +0000502 }
Benjamin Kramer2e0de6f2010-01-07 19:46:15 +0000503
504 if (!found)
505 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
506 Uses, NewVNs, LiveOut, Phis,
507 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000508
Lang Hames233a60e2009-11-03 23:52:08 +0000509 SlotIndex UseIndex = LIs->getInstructionIndex(Walker);
510 UseIndex = UseIndex.getUseIndex();
511 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000512 if (IsIntraBlock) {
Lang Hames74ab5ee2009-12-22 00:11:50 +0000513 EndIndex = LIs->getInstructionIndex(UseI).getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000514 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000515 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000516
517 // Now, recursively phi construct the VNInfo for the use we found,
518 // and then extend it to include the instruction we care about
Evan Cheng19a72582009-02-02 18:33:18 +0000519 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
520 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000521
Lang Hames74ab5ee2009-12-22 00:11:50 +0000522 LI->addRange(LiveRange(UseIndex, EndIndex, RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000523
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000524 // FIXME: Need to set kills properly for inter-block stuff.
Evan Cheng19a72582009-02-02 18:33:18 +0000525 } else if (ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000526 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
527 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000528
529 // This case is basically a merging of the two preceding case, with the
530 // special note that checking for defs must take precedence over checking
531 // for uses, because of two-address instructions.
Evan Cheng19a72582009-02-02 18:33:18 +0000532 MachineBasicBlock::iterator Walker = UseI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000533 bool foundDef = false;
534 bool foundUse = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000535 while (Walker != MBB->begin()) {
Benjamin Kramer2e0de6f2010-01-07 19:46:15 +0000536 --Walker;
Evan Cheng19a72582009-02-02 18:33:18 +0000537 if (BlockDefs.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000538 foundDef = true;
539 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000540 } else if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000541 foundUse = true;
542 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000543 }
Evan Cheng19a72582009-02-02 18:33:18 +0000544 }
Benjamin Kramer2e0de6f2010-01-07 19:46:15 +0000545
546 if (!foundDef && !foundUse)
547 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
548 Uses, NewVNs, LiveOut, Phis,
549 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000550
Lang Hames233a60e2009-11-03 23:52:08 +0000551 SlotIndex StartIndex = LIs->getInstructionIndex(Walker);
552 StartIndex = foundDef ? StartIndex.getDefIndex() : StartIndex.getUseIndex();
553 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000554 if (IsIntraBlock) {
Lang Hames74ab5ee2009-12-22 00:11:50 +0000555 EndIndex = LIs->getInstructionIndex(UseI).getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000556 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000557 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000558
559 if (foundDef)
Evan Cheng19a72582009-02-02 18:33:18 +0000560 RetVNI = NewVNs[Walker];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000561 else
Evan Cheng19a72582009-02-02 18:33:18 +0000562 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
563 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000564
Lang Hames74ab5ee2009-12-22 00:11:50 +0000565 LI->addRange(LiveRange(StartIndex, EndIndex, RetVNI));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000566 }
567
568 // Memoize results so we don't have to recompute them.
Evan Cheng19a72582009-02-02 18:33:18 +0000569 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000570 else {
Evan Cheng19a72582009-02-02 18:33:18 +0000571 if (!NewVNs.count(UseI))
572 NewVNs[UseI] = RetVNI;
573 Visited.insert(UseI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000574 }
575
Evan Cheng19a72582009-02-02 18:33:18 +0000576 return RetVNI;
577}
578
579/// PerformPHIConstructionFallBack - PerformPHIConstruction fall back path.
580///
581VNInfo*
582PreAllocSplitting::PerformPHIConstructionFallBack(MachineBasicBlock::iterator UseI,
583 MachineBasicBlock* MBB, LiveInterval* LI,
584 SmallPtrSet<MachineInstr*, 4>& Visited,
585 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
586 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
587 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
588 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
589 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
590 bool IsTopLevel, bool IsIntraBlock) {
591 // NOTE: Because this is the fallback case from other cases, we do NOT
592 // assume that we are not intrablock here.
593 if (Phis.count(MBB)) return Phis[MBB];
594
Lang Hames233a60e2009-11-03 23:52:08 +0000595 SlotIndex StartIndex = LIs->getMBBStartIdx(MBB);
Lang Hames857c4e02009-06-17 21:01:20 +0000596 VNInfo *RetVNI = Phis[MBB] =
Lang Hames6e2968c2010-09-25 12:04:16 +0000597 LI->getNextValue(SlotIndex(), /*FIXME*/ 0,
Lang Hames86511252009-09-04 20:41:11 +0000598 LIs->getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +0000599
Evan Cheng19a72582009-02-02 18:33:18 +0000600 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
601
602 // If there are no uses or defs between our starting point and the
603 // beginning of the block, then recursive perform phi construction
604 // on our predecessors.
605 DenseMap<MachineBasicBlock*, VNInfo*> IncomingVNs;
606 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
607 PE = MBB->pred_end(); PI != PE; ++PI) {
608 VNInfo* Incoming = PerformPHIConstruction((*PI)->end(), *PI, LI,
609 Visited, Defs, Uses, NewVNs,
610 LiveOut, Phis, false, false);
611 if (Incoming != 0)
612 IncomingVNs[*PI] = Incoming;
613 }
614
Lang Hames857c4e02009-06-17 21:01:20 +0000615 if (MBB->pred_size() == 1 && !RetVNI->hasPHIKill()) {
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000616 VNInfo* OldVN = RetVNI;
617 VNInfo* NewVN = IncomingVNs.begin()->second;
618 VNInfo* MergedVN = LI->MergeValueNumberInto(OldVN, NewVN);
619 if (MergedVN == OldVN) std::swap(OldVN, NewVN);
620
621 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator LOI = LiveOut.begin(),
622 LOE = LiveOut.end(); LOI != LOE; ++LOI)
623 if (LOI->second == OldVN)
624 LOI->second = MergedVN;
625 for (DenseMap<MachineInstr*, VNInfo*>::iterator NVI = NewVNs.begin(),
626 NVE = NewVNs.end(); NVI != NVE; ++NVI)
627 if (NVI->second == OldVN)
628 NVI->second = MergedVN;
629 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator PI = Phis.begin(),
630 PE = Phis.end(); PI != PE; ++PI)
631 if (PI->second == OldVN)
632 PI->second = MergedVN;
633 RetVNI = MergedVN;
Evan Cheng19a72582009-02-02 18:33:18 +0000634 } else {
635 // Otherwise, merge the incoming VNInfos with a phi join. Create a new
636 // VNInfo to represent the joined value.
637 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator I =
638 IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) {
Lang Hames857c4e02009-06-17 21:01:20 +0000639 I->second->setHasPHIKill(true);
Evan Cheng19a72582009-02-02 18:33:18 +0000640 }
641 }
642
Lang Hames233a60e2009-11-03 23:52:08 +0000643 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000644 if (IsIntraBlock) {
Lang Hames74ab5ee2009-12-22 00:11:50 +0000645 EndIndex = LIs->getInstructionIndex(UseI).getDefIndex();
Evan Cheng19a72582009-02-02 18:33:18 +0000646 } else
647 EndIndex = LIs->getMBBEndIdx(MBB);
Lang Hames74ab5ee2009-12-22 00:11:50 +0000648 LI->addRange(LiveRange(StartIndex, EndIndex, RetVNI));
Evan Cheng19a72582009-02-02 18:33:18 +0000649
650 // Memoize results so we don't have to recompute them.
651 if (!IsIntraBlock)
652 LiveOut[MBB] = RetVNI;
653 else {
654 if (!NewVNs.count(UseI))
655 NewVNs[UseI] = RetVNI;
656 Visited.insert(UseI);
657 }
658
659 return RetVNI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000660}
661
662/// ReconstructLiveInterval - Recompute a live interval from scratch.
663void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) {
Benjamin Kramer991de142010-03-30 20:16:45 +0000664 VNInfo::Allocator& Alloc = LIs->getVNInfoAllocator();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000665
666 // Clear the old ranges and valnos;
667 LI->clear();
668
669 // Cache the uses and defs of the register
670 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
671 RegMap Defs, Uses;
672
673 // Keep track of the new VNs we're creating.
674 DenseMap<MachineInstr*, VNInfo*> NewVNs;
675 SmallPtrSet<VNInfo*, 2> PhiVNs;
676
677 // Cache defs, and create a new VNInfo for each def.
678 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
679 DE = MRI->def_end(); DI != DE; ++DI) {
680 Defs[(*DI).getParent()].insert(&*DI);
681
Lang Hames233a60e2009-11-03 23:52:08 +0000682 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI);
683 DefIdx = DefIdx.getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000684
Chris Lattner518bb532010-02-09 19:54:29 +0000685 assert(!DI->isPHI() && "PHI instr in code during pre-alloc splitting.");
Lang Hames6e2968c2010-09-25 12:04:16 +0000686 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, Alloc);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000687
688 // If the def is a move, set the copy field.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000689 if (DI->isCopyLike() && DI->getOperand(0).getReg() == LI->reg)
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000690 NewVN->setCopy(&*DI);
691
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000692 NewVNs[&*DI] = NewVN;
693 }
694
695 // Cache uses as a separate pass from actually processing them.
696 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
697 UE = MRI->use_end(); UI != UE; ++UI)
698 Uses[(*UI).getParent()].insert(&*UI);
699
700 // Now, actually process every use and use a phi construction algorithm
701 // to walk from it to its reaching definitions, building VNInfos along
702 // the way.
Owen Anderson7d211e22008-12-31 02:00:25 +0000703 DenseMap<MachineBasicBlock*, VNInfo*> LiveOut;
704 DenseMap<MachineBasicBlock*, VNInfo*> Phis;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000705 SmallPtrSet<MachineInstr*, 4> Visited;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000706 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
707 UE = MRI->use_end(); UI != UE; ++UI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000708 PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000709 Uses, NewVNs, LiveOut, Phis, true, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000710 }
Owen Andersond4f6fe52008-12-28 23:35:13 +0000711
712 // Add ranges for dead defs
713 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
714 DE = MRI->def_end(); DI != DE; ++DI) {
Lang Hames233a60e2009-11-03 23:52:08 +0000715 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI);
716 DefIdx = DefIdx.getDefIndex();
Owen Andersond4f6fe52008-12-28 23:35:13 +0000717
718 if (LI->liveAt(DefIdx)) continue;
719
720 VNInfo* DeadVN = NewVNs[&*DI];
Lang Hames233a60e2009-11-03 23:52:08 +0000721 LI->addRange(LiveRange(DefIdx, DefIdx.getNextSlot(), DeadVN));
Evan Cheng35ca9202009-10-09 01:17:11 +0000722 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000723}
724
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000725/// RenumberValno - Split the given valno out into a new vreg, allowing it to
726/// be allocated to a different register. This function creates a new vreg,
727/// copies the valno and its live ranges over to the new vreg's interval,
728/// removes them from the old interval, and rewrites all uses and defs of
729/// the original reg to the new vreg within those ranges.
730void PreAllocSplitting::RenumberValno(VNInfo* VN) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000731 SmallVector<VNInfo*, 4> Stack;
732 SmallVector<VNInfo*, 4> VNsToCopy;
733 Stack.push_back(VN);
734
735 // Walk through and copy the valno we care about, and any other valnos
736 // that are two-address redefinitions of the one we care about. These
737 // will need to be rewritten as well. We also check for safety of the
738 // renumbering here, by making sure that none of the valno involved has
739 // phi kills.
740 while (!Stack.empty()) {
741 VNInfo* OldVN = Stack.back();
742 Stack.pop_back();
743
744 // Bail out if we ever encounter a valno that has a PHI kill. We can't
745 // renumber these.
Lang Hames857c4e02009-06-17 21:01:20 +0000746 if (OldVN->hasPHIKill()) return;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000747
748 VNsToCopy.push_back(OldVN);
749
750 // Locate two-address redefinitions
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000751 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(CurrLI->reg),
752 DE = MRI->def_end(); DI != DE; ++DI) {
753 if (!DI->isRegTiedToUseOperand(DI.getOperandNo())) continue;
754 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI).getDefIndex();
755 VNInfo* NextVN = CurrLI->findDefinedVNInfoForRegInt(DefIdx);
756 if (std::find(VNsToCopy.begin(), VNsToCopy.end(), NextVN) !=
757 VNsToCopy.end())
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000758 Stack.push_back(NextVN);
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000759 }
760 }
761
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000762 // Create the new vreg
763 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
764
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000765 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000766 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000767
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000768 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
769 VNsToCopy.end(); OI != OE; ++OI) {
770 VNInfo* OldVN = *OI;
771
772 // Copy the valno over
Lang Hames857c4e02009-06-17 21:01:20 +0000773 VNInfo* NewVN = NewLI.createValueCopy(OldVN, LIs->getVNInfoAllocator());
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000774 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
775
776 // Remove the valno from the old interval
777 CurrLI->removeValNo(OldVN);
778 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000779
780 // Rewrite defs and uses. This is done in two stages to avoid invalidating
781 // the reg_iterator.
782 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
783
784 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
785 E = MRI->reg_end(); I != E; ++I) {
786 MachineOperand& MO = I.getOperand();
Lang Hames233a60e2009-11-03 23:52:08 +0000787 SlotIndex InstrIdx = LIs->getInstructionIndex(&*I);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000788
Lang Hames233a60e2009-11-03 23:52:08 +0000789 if ((MO.isUse() && NewLI.liveAt(InstrIdx.getUseIndex())) ||
790 (MO.isDef() && NewLI.liveAt(InstrIdx.getDefIndex())))
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000791 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
792 }
793
794 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
795 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
796 MachineInstr* Inst = I->first;
797 unsigned OpIdx = I->second;
798 MachineOperand& MO = Inst->getOperand(OpIdx);
799 MO.setReg(NewVReg);
800 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000801
Owen Anderson420dd372009-03-14 21:40:05 +0000802 // Grow the VirtRegMap, since we've created a new vreg.
803 VRM->grow();
804
Owen Anderson45e68552009-01-29 05:28:55 +0000805 // The renumbered vreg shares a stack slot with the old register.
806 if (IntervalSSMap.count(CurrLI->reg))
807 IntervalSSMap[NewVReg] = IntervalSSMap[CurrLI->reg];
808
Dan Gohmanfe601042010-06-22 15:08:57 +0000809 ++NumRenumbers;
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000810}
811
Evan Cheng37844532009-07-16 09:20:10 +0000812bool PreAllocSplitting::Rematerialize(unsigned VReg, VNInfo* ValNo,
Owen Anderson6002e992008-12-04 21:20:30 +0000813 MachineInstr* DefMI,
814 MachineBasicBlock::iterator RestorePt,
Owen Anderson6002e992008-12-04 21:20:30 +0000815 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
816 MachineBasicBlock& MBB = *RestorePt->getParent();
817
818 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
Lang Hamescec29452010-09-26 03:37:09 +0000819 if (!DefMI || DefMI->getParent() == BarrierMBB)
Lang Hamesb3661582009-11-14 00:02:51 +0000820 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB);
Owen Anderson6002e992008-12-04 21:20:30 +0000821 else
Chris Lattner7896c9f2009-12-03 00:50:42 +0000822 KillPt = llvm::next(MachineBasicBlock::iterator(DefMI));
Owen Anderson6002e992008-12-04 21:20:30 +0000823
824 if (KillPt == DefMI->getParent()->end())
825 return false;
826
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000827 TII->reMaterialize(MBB, RestorePt, VReg, 0, DefMI, *TRI);
Lang Hamesb3661582009-11-14 00:02:51 +0000828 SlotIndex RematIdx = LIs->InsertMachineInstrInMaps(prior(RestorePt));
Owen Anderson6002e992008-12-04 21:20:30 +0000829
Owen Andersonb4b84362009-01-26 21:57:31 +0000830 ReconstructLiveInterval(CurrLI);
Lang Hames233a60e2009-11-03 23:52:08 +0000831 RematIdx = RematIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +0000832 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RematIdx));
Owen Andersone1762c92009-01-12 03:10:40 +0000833
Owen Anderson75fa96b2008-11-19 04:28:29 +0000834 ++NumSplits;
835 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000836 return true;
837}
838
839MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
840 const TargetRegisterClass* RC,
841 MachineInstr* DefMI,
842 MachineInstr* Barrier,
843 MachineBasicBlock* MBB,
844 int& SS,
845 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000846 // Go top down if RefsInMBB is empty.
847 if (RefsInMBB.empty())
848 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000849
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000850 MachineBasicBlock::iterator FoldPt = Barrier;
851 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
852 !RefsInMBB.count(FoldPt))
853 --FoldPt;
854
Evan Cheng1015ba72010-05-21 20:53:24 +0000855 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000856 if (OpIdx == -1)
857 return 0;
858
859 SmallVector<unsigned, 1> Ops;
860 Ops.push_back(OpIdx);
861
862 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
863 return 0;
864
865 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
866 if (I != IntervalSSMap.end()) {
867 SS = I->second;
868 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000869 SS = MFI->CreateSpillStackObject(RC->getSize(), RC->getAlignment());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000870 }
871
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000872 MachineInstr* FMI = TII->foldMemoryOperand(FoldPt, Ops, SS);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000873
874 if (FMI) {
875 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000876 FoldPt->eraseFromParent();
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000877 ++NumFolds;
878
879 IntervalSSMap[vreg] = SS;
Evan Chengc781a242009-05-03 18:32:42 +0000880 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000881 if (CurrSLI->hasAtLeastOneValue())
882 CurrSValNo = CurrSLI->getValNumInfo(0);
883 else
Lang Hames6e2968c2010-09-25 12:04:16 +0000884 CurrSValNo = CurrSLI->getNextValue(SlotIndex(), 0,
Lang Hames86511252009-09-04 20:41:11 +0000885 LSs->getVNInfoAllocator());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000886 }
887
888 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000889}
890
Owen Andersonc93023a2009-03-04 08:52:31 +0000891MachineInstr* PreAllocSplitting::FoldRestore(unsigned vreg,
892 const TargetRegisterClass* RC,
893 MachineInstr* Barrier,
894 MachineBasicBlock* MBB,
895 int SS,
896 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
Owen Andersona2bfb542009-03-05 08:23:20 +0000897 if ((int)RestoreFoldLimit != -1 && RestoreFoldLimit == (int)NumRestoreFolds)
Owen Anderson323c58d2009-03-05 07:19:18 +0000898 return 0;
899
Owen Andersonc93023a2009-03-04 08:52:31 +0000900 // Go top down if RefsInMBB is empty.
901 if (RefsInMBB.empty())
902 return 0;
903
904 // Can't fold a restore between a call stack setup and teardown.
905 MachineBasicBlock::iterator FoldPt = Barrier;
Owen Anderson323c58d2009-03-05 07:19:18 +0000906
907 // Advance from barrier to call frame teardown.
908 while (FoldPt != MBB->getFirstTerminator() &&
909 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
910 if (RefsInMBB.count(FoldPt))
911 return 0;
Owen Andersonc93023a2009-03-04 08:52:31 +0000912
Owen Anderson323c58d2009-03-05 07:19:18 +0000913 ++FoldPt;
914 }
915
916 if (FoldPt == MBB->getFirstTerminator())
917 return 0;
918 else
919 ++FoldPt;
920
921 // Now find the restore point.
922 while (FoldPt != MBB->getFirstTerminator() && !RefsInMBB.count(FoldPt)) {
Owen Andersonc93023a2009-03-04 08:52:31 +0000923 if (FoldPt->getOpcode() == TRI->getCallFrameSetupOpcode()) {
924 while (FoldPt != MBB->getFirstTerminator() &&
925 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
926 if (RefsInMBB.count(FoldPt))
927 return 0;
928
929 ++FoldPt;
930 }
931
Owen Anderson323c58d2009-03-05 07:19:18 +0000932 if (FoldPt == MBB->getFirstTerminator())
933 return 0;
934 }
935
936 ++FoldPt;
Owen Andersonc93023a2009-03-04 08:52:31 +0000937 }
938
939 if (FoldPt == MBB->getFirstTerminator())
940 return 0;
941
942 int OpIdx = FoldPt->findRegisterUseOperandIdx(vreg, true);
943 if (OpIdx == -1)
944 return 0;
945
946 SmallVector<unsigned, 1> Ops;
947 Ops.push_back(OpIdx);
948
949 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
950 return 0;
951
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000952 MachineInstr* FMI = TII->foldMemoryOperand(FoldPt, Ops, SS);
Owen Andersonc93023a2009-03-04 08:52:31 +0000953
954 if (FMI) {
955 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000956 FoldPt->eraseFromParent();
Owen Anderson323c58d2009-03-05 07:19:18 +0000957 ++NumRestoreFolds;
Owen Andersonc93023a2009-03-04 08:52:31 +0000958 }
959
960 return FMI;
961}
962
Evan Chengf5cd4f02008-10-23 20:43:13 +0000963/// SplitRegLiveInterval - Split (spill and restore) the given live interval
964/// so it would not cross the barrier that's being processed. Shrink wrap
965/// (minimize) the live interval to the last uses.
966bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
David Greene4175f582010-01-05 01:25:47 +0000967 DEBUG(dbgs() << "Pre-alloc splitting " << LI->reg << " for " << *Barrier
Lang Hames233a60e2009-11-03 23:52:08 +0000968 << " result: ");
969
Evan Chengf5cd4f02008-10-23 20:43:13 +0000970 CurrLI = LI;
971
972 // Find live range where current interval cross the barrier.
973 LiveInterval::iterator LR =
Lang Hames233a60e2009-11-03 23:52:08 +0000974 CurrLI->FindLiveRangeContaining(BarrierIdx.getUseIndex());
Evan Chengf5cd4f02008-10-23 20:43:13 +0000975 VNInfo *ValNo = LR->valno;
976
Torok Edwinf3689232009-07-12 20:07:01 +0000977 assert(!ValNo->isUnused() && "Val# is defined by a dead def?");
Evan Chengf5cd4f02008-10-23 20:43:13 +0000978
Lang Hames6e2968c2010-09-25 12:04:16 +0000979 MachineInstr *DefMI = LIs->getInstructionFromIndex(ValNo->def);
Evan Cheng06587492008-10-24 02:05:00 +0000980
Owen Andersond3be4622009-01-21 08:18:03 +0000981 // If this would create a new join point, do not split.
Lang Hames233a60e2009-11-03 23:52:08 +0000982 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent())) {
David Greene4175f582010-01-05 01:25:47 +0000983 DEBUG(dbgs() << "FAILED (would create a new join point).\n");
Owen Andersond3be4622009-01-21 08:18:03 +0000984 return false;
Lang Hames233a60e2009-11-03 23:52:08 +0000985 }
Owen Andersond3be4622009-01-21 08:18:03 +0000986
Evan Chengf5cd4f02008-10-23 20:43:13 +0000987 // Find all references in the barrier mbb.
988 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
989 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
990 E = MRI->reg_end(); I != E; ++I) {
991 MachineInstr *RefMI = &*I;
992 if (RefMI->getParent() == BarrierMBB)
993 RefsInMBB.insert(RefMI);
994 }
995
996 // Find a point to restore the value after the barrier.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000997 MachineBasicBlock::iterator RestorePt =
Lang Hamesb3661582009-11-14 00:02:51 +0000998 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +0000999 if (RestorePt == BarrierMBB->end()) {
David Greene4175f582010-01-05 01:25:47 +00001000 DEBUG(dbgs() << "FAILED (could not find a suitable restore point).\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001001 return false;
Lang Hames233a60e2009-11-03 23:52:08 +00001002 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001003
Owen Anderson75fa96b2008-11-19 04:28:29 +00001004 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
Lang Hamesb3661582009-11-14 00:02:51 +00001005 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt, RefsInMBB)) {
David Greene4175f582010-01-05 01:25:47 +00001006 DEBUG(dbgs() << "success (remat).\n");
Lang Hames233a60e2009-11-03 23:52:08 +00001007 return true;
1008 }
Owen Anderson75fa96b2008-11-19 04:28:29 +00001009
Evan Chengf5cd4f02008-10-23 20:43:13 +00001010 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +00001011 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001012 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Lang Hames233a60e2009-11-03 23:52:08 +00001013 SlotIndex SpillIndex;
Evan Cheng06587492008-10-24 02:05:00 +00001014 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +00001015 int SS = -1;
Lang Hamescec29452010-09-26 03:37:09 +00001016 if (!DefMI) {
Lang Hames857c4e02009-06-17 21:01:20 +00001017 // If we don't know where the def is we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001018 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
1019 BarrierMBB, SS, RefsInMBB))) {
1020 SpillIndex = LIs->getInstructionIndex(SpillMI);
1021 } else {
1022 MachineBasicBlock::iterator SpillPt =
Lang Hamesb3661582009-11-14 00:02:51 +00001023 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +00001024 if (SpillPt == BarrierMBB->begin()) {
David Greene4175f582010-01-05 01:25:47 +00001025 DEBUG(dbgs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001026 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001027 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001028 // Add spill.
1029
1030 SS = CreateSpillStackSlot(CurrLI->reg, RC);
Evan Cheng746ad692010-05-06 19:06:44 +00001031 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC,
1032 TRI);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001033 SpillMI = prior(SpillPt);
Lang Hamesb3661582009-11-14 00:02:51 +00001034 SpillIndex = LIs->InsertMachineInstrInMaps(SpillMI);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001035 }
Evan Cheng54898932008-10-29 08:39:34 +00001036 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
Lang Hamesb3661582009-11-14 00:02:51 +00001037 LIs->getZeroIndex(), SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +00001038 // If it's already split, just restore the value. There is no need to spill
1039 // the def again.
Lang Hames233a60e2009-11-03 23:52:08 +00001040 if (!DefMI) {
David Greene4175f582010-01-05 01:25:47 +00001041 DEBUG(dbgs() << "FAILED (def is dead).\n");
Evan Chengd0e32c52008-10-29 05:06:14 +00001042 return false; // Def is dead. Do nothing.
Lang Hames233a60e2009-11-03 23:52:08 +00001043 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001044
1045 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
Evan Cheng35ca9202009-10-09 01:17:11 +00001046 BarrierMBB, SS, RefsInMBB))) {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001047 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001048 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001049 // Check if it's possible to insert a spill after the def MI.
1050 MachineBasicBlock::iterator SpillPt;
1051 if (DefMBB == BarrierMBB) {
1052 // Add spill after the def and the last use before the barrier.
1053 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
Lang Hamesb3661582009-11-14 00:02:51 +00001054 RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +00001055 if (SpillPt == DefMBB->begin()) {
David Greene4175f582010-01-05 01:25:47 +00001056 DEBUG(dbgs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001057 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001058 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001059 } else {
Chris Lattner7896c9f2009-12-03 00:50:42 +00001060 SpillPt = llvm::next(MachineBasicBlock::iterator(DefMI));
Lang Hames233a60e2009-11-03 23:52:08 +00001061 if (SpillPt == DefMBB->end()) {
David Greene4175f582010-01-05 01:25:47 +00001062 DEBUG(dbgs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001063 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001064 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001065 }
Evan Cheng35ca9202009-10-09 01:17:11 +00001066 // Add spill.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001067 SS = CreateSpillStackSlot(CurrLI->reg, RC);
Evan Cheng746ad692010-05-06 19:06:44 +00001068 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg, false, SS, RC,
1069 TRI);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001070 SpillMI = prior(SpillPt);
Lang Hamesb3661582009-11-14 00:02:51 +00001071 SpillIndex = LIs->InsertMachineInstrInMaps(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001072 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001073 }
1074
Evan Cheng54898932008-10-29 08:39:34 +00001075 // Remember def instruction index to spill index mapping.
1076 if (DefMI && SpillMI)
1077 Def2SpillMap[ValNo->def] = SpillIndex;
1078
Evan Chengf5cd4f02008-10-23 20:43:13 +00001079 // Add restore.
Owen Andersonc93023a2009-03-04 08:52:31 +00001080 bool FoldedRestore = false;
Lang Hamesb3661582009-11-14 00:02:51 +00001081 SlotIndex RestoreIndex;
Owen Andersonc93023a2009-03-04 08:52:31 +00001082 if (MachineInstr* LMI = FoldRestore(CurrLI->reg, RC, Barrier,
1083 BarrierMBB, SS, RefsInMBB)) {
1084 RestorePt = LMI;
Owen Anderson323c58d2009-03-05 07:19:18 +00001085 RestoreIndex = LIs->getInstructionIndex(RestorePt);
Owen Andersonc93023a2009-03-04 08:52:31 +00001086 FoldedRestore = true;
1087 } else {
Evan Cheng746ad692010-05-06 19:06:44 +00001088 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC, TRI);
Owen Andersonc93023a2009-03-04 08:52:31 +00001089 MachineInstr *LoadMI = prior(RestorePt);
Lang Hamesb3661582009-11-14 00:02:51 +00001090 RestoreIndex = LIs->InsertMachineInstrInMaps(LoadMI);
Owen Andersonc93023a2009-03-04 08:52:31 +00001091 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001092
Evan Chengd0e32c52008-10-29 05:06:14 +00001093 // Update spill stack slot live interval.
Lang Hames233a60e2009-11-03 23:52:08 +00001094 UpdateSpillSlotInterval(ValNo, SpillIndex.getUseIndex().getNextSlot(),
1095 RestoreIndex.getDefIndex());
Evan Chengd0e32c52008-10-29 05:06:14 +00001096
Owen Andersonb4b84362009-01-26 21:57:31 +00001097 ReconstructLiveInterval(CurrLI);
Evan Cheng35ca9202009-10-09 01:17:11 +00001098
Owen Andersonc93023a2009-03-04 08:52:31 +00001099 if (!FoldedRestore) {
Lang Hames233a60e2009-11-03 23:52:08 +00001100 SlotIndex RestoreIdx = LIs->getInstructionIndex(prior(RestorePt));
1101 RestoreIdx = RestoreIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +00001102 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RestoreIdx));
Owen Andersonc93023a2009-03-04 08:52:31 +00001103 }
Owen Anderson7d211e22008-12-31 02:00:25 +00001104
Evan Chengae7fa5b2008-10-28 01:48:24 +00001105 ++NumSplits;
David Greene4175f582010-01-05 01:25:47 +00001106 DEBUG(dbgs() << "success.\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001107 return true;
1108}
1109
1110/// SplitRegLiveIntervals - Split all register live intervals that cross the
1111/// barrier that's being processed.
1112bool
Owen Anderson956ec272009-01-23 00:23:32 +00001113PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs,
1114 SmallPtrSet<LiveInterval*, 8>& Split) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001115 // First find all the virtual registers whose live intervals are intercepted
1116 // by the current barrier.
1117 SmallVector<LiveInterval*, 8> Intervals;
1118 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng4350eb82009-02-06 17:17:30 +00001119 // FIXME: If it's not safe to move any instruction that defines the barrier
1120 // register class, then it means there are some special dependencies which
1121 // codegen is not modelling. Ignore these barriers for now.
1122 if (!TII->isSafeToMoveRegClassDefs(*RC))
Evan Cheng23066282008-10-27 07:14:50 +00001123 continue;
Chris Lattner65569b82010-05-21 17:47:50 +00001124 const std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
Evan Chengf5cd4f02008-10-23 20:43:13 +00001125 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1126 unsigned Reg = VRs[i];
1127 if (!LIs->hasInterval(Reg))
1128 continue;
1129 LiveInterval *LI = &LIs->getInterval(Reg);
1130 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1131 // Virtual register live interval is intercepted by the barrier. We
1132 // should split and shrink wrap its interval if possible.
1133 Intervals.push_back(LI);
1134 }
1135 }
1136
1137 // Process the affected live intervals.
1138 bool Change = false;
1139 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001140 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1141 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001142 LiveInterval *LI = Intervals.back();
1143 Intervals.pop_back();
Owen Anderson956ec272009-01-23 00:23:32 +00001144 bool result = SplitRegLiveInterval(LI);
1145 if (result) Split.insert(LI);
1146 Change |= result;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001147 }
1148
1149 return Change;
1150}
1151
Owen Anderson45e68552009-01-29 05:28:55 +00001152unsigned PreAllocSplitting::getNumberOfNonSpills(
Owen Anderson32ca8652009-01-24 10:07:43 +00001153 SmallPtrSet<MachineInstr*, 4>& MIs,
Owen Anderson45e68552009-01-29 05:28:55 +00001154 unsigned Reg, int FrameIndex,
1155 bool& FeedsTwoAddr) {
1156 unsigned NonSpills = 0;
Owen Anderson32ca8652009-01-24 10:07:43 +00001157 for (SmallPtrSet<MachineInstr*, 4>::iterator UI = MIs.begin(), UE = MIs.end();
Owen Anderson45e68552009-01-29 05:28:55 +00001158 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001159 int StoreFrameIndex;
1160 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001161 if (StoreVReg != Reg || StoreFrameIndex != FrameIndex)
Dan Gohmanfe601042010-06-22 15:08:57 +00001162 ++NonSpills;
Owen Anderson45e68552009-01-29 05:28:55 +00001163
1164 int DefIdx = (*UI)->findRegisterDefOperandIdx(Reg);
Bob Wilsond9df5012009-04-09 17:16:43 +00001165 if (DefIdx != -1 && (*UI)->isRegTiedToUseOperand(DefIdx))
Owen Anderson45e68552009-01-29 05:28:55 +00001166 FeedsTwoAddr = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001167 }
1168
Owen Anderson45e68552009-01-29 05:28:55 +00001169 return NonSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001170}
1171
Owen Anderson956ec272009-01-23 00:23:32 +00001172/// removeDeadSpills - After doing splitting, filter through all intervals we've
1173/// split, and see if any of the spills are unnecessary. If so, remove them.
1174bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) {
1175 bool changed = false;
1176
Owen Anderson4bfc2092009-01-29 05:41:02 +00001177 // Walk over all of the live intervals that were touched by the splitter,
1178 // and see if we can do any DCE and/or folding.
Owen Anderson956ec272009-01-23 00:23:32 +00001179 for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(),
1180 LE = split.end(); LI != LE; ++LI) {
Owen Anderson9ce499a2009-01-23 03:28:53 +00001181 DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> > VNUseCount;
Owen Anderson956ec272009-01-23 00:23:32 +00001182
Owen Anderson4bfc2092009-01-29 05:41:02 +00001183 // First, collect all the uses of the vreg, and sort them by their
1184 // reaching definition (VNInfo).
Owen Anderson956ec272009-01-23 00:23:32 +00001185 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg),
1186 UE = MRI->use_end(); UI != UE; ++UI) {
Lang Hames233a60e2009-11-03 23:52:08 +00001187 SlotIndex index = LIs->getInstructionIndex(&*UI);
1188 index = index.getUseIndex();
Owen Anderson956ec272009-01-23 00:23:32 +00001189
1190 const LiveRange* LR = (*LI)->getLiveRangeContaining(index);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001191 VNUseCount[LR->valno].insert(&*UI);
Owen Anderson956ec272009-01-23 00:23:32 +00001192 }
1193
Owen Anderson4bfc2092009-01-29 05:41:02 +00001194 // Now, take the definitions (VNInfo's) one at a time and try to DCE
1195 // and/or fold them away.
Owen Anderson956ec272009-01-23 00:23:32 +00001196 for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(),
1197 VE = (*LI)->vni_end(); VI != VE; ++VI) {
Owen Anderson45e68552009-01-29 05:28:55 +00001198
1199 if (DeadSplitLimit != -1 && (int)NumDeadSpills == DeadSplitLimit)
1200 return changed;
1201
Owen Anderson956ec272009-01-23 00:23:32 +00001202 VNInfo* CurrVN = *VI;
Owen Anderson4bfc2092009-01-29 05:41:02 +00001203
1204 // We don't currently try to handle definitions with PHI kills, because
1205 // it would involve processing more than one VNInfo at once.
Lang Hames857c4e02009-06-17 21:01:20 +00001206 if (CurrVN->hasPHIKill()) continue;
Owen Anderson956ec272009-01-23 00:23:32 +00001207
Owen Anderson4bfc2092009-01-29 05:41:02 +00001208 // We also don't try to handle the results of PHI joins, since there's
1209 // no defining instruction to analyze.
Lang Hames6e2968c2010-09-25 12:04:16 +00001210 MachineInstr* DefMI = LIs->getInstructionFromIndex(CurrVN->def);
1211 if (!DefMI || CurrVN->isUnused()) continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001212
Owen Anderson4bfc2092009-01-29 05:41:02 +00001213 // We're only interested in eliminating cruft introduced by the splitter,
1214 // is of the form load-use or load-use-store. First, check that the
1215 // definition is a load, and remember what stack slot we loaded it from.
Owen Anderson956ec272009-01-23 00:23:32 +00001216 int FrameIndex;
1217 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
1218
Owen Anderson4bfc2092009-01-29 05:41:02 +00001219 // If the definition has no uses at all, just DCE it.
Owen Anderson9ce499a2009-01-23 03:28:53 +00001220 if (VNUseCount[CurrVN].size() == 0) {
1221 LIs->RemoveMachineInstrFromMaps(DefMI);
1222 (*LI)->removeValNo(CurrVN);
1223 DefMI->eraseFromParent();
Owen Andersonc0f3a032009-01-29 08:22:06 +00001224 VNUseCount.erase(CurrVN);
Dan Gohmanfe601042010-06-22 15:08:57 +00001225 ++NumDeadSpills;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001226 changed = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001227 continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001228 }
Owen Anderson32ca8652009-01-24 10:07:43 +00001229
Owen Anderson4bfc2092009-01-29 05:41:02 +00001230 // Second, get the number of non-store uses of the definition, as well as
1231 // a flag indicating whether it feeds into a later two-address definition.
Owen Anderson45e68552009-01-29 05:28:55 +00001232 bool FeedsTwoAddr = false;
1233 unsigned NonSpillCount = getNumberOfNonSpills(VNUseCount[CurrVN],
1234 (*LI)->reg, FrameIndex,
1235 FeedsTwoAddr);
1236
Owen Anderson4bfc2092009-01-29 05:41:02 +00001237 // If there's one non-store use and it doesn't feed a two-addr, then
1238 // this is a load-use-store case that we can try to fold.
Owen Anderson45e68552009-01-29 05:28:55 +00001239 if (NonSpillCount == 1 && !FeedsTwoAddr) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001240 // Start by finding the non-store use MachineInstr.
Owen Anderson45e68552009-01-29 05:28:55 +00001241 SmallPtrSet<MachineInstr*, 4>::iterator UI = VNUseCount[CurrVN].begin();
1242 int StoreFrameIndex;
1243 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1244 while (UI != VNUseCount[CurrVN].end() &&
1245 (StoreVReg == (*LI)->reg && StoreFrameIndex == FrameIndex)) {
1246 ++UI;
1247 if (UI != VNUseCount[CurrVN].end())
1248 StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1249 }
Owen Anderson45e68552009-01-29 05:28:55 +00001250 if (UI == VNUseCount[CurrVN].end()) continue;
1251
1252 MachineInstr* use = *UI;
1253
Owen Anderson4bfc2092009-01-29 05:41:02 +00001254 // Attempt to fold it away!
Owen Anderson45e68552009-01-29 05:28:55 +00001255 int OpIdx = use->findRegisterUseOperandIdx((*LI)->reg, false);
1256 if (OpIdx == -1) continue;
Owen Anderson45e68552009-01-29 05:28:55 +00001257 SmallVector<unsigned, 1> Ops;
1258 Ops.push_back(OpIdx);
Owen Anderson45e68552009-01-29 05:28:55 +00001259 if (!TII->canFoldMemoryOperand(use, Ops)) continue;
1260
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +00001261 MachineInstr* NewMI = TII->foldMemoryOperand(use, Ops, FrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001262
1263 if (!NewMI) continue;
1264
Owen Anderson4bfc2092009-01-29 05:41:02 +00001265 // Update relevant analyses.
Owen Anderson45e68552009-01-29 05:28:55 +00001266 LIs->RemoveMachineInstrFromMaps(DefMI);
1267 LIs->ReplaceMachineInstrInMaps(use, NewMI);
1268 (*LI)->removeValNo(CurrVN);
1269
1270 DefMI->eraseFromParent();
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +00001271 use->eraseFromParent();
Owen Anderson45e68552009-01-29 05:28:55 +00001272 VNUseCount[CurrVN].erase(use);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +00001273
Owen Anderson4bfc2092009-01-29 05:41:02 +00001274 // Remove deleted instructions. Note that we need to remove them from
1275 // the VNInfo->use map as well, just to be safe.
Owen Anderson45e68552009-01-29 05:28:55 +00001276 for (SmallPtrSet<MachineInstr*, 4>::iterator II =
1277 VNUseCount[CurrVN].begin(), IE = VNUseCount[CurrVN].end();
1278 II != IE; ++II) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001279 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
Owen Andersonc0f3a032009-01-29 08:22:06 +00001280 VNI = VNUseCount.begin(), VNE = VNUseCount.end(); VNI != VNE;
1281 ++VNI)
1282 if (VNI->first != CurrVN)
1283 VNI->second.erase(*II);
Owen Anderson45e68552009-01-29 05:28:55 +00001284 LIs->RemoveMachineInstrFromMaps(*II);
1285 (*II)->eraseFromParent();
1286 }
Owen Andersonc0f3a032009-01-29 08:22:06 +00001287
1288 VNUseCount.erase(CurrVN);
Owen Anderson45e68552009-01-29 05:28:55 +00001289
1290 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
1291 VI = VNUseCount.begin(), VE = VNUseCount.end(); VI != VE; ++VI)
1292 if (VI->second.erase(use))
1293 VI->second.insert(NewMI);
1294
Dan Gohmanfe601042010-06-22 15:08:57 +00001295 ++NumDeadSpills;
Owen Anderson45e68552009-01-29 05:28:55 +00001296 changed = true;
1297 continue;
1298 }
1299
Owen Anderson4bfc2092009-01-29 05:41:02 +00001300 // If there's more than one non-store instruction, we can't profitably
1301 // fold it, so bail.
Owen Anderson45e68552009-01-29 05:28:55 +00001302 if (NonSpillCount) continue;
Owen Anderson32ca8652009-01-24 10:07:43 +00001303
Owen Anderson4bfc2092009-01-29 05:41:02 +00001304 // Otherwise, this is a load-store case, so DCE them.
Owen Anderson32ca8652009-01-24 10:07:43 +00001305 for (SmallPtrSet<MachineInstr*, 4>::iterator UI =
1306 VNUseCount[CurrVN].begin(), UE = VNUseCount[CurrVN].end();
Lang Hamesf035ce52009-11-12 01:24:08 +00001307 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001308 LIs->RemoveMachineInstrFromMaps(*UI);
1309 (*UI)->eraseFromParent();
1310 }
1311
Owen Andersonc0f3a032009-01-29 08:22:06 +00001312 VNUseCount.erase(CurrVN);
1313
Owen Anderson32ca8652009-01-24 10:07:43 +00001314 LIs->RemoveMachineInstrFromMaps(DefMI);
1315 (*LI)->removeValNo(CurrVN);
1316 DefMI->eraseFromParent();
Dan Gohmanfe601042010-06-22 15:08:57 +00001317 ++NumDeadSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001318 changed = true;
Owen Anderson956ec272009-01-23 00:23:32 +00001319 }
1320 }
1321
1322 return changed;
1323}
1324
Owen Andersonf1f75b12008-11-04 22:22:41 +00001325bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1326 MachineBasicBlock* DefMBB,
1327 MachineBasicBlock* BarrierMBB) {
1328 if (DefMBB == BarrierMBB)
1329 return false;
1330
Lang Hames857c4e02009-06-17 21:01:20 +00001331 if (LR->valno->hasPHIKill())
Owen Andersonf1f75b12008-11-04 22:22:41 +00001332 return false;
1333
Lang Hames233a60e2009-11-03 23:52:08 +00001334 SlotIndex MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
Owen Andersonf1f75b12008-11-04 22:22:41 +00001335 if (LR->end < MBBEnd)
1336 return false;
1337
1338 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1339 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1340 return true;
1341
1342 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1343 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1344 typedef std::pair<MachineBasicBlock*,
1345 MachineBasicBlock::succ_iterator> ItPair;
1346 SmallVector<ItPair, 4> Stack;
1347 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1348
1349 while (!Stack.empty()) {
1350 ItPair P = Stack.back();
1351 Stack.pop_back();
1352
1353 MachineBasicBlock* PredMBB = P.first;
1354 MachineBasicBlock::succ_iterator S = P.second;
1355
1356 if (S == PredMBB->succ_end())
1357 continue;
1358 else if (Visited.count(*S)) {
1359 Stack.push_back(std::make_pair(PredMBB, ++S));
1360 continue;
1361 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001362 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001363
1364 MachineBasicBlock* MBB = *S;
1365 Visited.insert(MBB);
1366
1367 if (MBB == BarrierMBB)
1368 return true;
1369
1370 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1371 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1372 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1373 while (MDTN) {
1374 if (MDTN == DefMDTN)
1375 return true;
1376 else if (MDTN == BarrierMDTN)
1377 break;
1378 MDTN = MDTN->getIDom();
1379 }
1380
1381 MBBEnd = LIs->getMBBEndIdx(MBB);
1382 if (LR->end > MBBEnd)
1383 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1384 }
1385
1386 return false;
1387}
1388
1389
Evan Cheng09e8ca82008-10-20 21:44:59 +00001390bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001391 CurrMF = &MF;
1392 TM = &MF.getTarget();
Owen Anderson3ef45492009-01-29 22:13:06 +00001393 TRI = TM->getRegisterInfo();
Evan Chengd0e32c52008-10-29 05:06:14 +00001394 TII = TM->getInstrInfo();
1395 MFI = MF.getFrameInfo();
1396 MRI = &MF.getRegInfo();
Lang Hames233a60e2009-11-03 23:52:08 +00001397 SIs = &getAnalysis<SlotIndexes>();
Evan Chengd0e32c52008-10-29 05:06:14 +00001398 LIs = &getAnalysis<LiveIntervals>();
1399 LSs = &getAnalysis<LiveStacks>();
Owen Anderson420dd372009-03-14 21:40:05 +00001400 VRM = &getAnalysis<VirtRegMap>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001401
1402 bool MadeChange = false;
1403
1404 // Make sure blocks are numbered in order.
1405 MF.RenumberBlocks();
1406
Evan Cheng54898932008-10-29 08:39:34 +00001407 MachineBasicBlock *Entry = MF.begin();
1408 SmallPtrSet<MachineBasicBlock*,16> Visited;
1409
Owen Anderson956ec272009-01-23 00:23:32 +00001410 SmallPtrSet<LiveInterval*, 8> Split;
1411
Evan Cheng54898932008-10-29 08:39:34 +00001412 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1413 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1414 DFI != E; ++DFI) {
1415 BarrierMBB = *DFI;
1416 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1417 E = BarrierMBB->end(); I != E; ++I) {
1418 Barrier = &*I;
1419 const TargetRegisterClass **BarrierRCs =
1420 Barrier->getDesc().getRegClassBarriers();
1421 if (!BarrierRCs)
1422 continue;
1423 BarrierIdx = LIs->getInstructionIndex(Barrier);
Owen Anderson956ec272009-01-23 00:23:32 +00001424 MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split);
Evan Cheng54898932008-10-29 08:39:34 +00001425 }
1426 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001427
Owen Anderson956ec272009-01-23 00:23:32 +00001428 MadeChange |= removeDeadSpills(Split);
1429
Evan Chengf5cd4f02008-10-23 20:43:13 +00001430 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001431}