blob: 837a526537f0394b18058a3302c0d0bb0c858c52 [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>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000110 if (StrongPHIElim)
111 AU.addPreservedID(StrongPHIEliminationID);
112 else
113 AU.addPreservedID(PHIEliminationID);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000114 AU.addRequired<MachineDominatorTree>();
115 AU.addRequired<MachineLoopInfo>();
Owen Anderson420dd372009-03-14 21:40:05 +0000116 AU.addRequired<VirtRegMap>();
Owen Andersonf1f75b12008-11-04 22:22:41 +0000117 AU.addPreserved<MachineDominatorTree>();
118 AU.addPreserved<MachineLoopInfo>();
Owen Anderson420dd372009-03-14 21:40:05 +0000119 AU.addPreserved<VirtRegMap>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000120 MachineFunctionPass::getAnalysisUsage(AU);
121 }
122
123 virtual void releaseMemory() {
Evan Chengd0e32c52008-10-29 05:06:14 +0000124 IntervalSSMap.clear();
Evan Cheng54898932008-10-29 08:39:34 +0000125 Def2SpillMap.clear();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000126 }
127
128 virtual const char *getPassName() const {
129 return "Pre-Register Allocaton Live Interval Splitting";
130 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000131
132 /// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000133 virtual void print(raw_ostream &O, const Module* M = 0) const {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000134 LIs->print(O, M);
135 }
136
Evan Chengf5cd4f02008-10-23 20:43:13 +0000137
138 private:
Evan Chengf5cd4f02008-10-23 20:43:13 +0000139
140 MachineBasicBlock::iterator
Evan Cheng1f08cc22008-10-28 05:28:21 +0000141 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
Lang Hamesb3661582009-11-14 00:02:51 +0000142 SmallPtrSet<MachineInstr*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000143
144 MachineBasicBlock::iterator
Lang Hames233a60e2009-11-03 23:52:08 +0000145 findRestorePoint(MachineBasicBlock*, MachineInstr*, SlotIndex,
Lang Hamesb3661582009-11-14 00:02:51 +0000146 SmallPtrSet<MachineInstr*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000147
Evan Chengd0e32c52008-10-29 05:06:14 +0000148 int CreateSpillStackSlot(unsigned, const TargetRegisterClass *);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000149
Lang Hames86511252009-09-04 20:41:11 +0000150 bool IsAvailableInStack(MachineBasicBlock*, unsigned,
Lang Hames233a60e2009-11-03 23:52:08 +0000151 SlotIndex, SlotIndex,
152 SlotIndex&, int&) const;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000153
Lang Hames233a60e2009-11-03 23:52:08 +0000154 void UpdateSpillSlotInterval(VNInfo*, SlotIndex, SlotIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000155
Evan Chengf5cd4f02008-10-23 20:43:13 +0000156 bool SplitRegLiveInterval(LiveInterval*);
157
Owen Anderson956ec272009-01-23 00:23:32 +0000158 bool SplitRegLiveIntervals(const TargetRegisterClass **,
159 SmallPtrSet<LiveInterval*, 8>&);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000160
161 bool createsNewJoin(LiveRange* LR, MachineBasicBlock* DefMBB,
162 MachineBasicBlock* BarrierMBB);
Owen Anderson75fa96b2008-11-19 04:28:29 +0000163 bool Rematerialize(unsigned vreg, VNInfo* ValNo,
164 MachineInstr* DefMI,
165 MachineBasicBlock::iterator RestorePt,
Owen Anderson75fa96b2008-11-19 04:28:29 +0000166 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000167 MachineInstr* FoldSpill(unsigned vreg, const TargetRegisterClass* RC,
168 MachineInstr* DefMI,
169 MachineInstr* Barrier,
170 MachineBasicBlock* MBB,
171 int& SS,
172 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersonc93023a2009-03-04 08:52:31 +0000173 MachineInstr* FoldRestore(unsigned vreg,
174 const TargetRegisterClass* RC,
175 MachineInstr* Barrier,
176 MachineBasicBlock* MBB,
177 int SS,
178 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000179 void RenumberValno(VNInfo* VN);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000180 void ReconstructLiveInterval(LiveInterval* LI);
Owen Anderson956ec272009-01-23 00:23:32 +0000181 bool removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split);
Owen Anderson45e68552009-01-29 05:28:55 +0000182 unsigned getNumberOfNonSpills(SmallPtrSet<MachineInstr*, 4>& MIs,
183 unsigned Reg, int FrameIndex, bool& TwoAddr);
Evan Cheng19a72582009-02-02 18:33:18 +0000184 VNInfo* PerformPHIConstruction(MachineBasicBlock::iterator Use,
185 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000186 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000187 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
188 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
189 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000190 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
191 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000192 bool IsTopLevel, bool IsIntraBlock);
193 VNInfo* PerformPHIConstructionFallBack(MachineBasicBlock::iterator Use,
194 MachineBasicBlock* MBB, LiveInterval* LI,
195 SmallPtrSet<MachineInstr*, 4>& Visited,
196 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
197 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
198 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
199 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
200 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
201 bool IsTopLevel, bool IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000202};
Evan Cheng09e8ca82008-10-20 21:44:59 +0000203} // end anonymous namespace
204
205char PreAllocSplitting::ID = 0;
206
Owen Anderson2ab36d32010-10-12 19:48:12 +0000207INITIALIZE_PASS_BEGIN(PreAllocSplitting, "pre-alloc-splitting",
208 "Pre-Register Allocation Live Interval Splitting",
209 false, false)
210INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
211INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
212INITIALIZE_PASS_DEPENDENCY(LiveStacks)
213INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
214INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
215INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
216INITIALIZE_PASS_END(PreAllocSplitting, "pre-alloc-splitting",
Owen Anderson02dd53e2010-08-23 17:52:01 +0000217 "Pre-Register Allocation Live Interval Splitting",
Owen Andersonce665bd2010-10-07 22:25:06 +0000218 false, false)
Evan Cheng09e8ca82008-10-20 21:44:59 +0000219
Owen Anderson90c579d2010-08-06 18:33:48 +0000220char &llvm::PreAllocSplittingID = PreAllocSplitting::ID;
Evan Cheng09e8ca82008-10-20 21:44:59 +0000221
Evan Chengf5cd4f02008-10-23 20:43:13 +0000222/// findSpillPoint - Find a gap as far away from the given MI that's suitable
223/// for spilling the current live interval. The index must be before any
224/// defs and uses of the live interval register in the mbb. Return begin() if
225/// none is found.
226MachineBasicBlock::iterator
227PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000228 MachineInstr *DefMI,
Lang Hamesb3661582009-11-14 00:02:51 +0000229 SmallPtrSet<MachineInstr*, 4> &RefsInMBB) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000230 MachineBasicBlock::iterator Pt = MBB->begin();
231
Owen Anderson696a1302009-03-31 08:27:09 +0000232 MachineBasicBlock::iterator MII = MI;
233 MachineBasicBlock::iterator EndPt = DefMI
234 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
Owen Anderson4cafbb52009-02-20 10:02:23 +0000235
Owen Anderson696a1302009-03-31 08:27:09 +0000236 while (MII != EndPt && !RefsInMBB.count(MII) &&
237 MII->getOpcode() != TRI->getCallFrameSetupOpcode())
238 --MII;
239 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson4cafbb52009-02-20 10:02:23 +0000240
Owen Anderson696a1302009-03-31 08:27:09 +0000241 while (MII != EndPt && !RefsInMBB.count(MII)) {
Owen Anderson696a1302009-03-31 08:27:09 +0000242 // We can't insert the spill between the barrier (a call), and its
243 // corresponding call frame setup.
244 if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode()) {
245 while (MII->getOpcode() != TRI->getCallFrameSetupOpcode()) {
Owen Anderson5734c942009-02-05 05:58:41 +0000246 --MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000247 if (MII == EndPt) {
248 return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000249 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000250 }
Owen Anderson696a1302009-03-31 08:27:09 +0000251 continue;
Lang Hamesb3661582009-11-14 00:02:51 +0000252 } else {
Owen Anderson696a1302009-03-31 08:27:09 +0000253 Pt = MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000254 }
Owen Anderson696a1302009-03-31 08:27:09 +0000255
256 if (RefsInMBB.count(MII))
257 return Pt;
258
259
260 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000261 }
262
263 return Pt;
264}
265
266/// findRestorePoint - Find a gap in the instruction index map that's suitable
267/// for restoring the current live interval value. The index must be before any
268/// uses of the live interval register in the mbb. Return end() if none is
269/// found.
270MachineBasicBlock::iterator
271PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000272 SlotIndex LastIdx,
Lang Hamesb3661582009-11-14 00:02:51 +0000273 SmallPtrSet<MachineInstr*, 4> &RefsInMBB) {
Evan Cheng54898932008-10-29 08:39:34 +0000274 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
275 // begin index accordingly.
Owen Anderson5a92d4e2008-11-18 20:53:59 +0000276 MachineBasicBlock::iterator Pt = MBB->end();
Owen Anderson696a1302009-03-31 08:27:09 +0000277 MachineBasicBlock::iterator EndPt = MBB->getFirstTerminator();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000278
Owen Anderson696a1302009-03-31 08:27:09 +0000279 // We start at the call, so walk forward until we find the call frame teardown
280 // since we can't insert restores before that. Bail if we encounter a use
281 // during this time.
282 MachineBasicBlock::iterator MII = MI;
283 if (MII == EndPt) return Pt;
284
285 while (MII != EndPt && !RefsInMBB.count(MII) &&
286 MII->getOpcode() != TRI->getCallFrameDestroyOpcode())
287 ++MII;
288 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
289 ++MII;
290
291 // FIXME: Limit the number of instructions to examine to reduce
292 // compile time?
293 while (MII != EndPt) {
Lang Hames233a60e2009-11-03 23:52:08 +0000294 SlotIndex Index = LIs->getInstructionIndex(MII);
Owen Anderson696a1302009-03-31 08:27:09 +0000295 if (Index > LastIdx)
296 break;
Owen Anderson3ef45492009-01-29 22:13:06 +0000297
Owen Anderson696a1302009-03-31 08:27:09 +0000298 // We can't insert a restore between the barrier (a call) and its
299 // corresponding call frame teardown.
300 if (MII->getOpcode() == TRI->getCallFrameSetupOpcode()) {
301 do {
302 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000303 ++MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000304 } while (MII->getOpcode() != TRI->getCallFrameDestroyOpcode());
Lang Hamesb3661582009-11-14 00:02:51 +0000305 } else {
Owen Anderson696a1302009-03-31 08:27:09 +0000306 Pt = MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000307 }
Owen Anderson696a1302009-03-31 08:27:09 +0000308
309 if (RefsInMBB.count(MII))
310 return Pt;
311
312 ++MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000313 }
314
315 return Pt;
316}
317
Evan Chengd0e32c52008-10-29 05:06:14 +0000318/// CreateSpillStackSlot - Create a stack slot for the live interval being
319/// split. If the live interval was previously split, just reuse the same
320/// slot.
321int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
322 const TargetRegisterClass *RC) {
323 int SS;
324 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
325 if (I != IntervalSSMap.end()) {
326 SS = I->second;
327 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000328 SS = MFI->CreateSpillStackObject(RC->getSize(), RC->getAlignment());
Evan Chengd0e32c52008-10-29 05:06:14 +0000329 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000330 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000331
332 // Create live interval for stack slot.
Evan Chengc781a242009-05-03 18:32:42 +0000333 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Evan Cheng54898932008-10-29 08:39:34 +0000334 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000335 CurrSValNo = CurrSLI->getValNumInfo(0);
336 else
Lang Hames6e2968c2010-09-25 12:04:16 +0000337 CurrSValNo = CurrSLI->getNextValue(SlotIndex(), 0,
Lang Hames86511252009-09-04 20:41:11 +0000338 LSs->getVNInfoAllocator());
Evan Chengd0e32c52008-10-29 05:06:14 +0000339 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000340}
341
Evan Chengd0e32c52008-10-29 05:06:14 +0000342/// IsAvailableInStack - Return true if register is available in a split stack
343/// slot at the specified index.
344bool
Evan Cheng54898932008-10-29 08:39:34 +0000345PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
Lang Hames233a60e2009-11-03 23:52:08 +0000346 unsigned Reg, SlotIndex DefIndex,
347 SlotIndex RestoreIndex,
348 SlotIndex &SpillIndex,
Evan Cheng54898932008-10-29 08:39:34 +0000349 int& SS) const {
350 if (!DefMBB)
351 return false;
352
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000353 DenseMap<unsigned, int>::const_iterator I = IntervalSSMap.find(Reg);
Evan Chengd0e32c52008-10-29 05:06:14 +0000354 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000355 return false;
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000356 DenseMap<SlotIndex, SlotIndex>::const_iterator
Lang Hames86511252009-09-04 20:41:11 +0000357 II = Def2SpillMap.find(DefIndex);
Evan Cheng54898932008-10-29 08:39:34 +0000358 if (II == Def2SpillMap.end())
359 return false;
360
361 // If last spill of def is in the same mbb as barrier mbb (where restore will
362 // be), make sure it's not below the intended restore index.
363 // FIXME: Undo the previous spill?
364 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
365 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
366 return false;
367
368 SS = I->second;
369 SpillIndex = II->second;
370 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000371}
372
Evan Chengd0e32c52008-10-29 05:06:14 +0000373/// UpdateSpillSlotInterval - Given the specified val# of the register live
374/// interval being split, and the spill and restore indicies, update the live
375/// interval of the spill stack slot.
376void
Lang Hames233a60e2009-11-03 23:52:08 +0000377PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, SlotIndex SpillIndex,
378 SlotIndex RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000379 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
380 "Expect restore in the barrier mbb");
381
382 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
383 if (MBB == BarrierMBB) {
384 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000385 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
386 CurrSLI->addRange(SLR);
387 return;
388 }
389
Evan Cheng54898932008-10-29 08:39:34 +0000390 SmallPtrSet<MachineBasicBlock*, 4> Processed;
Lang Hames233a60e2009-11-03 23:52:08 +0000391 SlotIndex EndIdx = LIs->getMBBEndIdx(MBB);
Lang Hames74ab5ee2009-12-22 00:11:50 +0000392 LiveRange SLR(SpillIndex, EndIdx, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000393 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000394 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000395
396 // Start from the spill mbb, figure out the extend of the spill slot's
397 // live interval.
398 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000399 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
400 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000401 // If live range extend beyond end of mbb, add successors to work list.
402 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
403 SE = MBB->succ_end(); SI != SE; ++SI)
404 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000405
406 while (!WorkList.empty()) {
407 MachineBasicBlock *MBB = WorkList.back();
408 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000409 if (Processed.count(MBB))
410 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000411 SlotIndex Idx = LIs->getMBBStartIdx(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000412 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000413 if (LR && LR->valno == ValNo) {
414 EndIdx = LIs->getMBBEndIdx(MBB);
415 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000416 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000417 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000418 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000419 } else if (LR->end > EndIdx) {
420 // Live range extends beyond end of mbb, process successors.
Lang Hames233a60e2009-11-03 23:52:08 +0000421 LiveRange SLR(Idx, EndIdx.getNextIndex(), CurrSValNo);
Evan Cheng54898932008-10-29 08:39:34 +0000422 CurrSLI->addRange(SLR);
423 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
424 SE = MBB->succ_end(); SI != SE; ++SI)
425 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000426 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000427 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000428 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000429 }
Evan Cheng54898932008-10-29 08:39:34 +0000430 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000431 }
432 }
433}
434
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000435/// PerformPHIConstruction - From properly set up use and def lists, use a PHI
436/// construction algorithm to compute the ranges and valnos for an interval.
Evan Cheng19a72582009-02-02 18:33:18 +0000437VNInfo*
438PreAllocSplitting::PerformPHIConstruction(MachineBasicBlock::iterator UseI,
439 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000440 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000441 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
442 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
443 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000444 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
445 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000446 bool IsTopLevel, bool IsIntraBlock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000447 // Return memoized result if it's available.
Evan Cheng19a72582009-02-02 18:33:18 +0000448 if (IsTopLevel && Visited.count(UseI) && NewVNs.count(UseI))
449 return NewVNs[UseI];
450 else if (!IsTopLevel && IsIntraBlock && NewVNs.count(UseI))
451 return NewVNs[UseI];
452 else if (!IsIntraBlock && LiveOut.count(MBB))
Owen Anderson7d211e22008-12-31 02:00:25 +0000453 return LiveOut[MBB];
454
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000455 // Check if our block contains any uses or defs.
Owen Anderson7d211e22008-12-31 02:00:25 +0000456 bool ContainsDefs = Defs.count(MBB);
457 bool ContainsUses = Uses.count(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000458
Evan Cheng19a72582009-02-02 18:33:18 +0000459 VNInfo* RetVNI = 0;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000460
461 // Enumerate the cases of use/def contaning blocks.
462 if (!ContainsDefs && !ContainsUses) {
Evan Cheng19a72582009-02-02 18:33:18 +0000463 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
464 NewVNs, LiveOut, Phis,
465 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000466 } else if (ContainsDefs && !ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000467 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000468
469 // Search for the def in this block. If we don't find it before the
470 // instruction we care about, go to the fallback case. Note that that
Owen Anderson7d211e22008-12-31 02:00:25 +0000471 // should never happen: this cannot be intrablock, so use should
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000472 // always be an end() iterator.
Evan Cheng19a72582009-02-02 18:33:18 +0000473 assert(UseI == MBB->end() && "No use marked in intrablock");
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000474
Evan Cheng19a72582009-02-02 18:33:18 +0000475 MachineBasicBlock::iterator Walker = UseI;
476 --Walker;
477 while (Walker != MBB->begin()) {
478 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000479 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000480 --Walker;
481 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000482
483 // Once we've found it, extend its VNInfo to our instruction.
Lang Hames233a60e2009-11-03 23:52:08 +0000484 SlotIndex DefIndex = LIs->getInstructionIndex(Walker);
485 DefIndex = DefIndex.getDefIndex();
486 SlotIndex EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000487
Evan Cheng19a72582009-02-02 18:33:18 +0000488 RetVNI = NewVNs[Walker];
Lang Hames74ab5ee2009-12-22 00:11:50 +0000489 LI->addRange(LiveRange(DefIndex, EndIndex, RetVNI));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000490 } else if (!ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000491 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000492
493 // Search for the use in this block that precedes the instruction we care
Evan Cheng19a72582009-02-02 18:33:18 +0000494 // about, going to the fallback case if we don't find it.
Evan Cheng19a72582009-02-02 18:33:18 +0000495 MachineBasicBlock::iterator Walker = UseI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000496 bool found = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000497 while (Walker != MBB->begin()) {
Benjamin Kramer2e0de6f2010-01-07 19:46:15 +0000498 --Walker;
Evan Cheng19a72582009-02-02 18:33:18 +0000499 if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000500 found = true;
501 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000502 }
Evan Cheng19a72582009-02-02 18:33:18 +0000503 }
Benjamin Kramer2e0de6f2010-01-07 19:46:15 +0000504
505 if (!found)
506 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
507 Uses, NewVNs, LiveOut, Phis,
508 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000509
Lang Hames233a60e2009-11-03 23:52:08 +0000510 SlotIndex UseIndex = LIs->getInstructionIndex(Walker);
511 UseIndex = UseIndex.getUseIndex();
512 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000513 if (IsIntraBlock) {
Lang Hames74ab5ee2009-12-22 00:11:50 +0000514 EndIndex = LIs->getInstructionIndex(UseI).getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000515 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000516 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000517
518 // Now, recursively phi construct the VNInfo for the use we found,
519 // and then extend it to include the instruction we care about
Evan Cheng19a72582009-02-02 18:33:18 +0000520 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
521 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000522
Lang Hames74ab5ee2009-12-22 00:11:50 +0000523 LI->addRange(LiveRange(UseIndex, EndIndex, RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000524
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000525 // FIXME: Need to set kills properly for inter-block stuff.
Evan Cheng19a72582009-02-02 18:33:18 +0000526 } else if (ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000527 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
528 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000529
530 // This case is basically a merging of the two preceding case, with the
531 // special note that checking for defs must take precedence over checking
532 // for uses, because of two-address instructions.
Evan Cheng19a72582009-02-02 18:33:18 +0000533 MachineBasicBlock::iterator Walker = UseI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000534 bool foundDef = false;
535 bool foundUse = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000536 while (Walker != MBB->begin()) {
Benjamin Kramer2e0de6f2010-01-07 19:46:15 +0000537 --Walker;
Evan Cheng19a72582009-02-02 18:33:18 +0000538 if (BlockDefs.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000539 foundDef = true;
540 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000541 } else if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000542 foundUse = true;
543 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000544 }
Evan Cheng19a72582009-02-02 18:33:18 +0000545 }
Benjamin Kramer2e0de6f2010-01-07 19:46:15 +0000546
547 if (!foundDef && !foundUse)
548 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
549 Uses, NewVNs, LiveOut, Phis,
550 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000551
Lang Hames233a60e2009-11-03 23:52:08 +0000552 SlotIndex StartIndex = LIs->getInstructionIndex(Walker);
553 StartIndex = foundDef ? StartIndex.getDefIndex() : StartIndex.getUseIndex();
554 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000555 if (IsIntraBlock) {
Lang Hames74ab5ee2009-12-22 00:11:50 +0000556 EndIndex = LIs->getInstructionIndex(UseI).getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000557 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000558 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000559
560 if (foundDef)
Evan Cheng19a72582009-02-02 18:33:18 +0000561 RetVNI = NewVNs[Walker];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000562 else
Evan Cheng19a72582009-02-02 18:33:18 +0000563 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
564 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000565
Lang Hames74ab5ee2009-12-22 00:11:50 +0000566 LI->addRange(LiveRange(StartIndex, EndIndex, RetVNI));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000567 }
568
569 // Memoize results so we don't have to recompute them.
Evan Cheng19a72582009-02-02 18:33:18 +0000570 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000571 else {
Evan Cheng19a72582009-02-02 18:33:18 +0000572 if (!NewVNs.count(UseI))
573 NewVNs[UseI] = RetVNI;
574 Visited.insert(UseI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000575 }
576
Evan Cheng19a72582009-02-02 18:33:18 +0000577 return RetVNI;
578}
579
580/// PerformPHIConstructionFallBack - PerformPHIConstruction fall back path.
581///
582VNInfo*
583PreAllocSplitting::PerformPHIConstructionFallBack(MachineBasicBlock::iterator UseI,
584 MachineBasicBlock* MBB, LiveInterval* LI,
585 SmallPtrSet<MachineInstr*, 4>& Visited,
586 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
587 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
588 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
589 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
590 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
591 bool IsTopLevel, bool IsIntraBlock) {
592 // NOTE: Because this is the fallback case from other cases, we do NOT
593 // assume that we are not intrablock here.
594 if (Phis.count(MBB)) return Phis[MBB];
595
Lang Hames233a60e2009-11-03 23:52:08 +0000596 SlotIndex StartIndex = LIs->getMBBStartIdx(MBB);
Lang Hames857c4e02009-06-17 21:01:20 +0000597 VNInfo *RetVNI = Phis[MBB] =
Lang Hames6e2968c2010-09-25 12:04:16 +0000598 LI->getNextValue(SlotIndex(), /*FIXME*/ 0,
Lang Hames86511252009-09-04 20:41:11 +0000599 LIs->getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +0000600
Evan Cheng19a72582009-02-02 18:33:18 +0000601 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
602
603 // If there are no uses or defs between our starting point and the
604 // beginning of the block, then recursive perform phi construction
605 // on our predecessors.
606 DenseMap<MachineBasicBlock*, VNInfo*> IncomingVNs;
607 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
608 PE = MBB->pred_end(); PI != PE; ++PI) {
609 VNInfo* Incoming = PerformPHIConstruction((*PI)->end(), *PI, LI,
610 Visited, Defs, Uses, NewVNs,
611 LiveOut, Phis, false, false);
612 if (Incoming != 0)
613 IncomingVNs[*PI] = Incoming;
614 }
615
Lang Hames857c4e02009-06-17 21:01:20 +0000616 if (MBB->pred_size() == 1 && !RetVNI->hasPHIKill()) {
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000617 VNInfo* OldVN = RetVNI;
618 VNInfo* NewVN = IncomingVNs.begin()->second;
619 VNInfo* MergedVN = LI->MergeValueNumberInto(OldVN, NewVN);
620 if (MergedVN == OldVN) std::swap(OldVN, NewVN);
621
622 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator LOI = LiveOut.begin(),
623 LOE = LiveOut.end(); LOI != LOE; ++LOI)
624 if (LOI->second == OldVN)
625 LOI->second = MergedVN;
626 for (DenseMap<MachineInstr*, VNInfo*>::iterator NVI = NewVNs.begin(),
627 NVE = NewVNs.end(); NVI != NVE; ++NVI)
628 if (NVI->second == OldVN)
629 NVI->second = MergedVN;
630 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator PI = Phis.begin(),
631 PE = Phis.end(); PI != PE; ++PI)
632 if (PI->second == OldVN)
633 PI->second = MergedVN;
634 RetVNI = MergedVN;
Evan Cheng19a72582009-02-02 18:33:18 +0000635 } else {
636 // Otherwise, merge the incoming VNInfos with a phi join. Create a new
637 // VNInfo to represent the joined value.
638 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator I =
639 IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) {
Lang Hames857c4e02009-06-17 21:01:20 +0000640 I->second->setHasPHIKill(true);
Evan Cheng19a72582009-02-02 18:33:18 +0000641 }
642 }
643
Lang Hames233a60e2009-11-03 23:52:08 +0000644 SlotIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000645 if (IsIntraBlock) {
Lang Hames74ab5ee2009-12-22 00:11:50 +0000646 EndIndex = LIs->getInstructionIndex(UseI).getDefIndex();
Evan Cheng19a72582009-02-02 18:33:18 +0000647 } else
648 EndIndex = LIs->getMBBEndIdx(MBB);
Lang Hames74ab5ee2009-12-22 00:11:50 +0000649 LI->addRange(LiveRange(StartIndex, EndIndex, RetVNI));
Evan Cheng19a72582009-02-02 18:33:18 +0000650
651 // Memoize results so we don't have to recompute them.
652 if (!IsIntraBlock)
653 LiveOut[MBB] = RetVNI;
654 else {
655 if (!NewVNs.count(UseI))
656 NewVNs[UseI] = RetVNI;
657 Visited.insert(UseI);
658 }
659
660 return RetVNI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000661}
662
663/// ReconstructLiveInterval - Recompute a live interval from scratch.
664void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) {
Benjamin Kramer991de142010-03-30 20:16:45 +0000665 VNInfo::Allocator& Alloc = LIs->getVNInfoAllocator();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000666
667 // Clear the old ranges and valnos;
668 LI->clear();
669
670 // Cache the uses and defs of the register
671 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
672 RegMap Defs, Uses;
673
674 // Keep track of the new VNs we're creating.
675 DenseMap<MachineInstr*, VNInfo*> NewVNs;
676 SmallPtrSet<VNInfo*, 2> PhiVNs;
677
678 // Cache defs, and create a new VNInfo for each def.
679 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
680 DE = MRI->def_end(); DI != DE; ++DI) {
681 Defs[(*DI).getParent()].insert(&*DI);
682
Lang Hames233a60e2009-11-03 23:52:08 +0000683 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI);
684 DefIdx = DefIdx.getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000685
Chris Lattner518bb532010-02-09 19:54:29 +0000686 assert(!DI->isPHI() && "PHI instr in code during pre-alloc splitting.");
Lang Hames6e2968c2010-09-25 12:04:16 +0000687 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, Alloc);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000688
689 // If the def is a move, set the copy field.
Jakob Stoklund Olesen04c528a2010-07-16 04:45:42 +0000690 if (DI->isCopyLike() && DI->getOperand(0).getReg() == LI->reg)
Jakob Stoklund Olesen273f7e42010-07-03 00:04:37 +0000691 NewVN->setCopy(&*DI);
692
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000693 NewVNs[&*DI] = NewVN;
694 }
695
696 // Cache uses as a separate pass from actually processing them.
697 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
698 UE = MRI->use_end(); UI != UE; ++UI)
699 Uses[(*UI).getParent()].insert(&*UI);
700
701 // Now, actually process every use and use a phi construction algorithm
702 // to walk from it to its reaching definitions, building VNInfos along
703 // the way.
Owen Anderson7d211e22008-12-31 02:00:25 +0000704 DenseMap<MachineBasicBlock*, VNInfo*> LiveOut;
705 DenseMap<MachineBasicBlock*, VNInfo*> Phis;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000706 SmallPtrSet<MachineInstr*, 4> Visited;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000707 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
708 UE = MRI->use_end(); UI != UE; ++UI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000709 PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000710 Uses, NewVNs, LiveOut, Phis, true, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000711 }
Owen Andersond4f6fe52008-12-28 23:35:13 +0000712
713 // Add ranges for dead defs
714 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
715 DE = MRI->def_end(); DI != DE; ++DI) {
Lang Hames233a60e2009-11-03 23:52:08 +0000716 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI);
717 DefIdx = DefIdx.getDefIndex();
Owen Andersond4f6fe52008-12-28 23:35:13 +0000718
719 if (LI->liveAt(DefIdx)) continue;
720
721 VNInfo* DeadVN = NewVNs[&*DI];
Lang Hames233a60e2009-11-03 23:52:08 +0000722 LI->addRange(LiveRange(DefIdx, DefIdx.getNextSlot(), DeadVN));
Evan Cheng35ca9202009-10-09 01:17:11 +0000723 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000724}
725
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000726/// RenumberValno - Split the given valno out into a new vreg, allowing it to
727/// be allocated to a different register. This function creates a new vreg,
728/// copies the valno and its live ranges over to the new vreg's interval,
729/// removes them from the old interval, and rewrites all uses and defs of
730/// the original reg to the new vreg within those ranges.
731void PreAllocSplitting::RenumberValno(VNInfo* VN) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000732 SmallVector<VNInfo*, 4> Stack;
733 SmallVector<VNInfo*, 4> VNsToCopy;
734 Stack.push_back(VN);
735
736 // Walk through and copy the valno we care about, and any other valnos
737 // that are two-address redefinitions of the one we care about. These
738 // will need to be rewritten as well. We also check for safety of the
739 // renumbering here, by making sure that none of the valno involved has
740 // phi kills.
741 while (!Stack.empty()) {
742 VNInfo* OldVN = Stack.back();
743 Stack.pop_back();
744
745 // Bail out if we ever encounter a valno that has a PHI kill. We can't
746 // renumber these.
Lang Hames857c4e02009-06-17 21:01:20 +0000747 if (OldVN->hasPHIKill()) return;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000748
749 VNsToCopy.push_back(OldVN);
750
751 // Locate two-address redefinitions
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000752 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(CurrLI->reg),
753 DE = MRI->def_end(); DI != DE; ++DI) {
754 if (!DI->isRegTiedToUseOperand(DI.getOperandNo())) continue;
755 SlotIndex DefIdx = LIs->getInstructionIndex(&*DI).getDefIndex();
756 VNInfo* NextVN = CurrLI->findDefinedVNInfoForRegInt(DefIdx);
757 if (std::find(VNsToCopy.begin(), VNsToCopy.end(), NextVN) !=
758 VNsToCopy.end())
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000759 Stack.push_back(NextVN);
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000760 }
761 }
762
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000763 // Create the new vreg
764 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
765
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000766 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000767 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000768
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000769 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
770 VNsToCopy.end(); OI != OE; ++OI) {
771 VNInfo* OldVN = *OI;
772
773 // Copy the valno over
Lang Hames857c4e02009-06-17 21:01:20 +0000774 VNInfo* NewVN = NewLI.createValueCopy(OldVN, LIs->getVNInfoAllocator());
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000775 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
776
777 // Remove the valno from the old interval
778 CurrLI->removeValNo(OldVN);
779 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000780
781 // Rewrite defs and uses. This is done in two stages to avoid invalidating
782 // the reg_iterator.
783 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
784
785 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
786 E = MRI->reg_end(); I != E; ++I) {
787 MachineOperand& MO = I.getOperand();
Lang Hames233a60e2009-11-03 23:52:08 +0000788 SlotIndex InstrIdx = LIs->getInstructionIndex(&*I);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000789
Lang Hames233a60e2009-11-03 23:52:08 +0000790 if ((MO.isUse() && NewLI.liveAt(InstrIdx.getUseIndex())) ||
791 (MO.isDef() && NewLI.liveAt(InstrIdx.getDefIndex())))
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000792 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
793 }
794
795 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
796 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
797 MachineInstr* Inst = I->first;
798 unsigned OpIdx = I->second;
799 MachineOperand& MO = Inst->getOperand(OpIdx);
800 MO.setReg(NewVReg);
801 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000802
Owen Anderson420dd372009-03-14 21:40:05 +0000803 // Grow the VirtRegMap, since we've created a new vreg.
804 VRM->grow();
805
Owen Anderson45e68552009-01-29 05:28:55 +0000806 // The renumbered vreg shares a stack slot with the old register.
807 if (IntervalSSMap.count(CurrLI->reg))
808 IntervalSSMap[NewVReg] = IntervalSSMap[CurrLI->reg];
809
Dan Gohmanfe601042010-06-22 15:08:57 +0000810 ++NumRenumbers;
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000811}
812
Evan Cheng37844532009-07-16 09:20:10 +0000813bool PreAllocSplitting::Rematerialize(unsigned VReg, VNInfo* ValNo,
Owen Anderson6002e992008-12-04 21:20:30 +0000814 MachineInstr* DefMI,
815 MachineBasicBlock::iterator RestorePt,
Owen Anderson6002e992008-12-04 21:20:30 +0000816 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
817 MachineBasicBlock& MBB = *RestorePt->getParent();
818
819 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
Lang Hamescec29452010-09-26 03:37:09 +0000820 if (!DefMI || DefMI->getParent() == BarrierMBB)
Lang Hamesb3661582009-11-14 00:02:51 +0000821 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB);
Owen Anderson6002e992008-12-04 21:20:30 +0000822 else
Chris Lattner7896c9f2009-12-03 00:50:42 +0000823 KillPt = llvm::next(MachineBasicBlock::iterator(DefMI));
Owen Anderson6002e992008-12-04 21:20:30 +0000824
825 if (KillPt == DefMI->getParent()->end())
826 return false;
827
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000828 TII->reMaterialize(MBB, RestorePt, VReg, 0, DefMI, *TRI);
Lang Hamesb3661582009-11-14 00:02:51 +0000829 SlotIndex RematIdx = LIs->InsertMachineInstrInMaps(prior(RestorePt));
Owen Anderson6002e992008-12-04 21:20:30 +0000830
Owen Andersonb4b84362009-01-26 21:57:31 +0000831 ReconstructLiveInterval(CurrLI);
Lang Hames233a60e2009-11-03 23:52:08 +0000832 RematIdx = RematIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +0000833 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RematIdx));
Owen Andersone1762c92009-01-12 03:10:40 +0000834
Owen Anderson75fa96b2008-11-19 04:28:29 +0000835 ++NumSplits;
836 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000837 return true;
838}
839
840MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
841 const TargetRegisterClass* RC,
842 MachineInstr* DefMI,
843 MachineInstr* Barrier,
844 MachineBasicBlock* MBB,
845 int& SS,
846 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000847 // Go top down if RefsInMBB is empty.
848 if (RefsInMBB.empty())
849 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000850
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000851 MachineBasicBlock::iterator FoldPt = Barrier;
852 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
853 !RefsInMBB.count(FoldPt))
854 --FoldPt;
855
Evan Cheng1015ba72010-05-21 20:53:24 +0000856 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000857 if (OpIdx == -1)
858 return 0;
859
860 SmallVector<unsigned, 1> Ops;
861 Ops.push_back(OpIdx);
862
863 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
864 return 0;
865
866 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
867 if (I != IntervalSSMap.end()) {
868 SS = I->second;
869 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000870 SS = MFI->CreateSpillStackObject(RC->getSize(), RC->getAlignment());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000871 }
872
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000873 MachineInstr* FMI = TII->foldMemoryOperand(FoldPt, Ops, SS);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000874
875 if (FMI) {
876 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000877 FoldPt->eraseFromParent();
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000878 ++NumFolds;
879
880 IntervalSSMap[vreg] = SS;
Evan Chengc781a242009-05-03 18:32:42 +0000881 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000882 if (CurrSLI->hasAtLeastOneValue())
883 CurrSValNo = CurrSLI->getValNumInfo(0);
884 else
Lang Hames6e2968c2010-09-25 12:04:16 +0000885 CurrSValNo = CurrSLI->getNextValue(SlotIndex(), 0,
Lang Hames86511252009-09-04 20:41:11 +0000886 LSs->getVNInfoAllocator());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000887 }
888
889 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000890}
891
Owen Andersonc93023a2009-03-04 08:52:31 +0000892MachineInstr* PreAllocSplitting::FoldRestore(unsigned vreg,
893 const TargetRegisterClass* RC,
894 MachineInstr* Barrier,
895 MachineBasicBlock* MBB,
896 int SS,
897 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
Owen Andersona2bfb542009-03-05 08:23:20 +0000898 if ((int)RestoreFoldLimit != -1 && RestoreFoldLimit == (int)NumRestoreFolds)
Owen Anderson323c58d2009-03-05 07:19:18 +0000899 return 0;
900
Owen Andersonc93023a2009-03-04 08:52:31 +0000901 // Go top down if RefsInMBB is empty.
902 if (RefsInMBB.empty())
903 return 0;
904
905 // Can't fold a restore between a call stack setup and teardown.
906 MachineBasicBlock::iterator FoldPt = Barrier;
Owen Anderson323c58d2009-03-05 07:19:18 +0000907
908 // Advance from barrier to call frame teardown.
909 while (FoldPt != MBB->getFirstTerminator() &&
910 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
911 if (RefsInMBB.count(FoldPt))
912 return 0;
Owen Andersonc93023a2009-03-04 08:52:31 +0000913
Owen Anderson323c58d2009-03-05 07:19:18 +0000914 ++FoldPt;
915 }
916
917 if (FoldPt == MBB->getFirstTerminator())
918 return 0;
919 else
920 ++FoldPt;
921
922 // Now find the restore point.
923 while (FoldPt != MBB->getFirstTerminator() && !RefsInMBB.count(FoldPt)) {
Owen Andersonc93023a2009-03-04 08:52:31 +0000924 if (FoldPt->getOpcode() == TRI->getCallFrameSetupOpcode()) {
925 while (FoldPt != MBB->getFirstTerminator() &&
926 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
927 if (RefsInMBB.count(FoldPt))
928 return 0;
929
930 ++FoldPt;
931 }
932
Owen Anderson323c58d2009-03-05 07:19:18 +0000933 if (FoldPt == MBB->getFirstTerminator())
934 return 0;
935 }
936
937 ++FoldPt;
Owen Andersonc93023a2009-03-04 08:52:31 +0000938 }
939
940 if (FoldPt == MBB->getFirstTerminator())
941 return 0;
942
943 int OpIdx = FoldPt->findRegisterUseOperandIdx(vreg, true);
944 if (OpIdx == -1)
945 return 0;
946
947 SmallVector<unsigned, 1> Ops;
948 Ops.push_back(OpIdx);
949
950 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
951 return 0;
952
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000953 MachineInstr* FMI = TII->foldMemoryOperand(FoldPt, Ops, SS);
Owen Andersonc93023a2009-03-04 08:52:31 +0000954
955 if (FMI) {
956 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000957 FoldPt->eraseFromParent();
Owen Anderson323c58d2009-03-05 07:19:18 +0000958 ++NumRestoreFolds;
Owen Andersonc93023a2009-03-04 08:52:31 +0000959 }
960
961 return FMI;
962}
963
Evan Chengf5cd4f02008-10-23 20:43:13 +0000964/// SplitRegLiveInterval - Split (spill and restore) the given live interval
965/// so it would not cross the barrier that's being processed. Shrink wrap
966/// (minimize) the live interval to the last uses.
967bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
David Greene4175f582010-01-05 01:25:47 +0000968 DEBUG(dbgs() << "Pre-alloc splitting " << LI->reg << " for " << *Barrier
Lang Hames233a60e2009-11-03 23:52:08 +0000969 << " result: ");
970
Evan Chengf5cd4f02008-10-23 20:43:13 +0000971 CurrLI = LI;
972
973 // Find live range where current interval cross the barrier.
974 LiveInterval::iterator LR =
Lang Hames233a60e2009-11-03 23:52:08 +0000975 CurrLI->FindLiveRangeContaining(BarrierIdx.getUseIndex());
Evan Chengf5cd4f02008-10-23 20:43:13 +0000976 VNInfo *ValNo = LR->valno;
977
Torok Edwinf3689232009-07-12 20:07:01 +0000978 assert(!ValNo->isUnused() && "Val# is defined by a dead def?");
Evan Chengf5cd4f02008-10-23 20:43:13 +0000979
Lang Hames6e2968c2010-09-25 12:04:16 +0000980 MachineInstr *DefMI = LIs->getInstructionFromIndex(ValNo->def);
Evan Cheng06587492008-10-24 02:05:00 +0000981
Owen Andersond3be4622009-01-21 08:18:03 +0000982 // If this would create a new join point, do not split.
Lang Hames233a60e2009-11-03 23:52:08 +0000983 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent())) {
David Greene4175f582010-01-05 01:25:47 +0000984 DEBUG(dbgs() << "FAILED (would create a new join point).\n");
Owen Andersond3be4622009-01-21 08:18:03 +0000985 return false;
Lang Hames233a60e2009-11-03 23:52:08 +0000986 }
Owen Andersond3be4622009-01-21 08:18:03 +0000987
Evan Chengf5cd4f02008-10-23 20:43:13 +0000988 // Find all references in the barrier mbb.
989 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
990 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
991 E = MRI->reg_end(); I != E; ++I) {
992 MachineInstr *RefMI = &*I;
993 if (RefMI->getParent() == BarrierMBB)
994 RefsInMBB.insert(RefMI);
995 }
996
997 // Find a point to restore the value after the barrier.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000998 MachineBasicBlock::iterator RestorePt =
Lang Hamesb3661582009-11-14 00:02:51 +0000999 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +00001000 if (RestorePt == BarrierMBB->end()) {
David Greene4175f582010-01-05 01:25:47 +00001001 DEBUG(dbgs() << "FAILED (could not find a suitable restore point).\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001002 return false;
Lang Hames233a60e2009-11-03 23:52:08 +00001003 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001004
Owen Anderson75fa96b2008-11-19 04:28:29 +00001005 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
Lang Hamesb3661582009-11-14 00:02:51 +00001006 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt, RefsInMBB)) {
David Greene4175f582010-01-05 01:25:47 +00001007 DEBUG(dbgs() << "success (remat).\n");
Lang Hames233a60e2009-11-03 23:52:08 +00001008 return true;
1009 }
Owen Anderson75fa96b2008-11-19 04:28:29 +00001010
Evan Chengf5cd4f02008-10-23 20:43:13 +00001011 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +00001012 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001013 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Lang Hames233a60e2009-11-03 23:52:08 +00001014 SlotIndex SpillIndex;
Evan Cheng06587492008-10-24 02:05:00 +00001015 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +00001016 int SS = -1;
Lang Hamescec29452010-09-26 03:37:09 +00001017 if (!DefMI) {
Lang Hames857c4e02009-06-17 21:01:20 +00001018 // If we don't know where the def is we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001019 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
1020 BarrierMBB, SS, RefsInMBB))) {
1021 SpillIndex = LIs->getInstructionIndex(SpillMI);
1022 } else {
1023 MachineBasicBlock::iterator SpillPt =
Lang Hamesb3661582009-11-14 00:02:51 +00001024 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +00001025 if (SpillPt == BarrierMBB->begin()) {
David Greene4175f582010-01-05 01:25:47 +00001026 DEBUG(dbgs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001027 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001028 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001029 // Add spill.
1030
1031 SS = CreateSpillStackSlot(CurrLI->reg, RC);
Evan Cheng746ad692010-05-06 19:06:44 +00001032 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC,
1033 TRI);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001034 SpillMI = prior(SpillPt);
Lang Hamesb3661582009-11-14 00:02:51 +00001035 SpillIndex = LIs->InsertMachineInstrInMaps(SpillMI);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001036 }
Evan Cheng54898932008-10-29 08:39:34 +00001037 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
Lang Hamesb3661582009-11-14 00:02:51 +00001038 LIs->getZeroIndex(), SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +00001039 // If it's already split, just restore the value. There is no need to spill
1040 // the def again.
Lang Hames233a60e2009-11-03 23:52:08 +00001041 if (!DefMI) {
David Greene4175f582010-01-05 01:25:47 +00001042 DEBUG(dbgs() << "FAILED (def is dead).\n");
Evan Chengd0e32c52008-10-29 05:06:14 +00001043 return false; // Def is dead. Do nothing.
Lang Hames233a60e2009-11-03 23:52:08 +00001044 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001045
1046 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
Evan Cheng35ca9202009-10-09 01:17:11 +00001047 BarrierMBB, SS, RefsInMBB))) {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001048 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001049 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001050 // Check if it's possible to insert a spill after the def MI.
1051 MachineBasicBlock::iterator SpillPt;
1052 if (DefMBB == BarrierMBB) {
1053 // Add spill after the def and the last use before the barrier.
1054 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
Lang Hamesb3661582009-11-14 00:02:51 +00001055 RefsInMBB);
Lang Hames233a60e2009-11-03 23:52:08 +00001056 if (SpillPt == DefMBB->begin()) {
David Greene4175f582010-01-05 01:25:47 +00001057 DEBUG(dbgs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001058 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001059 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001060 } else {
Chris Lattner7896c9f2009-12-03 00:50:42 +00001061 SpillPt = llvm::next(MachineBasicBlock::iterator(DefMI));
Lang Hames233a60e2009-11-03 23:52:08 +00001062 if (SpillPt == DefMBB->end()) {
David Greene4175f582010-01-05 01:25:47 +00001063 DEBUG(dbgs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001064 return false; // No gap to insert spill.
Lang Hames233a60e2009-11-03 23:52:08 +00001065 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001066 }
Evan Cheng35ca9202009-10-09 01:17:11 +00001067 // Add spill.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001068 SS = CreateSpillStackSlot(CurrLI->reg, RC);
Evan Cheng746ad692010-05-06 19:06:44 +00001069 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg, false, SS, RC,
1070 TRI);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001071 SpillMI = prior(SpillPt);
Lang Hamesb3661582009-11-14 00:02:51 +00001072 SpillIndex = LIs->InsertMachineInstrInMaps(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001073 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001074 }
1075
Evan Cheng54898932008-10-29 08:39:34 +00001076 // Remember def instruction index to spill index mapping.
1077 if (DefMI && SpillMI)
1078 Def2SpillMap[ValNo->def] = SpillIndex;
1079
Evan Chengf5cd4f02008-10-23 20:43:13 +00001080 // Add restore.
Owen Andersonc93023a2009-03-04 08:52:31 +00001081 bool FoldedRestore = false;
Lang Hamesb3661582009-11-14 00:02:51 +00001082 SlotIndex RestoreIndex;
Owen Andersonc93023a2009-03-04 08:52:31 +00001083 if (MachineInstr* LMI = FoldRestore(CurrLI->reg, RC, Barrier,
1084 BarrierMBB, SS, RefsInMBB)) {
1085 RestorePt = LMI;
Owen Anderson323c58d2009-03-05 07:19:18 +00001086 RestoreIndex = LIs->getInstructionIndex(RestorePt);
Owen Andersonc93023a2009-03-04 08:52:31 +00001087 FoldedRestore = true;
1088 } else {
Evan Cheng746ad692010-05-06 19:06:44 +00001089 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC, TRI);
Owen Andersonc93023a2009-03-04 08:52:31 +00001090 MachineInstr *LoadMI = prior(RestorePt);
Lang Hamesb3661582009-11-14 00:02:51 +00001091 RestoreIndex = LIs->InsertMachineInstrInMaps(LoadMI);
Owen Andersonc93023a2009-03-04 08:52:31 +00001092 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001093
Evan Chengd0e32c52008-10-29 05:06:14 +00001094 // Update spill stack slot live interval.
Lang Hames233a60e2009-11-03 23:52:08 +00001095 UpdateSpillSlotInterval(ValNo, SpillIndex.getUseIndex().getNextSlot(),
1096 RestoreIndex.getDefIndex());
Evan Chengd0e32c52008-10-29 05:06:14 +00001097
Owen Andersonb4b84362009-01-26 21:57:31 +00001098 ReconstructLiveInterval(CurrLI);
Evan Cheng35ca9202009-10-09 01:17:11 +00001099
Owen Andersonc93023a2009-03-04 08:52:31 +00001100 if (!FoldedRestore) {
Lang Hames233a60e2009-11-03 23:52:08 +00001101 SlotIndex RestoreIdx = LIs->getInstructionIndex(prior(RestorePt));
1102 RestoreIdx = RestoreIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +00001103 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RestoreIdx));
Owen Andersonc93023a2009-03-04 08:52:31 +00001104 }
Owen Anderson7d211e22008-12-31 02:00:25 +00001105
Evan Chengae7fa5b2008-10-28 01:48:24 +00001106 ++NumSplits;
David Greene4175f582010-01-05 01:25:47 +00001107 DEBUG(dbgs() << "success.\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001108 return true;
1109}
1110
1111/// SplitRegLiveIntervals - Split all register live intervals that cross the
1112/// barrier that's being processed.
1113bool
Owen Anderson956ec272009-01-23 00:23:32 +00001114PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs,
1115 SmallPtrSet<LiveInterval*, 8>& Split) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001116 // First find all the virtual registers whose live intervals are intercepted
1117 // by the current barrier.
1118 SmallVector<LiveInterval*, 8> Intervals;
1119 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng4350eb82009-02-06 17:17:30 +00001120 // FIXME: If it's not safe to move any instruction that defines the barrier
1121 // register class, then it means there are some special dependencies which
1122 // codegen is not modelling. Ignore these barriers for now.
1123 if (!TII->isSafeToMoveRegClassDefs(*RC))
Evan Cheng23066282008-10-27 07:14:50 +00001124 continue;
Chris Lattner65569b82010-05-21 17:47:50 +00001125 const std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
Evan Chengf5cd4f02008-10-23 20:43:13 +00001126 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1127 unsigned Reg = VRs[i];
1128 if (!LIs->hasInterval(Reg))
1129 continue;
1130 LiveInterval *LI = &LIs->getInterval(Reg);
1131 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1132 // Virtual register live interval is intercepted by the barrier. We
1133 // should split and shrink wrap its interval if possible.
1134 Intervals.push_back(LI);
1135 }
1136 }
1137
1138 // Process the affected live intervals.
1139 bool Change = false;
1140 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001141 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1142 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001143 LiveInterval *LI = Intervals.back();
1144 Intervals.pop_back();
Owen Anderson956ec272009-01-23 00:23:32 +00001145 bool result = SplitRegLiveInterval(LI);
1146 if (result) Split.insert(LI);
1147 Change |= result;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001148 }
1149
1150 return Change;
1151}
1152
Owen Anderson45e68552009-01-29 05:28:55 +00001153unsigned PreAllocSplitting::getNumberOfNonSpills(
Owen Anderson32ca8652009-01-24 10:07:43 +00001154 SmallPtrSet<MachineInstr*, 4>& MIs,
Owen Anderson45e68552009-01-29 05:28:55 +00001155 unsigned Reg, int FrameIndex,
1156 bool& FeedsTwoAddr) {
1157 unsigned NonSpills = 0;
Owen Anderson32ca8652009-01-24 10:07:43 +00001158 for (SmallPtrSet<MachineInstr*, 4>::iterator UI = MIs.begin(), UE = MIs.end();
Owen Anderson45e68552009-01-29 05:28:55 +00001159 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001160 int StoreFrameIndex;
1161 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001162 if (StoreVReg != Reg || StoreFrameIndex != FrameIndex)
Dan Gohmanfe601042010-06-22 15:08:57 +00001163 ++NonSpills;
Owen Anderson45e68552009-01-29 05:28:55 +00001164
1165 int DefIdx = (*UI)->findRegisterDefOperandIdx(Reg);
Bob Wilsond9df5012009-04-09 17:16:43 +00001166 if (DefIdx != -1 && (*UI)->isRegTiedToUseOperand(DefIdx))
Owen Anderson45e68552009-01-29 05:28:55 +00001167 FeedsTwoAddr = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001168 }
1169
Owen Anderson45e68552009-01-29 05:28:55 +00001170 return NonSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001171}
1172
Owen Anderson956ec272009-01-23 00:23:32 +00001173/// removeDeadSpills - After doing splitting, filter through all intervals we've
1174/// split, and see if any of the spills are unnecessary. If so, remove them.
1175bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) {
1176 bool changed = false;
1177
Owen Anderson4bfc2092009-01-29 05:41:02 +00001178 // Walk over all of the live intervals that were touched by the splitter,
1179 // and see if we can do any DCE and/or folding.
Owen Anderson956ec272009-01-23 00:23:32 +00001180 for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(),
1181 LE = split.end(); LI != LE; ++LI) {
Owen Anderson9ce499a2009-01-23 03:28:53 +00001182 DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> > VNUseCount;
Owen Anderson956ec272009-01-23 00:23:32 +00001183
Owen Anderson4bfc2092009-01-29 05:41:02 +00001184 // First, collect all the uses of the vreg, and sort them by their
1185 // reaching definition (VNInfo).
Owen Anderson956ec272009-01-23 00:23:32 +00001186 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg),
1187 UE = MRI->use_end(); UI != UE; ++UI) {
Lang Hames233a60e2009-11-03 23:52:08 +00001188 SlotIndex index = LIs->getInstructionIndex(&*UI);
1189 index = index.getUseIndex();
Owen Anderson956ec272009-01-23 00:23:32 +00001190
1191 const LiveRange* LR = (*LI)->getLiveRangeContaining(index);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001192 VNUseCount[LR->valno].insert(&*UI);
Owen Anderson956ec272009-01-23 00:23:32 +00001193 }
1194
Owen Anderson4bfc2092009-01-29 05:41:02 +00001195 // Now, take the definitions (VNInfo's) one at a time and try to DCE
1196 // and/or fold them away.
Owen Anderson956ec272009-01-23 00:23:32 +00001197 for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(),
1198 VE = (*LI)->vni_end(); VI != VE; ++VI) {
Owen Anderson45e68552009-01-29 05:28:55 +00001199
1200 if (DeadSplitLimit != -1 && (int)NumDeadSpills == DeadSplitLimit)
1201 return changed;
1202
Owen Anderson956ec272009-01-23 00:23:32 +00001203 VNInfo* CurrVN = *VI;
Owen Anderson4bfc2092009-01-29 05:41:02 +00001204
1205 // We don't currently try to handle definitions with PHI kills, because
1206 // it would involve processing more than one VNInfo at once.
Lang Hames857c4e02009-06-17 21:01:20 +00001207 if (CurrVN->hasPHIKill()) continue;
Owen Anderson956ec272009-01-23 00:23:32 +00001208
Owen Anderson4bfc2092009-01-29 05:41:02 +00001209 // We also don't try to handle the results of PHI joins, since there's
1210 // no defining instruction to analyze.
Lang Hames6e2968c2010-09-25 12:04:16 +00001211 MachineInstr* DefMI = LIs->getInstructionFromIndex(CurrVN->def);
1212 if (!DefMI || CurrVN->isUnused()) continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001213
Owen Anderson4bfc2092009-01-29 05:41:02 +00001214 // We're only interested in eliminating cruft introduced by the splitter,
1215 // is of the form load-use or load-use-store. First, check that the
1216 // definition is a load, and remember what stack slot we loaded it from.
Owen Anderson956ec272009-01-23 00:23:32 +00001217 int FrameIndex;
1218 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
1219
Owen Anderson4bfc2092009-01-29 05:41:02 +00001220 // If the definition has no uses at all, just DCE it.
Owen Anderson9ce499a2009-01-23 03:28:53 +00001221 if (VNUseCount[CurrVN].size() == 0) {
1222 LIs->RemoveMachineInstrFromMaps(DefMI);
1223 (*LI)->removeValNo(CurrVN);
1224 DefMI->eraseFromParent();
Owen Andersonc0f3a032009-01-29 08:22:06 +00001225 VNUseCount.erase(CurrVN);
Dan Gohmanfe601042010-06-22 15:08:57 +00001226 ++NumDeadSpills;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001227 changed = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001228 continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001229 }
Owen Anderson32ca8652009-01-24 10:07:43 +00001230
Owen Anderson4bfc2092009-01-29 05:41:02 +00001231 // Second, get the number of non-store uses of the definition, as well as
1232 // a flag indicating whether it feeds into a later two-address definition.
Owen Anderson45e68552009-01-29 05:28:55 +00001233 bool FeedsTwoAddr = false;
1234 unsigned NonSpillCount = getNumberOfNonSpills(VNUseCount[CurrVN],
1235 (*LI)->reg, FrameIndex,
1236 FeedsTwoAddr);
1237
Owen Anderson4bfc2092009-01-29 05:41:02 +00001238 // If there's one non-store use and it doesn't feed a two-addr, then
1239 // this is a load-use-store case that we can try to fold.
Owen Anderson45e68552009-01-29 05:28:55 +00001240 if (NonSpillCount == 1 && !FeedsTwoAddr) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001241 // Start by finding the non-store use MachineInstr.
Owen Anderson45e68552009-01-29 05:28:55 +00001242 SmallPtrSet<MachineInstr*, 4>::iterator UI = VNUseCount[CurrVN].begin();
1243 int StoreFrameIndex;
1244 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1245 while (UI != VNUseCount[CurrVN].end() &&
1246 (StoreVReg == (*LI)->reg && StoreFrameIndex == FrameIndex)) {
1247 ++UI;
1248 if (UI != VNUseCount[CurrVN].end())
1249 StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1250 }
Owen Anderson45e68552009-01-29 05:28:55 +00001251 if (UI == VNUseCount[CurrVN].end()) continue;
1252
1253 MachineInstr* use = *UI;
1254
Owen Anderson4bfc2092009-01-29 05:41:02 +00001255 // Attempt to fold it away!
Owen Anderson45e68552009-01-29 05:28:55 +00001256 int OpIdx = use->findRegisterUseOperandIdx((*LI)->reg, false);
1257 if (OpIdx == -1) continue;
Owen Anderson45e68552009-01-29 05:28:55 +00001258 SmallVector<unsigned, 1> Ops;
1259 Ops.push_back(OpIdx);
Owen Anderson45e68552009-01-29 05:28:55 +00001260 if (!TII->canFoldMemoryOperand(use, Ops)) continue;
1261
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +00001262 MachineInstr* NewMI = TII->foldMemoryOperand(use, Ops, FrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001263
1264 if (!NewMI) continue;
1265
Owen Anderson4bfc2092009-01-29 05:41:02 +00001266 // Update relevant analyses.
Owen Anderson45e68552009-01-29 05:28:55 +00001267 LIs->RemoveMachineInstrFromMaps(DefMI);
1268 LIs->ReplaceMachineInstrInMaps(use, NewMI);
1269 (*LI)->removeValNo(CurrVN);
1270
1271 DefMI->eraseFromParent();
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +00001272 use->eraseFromParent();
Owen Anderson45e68552009-01-29 05:28:55 +00001273 VNUseCount[CurrVN].erase(use);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +00001274
Owen Anderson4bfc2092009-01-29 05:41:02 +00001275 // Remove deleted instructions. Note that we need to remove them from
1276 // the VNInfo->use map as well, just to be safe.
Owen Anderson45e68552009-01-29 05:28:55 +00001277 for (SmallPtrSet<MachineInstr*, 4>::iterator II =
1278 VNUseCount[CurrVN].begin(), IE = VNUseCount[CurrVN].end();
1279 II != IE; ++II) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001280 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
Owen Andersonc0f3a032009-01-29 08:22:06 +00001281 VNI = VNUseCount.begin(), VNE = VNUseCount.end(); VNI != VNE;
1282 ++VNI)
1283 if (VNI->first != CurrVN)
1284 VNI->second.erase(*II);
Owen Anderson45e68552009-01-29 05:28:55 +00001285 LIs->RemoveMachineInstrFromMaps(*II);
1286 (*II)->eraseFromParent();
1287 }
Owen Andersonc0f3a032009-01-29 08:22:06 +00001288
1289 VNUseCount.erase(CurrVN);
Owen Anderson45e68552009-01-29 05:28:55 +00001290
1291 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
1292 VI = VNUseCount.begin(), VE = VNUseCount.end(); VI != VE; ++VI)
1293 if (VI->second.erase(use))
1294 VI->second.insert(NewMI);
1295
Dan Gohmanfe601042010-06-22 15:08:57 +00001296 ++NumDeadSpills;
Owen Anderson45e68552009-01-29 05:28:55 +00001297 changed = true;
1298 continue;
1299 }
1300
Owen Anderson4bfc2092009-01-29 05:41:02 +00001301 // If there's more than one non-store instruction, we can't profitably
1302 // fold it, so bail.
Owen Anderson45e68552009-01-29 05:28:55 +00001303 if (NonSpillCount) continue;
Owen Anderson32ca8652009-01-24 10:07:43 +00001304
Owen Anderson4bfc2092009-01-29 05:41:02 +00001305 // Otherwise, this is a load-store case, so DCE them.
Owen Anderson32ca8652009-01-24 10:07:43 +00001306 for (SmallPtrSet<MachineInstr*, 4>::iterator UI =
1307 VNUseCount[CurrVN].begin(), UE = VNUseCount[CurrVN].end();
Lang Hamesf035ce52009-11-12 01:24:08 +00001308 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001309 LIs->RemoveMachineInstrFromMaps(*UI);
1310 (*UI)->eraseFromParent();
1311 }
1312
Owen Andersonc0f3a032009-01-29 08:22:06 +00001313 VNUseCount.erase(CurrVN);
1314
Owen Anderson32ca8652009-01-24 10:07:43 +00001315 LIs->RemoveMachineInstrFromMaps(DefMI);
1316 (*LI)->removeValNo(CurrVN);
1317 DefMI->eraseFromParent();
Dan Gohmanfe601042010-06-22 15:08:57 +00001318 ++NumDeadSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001319 changed = true;
Owen Anderson956ec272009-01-23 00:23:32 +00001320 }
1321 }
1322
1323 return changed;
1324}
1325
Owen Andersonf1f75b12008-11-04 22:22:41 +00001326bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1327 MachineBasicBlock* DefMBB,
1328 MachineBasicBlock* BarrierMBB) {
1329 if (DefMBB == BarrierMBB)
1330 return false;
1331
Lang Hames857c4e02009-06-17 21:01:20 +00001332 if (LR->valno->hasPHIKill())
Owen Andersonf1f75b12008-11-04 22:22:41 +00001333 return false;
1334
Lang Hames233a60e2009-11-03 23:52:08 +00001335 SlotIndex MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
Owen Andersonf1f75b12008-11-04 22:22:41 +00001336 if (LR->end < MBBEnd)
1337 return false;
1338
1339 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1340 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1341 return true;
1342
1343 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1344 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1345 typedef std::pair<MachineBasicBlock*,
1346 MachineBasicBlock::succ_iterator> ItPair;
1347 SmallVector<ItPair, 4> Stack;
1348 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1349
1350 while (!Stack.empty()) {
1351 ItPair P = Stack.back();
1352 Stack.pop_back();
1353
1354 MachineBasicBlock* PredMBB = P.first;
1355 MachineBasicBlock::succ_iterator S = P.second;
1356
1357 if (S == PredMBB->succ_end())
1358 continue;
1359 else if (Visited.count(*S)) {
1360 Stack.push_back(std::make_pair(PredMBB, ++S));
1361 continue;
1362 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001363 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001364
1365 MachineBasicBlock* MBB = *S;
1366 Visited.insert(MBB);
1367
1368 if (MBB == BarrierMBB)
1369 return true;
1370
1371 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1372 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1373 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1374 while (MDTN) {
1375 if (MDTN == DefMDTN)
1376 return true;
1377 else if (MDTN == BarrierMDTN)
1378 break;
1379 MDTN = MDTN->getIDom();
1380 }
1381
1382 MBBEnd = LIs->getMBBEndIdx(MBB);
1383 if (LR->end > MBBEnd)
1384 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1385 }
1386
1387 return false;
1388}
1389
1390
Evan Cheng09e8ca82008-10-20 21:44:59 +00001391bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001392 CurrMF = &MF;
1393 TM = &MF.getTarget();
Owen Anderson3ef45492009-01-29 22:13:06 +00001394 TRI = TM->getRegisterInfo();
Evan Chengd0e32c52008-10-29 05:06:14 +00001395 TII = TM->getInstrInfo();
1396 MFI = MF.getFrameInfo();
1397 MRI = &MF.getRegInfo();
Lang Hames233a60e2009-11-03 23:52:08 +00001398 SIs = &getAnalysis<SlotIndexes>();
Evan Chengd0e32c52008-10-29 05:06:14 +00001399 LIs = &getAnalysis<LiveIntervals>();
1400 LSs = &getAnalysis<LiveStacks>();
Owen Anderson420dd372009-03-14 21:40:05 +00001401 VRM = &getAnalysis<VirtRegMap>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001402
1403 bool MadeChange = false;
1404
1405 // Make sure blocks are numbered in order.
1406 MF.RenumberBlocks();
1407
Evan Cheng54898932008-10-29 08:39:34 +00001408 MachineBasicBlock *Entry = MF.begin();
1409 SmallPtrSet<MachineBasicBlock*,16> Visited;
1410
Owen Anderson956ec272009-01-23 00:23:32 +00001411 SmallPtrSet<LiveInterval*, 8> Split;
1412
Evan Cheng54898932008-10-29 08:39:34 +00001413 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1414 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1415 DFI != E; ++DFI) {
1416 BarrierMBB = *DFI;
1417 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1418 E = BarrierMBB->end(); I != E; ++I) {
1419 Barrier = &*I;
1420 const TargetRegisterClass **BarrierRCs =
1421 Barrier->getDesc().getRegClassBarriers();
1422 if (!BarrierRCs)
1423 continue;
1424 BarrierIdx = LIs->getInstructionIndex(Barrier);
Owen Anderson956ec272009-01-23 00:23:32 +00001425 MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split);
Evan Cheng54898932008-10-29 08:39:34 +00001426 }
1427 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001428
Owen Anderson956ec272009-01-23 00:23:32 +00001429 MadeChange |= removeDeadSpills(Split);
1430
Evan Chengf5cd4f02008-10-23 20:43:13 +00001431 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001432}