blob: 9571b7a5eaf047aee2bc46f37ca60befce581afc [file] [log] [blame]
Evan Cheng09e8ca82008-10-20 21:44:59 +00001//===-- PreAllocSplitting.cpp - Pre-allocation Interval Spltting Pass. ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the machine instruction level pre-register allocation
11// live interval splitting pass. It finds live interval barriers, i.e.
12// instructions which will kill all physical registers in certain register
13// classes, and split all live intervals which cross the barrier.
14//
15//===----------------------------------------------------------------------===//
16
17#define DEBUG_TYPE "pre-alloc-split"
Owen Anderson420dd372009-03-14 21:40:05 +000018#include "VirtRegMap.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000020#include "llvm/CodeGen/LiveStackAnalysis.h"
Owen Andersonf1f75b12008-11-04 22:22:41 +000021#include "llvm/CodeGen/MachineDominators.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineLoopInfo.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/Passes.h"
27#include "llvm/CodeGen/RegisterCoalescer.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000028#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000029#include "llvm/Target/TargetMachine.h"
30#include "llvm/Target/TargetOptions.h"
31#include "llvm/Target/TargetRegisterInfo.h"
32#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000035#include "llvm/ADT/DenseMap.h"
Evan Cheng54898932008-10-29 08:39:34 +000036#include "llvm/ADT/DepthFirstIterator.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000037#include "llvm/ADT/SmallPtrSet.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000038#include "llvm/ADT/Statistic.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000039using namespace llvm;
40
Evan Chengae7fa5b2008-10-28 01:48:24 +000041static cl::opt<int> PreSplitLimit("pre-split-limit", cl::init(-1), cl::Hidden);
Owen Anderson45e68552009-01-29 05:28:55 +000042static cl::opt<int> DeadSplitLimit("dead-split-limit", cl::init(-1), cl::Hidden);
Owen Anderson323c58d2009-03-05 07:19:18 +000043static cl::opt<int> RestoreFoldLimit("restore-fold-limit", cl::init(-1), cl::Hidden);
Evan Chengae7fa5b2008-10-28 01:48:24 +000044
Owen Anderson45e68552009-01-29 05:28:55 +000045STATISTIC(NumSplits, "Number of intervals split");
Owen Anderson75fa96b2008-11-19 04:28:29 +000046STATISTIC(NumRemats, "Number of intervals split by rematerialization");
Owen Anderson7b9d67c2008-12-02 18:53:47 +000047STATISTIC(NumFolds, "Number of intervals split with spill folding");
Owen Anderson323c58d2009-03-05 07:19:18 +000048STATISTIC(NumRestoreFolds, "Number of intervals split with restore folding");
Owen Anderson2ebf63f2008-12-18 01:27:19 +000049STATISTIC(NumRenumbers, "Number of intervals renumbered into new registers");
Owen Anderson956ec272009-01-23 00:23:32 +000050STATISTIC(NumDeadSpills, "Number of dead spills removed");
Evan Chengf5cd4f02008-10-23 20:43:13 +000051
Evan Cheng09e8ca82008-10-20 21:44:59 +000052namespace {
53 class VISIBILITY_HIDDEN PreAllocSplitting : public MachineFunctionPass {
Evan Chengd0e32c52008-10-29 05:06:14 +000054 MachineFunction *CurrMF;
Evan Chengf5cd4f02008-10-23 20:43:13 +000055 const TargetMachine *TM;
56 const TargetInstrInfo *TII;
Owen Anderson3ef45492009-01-29 22:13:06 +000057 const TargetRegisterInfo* TRI;
Evan Chengf5cd4f02008-10-23 20:43:13 +000058 MachineFrameInfo *MFI;
59 MachineRegisterInfo *MRI;
60 LiveIntervals *LIs;
Evan Chengd0e32c52008-10-29 05:06:14 +000061 LiveStacks *LSs;
Owen Anderson420dd372009-03-14 21:40:05 +000062 VirtRegMap *VRM;
Evan Cheng09e8ca82008-10-20 21:44:59 +000063
Evan Chengf5cd4f02008-10-23 20:43:13 +000064 // Barrier - Current barrier being processed.
65 MachineInstr *Barrier;
66
67 // BarrierMBB - Basic block where the barrier resides in.
68 MachineBasicBlock *BarrierMBB;
69
70 // Barrier - Current barrier index.
Lang Hamescc3b0652009-10-03 04:21:37 +000071 LiveIndex BarrierIdx;
Evan Chengf5cd4f02008-10-23 20:43:13 +000072
73 // CurrLI - Current live interval being split.
74 LiveInterval *CurrLI;
75
Evan Chengd0e32c52008-10-29 05:06:14 +000076 // CurrSLI - Current stack slot live interval.
77 LiveInterval *CurrSLI;
78
79 // CurrSValNo - Current val# for the stack slot live interval.
80 VNInfo *CurrSValNo;
81
82 // IntervalSSMap - A map from live interval to spill slots.
83 DenseMap<unsigned, int> IntervalSSMap;
Evan Chengf5cd4f02008-10-23 20:43:13 +000084
Evan Cheng54898932008-10-29 08:39:34 +000085 // Def2SpillMap - A map from a def instruction index to spill index.
Lang Hames6cc91e32009-10-03 04:31:31 +000086 DenseMap<LiveIndex, LiveIndex> Def2SpillMap;
Evan Cheng06587492008-10-24 02:05:00 +000087
Evan Cheng09e8ca82008-10-20 21:44:59 +000088 public:
89 static char ID;
90 PreAllocSplitting() : MachineFunctionPass(&ID) {}
91
92 virtual bool runOnMachineFunction(MachineFunction &MF);
93
94 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000095 AU.setPreservesCFG();
Lang Hames67f25662009-10-20 21:25:13 +000096 AU.addRequired<LiveIndexes>();
97 AU.addPreserved<LiveIndexes>();
Evan Cheng09e8ca82008-10-20 21:44:59 +000098 AU.addRequired<LiveIntervals>();
99 AU.addPreserved<LiveIntervals>();
Evan Chengd0e32c52008-10-29 05:06:14 +0000100 AU.addRequired<LiveStacks>();
101 AU.addPreserved<LiveStacks>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000102 AU.addPreserved<RegisterCoalescer>();
103 if (StrongPHIElim)
104 AU.addPreservedID(StrongPHIEliminationID);
105 else
106 AU.addPreservedID(PHIEliminationID);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000107 AU.addRequired<MachineDominatorTree>();
108 AU.addRequired<MachineLoopInfo>();
Owen Anderson420dd372009-03-14 21:40:05 +0000109 AU.addRequired<VirtRegMap>();
Owen Andersonf1f75b12008-11-04 22:22:41 +0000110 AU.addPreserved<MachineDominatorTree>();
111 AU.addPreserved<MachineLoopInfo>();
Owen Anderson420dd372009-03-14 21:40:05 +0000112 AU.addPreserved<VirtRegMap>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000113 MachineFunctionPass::getAnalysisUsage(AU);
114 }
115
116 virtual void releaseMemory() {
Evan Chengd0e32c52008-10-29 05:06:14 +0000117 IntervalSSMap.clear();
Evan Cheng54898932008-10-29 08:39:34 +0000118 Def2SpillMap.clear();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000119 }
120
121 virtual const char *getPassName() const {
122 return "Pre-Register Allocaton Live Interval Splitting";
123 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000124
125 /// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000126 virtual void print(raw_ostream &O, const Module* M = 0) const {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000127 LIs->print(O, M);
128 }
129
Evan Chengf5cd4f02008-10-23 20:43:13 +0000130
131 private:
132 MachineBasicBlock::iterator
133 findNextEmptySlot(MachineBasicBlock*, MachineInstr*,
Lang Hamescc3b0652009-10-03 04:21:37 +0000134 LiveIndex&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000135
136 MachineBasicBlock::iterator
Evan Cheng1f08cc22008-10-28 05:28:21 +0000137 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
Lang Hamescc3b0652009-10-03 04:21:37 +0000138 SmallPtrSet<MachineInstr*, 4>&, LiveIndex&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000139
140 MachineBasicBlock::iterator
Lang Hamescc3b0652009-10-03 04:21:37 +0000141 findRestorePoint(MachineBasicBlock*, MachineInstr*, LiveIndex,
142 SmallPtrSet<MachineInstr*, 4>&, LiveIndex&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000143
Evan Chengd0e32c52008-10-29 05:06:14 +0000144 int CreateSpillStackSlot(unsigned, const TargetRegisterClass *);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000145
Lang Hames86511252009-09-04 20:41:11 +0000146 bool IsAvailableInStack(MachineBasicBlock*, unsigned,
Lang Hames6cc91e32009-10-03 04:31:31 +0000147 LiveIndex, LiveIndex,
Lang Hamescc3b0652009-10-03 04:21:37 +0000148 LiveIndex&, int&) const;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000149
Lang Hames6cc91e32009-10-03 04:31:31 +0000150 void UpdateSpillSlotInterval(VNInfo*, LiveIndex, LiveIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000151
Evan Chengf5cd4f02008-10-23 20:43:13 +0000152 bool SplitRegLiveInterval(LiveInterval*);
153
Owen Anderson956ec272009-01-23 00:23:32 +0000154 bool SplitRegLiveIntervals(const TargetRegisterClass **,
155 SmallPtrSet<LiveInterval*, 8>&);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000156
157 bool createsNewJoin(LiveRange* LR, MachineBasicBlock* DefMBB,
158 MachineBasicBlock* BarrierMBB);
Owen Anderson75fa96b2008-11-19 04:28:29 +0000159 bool Rematerialize(unsigned vreg, VNInfo* ValNo,
160 MachineInstr* DefMI,
161 MachineBasicBlock::iterator RestorePt,
Lang Hamescc3b0652009-10-03 04:21:37 +0000162 LiveIndex RestoreIdx,
Owen Anderson75fa96b2008-11-19 04:28:29 +0000163 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000164 MachineInstr* FoldSpill(unsigned vreg, const TargetRegisterClass* RC,
165 MachineInstr* DefMI,
166 MachineInstr* Barrier,
167 MachineBasicBlock* MBB,
168 int& SS,
169 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersonc93023a2009-03-04 08:52:31 +0000170 MachineInstr* FoldRestore(unsigned vreg,
171 const TargetRegisterClass* RC,
172 MachineInstr* Barrier,
173 MachineBasicBlock* MBB,
174 int SS,
175 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000176 void RenumberValno(VNInfo* VN);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000177 void ReconstructLiveInterval(LiveInterval* LI);
Owen Anderson956ec272009-01-23 00:23:32 +0000178 bool removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split);
Owen Anderson45e68552009-01-29 05:28:55 +0000179 unsigned getNumberOfNonSpills(SmallPtrSet<MachineInstr*, 4>& MIs,
180 unsigned Reg, int FrameIndex, bool& TwoAddr);
Evan Cheng19a72582009-02-02 18:33:18 +0000181 VNInfo* PerformPHIConstruction(MachineBasicBlock::iterator Use,
182 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000183 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000184 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
185 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
186 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000187 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
188 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000189 bool IsTopLevel, bool IsIntraBlock);
190 VNInfo* PerformPHIConstructionFallBack(MachineBasicBlock::iterator Use,
191 MachineBasicBlock* MBB, LiveInterval* LI,
192 SmallPtrSet<MachineInstr*, 4>& Visited,
193 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
194 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
195 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
196 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
197 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
198 bool IsTopLevel, bool IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000199};
Evan Cheng09e8ca82008-10-20 21:44:59 +0000200} // end anonymous namespace
201
202char PreAllocSplitting::ID = 0;
203
204static RegisterPass<PreAllocSplitting>
205X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting");
206
207const PassInfo *const llvm::PreAllocSplittingID = &X;
208
Evan Chengf5cd4f02008-10-23 20:43:13 +0000209
210/// findNextEmptySlot - Find a gap after the given machine instruction in the
211/// instruction index map. If there isn't one, return end().
212MachineBasicBlock::iterator
213PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI,
Lang Hamescc3b0652009-10-03 04:21:37 +0000214 LiveIndex &SpotIndex) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000215 MachineBasicBlock::iterator MII = MI;
216 if (++MII != MBB->end()) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000217 LiveIndex Index =
Lang Hames86511252009-09-04 20:41:11 +0000218 LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII));
Lang Hamescc3b0652009-10-03 04:21:37 +0000219 if (Index != LiveIndex()) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000220 SpotIndex = Index;
221 return MII;
222 }
223 }
224 return MBB->end();
225}
226
227/// findSpillPoint - Find a gap as far away from the given MI that's suitable
228/// for spilling the current live interval. The index must be before any
229/// defs and uses of the live interval register in the mbb. Return begin() if
230/// none is found.
231MachineBasicBlock::iterator
232PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000233 MachineInstr *DefMI,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000234 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
Lang Hamescc3b0652009-10-03 04:21:37 +0000235 LiveIndex &SpillIndex) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000236 MachineBasicBlock::iterator Pt = MBB->begin();
237
Owen Anderson696a1302009-03-31 08:27:09 +0000238 MachineBasicBlock::iterator MII = MI;
239 MachineBasicBlock::iterator EndPt = DefMI
240 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
Owen Anderson4cafbb52009-02-20 10:02:23 +0000241
Owen Anderson696a1302009-03-31 08:27:09 +0000242 while (MII != EndPt && !RefsInMBB.count(MII) &&
243 MII->getOpcode() != TRI->getCallFrameSetupOpcode())
244 --MII;
245 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson4cafbb52009-02-20 10:02:23 +0000246
Owen Anderson696a1302009-03-31 08:27:09 +0000247 while (MII != EndPt && !RefsInMBB.count(MII)) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000248 LiveIndex Index = LIs->getInstructionIndex(MII);
Owen Anderson3ef45492009-01-29 22:13:06 +0000249
Owen Anderson696a1302009-03-31 08:27:09 +0000250 // We can't insert the spill between the barrier (a call), and its
251 // corresponding call frame setup.
252 if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode()) {
253 while (MII->getOpcode() != TRI->getCallFrameSetupOpcode()) {
Owen Anderson5734c942009-02-05 05:58:41 +0000254 --MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000255 if (MII == EndPt) {
256 return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000257 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000258 }
Owen Anderson696a1302009-03-31 08:27:09 +0000259 continue;
260 } else if (LIs->hasGapBeforeInstr(Index)) {
261 Pt = MII;
262 SpillIndex = LIs->findGapBeforeInstr(Index, true);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000263 }
Owen Anderson696a1302009-03-31 08:27:09 +0000264
265 if (RefsInMBB.count(MII))
266 return Pt;
267
268
269 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000270 }
271
272 return Pt;
273}
274
275/// findRestorePoint - Find a gap in the instruction index map that's suitable
276/// for restoring the current live interval value. The index must be before any
277/// uses of the live interval register in the mbb. Return end() if none is
278/// found.
279MachineBasicBlock::iterator
280PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Lang Hamescc3b0652009-10-03 04:21:37 +0000281 LiveIndex LastIdx,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000282 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
Lang Hamescc3b0652009-10-03 04:21:37 +0000283 LiveIndex &RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000284 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
285 // begin index accordingly.
Owen Anderson5a92d4e2008-11-18 20:53:59 +0000286 MachineBasicBlock::iterator Pt = MBB->end();
Owen Anderson696a1302009-03-31 08:27:09 +0000287 MachineBasicBlock::iterator EndPt = MBB->getFirstTerminator();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000288
Owen Anderson696a1302009-03-31 08:27:09 +0000289 // We start at the call, so walk forward until we find the call frame teardown
290 // since we can't insert restores before that. Bail if we encounter a use
291 // during this time.
292 MachineBasicBlock::iterator MII = MI;
293 if (MII == EndPt) return Pt;
294
295 while (MII != EndPt && !RefsInMBB.count(MII) &&
296 MII->getOpcode() != TRI->getCallFrameDestroyOpcode())
297 ++MII;
298 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
299 ++MII;
300
301 // FIXME: Limit the number of instructions to examine to reduce
302 // compile time?
303 while (MII != EndPt) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000304 LiveIndex Index = LIs->getInstructionIndex(MII);
Owen Anderson696a1302009-03-31 08:27:09 +0000305 if (Index > LastIdx)
306 break;
Lang Hamescc3b0652009-10-03 04:21:37 +0000307 LiveIndex Gap = LIs->findGapBeforeInstr(Index);
Owen Anderson3ef45492009-01-29 22:13:06 +0000308
Owen Anderson696a1302009-03-31 08:27:09 +0000309 // We can't insert a restore between the barrier (a call) and its
310 // corresponding call frame teardown.
311 if (MII->getOpcode() == TRI->getCallFrameSetupOpcode()) {
312 do {
313 if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
Owen Anderson5734c942009-02-05 05:58:41 +0000314 ++MII;
Owen Anderson696a1302009-03-31 08:27:09 +0000315 } while (MII->getOpcode() != TRI->getCallFrameDestroyOpcode());
Lang Hamescc3b0652009-10-03 04:21:37 +0000316 } else if (Gap != LiveIndex()) {
Owen Anderson696a1302009-03-31 08:27:09 +0000317 Pt = MII;
318 RestoreIndex = Gap;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000319 }
Owen Anderson696a1302009-03-31 08:27:09 +0000320
321 if (RefsInMBB.count(MII))
322 return Pt;
323
324 ++MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000325 }
326
327 return Pt;
328}
329
Evan Chengd0e32c52008-10-29 05:06:14 +0000330/// CreateSpillStackSlot - Create a stack slot for the live interval being
331/// split. If the live interval was previously split, just reuse the same
332/// slot.
333int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
334 const TargetRegisterClass *RC) {
335 int SS;
336 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
337 if (I != IntervalSSMap.end()) {
338 SS = I->second;
339 } else {
340 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
341 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000342 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000343
344 // Create live interval for stack slot.
Evan Chengc781a242009-05-03 18:32:42 +0000345 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Evan Cheng54898932008-10-29 08:39:34 +0000346 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000347 CurrSValNo = CurrSLI->getValNumInfo(0);
348 else
Lang Hamescc3b0652009-10-03 04:21:37 +0000349 CurrSValNo = CurrSLI->getNextValue(LiveIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +0000350 LSs->getVNInfoAllocator());
Evan Chengd0e32c52008-10-29 05:06:14 +0000351 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000352}
353
Evan Chengd0e32c52008-10-29 05:06:14 +0000354/// IsAvailableInStack - Return true if register is available in a split stack
355/// slot at the specified index.
356bool
Evan Cheng54898932008-10-29 08:39:34 +0000357PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
Lang Hamescc3b0652009-10-03 04:21:37 +0000358 unsigned Reg, LiveIndex DefIndex,
359 LiveIndex RestoreIndex,
360 LiveIndex &SpillIndex,
Evan Cheng54898932008-10-29 08:39:34 +0000361 int& SS) const {
362 if (!DefMBB)
363 return false;
364
Evan Chengd0e32c52008-10-29 05:06:14 +0000365 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
366 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000367 return false;
Lang Hames6cc91e32009-10-03 04:31:31 +0000368 DenseMap<LiveIndex, LiveIndex>::iterator
Lang Hames86511252009-09-04 20:41:11 +0000369 II = Def2SpillMap.find(DefIndex);
Evan Cheng54898932008-10-29 08:39:34 +0000370 if (II == Def2SpillMap.end())
371 return false;
372
373 // If last spill of def is in the same mbb as barrier mbb (where restore will
374 // be), make sure it's not below the intended restore index.
375 // FIXME: Undo the previous spill?
376 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
377 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
378 return false;
379
380 SS = I->second;
381 SpillIndex = II->second;
382 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000383}
384
Evan Chengd0e32c52008-10-29 05:06:14 +0000385/// UpdateSpillSlotInterval - Given the specified val# of the register live
386/// interval being split, and the spill and restore indicies, update the live
387/// interval of the spill stack slot.
388void
Lang Hamescc3b0652009-10-03 04:21:37 +0000389PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, LiveIndex SpillIndex,
390 LiveIndex RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000391 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
392 "Expect restore in the barrier mbb");
393
394 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
395 if (MBB == BarrierMBB) {
396 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000397 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
398 CurrSLI->addRange(SLR);
399 return;
400 }
401
Evan Cheng54898932008-10-29 08:39:34 +0000402 SmallPtrSet<MachineBasicBlock*, 4> Processed;
Lang Hamescc3b0652009-10-03 04:21:37 +0000403 LiveIndex EndIdx = LIs->getMBBEndIdx(MBB);
Lang Hames67f25662009-10-20 21:25:13 +0000404 LiveRange SLR(SpillIndex, EndIdx.getNextSlot(), CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000405 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000406 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000407
408 // Start from the spill mbb, figure out the extend of the spill slot's
409 // live interval.
410 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000411 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
412 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000413 // If live range extend beyond end of mbb, add successors to work list.
414 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
415 SE = MBB->succ_end(); SI != SE; ++SI)
416 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000417
418 while (!WorkList.empty()) {
419 MachineBasicBlock *MBB = WorkList.back();
420 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000421 if (Processed.count(MBB))
422 continue;
Lang Hamescc3b0652009-10-03 04:21:37 +0000423 LiveIndex Idx = LIs->getMBBStartIdx(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000424 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000425 if (LR && LR->valno == ValNo) {
426 EndIdx = LIs->getMBBEndIdx(MBB);
427 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000428 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000429 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000430 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000431 } else if (LR->end > EndIdx) {
432 // Live range extends beyond end of mbb, process successors.
Lang Hames67f25662009-10-20 21:25:13 +0000433 LiveRange SLR(Idx, EndIdx.getNextIndex(), CurrSValNo);
Evan Cheng54898932008-10-29 08:39:34 +0000434 CurrSLI->addRange(SLR);
435 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
436 SE = MBB->succ_end(); SI != SE; ++SI)
437 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000438 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000439 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000440 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000441 }
Evan Cheng54898932008-10-29 08:39:34 +0000442 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000443 }
444 }
445}
446
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000447/// PerformPHIConstruction - From properly set up use and def lists, use a PHI
448/// construction algorithm to compute the ranges and valnos for an interval.
Evan Cheng19a72582009-02-02 18:33:18 +0000449VNInfo*
450PreAllocSplitting::PerformPHIConstruction(MachineBasicBlock::iterator UseI,
451 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000452 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000453 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
454 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
455 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000456 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
457 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000458 bool IsTopLevel, bool IsIntraBlock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000459 // Return memoized result if it's available.
Evan Cheng19a72582009-02-02 18:33:18 +0000460 if (IsTopLevel && Visited.count(UseI) && NewVNs.count(UseI))
461 return NewVNs[UseI];
462 else if (!IsTopLevel && IsIntraBlock && NewVNs.count(UseI))
463 return NewVNs[UseI];
464 else if (!IsIntraBlock && LiveOut.count(MBB))
Owen Anderson7d211e22008-12-31 02:00:25 +0000465 return LiveOut[MBB];
466
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000467 // Check if our block contains any uses or defs.
Owen Anderson7d211e22008-12-31 02:00:25 +0000468 bool ContainsDefs = Defs.count(MBB);
469 bool ContainsUses = Uses.count(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000470
Evan Cheng19a72582009-02-02 18:33:18 +0000471 VNInfo* RetVNI = 0;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000472
473 // Enumerate the cases of use/def contaning blocks.
474 if (!ContainsDefs && !ContainsUses) {
Evan Cheng19a72582009-02-02 18:33:18 +0000475 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
476 NewVNs, LiveOut, Phis,
477 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000478 } else if (ContainsDefs && !ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000479 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000480
481 // Search for the def in this block. If we don't find it before the
482 // instruction we care about, go to the fallback case. Note that that
Owen Anderson7d211e22008-12-31 02:00:25 +0000483 // should never happen: this cannot be intrablock, so use should
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000484 // always be an end() iterator.
Evan Cheng19a72582009-02-02 18:33:18 +0000485 assert(UseI == MBB->end() && "No use marked in intrablock");
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000486
Evan Cheng19a72582009-02-02 18:33:18 +0000487 MachineBasicBlock::iterator Walker = UseI;
488 --Walker;
489 while (Walker != MBB->begin()) {
490 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000491 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000492 --Walker;
493 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000494
495 // Once we've found it, extend its VNInfo to our instruction.
Lang Hamescc3b0652009-10-03 04:21:37 +0000496 LiveIndex DefIndex = LIs->getInstructionIndex(Walker);
Lang Hames67f25662009-10-20 21:25:13 +0000497 DefIndex = DefIndex.getDefIndex();
Lang Hamescc3b0652009-10-03 04:21:37 +0000498 LiveIndex EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000499
Evan Cheng19a72582009-02-02 18:33:18 +0000500 RetVNI = NewVNs[Walker];
Lang Hames67f25662009-10-20 21:25:13 +0000501 LI->addRange(LiveRange(DefIndex, EndIndex.getNextSlot(), RetVNI));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000502 } else if (!ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000503 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000504
505 // Search for the use in this block that precedes the instruction we care
Evan Cheng19a72582009-02-02 18:33:18 +0000506 // about, going to the fallback case if we don't find it.
507 if (UseI == MBB->begin())
508 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
509 Uses, NewVNs, LiveOut, Phis,
510 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000511
Evan Cheng19a72582009-02-02 18:33:18 +0000512 MachineBasicBlock::iterator Walker = UseI;
513 --Walker;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000514 bool found = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000515 while (Walker != MBB->begin()) {
516 if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000517 found = true;
518 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000519 }
520 --Walker;
521 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000522
523 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000524 if (!found) {
Evan Cheng19a72582009-02-02 18:33:18 +0000525 if (BlockUses.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000526 found = true;
527 else
Evan Cheng19a72582009-02-02 18:33:18 +0000528 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
529 Uses, NewVNs, LiveOut, Phis,
530 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000531 }
532
Lang Hamescc3b0652009-10-03 04:21:37 +0000533 LiveIndex UseIndex = LIs->getInstructionIndex(Walker);
Lang Hames67f25662009-10-20 21:25:13 +0000534 UseIndex = UseIndex.getUseIndex();
Lang Hamescc3b0652009-10-03 04:21:37 +0000535 LiveIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000536 if (IsIntraBlock) {
537 EndIndex = LIs->getInstructionIndex(UseI);
Lang Hames67f25662009-10-20 21:25:13 +0000538 EndIndex = EndIndex.getUseIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000539 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000540 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000541
542 // Now, recursively phi construct the VNInfo for the use we found,
543 // and then extend it to include the instruction we care about
Evan Cheng19a72582009-02-02 18:33:18 +0000544 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
545 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000546
Lang Hames67f25662009-10-20 21:25:13 +0000547 LI->addRange(LiveRange(UseIndex, EndIndex.getNextSlot(), RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000548
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000549 // FIXME: Need to set kills properly for inter-block stuff.
Lang Hames86511252009-09-04 20:41:11 +0000550 if (RetVNI->isKill(UseIndex)) RetVNI->removeKill(UseIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000551 if (IsIntraBlock)
Lang Hames86511252009-09-04 20:41:11 +0000552 RetVNI->addKill(EndIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000553 } else if (ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000554 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
555 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000556
557 // This case is basically a merging of the two preceding case, with the
558 // special note that checking for defs must take precedence over checking
559 // for uses, because of two-address instructions.
560
Evan Cheng19a72582009-02-02 18:33:18 +0000561 if (UseI == MBB->begin())
562 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
563 NewVNs, LiveOut, Phis,
564 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000565
Evan Cheng19a72582009-02-02 18:33:18 +0000566 MachineBasicBlock::iterator Walker = UseI;
567 --Walker;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000568 bool foundDef = false;
569 bool foundUse = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000570 while (Walker != MBB->begin()) {
571 if (BlockDefs.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000572 foundDef = true;
573 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000574 } else if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000575 foundUse = true;
576 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000577 }
578 --Walker;
579 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000580
581 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000582 if (!foundDef && !foundUse) {
Evan Cheng19a72582009-02-02 18:33:18 +0000583 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000584 foundDef = true;
Evan Cheng19a72582009-02-02 18:33:18 +0000585 else if (BlockUses.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000586 foundUse = true;
587 else
Evan Cheng19a72582009-02-02 18:33:18 +0000588 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
589 Uses, NewVNs, LiveOut, Phis,
590 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000591 }
592
Lang Hamescc3b0652009-10-03 04:21:37 +0000593 LiveIndex StartIndex = LIs->getInstructionIndex(Walker);
Lang Hames67f25662009-10-20 21:25:13 +0000594 StartIndex = foundDef ? StartIndex.getDefIndex() : StartIndex.getUseIndex();
Lang Hamescc3b0652009-10-03 04:21:37 +0000595 LiveIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000596 if (IsIntraBlock) {
597 EndIndex = LIs->getInstructionIndex(UseI);
Lang Hames67f25662009-10-20 21:25:13 +0000598 EndIndex = EndIndex.getUseIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000599 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000600 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000601
602 if (foundDef)
Evan Cheng19a72582009-02-02 18:33:18 +0000603 RetVNI = NewVNs[Walker];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000604 else
Evan Cheng19a72582009-02-02 18:33:18 +0000605 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
606 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000607
Lang Hames67f25662009-10-20 21:25:13 +0000608 LI->addRange(LiveRange(StartIndex, EndIndex.getNextSlot(), RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000609
Lang Hames86511252009-09-04 20:41:11 +0000610 if (foundUse && RetVNI->isKill(StartIndex))
611 RetVNI->removeKill(StartIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000612 if (IsIntraBlock) {
Lang Hames86511252009-09-04 20:41:11 +0000613 RetVNI->addKill(EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000614 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000615 }
616
617 // Memoize results so we don't have to recompute them.
Evan Cheng19a72582009-02-02 18:33:18 +0000618 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000619 else {
Evan Cheng19a72582009-02-02 18:33:18 +0000620 if (!NewVNs.count(UseI))
621 NewVNs[UseI] = RetVNI;
622 Visited.insert(UseI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000623 }
624
Evan Cheng19a72582009-02-02 18:33:18 +0000625 return RetVNI;
626}
627
628/// PerformPHIConstructionFallBack - PerformPHIConstruction fall back path.
629///
630VNInfo*
631PreAllocSplitting::PerformPHIConstructionFallBack(MachineBasicBlock::iterator UseI,
632 MachineBasicBlock* MBB, LiveInterval* LI,
633 SmallPtrSet<MachineInstr*, 4>& Visited,
634 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
635 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
636 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
637 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
638 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
639 bool IsTopLevel, bool IsIntraBlock) {
640 // NOTE: Because this is the fallback case from other cases, we do NOT
641 // assume that we are not intrablock here.
642 if (Phis.count(MBB)) return Phis[MBB];
643
Lang Hamescc3b0652009-10-03 04:21:37 +0000644 LiveIndex StartIndex = LIs->getMBBStartIdx(MBB);
Lang Hames857c4e02009-06-17 21:01:20 +0000645 VNInfo *RetVNI = Phis[MBB] =
Lang Hamescc3b0652009-10-03 04:21:37 +0000646 LI->getNextValue(LiveIndex(), /*FIXME*/ 0, false,
Lang Hames86511252009-09-04 20:41:11 +0000647 LIs->getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +0000648
Evan Cheng19a72582009-02-02 18:33:18 +0000649 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
650
651 // If there are no uses or defs between our starting point and the
652 // beginning of the block, then recursive perform phi construction
653 // on our predecessors.
654 DenseMap<MachineBasicBlock*, VNInfo*> IncomingVNs;
655 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
656 PE = MBB->pred_end(); PI != PE; ++PI) {
657 VNInfo* Incoming = PerformPHIConstruction((*PI)->end(), *PI, LI,
658 Visited, Defs, Uses, NewVNs,
659 LiveOut, Phis, false, false);
660 if (Incoming != 0)
661 IncomingVNs[*PI] = Incoming;
662 }
663
Lang Hames857c4e02009-06-17 21:01:20 +0000664 if (MBB->pred_size() == 1 && !RetVNI->hasPHIKill()) {
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000665 VNInfo* OldVN = RetVNI;
666 VNInfo* NewVN = IncomingVNs.begin()->second;
667 VNInfo* MergedVN = LI->MergeValueNumberInto(OldVN, NewVN);
668 if (MergedVN == OldVN) std::swap(OldVN, NewVN);
669
670 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator LOI = LiveOut.begin(),
671 LOE = LiveOut.end(); LOI != LOE; ++LOI)
672 if (LOI->second == OldVN)
673 LOI->second = MergedVN;
674 for (DenseMap<MachineInstr*, VNInfo*>::iterator NVI = NewVNs.begin(),
675 NVE = NewVNs.end(); NVI != NVE; ++NVI)
676 if (NVI->second == OldVN)
677 NVI->second = MergedVN;
678 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator PI = Phis.begin(),
679 PE = Phis.end(); PI != PE; ++PI)
680 if (PI->second == OldVN)
681 PI->second = MergedVN;
682 RetVNI = MergedVN;
Evan Cheng19a72582009-02-02 18:33:18 +0000683 } else {
684 // Otherwise, merge the incoming VNInfos with a phi join. Create a new
685 // VNInfo to represent the joined value.
686 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator I =
687 IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) {
Lang Hames857c4e02009-06-17 21:01:20 +0000688 I->second->setHasPHIKill(true);
Lang Hamescc3b0652009-10-03 04:21:37 +0000689 LiveIndex KillIndex = LIs->getMBBEndIdx(I->first);
Lang Hames86511252009-09-04 20:41:11 +0000690 if (!I->second->isKill(KillIndex))
691 I->second->addKill(KillIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000692 }
693 }
694
Lang Hamescc3b0652009-10-03 04:21:37 +0000695 LiveIndex EndIndex;
Evan Cheng19a72582009-02-02 18:33:18 +0000696 if (IsIntraBlock) {
697 EndIndex = LIs->getInstructionIndex(UseI);
Lang Hames67f25662009-10-20 21:25:13 +0000698 EndIndex = EndIndex.getUseIndex();
Evan Cheng19a72582009-02-02 18:33:18 +0000699 } else
700 EndIndex = LIs->getMBBEndIdx(MBB);
Lang Hames67f25662009-10-20 21:25:13 +0000701 LI->addRange(LiveRange(StartIndex, EndIndex.getNextSlot(), RetVNI));
Evan Cheng19a72582009-02-02 18:33:18 +0000702 if (IsIntraBlock)
Lang Hames86511252009-09-04 20:41:11 +0000703 RetVNI->addKill(EndIndex);
Evan Cheng19a72582009-02-02 18:33:18 +0000704
705 // Memoize results so we don't have to recompute them.
706 if (!IsIntraBlock)
707 LiveOut[MBB] = RetVNI;
708 else {
709 if (!NewVNs.count(UseI))
710 NewVNs[UseI] = RetVNI;
711 Visited.insert(UseI);
712 }
713
714 return RetVNI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000715}
716
717/// ReconstructLiveInterval - Recompute a live interval from scratch.
718void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) {
719 BumpPtrAllocator& Alloc = LIs->getVNInfoAllocator();
720
721 // Clear the old ranges and valnos;
722 LI->clear();
723
724 // Cache the uses and defs of the register
725 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
726 RegMap Defs, Uses;
727
728 // Keep track of the new VNs we're creating.
729 DenseMap<MachineInstr*, VNInfo*> NewVNs;
730 SmallPtrSet<VNInfo*, 2> PhiVNs;
731
732 // Cache defs, and create a new VNInfo for each def.
733 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
734 DE = MRI->def_end(); DI != DE; ++DI) {
735 Defs[(*DI).getParent()].insert(&*DI);
736
Lang Hamescc3b0652009-10-03 04:21:37 +0000737 LiveIndex DefIdx = LIs->getInstructionIndex(&*DI);
Lang Hames67f25662009-10-20 21:25:13 +0000738 DefIdx = DefIdx.getDefIndex();
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000739
Lang Hames857c4e02009-06-17 21:01:20 +0000740 assert(DI->getOpcode() != TargetInstrInfo::PHI &&
741 "Following NewVN isPHIDef flag incorrect. Fix me!");
742 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, true, Alloc);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000743
744 // If the def is a move, set the copy field.
Evan Cheng04ee5a12009-01-20 19:12:24 +0000745 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
746 if (TII->isMoveInstr(*DI, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
747 if (DstReg == LI->reg)
Lang Hames52c1afc2009-08-10 23:43:28 +0000748 NewVN->setCopy(&*DI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000749
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000750 NewVNs[&*DI] = NewVN;
751 }
752
753 // Cache uses as a separate pass from actually processing them.
754 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
755 UE = MRI->use_end(); UI != UE; ++UI)
756 Uses[(*UI).getParent()].insert(&*UI);
757
758 // Now, actually process every use and use a phi construction algorithm
759 // to walk from it to its reaching definitions, building VNInfos along
760 // the way.
Owen Anderson7d211e22008-12-31 02:00:25 +0000761 DenseMap<MachineBasicBlock*, VNInfo*> LiveOut;
762 DenseMap<MachineBasicBlock*, VNInfo*> Phis;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000763 SmallPtrSet<MachineInstr*, 4> Visited;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000764 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
765 UE = MRI->use_end(); UI != UE; ++UI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000766 PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000767 Uses, NewVNs, LiveOut, Phis, true, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000768 }
Owen Andersond4f6fe52008-12-28 23:35:13 +0000769
770 // Add ranges for dead defs
771 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
772 DE = MRI->def_end(); DI != DE; ++DI) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000773 LiveIndex DefIdx = LIs->getInstructionIndex(&*DI);
Lang Hames67f25662009-10-20 21:25:13 +0000774 DefIdx = DefIdx.getDefIndex();
Owen Andersond4f6fe52008-12-28 23:35:13 +0000775
776 if (LI->liveAt(DefIdx)) continue;
777
778 VNInfo* DeadVN = NewVNs[&*DI];
Lang Hames67f25662009-10-20 21:25:13 +0000779 LI->addRange(LiveRange(DefIdx, DefIdx.getNextSlot(), DeadVN));
Lang Hames86511252009-09-04 20:41:11 +0000780 DeadVN->addKill(DefIdx);
Owen Andersond4f6fe52008-12-28 23:35:13 +0000781 }
Evan Cheng35ca9202009-10-09 01:17:11 +0000782
783 // Update kill markers.
784 for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
785 VI != VE; ++VI) {
786 VNInfo* VNI = *VI;
787 for (unsigned i = 0, e = VNI->kills.size(); i != e; ++i) {
788 LiveIndex KillIdx = VNI->kills[i];
Lang Hames67f25662009-10-20 21:25:13 +0000789 if (KillIdx.isPHI())
Evan Cheng35ca9202009-10-09 01:17:11 +0000790 continue;
791 MachineInstr *KillMI = LIs->getInstructionFromIndex(KillIdx);
792 if (KillMI) {
793 MachineOperand *KillMO = KillMI->findRegisterUseOperand(CurrLI->reg);
794 if (KillMO)
795 // It could be a dead def.
796 KillMO->setIsKill();
797 }
798 }
799 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000800}
801
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000802/// RenumberValno - Split the given valno out into a new vreg, allowing it to
803/// be allocated to a different register. This function creates a new vreg,
804/// copies the valno and its live ranges over to the new vreg's interval,
805/// removes them from the old interval, and rewrites all uses and defs of
806/// the original reg to the new vreg within those ranges.
807void PreAllocSplitting::RenumberValno(VNInfo* VN) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000808 SmallVector<VNInfo*, 4> Stack;
809 SmallVector<VNInfo*, 4> VNsToCopy;
810 Stack.push_back(VN);
811
812 // Walk through and copy the valno we care about, and any other valnos
813 // that are two-address redefinitions of the one we care about. These
814 // will need to be rewritten as well. We also check for safety of the
815 // renumbering here, by making sure that none of the valno involved has
816 // phi kills.
817 while (!Stack.empty()) {
818 VNInfo* OldVN = Stack.back();
819 Stack.pop_back();
820
821 // Bail out if we ever encounter a valno that has a PHI kill. We can't
822 // renumber these.
Lang Hames857c4e02009-06-17 21:01:20 +0000823 if (OldVN->hasPHIKill()) return;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000824
825 VNsToCopy.push_back(OldVN);
826
827 // Locate two-address redefinitions
Lang Hamesffd13262009-07-09 03:57:02 +0000828 for (VNInfo::KillSet::iterator KI = OldVN->kills.begin(),
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000829 KE = OldVN->kills.end(); KI != KE; ++KI) {
Lang Hames67f25662009-10-20 21:25:13 +0000830 assert(!KI->isPHI() &&
Lang Hames86511252009-09-04 20:41:11 +0000831 "VN previously reported having no PHI kills.");
832 MachineInstr* MI = LIs->getInstructionFromIndex(*KI);
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000833 unsigned DefIdx = MI->findRegisterDefOperandIdx(CurrLI->reg);
834 if (DefIdx == ~0U) continue;
Bob Wilsond9df5012009-04-09 17:16:43 +0000835 if (MI->isRegTiedToUseOperand(DefIdx)) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000836 VNInfo* NextVN =
Lang Hames67f25662009-10-20 21:25:13 +0000837 CurrLI->findDefinedVNInfoForRegInt(KI->getDefIndex());
Owen Andersonb4b84362009-01-26 21:57:31 +0000838 if (NextVN == OldVN) continue;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000839 Stack.push_back(NextVN);
840 }
841 }
842 }
843
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000844 // Create the new vreg
845 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
846
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000847 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000848 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000849
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000850 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
851 VNsToCopy.end(); OI != OE; ++OI) {
852 VNInfo* OldVN = *OI;
853
854 // Copy the valno over
Lang Hames857c4e02009-06-17 21:01:20 +0000855 VNInfo* NewVN = NewLI.createValueCopy(OldVN, LIs->getVNInfoAllocator());
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000856 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
857
858 // Remove the valno from the old interval
859 CurrLI->removeValNo(OldVN);
860 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000861
862 // Rewrite defs and uses. This is done in two stages to avoid invalidating
863 // the reg_iterator.
864 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
865
866 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
867 E = MRI->reg_end(); I != E; ++I) {
868 MachineOperand& MO = I.getOperand();
Lang Hamescc3b0652009-10-03 04:21:37 +0000869 LiveIndex InstrIdx = LIs->getInstructionIndex(&*I);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000870
Lang Hames67f25662009-10-20 21:25:13 +0000871 if ((MO.isUse() && NewLI.liveAt(InstrIdx.getUseIndex())) ||
872 (MO.isDef() && NewLI.liveAt(InstrIdx.getDefIndex())))
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000873 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
874 }
875
876 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
877 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
878 MachineInstr* Inst = I->first;
879 unsigned OpIdx = I->second;
880 MachineOperand& MO = Inst->getOperand(OpIdx);
881 MO.setReg(NewVReg);
882 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000883
Owen Anderson420dd372009-03-14 21:40:05 +0000884 // Grow the VirtRegMap, since we've created a new vreg.
885 VRM->grow();
886
Owen Anderson45e68552009-01-29 05:28:55 +0000887 // The renumbered vreg shares a stack slot with the old register.
888 if (IntervalSSMap.count(CurrLI->reg))
889 IntervalSSMap[NewVReg] = IntervalSSMap[CurrLI->reg];
890
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000891 NumRenumbers++;
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000892}
893
Evan Cheng37844532009-07-16 09:20:10 +0000894bool PreAllocSplitting::Rematerialize(unsigned VReg, VNInfo* ValNo,
Owen Anderson6002e992008-12-04 21:20:30 +0000895 MachineInstr* DefMI,
896 MachineBasicBlock::iterator RestorePt,
Lang Hamescc3b0652009-10-03 04:21:37 +0000897 LiveIndex RestoreIdx,
Owen Anderson6002e992008-12-04 21:20:30 +0000898 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
899 MachineBasicBlock& MBB = *RestorePt->getParent();
900
901 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
Lang Hamescc3b0652009-10-03 04:21:37 +0000902 LiveIndex KillIdx;
Lang Hames857c4e02009-06-17 21:01:20 +0000903 if (!ValNo->isDefAccurate() || DefMI->getParent() == BarrierMBB)
Owen Anderson6002e992008-12-04 21:20:30 +0000904 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, KillIdx);
905 else
906 KillPt = findNextEmptySlot(DefMI->getParent(), DefMI, KillIdx);
907
908 if (KillPt == DefMI->getParent()->end())
909 return false;
910
Evan Cheng37844532009-07-16 09:20:10 +0000911 TII->reMaterialize(MBB, RestorePt, VReg, 0, DefMI);
Owen Anderson6002e992008-12-04 21:20:30 +0000912 LIs->InsertMachineInstrInMaps(prior(RestorePt), RestoreIdx);
913
Owen Andersonb4b84362009-01-26 21:57:31 +0000914 ReconstructLiveInterval(CurrLI);
Lang Hamescc3b0652009-10-03 04:21:37 +0000915 LiveIndex RematIdx = LIs->getInstructionIndex(prior(RestorePt));
Lang Hames67f25662009-10-20 21:25:13 +0000916 RematIdx = RematIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +0000917 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RematIdx));
Owen Andersone1762c92009-01-12 03:10:40 +0000918
Owen Anderson75fa96b2008-11-19 04:28:29 +0000919 ++NumSplits;
920 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000921 return true;
922}
923
924MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
925 const TargetRegisterClass* RC,
926 MachineInstr* DefMI,
927 MachineInstr* Barrier,
928 MachineBasicBlock* MBB,
929 int& SS,
930 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
931 MachineBasicBlock::iterator Pt = MBB->begin();
932
933 // Go top down if RefsInMBB is empty.
934 if (RefsInMBB.empty())
935 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000936
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000937 MachineBasicBlock::iterator FoldPt = Barrier;
938 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
939 !RefsInMBB.count(FoldPt))
940 --FoldPt;
941
942 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg, false);
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
952 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
953 if (I != IntervalSSMap.end()) {
954 SS = I->second;
955 } else {
Evan Cheng491f54f2009-10-17 09:20:14 +0000956 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000957 }
958
959 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
960 FoldPt, Ops, SS);
961
962 if (FMI) {
963 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
964 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
965 ++NumFolds;
966
967 IntervalSSMap[vreg] = SS;
Evan Chengc781a242009-05-03 18:32:42 +0000968 CurrSLI = &LSs->getOrCreateInterval(SS, RC);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000969 if (CurrSLI->hasAtLeastOneValue())
970 CurrSValNo = CurrSLI->getValNumInfo(0);
971 else
Lang Hamescc3b0652009-10-03 04:21:37 +0000972 CurrSValNo = CurrSLI->getNextValue(LiveIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +0000973 LSs->getVNInfoAllocator());
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000974 }
975
976 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000977}
978
Owen Andersonc93023a2009-03-04 08:52:31 +0000979MachineInstr* PreAllocSplitting::FoldRestore(unsigned vreg,
980 const TargetRegisterClass* RC,
981 MachineInstr* Barrier,
982 MachineBasicBlock* MBB,
983 int SS,
984 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
Owen Andersona2bfb542009-03-05 08:23:20 +0000985 if ((int)RestoreFoldLimit != -1 && RestoreFoldLimit == (int)NumRestoreFolds)
Owen Anderson323c58d2009-03-05 07:19:18 +0000986 return 0;
987
Owen Andersonc93023a2009-03-04 08:52:31 +0000988 // Go top down if RefsInMBB is empty.
989 if (RefsInMBB.empty())
990 return 0;
991
992 // Can't fold a restore between a call stack setup and teardown.
993 MachineBasicBlock::iterator FoldPt = Barrier;
Owen Anderson323c58d2009-03-05 07:19:18 +0000994
995 // Advance from barrier to call frame teardown.
996 while (FoldPt != MBB->getFirstTerminator() &&
997 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
998 if (RefsInMBB.count(FoldPt))
999 return 0;
Owen Andersonc93023a2009-03-04 08:52:31 +00001000
Owen Anderson323c58d2009-03-05 07:19:18 +00001001 ++FoldPt;
1002 }
1003
1004 if (FoldPt == MBB->getFirstTerminator())
1005 return 0;
1006 else
1007 ++FoldPt;
1008
1009 // Now find the restore point.
1010 while (FoldPt != MBB->getFirstTerminator() && !RefsInMBB.count(FoldPt)) {
Owen Andersonc93023a2009-03-04 08:52:31 +00001011 if (FoldPt->getOpcode() == TRI->getCallFrameSetupOpcode()) {
1012 while (FoldPt != MBB->getFirstTerminator() &&
1013 FoldPt->getOpcode() != TRI->getCallFrameDestroyOpcode()) {
1014 if (RefsInMBB.count(FoldPt))
1015 return 0;
1016
1017 ++FoldPt;
1018 }
1019
Owen Anderson323c58d2009-03-05 07:19:18 +00001020 if (FoldPt == MBB->getFirstTerminator())
1021 return 0;
1022 }
1023
1024 ++FoldPt;
Owen Andersonc93023a2009-03-04 08:52:31 +00001025 }
1026
1027 if (FoldPt == MBB->getFirstTerminator())
1028 return 0;
1029
1030 int OpIdx = FoldPt->findRegisterUseOperandIdx(vreg, true);
1031 if (OpIdx == -1)
1032 return 0;
1033
1034 SmallVector<unsigned, 1> Ops;
1035 Ops.push_back(OpIdx);
1036
1037 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
1038 return 0;
1039
1040 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
1041 FoldPt, Ops, SS);
1042
1043 if (FMI) {
1044 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
1045 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
Owen Anderson323c58d2009-03-05 07:19:18 +00001046 ++NumRestoreFolds;
Owen Andersonc93023a2009-03-04 08:52:31 +00001047 }
1048
1049 return FMI;
1050}
1051
Evan Chengf5cd4f02008-10-23 20:43:13 +00001052/// SplitRegLiveInterval - Split (spill and restore) the given live interval
1053/// so it would not cross the barrier that's being processed. Shrink wrap
1054/// (minimize) the live interval to the last uses.
1055bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
Lang Hames67f25662009-10-20 21:25:13 +00001056 DEBUG(errs() << "Pre-alloc splitting " << LI->reg << " for " << *Barrier
1057 << " result: ");
1058
Evan Chengf5cd4f02008-10-23 20:43:13 +00001059 CurrLI = LI;
1060
1061 // Find live range where current interval cross the barrier.
1062 LiveInterval::iterator LR =
Lang Hames67f25662009-10-20 21:25:13 +00001063 CurrLI->FindLiveRangeContaining(BarrierIdx.getUseIndex());
Evan Chengf5cd4f02008-10-23 20:43:13 +00001064 VNInfo *ValNo = LR->valno;
1065
Torok Edwinf3689232009-07-12 20:07:01 +00001066 assert(!ValNo->isUnused() && "Val# is defined by a dead def?");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001067
Lang Hames857c4e02009-06-17 21:01:20 +00001068 MachineInstr *DefMI = ValNo->isDefAccurate()
Evan Cheng06587492008-10-24 02:05:00 +00001069 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
Evan Cheng06587492008-10-24 02:05:00 +00001070
Owen Andersond3be4622009-01-21 08:18:03 +00001071 // If this would create a new join point, do not split.
Lang Hames67f25662009-10-20 21:25:13 +00001072 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent())) {
1073 DEBUG(errs() << "FAILED (would create a new join point).\n");
Owen Andersond3be4622009-01-21 08:18:03 +00001074 return false;
Lang Hames67f25662009-10-20 21:25:13 +00001075 }
Owen Andersond3be4622009-01-21 08:18:03 +00001076
Evan Chengf5cd4f02008-10-23 20:43:13 +00001077 // Find all references in the barrier mbb.
1078 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
1079 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
1080 E = MRI->reg_end(); I != E; ++I) {
1081 MachineInstr *RefMI = &*I;
1082 if (RefMI->getParent() == BarrierMBB)
1083 RefsInMBB.insert(RefMI);
1084 }
1085
1086 // Find a point to restore the value after the barrier.
Lang Hamescc3b0652009-10-03 04:21:37 +00001087 LiveIndex RestoreIndex;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001088 MachineBasicBlock::iterator RestorePt =
Evan Chengf62ce372008-10-28 00:47:49 +00001089 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex);
Lang Hames67f25662009-10-20 21:25:13 +00001090 if (RestorePt == BarrierMBB->end()) {
1091 DEBUG(errs() << "FAILED (could not find a suitable restore point).\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001092 return false;
Lang Hames67f25662009-10-20 21:25:13 +00001093 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001094
Owen Anderson75fa96b2008-11-19 04:28:29 +00001095 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
1096 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt,
Lang Hames67f25662009-10-20 21:25:13 +00001097 RestoreIndex, RefsInMBB)) {
1098 DEBUG(errs() << "success (remat).\n");
1099 return true;
1100 }
Owen Anderson75fa96b2008-11-19 04:28:29 +00001101
Evan Chengf5cd4f02008-10-23 20:43:13 +00001102 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +00001103 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001104 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Lang Hamescc3b0652009-10-03 04:21:37 +00001105 LiveIndex SpillIndex;
Evan Cheng06587492008-10-24 02:05:00 +00001106 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +00001107 int SS = -1;
Lang Hames857c4e02009-06-17 21:01:20 +00001108 if (!ValNo->isDefAccurate()) {
1109 // If we don't know where the def is we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001110 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
1111 BarrierMBB, SS, RefsInMBB))) {
1112 SpillIndex = LIs->getInstructionIndex(SpillMI);
1113 } else {
1114 MachineBasicBlock::iterator SpillPt =
1115 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex);
Lang Hames67f25662009-10-20 21:25:13 +00001116 if (SpillPt == BarrierMBB->begin()) {
1117 DEBUG(errs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001118 return false; // No gap to insert spill.
Lang Hames67f25662009-10-20 21:25:13 +00001119 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001120 // Add spill.
1121
1122 SS = CreateSpillStackSlot(CurrLI->reg, RC);
1123 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
1124 SpillMI = prior(SpillPt);
1125 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
1126 }
Evan Cheng54898932008-10-29 08:39:34 +00001127 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
1128 RestoreIndex, SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +00001129 // If it's already split, just restore the value. There is no need to spill
1130 // the def again.
Lang Hames67f25662009-10-20 21:25:13 +00001131 if (!DefMI) {
1132 DEBUG(errs() << "FAILED (def is dead).\n");
Evan Chengd0e32c52008-10-29 05:06:14 +00001133 return false; // Def is dead. Do nothing.
Lang Hames67f25662009-10-20 21:25:13 +00001134 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001135
1136 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
Evan Cheng35ca9202009-10-09 01:17:11 +00001137 BarrierMBB, SS, RefsInMBB))) {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001138 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001139 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001140 // Check if it's possible to insert a spill after the def MI.
1141 MachineBasicBlock::iterator SpillPt;
1142 if (DefMBB == BarrierMBB) {
1143 // Add spill after the def and the last use before the barrier.
1144 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
1145 RefsInMBB, SpillIndex);
Lang Hames67f25662009-10-20 21:25:13 +00001146 if (SpillPt == DefMBB->begin()) {
1147 DEBUG(errs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001148 return false; // No gap to insert spill.
Lang Hames67f25662009-10-20 21:25:13 +00001149 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001150 } else {
1151 SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex);
Lang Hames67f25662009-10-20 21:25:13 +00001152 if (SpillPt == DefMBB->end()) {
1153 DEBUG(errs() << "FAILED (could not find a suitable spill point).\n");
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001154 return false; // No gap to insert spill.
Lang Hames67f25662009-10-20 21:25:13 +00001155 }
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001156 }
Evan Cheng35ca9202009-10-09 01:17:11 +00001157 // Add spill.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001158 SS = CreateSpillStackSlot(CurrLI->reg, RC);
Evan Cheng35ca9202009-10-09 01:17:11 +00001159 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg, false, SS, RC);
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001160 SpillMI = prior(SpillPt);
1161 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001162 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001163 }
1164
Evan Cheng54898932008-10-29 08:39:34 +00001165 // Remember def instruction index to spill index mapping.
1166 if (DefMI && SpillMI)
1167 Def2SpillMap[ValNo->def] = SpillIndex;
1168
Evan Chengf5cd4f02008-10-23 20:43:13 +00001169 // Add restore.
Owen Andersonc93023a2009-03-04 08:52:31 +00001170 bool FoldedRestore = false;
1171 if (MachineInstr* LMI = FoldRestore(CurrLI->reg, RC, Barrier,
1172 BarrierMBB, SS, RefsInMBB)) {
1173 RestorePt = LMI;
Owen Anderson323c58d2009-03-05 07:19:18 +00001174 RestoreIndex = LIs->getInstructionIndex(RestorePt);
Owen Andersonc93023a2009-03-04 08:52:31 +00001175 FoldedRestore = true;
1176 } else {
1177 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
1178 MachineInstr *LoadMI = prior(RestorePt);
1179 LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex);
1180 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001181
Evan Chengd0e32c52008-10-29 05:06:14 +00001182 // Update spill stack slot live interval.
Lang Hames67f25662009-10-20 21:25:13 +00001183 UpdateSpillSlotInterval(ValNo, SpillIndex.getUseIndex().getNextSlot(),
1184 RestoreIndex.getDefIndex());
Evan Chengd0e32c52008-10-29 05:06:14 +00001185
Owen Andersonb4b84362009-01-26 21:57:31 +00001186 ReconstructLiveInterval(CurrLI);
Evan Cheng35ca9202009-10-09 01:17:11 +00001187
Owen Andersonc93023a2009-03-04 08:52:31 +00001188 if (!FoldedRestore) {
Lang Hamescc3b0652009-10-03 04:21:37 +00001189 LiveIndex RestoreIdx = LIs->getInstructionIndex(prior(RestorePt));
Lang Hames67f25662009-10-20 21:25:13 +00001190 RestoreIdx = RestoreIdx.getDefIndex();
Lang Hames86511252009-09-04 20:41:11 +00001191 RenumberValno(CurrLI->findDefinedVNInfoForRegInt(RestoreIdx));
Owen Andersonc93023a2009-03-04 08:52:31 +00001192 }
Owen Anderson7d211e22008-12-31 02:00:25 +00001193
Evan Chengae7fa5b2008-10-28 01:48:24 +00001194 ++NumSplits;
Lang Hames67f25662009-10-20 21:25:13 +00001195 DEBUG(errs() << "success.\n");
Evan Chengf5cd4f02008-10-23 20:43:13 +00001196 return true;
1197}
1198
1199/// SplitRegLiveIntervals - Split all register live intervals that cross the
1200/// barrier that's being processed.
1201bool
Owen Anderson956ec272009-01-23 00:23:32 +00001202PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs,
1203 SmallPtrSet<LiveInterval*, 8>& Split) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001204 // First find all the virtual registers whose live intervals are intercepted
1205 // by the current barrier.
1206 SmallVector<LiveInterval*, 8> Intervals;
1207 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng4350eb82009-02-06 17:17:30 +00001208 // FIXME: If it's not safe to move any instruction that defines the barrier
1209 // register class, then it means there are some special dependencies which
1210 // codegen is not modelling. Ignore these barriers for now.
1211 if (!TII->isSafeToMoveRegClassDefs(*RC))
Evan Cheng23066282008-10-27 07:14:50 +00001212 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001213 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
1214 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1215 unsigned Reg = VRs[i];
1216 if (!LIs->hasInterval(Reg))
1217 continue;
1218 LiveInterval *LI = &LIs->getInterval(Reg);
1219 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1220 // Virtual register live interval is intercepted by the barrier. We
1221 // should split and shrink wrap its interval if possible.
1222 Intervals.push_back(LI);
1223 }
1224 }
1225
1226 // Process the affected live intervals.
1227 bool Change = false;
1228 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001229 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1230 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001231 LiveInterval *LI = Intervals.back();
1232 Intervals.pop_back();
Owen Anderson956ec272009-01-23 00:23:32 +00001233 bool result = SplitRegLiveInterval(LI);
1234 if (result) Split.insert(LI);
1235 Change |= result;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001236 }
1237
1238 return Change;
1239}
1240
Owen Anderson45e68552009-01-29 05:28:55 +00001241unsigned PreAllocSplitting::getNumberOfNonSpills(
Owen Anderson32ca8652009-01-24 10:07:43 +00001242 SmallPtrSet<MachineInstr*, 4>& MIs,
Owen Anderson45e68552009-01-29 05:28:55 +00001243 unsigned Reg, int FrameIndex,
1244 bool& FeedsTwoAddr) {
1245 unsigned NonSpills = 0;
Owen Anderson32ca8652009-01-24 10:07:43 +00001246 for (SmallPtrSet<MachineInstr*, 4>::iterator UI = MIs.begin(), UE = MIs.end();
Owen Anderson45e68552009-01-29 05:28:55 +00001247 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001248 int StoreFrameIndex;
1249 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001250 if (StoreVReg != Reg || StoreFrameIndex != FrameIndex)
1251 NonSpills++;
1252
1253 int DefIdx = (*UI)->findRegisterDefOperandIdx(Reg);
Bob Wilsond9df5012009-04-09 17:16:43 +00001254 if (DefIdx != -1 && (*UI)->isRegTiedToUseOperand(DefIdx))
Owen Anderson45e68552009-01-29 05:28:55 +00001255 FeedsTwoAddr = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001256 }
1257
Owen Anderson45e68552009-01-29 05:28:55 +00001258 return NonSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001259}
1260
Owen Anderson956ec272009-01-23 00:23:32 +00001261/// removeDeadSpills - After doing splitting, filter through all intervals we've
1262/// split, and see if any of the spills are unnecessary. If so, remove them.
1263bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) {
1264 bool changed = false;
1265
Owen Anderson4bfc2092009-01-29 05:41:02 +00001266 // Walk over all of the live intervals that were touched by the splitter,
1267 // and see if we can do any DCE and/or folding.
Owen Anderson956ec272009-01-23 00:23:32 +00001268 for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(),
1269 LE = split.end(); LI != LE; ++LI) {
Owen Anderson9ce499a2009-01-23 03:28:53 +00001270 DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> > VNUseCount;
Owen Anderson956ec272009-01-23 00:23:32 +00001271
Owen Anderson4bfc2092009-01-29 05:41:02 +00001272 // First, collect all the uses of the vreg, and sort them by their
1273 // reaching definition (VNInfo).
Owen Anderson956ec272009-01-23 00:23:32 +00001274 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg),
1275 UE = MRI->use_end(); UI != UE; ++UI) {
Lang Hamescc3b0652009-10-03 04:21:37 +00001276 LiveIndex index = LIs->getInstructionIndex(&*UI);
Lang Hames67f25662009-10-20 21:25:13 +00001277 index = index.getUseIndex();
Owen Anderson956ec272009-01-23 00:23:32 +00001278
1279 const LiveRange* LR = (*LI)->getLiveRangeContaining(index);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001280 VNUseCount[LR->valno].insert(&*UI);
Owen Anderson956ec272009-01-23 00:23:32 +00001281 }
1282
Owen Anderson4bfc2092009-01-29 05:41:02 +00001283 // Now, take the definitions (VNInfo's) one at a time and try to DCE
1284 // and/or fold them away.
Owen Anderson956ec272009-01-23 00:23:32 +00001285 for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(),
1286 VE = (*LI)->vni_end(); VI != VE; ++VI) {
Owen Anderson45e68552009-01-29 05:28:55 +00001287
1288 if (DeadSplitLimit != -1 && (int)NumDeadSpills == DeadSplitLimit)
1289 return changed;
1290
Owen Anderson956ec272009-01-23 00:23:32 +00001291 VNInfo* CurrVN = *VI;
Owen Anderson4bfc2092009-01-29 05:41:02 +00001292
1293 // We don't currently try to handle definitions with PHI kills, because
1294 // it would involve processing more than one VNInfo at once.
Lang Hames857c4e02009-06-17 21:01:20 +00001295 if (CurrVN->hasPHIKill()) continue;
Owen Anderson956ec272009-01-23 00:23:32 +00001296
Owen Anderson4bfc2092009-01-29 05:41:02 +00001297 // We also don't try to handle the results of PHI joins, since there's
1298 // no defining instruction to analyze.
Lang Hames857c4e02009-06-17 21:01:20 +00001299 if (!CurrVN->isDefAccurate() || CurrVN->isUnused()) continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001300
Owen Anderson4bfc2092009-01-29 05:41:02 +00001301 // We're only interested in eliminating cruft introduced by the splitter,
1302 // is of the form load-use or load-use-store. First, check that the
1303 // definition is a load, and remember what stack slot we loaded it from.
Lang Hames857c4e02009-06-17 21:01:20 +00001304 MachineInstr* DefMI = LIs->getInstructionFromIndex(CurrVN->def);
Owen Anderson956ec272009-01-23 00:23:32 +00001305 int FrameIndex;
1306 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
1307
Owen Anderson4bfc2092009-01-29 05:41:02 +00001308 // If the definition has no uses at all, just DCE it.
Owen Anderson9ce499a2009-01-23 03:28:53 +00001309 if (VNUseCount[CurrVN].size() == 0) {
1310 LIs->RemoveMachineInstrFromMaps(DefMI);
1311 (*LI)->removeValNo(CurrVN);
1312 DefMI->eraseFromParent();
Owen Andersonc0f3a032009-01-29 08:22:06 +00001313 VNUseCount.erase(CurrVN);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001314 NumDeadSpills++;
1315 changed = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001316 continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001317 }
Owen Anderson32ca8652009-01-24 10:07:43 +00001318
Owen Anderson4bfc2092009-01-29 05:41:02 +00001319 // Second, get the number of non-store uses of the definition, as well as
1320 // a flag indicating whether it feeds into a later two-address definition.
Owen Anderson45e68552009-01-29 05:28:55 +00001321 bool FeedsTwoAddr = false;
1322 unsigned NonSpillCount = getNumberOfNonSpills(VNUseCount[CurrVN],
1323 (*LI)->reg, FrameIndex,
1324 FeedsTwoAddr);
1325
Owen Anderson4bfc2092009-01-29 05:41:02 +00001326 // If there's one non-store use and it doesn't feed a two-addr, then
1327 // this is a load-use-store case that we can try to fold.
Owen Anderson45e68552009-01-29 05:28:55 +00001328 if (NonSpillCount == 1 && !FeedsTwoAddr) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001329 // Start by finding the non-store use MachineInstr.
Owen Anderson45e68552009-01-29 05:28:55 +00001330 SmallPtrSet<MachineInstr*, 4>::iterator UI = VNUseCount[CurrVN].begin();
1331 int StoreFrameIndex;
1332 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1333 while (UI != VNUseCount[CurrVN].end() &&
1334 (StoreVReg == (*LI)->reg && StoreFrameIndex == FrameIndex)) {
1335 ++UI;
1336 if (UI != VNUseCount[CurrVN].end())
1337 StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1338 }
Owen Anderson45e68552009-01-29 05:28:55 +00001339 if (UI == VNUseCount[CurrVN].end()) continue;
1340
1341 MachineInstr* use = *UI;
1342
Owen Anderson4bfc2092009-01-29 05:41:02 +00001343 // Attempt to fold it away!
Owen Anderson45e68552009-01-29 05:28:55 +00001344 int OpIdx = use->findRegisterUseOperandIdx((*LI)->reg, false);
1345 if (OpIdx == -1) continue;
Owen Anderson45e68552009-01-29 05:28:55 +00001346 SmallVector<unsigned, 1> Ops;
1347 Ops.push_back(OpIdx);
Owen Anderson45e68552009-01-29 05:28:55 +00001348 if (!TII->canFoldMemoryOperand(use, Ops)) continue;
1349
1350 MachineInstr* NewMI =
1351 TII->foldMemoryOperand(*use->getParent()->getParent(),
1352 use, Ops, FrameIndex);
1353
1354 if (!NewMI) continue;
1355
Owen Anderson4bfc2092009-01-29 05:41:02 +00001356 // Update relevant analyses.
Owen Anderson45e68552009-01-29 05:28:55 +00001357 LIs->RemoveMachineInstrFromMaps(DefMI);
1358 LIs->ReplaceMachineInstrInMaps(use, NewMI);
1359 (*LI)->removeValNo(CurrVN);
1360
1361 DefMI->eraseFromParent();
1362 MachineBasicBlock* MBB = use->getParent();
1363 NewMI = MBB->insert(MBB->erase(use), NewMI);
1364 VNUseCount[CurrVN].erase(use);
1365
Owen Anderson4bfc2092009-01-29 05:41:02 +00001366 // Remove deleted instructions. Note that we need to remove them from
1367 // the VNInfo->use map as well, just to be safe.
Owen Anderson45e68552009-01-29 05:28:55 +00001368 for (SmallPtrSet<MachineInstr*, 4>::iterator II =
1369 VNUseCount[CurrVN].begin(), IE = VNUseCount[CurrVN].end();
1370 II != IE; ++II) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001371 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
Owen Andersonc0f3a032009-01-29 08:22:06 +00001372 VNI = VNUseCount.begin(), VNE = VNUseCount.end(); VNI != VNE;
1373 ++VNI)
1374 if (VNI->first != CurrVN)
1375 VNI->second.erase(*II);
Owen Anderson45e68552009-01-29 05:28:55 +00001376 LIs->RemoveMachineInstrFromMaps(*II);
1377 (*II)->eraseFromParent();
1378 }
Owen Andersonc0f3a032009-01-29 08:22:06 +00001379
1380 VNUseCount.erase(CurrVN);
Owen Anderson45e68552009-01-29 05:28:55 +00001381
1382 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
1383 VI = VNUseCount.begin(), VE = VNUseCount.end(); VI != VE; ++VI)
1384 if (VI->second.erase(use))
1385 VI->second.insert(NewMI);
1386
1387 NumDeadSpills++;
1388 changed = true;
1389 continue;
1390 }
1391
Owen Anderson4bfc2092009-01-29 05:41:02 +00001392 // If there's more than one non-store instruction, we can't profitably
1393 // fold it, so bail.
Owen Anderson45e68552009-01-29 05:28:55 +00001394 if (NonSpillCount) continue;
Owen Anderson32ca8652009-01-24 10:07:43 +00001395
Owen Anderson4bfc2092009-01-29 05:41:02 +00001396 // Otherwise, this is a load-store case, so DCE them.
Owen Anderson32ca8652009-01-24 10:07:43 +00001397 for (SmallPtrSet<MachineInstr*, 4>::iterator UI =
1398 VNUseCount[CurrVN].begin(), UE = VNUseCount[CurrVN].end();
1399 UI != UI; ++UI) {
1400 LIs->RemoveMachineInstrFromMaps(*UI);
1401 (*UI)->eraseFromParent();
1402 }
1403
Owen Andersonc0f3a032009-01-29 08:22:06 +00001404 VNUseCount.erase(CurrVN);
1405
Owen Anderson32ca8652009-01-24 10:07:43 +00001406 LIs->RemoveMachineInstrFromMaps(DefMI);
1407 (*LI)->removeValNo(CurrVN);
1408 DefMI->eraseFromParent();
1409 NumDeadSpills++;
1410 changed = true;
Owen Anderson956ec272009-01-23 00:23:32 +00001411 }
1412 }
1413
1414 return changed;
1415}
1416
Owen Andersonf1f75b12008-11-04 22:22:41 +00001417bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1418 MachineBasicBlock* DefMBB,
1419 MachineBasicBlock* BarrierMBB) {
1420 if (DefMBB == BarrierMBB)
1421 return false;
1422
Lang Hames857c4e02009-06-17 21:01:20 +00001423 if (LR->valno->hasPHIKill())
Owen Andersonf1f75b12008-11-04 22:22:41 +00001424 return false;
1425
Lang Hamescc3b0652009-10-03 04:21:37 +00001426 LiveIndex MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
Owen Andersonf1f75b12008-11-04 22:22:41 +00001427 if (LR->end < MBBEnd)
1428 return false;
1429
1430 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1431 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1432 return true;
1433
1434 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1435 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1436 typedef std::pair<MachineBasicBlock*,
1437 MachineBasicBlock::succ_iterator> ItPair;
1438 SmallVector<ItPair, 4> Stack;
1439 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1440
1441 while (!Stack.empty()) {
1442 ItPair P = Stack.back();
1443 Stack.pop_back();
1444
1445 MachineBasicBlock* PredMBB = P.first;
1446 MachineBasicBlock::succ_iterator S = P.second;
1447
1448 if (S == PredMBB->succ_end())
1449 continue;
1450 else if (Visited.count(*S)) {
1451 Stack.push_back(std::make_pair(PredMBB, ++S));
1452 continue;
1453 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001454 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001455
1456 MachineBasicBlock* MBB = *S;
1457 Visited.insert(MBB);
1458
1459 if (MBB == BarrierMBB)
1460 return true;
1461
1462 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1463 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1464 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1465 while (MDTN) {
1466 if (MDTN == DefMDTN)
1467 return true;
1468 else if (MDTN == BarrierMDTN)
1469 break;
1470 MDTN = MDTN->getIDom();
1471 }
1472
1473 MBBEnd = LIs->getMBBEndIdx(MBB);
1474 if (LR->end > MBBEnd)
1475 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1476 }
1477
1478 return false;
1479}
1480
1481
Evan Cheng09e8ca82008-10-20 21:44:59 +00001482bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001483 CurrMF = &MF;
1484 TM = &MF.getTarget();
Owen Anderson3ef45492009-01-29 22:13:06 +00001485 TRI = TM->getRegisterInfo();
Evan Chengd0e32c52008-10-29 05:06:14 +00001486 TII = TM->getInstrInfo();
1487 MFI = MF.getFrameInfo();
1488 MRI = &MF.getRegInfo();
1489 LIs = &getAnalysis<LiveIntervals>();
1490 LSs = &getAnalysis<LiveStacks>();
Owen Anderson420dd372009-03-14 21:40:05 +00001491 VRM = &getAnalysis<VirtRegMap>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001492
1493 bool MadeChange = false;
1494
1495 // Make sure blocks are numbered in order.
1496 MF.RenumberBlocks();
1497
Evan Cheng54898932008-10-29 08:39:34 +00001498 MachineBasicBlock *Entry = MF.begin();
1499 SmallPtrSet<MachineBasicBlock*,16> Visited;
1500
Owen Anderson956ec272009-01-23 00:23:32 +00001501 SmallPtrSet<LiveInterval*, 8> Split;
1502
Evan Cheng54898932008-10-29 08:39:34 +00001503 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1504 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1505 DFI != E; ++DFI) {
1506 BarrierMBB = *DFI;
1507 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1508 E = BarrierMBB->end(); I != E; ++I) {
1509 Barrier = &*I;
1510 const TargetRegisterClass **BarrierRCs =
1511 Barrier->getDesc().getRegClassBarriers();
1512 if (!BarrierRCs)
1513 continue;
1514 BarrierIdx = LIs->getInstructionIndex(Barrier);
Owen Anderson956ec272009-01-23 00:23:32 +00001515 MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split);
Evan Cheng54898932008-10-29 08:39:34 +00001516 }
1517 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001518
Owen Anderson956ec272009-01-23 00:23:32 +00001519 MadeChange |= removeDeadSpills(Split);
1520
Evan Chengf5cd4f02008-10-23 20:43:13 +00001521 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001522}