blob: a538db8be086ed0bc9bda8f119e5def9c2b8d5fa [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"
18#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000019#include "llvm/CodeGen/LiveStackAnalysis.h"
Owen Andersonf1f75b12008-11-04 22:22:41 +000020#include "llvm/CodeGen/MachineDominators.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineLoopInfo.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/CodeGen/Passes.h"
26#include "llvm/CodeGen/RegisterCoalescer.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000027#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000028#include "llvm/Target/TargetMachine.h"
29#include "llvm/Target/TargetOptions.h"
30#include "llvm/Target/TargetRegisterInfo.h"
31#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
Evan Chengd0e32c52008-10-29 05:06:14 +000033#include "llvm/ADT/DenseMap.h"
Evan Cheng54898932008-10-29 08:39:34 +000034#include "llvm/ADT/DepthFirstIterator.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000035#include "llvm/ADT/SmallPtrSet.h"
Evan Chengf5cd4f02008-10-23 20:43:13 +000036#include "llvm/ADT/Statistic.h"
Evan Cheng09e8ca82008-10-20 21:44:59 +000037using namespace llvm;
38
Evan Chengae7fa5b2008-10-28 01:48:24 +000039static cl::opt<int> PreSplitLimit("pre-split-limit", cl::init(-1), cl::Hidden);
Owen Anderson45e68552009-01-29 05:28:55 +000040static cl::opt<int> DeadSplitLimit("dead-split-limit", cl::init(-1), cl::Hidden);
Evan Chengae7fa5b2008-10-28 01:48:24 +000041
Owen Anderson45e68552009-01-29 05:28:55 +000042STATISTIC(NumSplits, "Number of intervals split");
Owen Anderson75fa96b2008-11-19 04:28:29 +000043STATISTIC(NumRemats, "Number of intervals split by rematerialization");
Owen Anderson7b9d67c2008-12-02 18:53:47 +000044STATISTIC(NumFolds, "Number of intervals split with spill folding");
Owen Anderson2ebf63f2008-12-18 01:27:19 +000045STATISTIC(NumRenumbers, "Number of intervals renumbered into new registers");
Owen Anderson956ec272009-01-23 00:23:32 +000046STATISTIC(NumDeadSpills, "Number of dead spills removed");
Evan Chengf5cd4f02008-10-23 20:43:13 +000047
Evan Cheng09e8ca82008-10-20 21:44:59 +000048namespace {
49 class VISIBILITY_HIDDEN PreAllocSplitting : public MachineFunctionPass {
Evan Chengd0e32c52008-10-29 05:06:14 +000050 MachineFunction *CurrMF;
Evan Chengf5cd4f02008-10-23 20:43:13 +000051 const TargetMachine *TM;
52 const TargetInstrInfo *TII;
Owen Anderson3ef45492009-01-29 22:13:06 +000053 const TargetRegisterInfo* TRI;
Evan Chengf5cd4f02008-10-23 20:43:13 +000054 MachineFrameInfo *MFI;
55 MachineRegisterInfo *MRI;
56 LiveIntervals *LIs;
Evan Chengd0e32c52008-10-29 05:06:14 +000057 LiveStacks *LSs;
Evan Cheng09e8ca82008-10-20 21:44:59 +000058
Evan Chengf5cd4f02008-10-23 20:43:13 +000059 // Barrier - Current barrier being processed.
60 MachineInstr *Barrier;
61
62 // BarrierMBB - Basic block where the barrier resides in.
63 MachineBasicBlock *BarrierMBB;
64
65 // Barrier - Current barrier index.
66 unsigned BarrierIdx;
67
68 // CurrLI - Current live interval being split.
69 LiveInterval *CurrLI;
70
Evan Chengd0e32c52008-10-29 05:06:14 +000071 // CurrSLI - Current stack slot live interval.
72 LiveInterval *CurrSLI;
73
74 // CurrSValNo - Current val# for the stack slot live interval.
75 VNInfo *CurrSValNo;
76
77 // IntervalSSMap - A map from live interval to spill slots.
78 DenseMap<unsigned, int> IntervalSSMap;
Evan Chengf5cd4f02008-10-23 20:43:13 +000079
Evan Cheng54898932008-10-29 08:39:34 +000080 // Def2SpillMap - A map from a def instruction index to spill index.
81 DenseMap<unsigned, unsigned> Def2SpillMap;
Evan Cheng06587492008-10-24 02:05:00 +000082
Evan Cheng09e8ca82008-10-20 21:44:59 +000083 public:
84 static char ID;
85 PreAllocSplitting() : MachineFunctionPass(&ID) {}
86
87 virtual bool runOnMachineFunction(MachineFunction &MF);
88
89 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
90 AU.addRequired<LiveIntervals>();
91 AU.addPreserved<LiveIntervals>();
Evan Chengd0e32c52008-10-29 05:06:14 +000092 AU.addRequired<LiveStacks>();
93 AU.addPreserved<LiveStacks>();
Evan Cheng09e8ca82008-10-20 21:44:59 +000094 AU.addPreserved<RegisterCoalescer>();
95 if (StrongPHIElim)
96 AU.addPreservedID(StrongPHIEliminationID);
97 else
98 AU.addPreservedID(PHIEliminationID);
Owen Andersonf1f75b12008-11-04 22:22:41 +000099 AU.addRequired<MachineDominatorTree>();
100 AU.addRequired<MachineLoopInfo>();
101 AU.addPreserved<MachineDominatorTree>();
102 AU.addPreserved<MachineLoopInfo>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000103 MachineFunctionPass::getAnalysisUsage(AU);
104 }
105
106 virtual void releaseMemory() {
Evan Chengd0e32c52008-10-29 05:06:14 +0000107 IntervalSSMap.clear();
Evan Cheng54898932008-10-29 08:39:34 +0000108 Def2SpillMap.clear();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000109 }
110
111 virtual const char *getPassName() const {
112 return "Pre-Register Allocaton Live Interval Splitting";
113 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000114
115 /// print - Implement the dump method.
116 virtual void print(std::ostream &O, const Module* M = 0) const {
117 LIs->print(O, M);
118 }
119
120 void print(std::ostream *O, const Module* M = 0) const {
121 if (O) print(*O, M);
122 }
123
124 private:
125 MachineBasicBlock::iterator
126 findNextEmptySlot(MachineBasicBlock*, MachineInstr*,
127 unsigned&);
128
129 MachineBasicBlock::iterator
Evan Cheng1f08cc22008-10-28 05:28:21 +0000130 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000131 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
132
133 MachineBasicBlock::iterator
Evan Chengf62ce372008-10-28 00:47:49 +0000134 findRestorePoint(MachineBasicBlock*, MachineInstr*, unsigned,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000135 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
136
Evan Chengd0e32c52008-10-29 05:06:14 +0000137 int CreateSpillStackSlot(unsigned, const TargetRegisterClass *);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000138
Evan Cheng54898932008-10-29 08:39:34 +0000139 bool IsAvailableInStack(MachineBasicBlock*, unsigned, unsigned, unsigned,
140 unsigned&, int&) const;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000141
Evan Chengd0e32c52008-10-29 05:06:14 +0000142 void UpdateSpillSlotInterval(VNInfo*, unsigned, unsigned);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000143
Evan Chengf5cd4f02008-10-23 20:43:13 +0000144 bool SplitRegLiveInterval(LiveInterval*);
145
Owen Anderson956ec272009-01-23 00:23:32 +0000146 bool SplitRegLiveIntervals(const TargetRegisterClass **,
147 SmallPtrSet<LiveInterval*, 8>&);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000148
149 bool createsNewJoin(LiveRange* LR, MachineBasicBlock* DefMBB,
150 MachineBasicBlock* BarrierMBB);
Owen Anderson75fa96b2008-11-19 04:28:29 +0000151 bool Rematerialize(unsigned vreg, VNInfo* ValNo,
152 MachineInstr* DefMI,
153 MachineBasicBlock::iterator RestorePt,
154 unsigned RestoreIdx,
155 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000156 MachineInstr* FoldSpill(unsigned vreg, const TargetRegisterClass* RC,
157 MachineInstr* DefMI,
158 MachineInstr* Barrier,
159 MachineBasicBlock* MBB,
160 int& SS,
161 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000162 void RenumberValno(VNInfo* VN);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000163 void ReconstructLiveInterval(LiveInterval* LI);
Owen Anderson956ec272009-01-23 00:23:32 +0000164 bool removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split);
Owen Anderson45e68552009-01-29 05:28:55 +0000165 unsigned getNumberOfNonSpills(SmallPtrSet<MachineInstr*, 4>& MIs,
166 unsigned Reg, int FrameIndex, bool& TwoAddr);
Evan Cheng19a72582009-02-02 18:33:18 +0000167 VNInfo* PerformPHIConstruction(MachineBasicBlock::iterator Use,
168 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000169 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000170 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
171 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
172 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000173 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
174 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000175 bool IsTopLevel, bool IsIntraBlock);
176 VNInfo* PerformPHIConstructionFallBack(MachineBasicBlock::iterator Use,
177 MachineBasicBlock* MBB, LiveInterval* LI,
178 SmallPtrSet<MachineInstr*, 4>& Visited,
179 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
180 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
181 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
182 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
183 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
184 bool IsTopLevel, bool IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000185};
Evan Cheng09e8ca82008-10-20 21:44:59 +0000186} // end anonymous namespace
187
188char PreAllocSplitting::ID = 0;
189
190static RegisterPass<PreAllocSplitting>
191X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting");
192
193const PassInfo *const llvm::PreAllocSplittingID = &X;
194
Evan Chengf5cd4f02008-10-23 20:43:13 +0000195
196/// findNextEmptySlot - Find a gap after the given machine instruction in the
197/// instruction index map. If there isn't one, return end().
198MachineBasicBlock::iterator
199PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI,
200 unsigned &SpotIndex) {
201 MachineBasicBlock::iterator MII = MI;
202 if (++MII != MBB->end()) {
203 unsigned Index = LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII));
204 if (Index) {
205 SpotIndex = Index;
206 return MII;
207 }
208 }
209 return MBB->end();
210}
211
212/// findSpillPoint - Find a gap as far away from the given MI that's suitable
213/// for spilling the current live interval. The index must be before any
214/// defs and uses of the live interval register in the mbb. Return begin() if
215/// none is found.
216MachineBasicBlock::iterator
217PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000218 MachineInstr *DefMI,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000219 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
220 unsigned &SpillIndex) {
221 MachineBasicBlock::iterator Pt = MBB->begin();
222
223 // Go top down if RefsInMBB is empty.
Evan Cheng1f08cc22008-10-28 05:28:21 +0000224 if (RefsInMBB.empty() && !DefMI) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000225 MachineBasicBlock::iterator MII = MBB->begin();
226 MachineBasicBlock::iterator EndPt = MI;
227 do {
228 ++MII;
229 unsigned Index = LIs->getInstructionIndex(MII);
230 unsigned Gap = LIs->findGapBeforeInstr(Index);
231 if (Gap) {
232 Pt = MII;
233 SpillIndex = Gap;
234 break;
Owen Anderson3ef45492009-01-29 22:13:06 +0000235
236 // We can't insert the spill between the barrier (a call), and its
237 // corresponding call frame setup.
238 } else if (prior(MII)->getOpcode() == TRI->getCallFrameSetupOpcode() &&
239 MII == MachineBasicBlock::iterator(MI))
240 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000241 } while (MII != EndPt);
242 } else {
243 MachineBasicBlock::iterator MII = MI;
Evan Cheng1f08cc22008-10-28 05:28:21 +0000244 MachineBasicBlock::iterator EndPt = DefMI
245 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
Owen Anderson3ef45492009-01-29 22:13:06 +0000246
247 // We can't insert the spill between the barrier (a call), and its
248 // corresponding call frame setup.
249 if (prior(MII)->getOpcode() == TRI->getCallFrameSetupOpcode()) --MII;
Evan Cheng1f08cc22008-10-28 05:28:21 +0000250 while (MII != EndPt && !RefsInMBB.count(MII)) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000251 unsigned Index = LIs->getInstructionIndex(MII);
252 if (LIs->hasGapBeforeInstr(Index)) {
253 Pt = MII;
254 SpillIndex = LIs->findGapBeforeInstr(Index, true);
255 }
256 --MII;
257 }
258 }
259
260 return Pt;
261}
262
263/// findRestorePoint - Find a gap in the instruction index map that's suitable
264/// for restoring the current live interval value. The index must be before any
265/// uses of the live interval register in the mbb. Return end() if none is
266/// found.
267MachineBasicBlock::iterator
268PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Chengf62ce372008-10-28 00:47:49 +0000269 unsigned LastIdx,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000270 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
271 unsigned &RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000272 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
273 // begin index accordingly.
Owen Anderson5a92d4e2008-11-18 20:53:59 +0000274 MachineBasicBlock::iterator Pt = MBB->end();
Evan Chengf62ce372008-10-28 00:47:49 +0000275 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000276
Evan Chengf62ce372008-10-28 00:47:49 +0000277 // Go bottom up if RefsInMBB is empty and the end of the mbb isn't beyond
278 // the last index in the live range.
279 if (RefsInMBB.empty() && LastIdx >= EndIdx) {
Owen Anderson711fd3d2008-11-13 21:53:14 +0000280 MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000281 MachineBasicBlock::iterator EndPt = MI;
Evan Cheng54898932008-10-29 08:39:34 +0000282 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000283 do {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000284 unsigned Index = LIs->getInstructionIndex(MII);
Evan Cheng56ab0de2008-10-24 18:46:44 +0000285 unsigned Gap = LIs->findGapBeforeInstr(Index);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000286 if (Gap) {
287 Pt = MII;
288 RestoreIndex = Gap;
289 break;
Owen Anderson3ef45492009-01-29 22:13:06 +0000290
291 // We can't insert a restore between the barrier (a call) and its
292 // corresponding call frame teardown.
293 } else if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode() &&
294 prior(MII) == MachineBasicBlock::iterator(MI))
295 break;
Evan Cheng54898932008-10-29 08:39:34 +0000296 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000297 } while (MII != EndPt);
298 } else {
299 MachineBasicBlock::iterator MII = MI;
300 MII = ++MII;
Owen Anderson3ef45492009-01-29 22:13:06 +0000301 // We can't insert a restore between the barrier (a call) and its
302 // corresponding call frame teardown.
303 if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode())
304 ++MII;
305
Evan Chengf62ce372008-10-28 00:47:49 +0000306 // FIXME: Limit the number of instructions to examine to reduce
307 // compile time?
Owen Andersonc0f3a032009-01-29 08:22:06 +0000308 while (MII != MBB->getFirstTerminator()) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000309 unsigned Index = LIs->getInstructionIndex(MII);
Evan Chengf62ce372008-10-28 00:47:49 +0000310 if (Index > LastIdx)
311 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000312 unsigned Gap = LIs->findGapBeforeInstr(Index);
313 if (Gap) {
314 Pt = MII;
315 RestoreIndex = Gap;
316 }
317 if (RefsInMBB.count(MII))
318 break;
319 ++MII;
320 }
321 }
322
323 return Pt;
324}
325
Evan Chengd0e32c52008-10-29 05:06:14 +0000326/// CreateSpillStackSlot - Create a stack slot for the live interval being
327/// split. If the live interval was previously split, just reuse the same
328/// slot.
329int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
330 const TargetRegisterClass *RC) {
331 int SS;
332 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
333 if (I != IntervalSSMap.end()) {
334 SS = I->second;
335 } else {
336 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
337 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000338 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000339
340 // Create live interval for stack slot.
341 CurrSLI = &LSs->getOrCreateInterval(SS);
Evan Cheng54898932008-10-29 08:39:34 +0000342 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000343 CurrSValNo = CurrSLI->getValNumInfo(0);
344 else
345 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
346 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000347}
348
Evan Chengd0e32c52008-10-29 05:06:14 +0000349/// IsAvailableInStack - Return true if register is available in a split stack
350/// slot at the specified index.
351bool
Evan Cheng54898932008-10-29 08:39:34 +0000352PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
353 unsigned Reg, unsigned DefIndex,
354 unsigned RestoreIndex, unsigned &SpillIndex,
355 int& SS) const {
356 if (!DefMBB)
357 return false;
358
Evan Chengd0e32c52008-10-29 05:06:14 +0000359 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
360 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000361 return false;
Evan Cheng54898932008-10-29 08:39:34 +0000362 DenseMap<unsigned, unsigned>::iterator II = Def2SpillMap.find(DefIndex);
363 if (II == Def2SpillMap.end())
364 return false;
365
366 // If last spill of def is in the same mbb as barrier mbb (where restore will
367 // be), make sure it's not below the intended restore index.
368 // FIXME: Undo the previous spill?
369 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
370 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
371 return false;
372
373 SS = I->second;
374 SpillIndex = II->second;
375 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000376}
377
Evan Chengd0e32c52008-10-29 05:06:14 +0000378/// UpdateSpillSlotInterval - Given the specified val# of the register live
379/// interval being split, and the spill and restore indicies, update the live
380/// interval of the spill stack slot.
381void
382PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, unsigned SpillIndex,
383 unsigned RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000384 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
385 "Expect restore in the barrier mbb");
386
387 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
388 if (MBB == BarrierMBB) {
389 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000390 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
391 CurrSLI->addRange(SLR);
392 return;
393 }
394
Evan Cheng54898932008-10-29 08:39:34 +0000395 SmallPtrSet<MachineBasicBlock*, 4> Processed;
396 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
397 LiveRange SLR(SpillIndex, EndIdx+1, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000398 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000399 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000400
401 // Start from the spill mbb, figure out the extend of the spill slot's
402 // live interval.
403 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000404 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
405 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000406 // If live range extend beyond end of mbb, add successors to work list.
407 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
408 SE = MBB->succ_end(); SI != SE; ++SI)
409 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000410
411 while (!WorkList.empty()) {
412 MachineBasicBlock *MBB = WorkList.back();
413 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000414 if (Processed.count(MBB))
415 continue;
Evan Chengd0e32c52008-10-29 05:06:14 +0000416 unsigned Idx = LIs->getMBBStartIdx(MBB);
417 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000418 if (LR && LR->valno == ValNo) {
419 EndIdx = LIs->getMBBEndIdx(MBB);
420 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000421 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000422 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000423 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000424 } else if (LR->end > EndIdx) {
425 // Live range extends beyond end of mbb, process successors.
426 LiveRange SLR(Idx, EndIdx+1, CurrSValNo);
427 CurrSLI->addRange(SLR);
428 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
429 SE = MBB->succ_end(); SI != SE; ++SI)
430 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000431 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000432 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000433 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000434 }
Evan Cheng54898932008-10-29 08:39:34 +0000435 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000436 }
437 }
438}
439
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000440/// PerformPHIConstruction - From properly set up use and def lists, use a PHI
441/// construction algorithm to compute the ranges and valnos for an interval.
Evan Cheng19a72582009-02-02 18:33:18 +0000442VNInfo*
443PreAllocSplitting::PerformPHIConstruction(MachineBasicBlock::iterator UseI,
444 MachineBasicBlock* MBB, LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000445 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000446 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
447 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
448 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000449 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
450 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
Evan Cheng19a72582009-02-02 18:33:18 +0000451 bool IsTopLevel, bool IsIntraBlock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000452 // Return memoized result if it's available.
Evan Cheng19a72582009-02-02 18:33:18 +0000453 if (IsTopLevel && Visited.count(UseI) && NewVNs.count(UseI))
454 return NewVNs[UseI];
455 else if (!IsTopLevel && IsIntraBlock && NewVNs.count(UseI))
456 return NewVNs[UseI];
457 else if (!IsIntraBlock && LiveOut.count(MBB))
Owen Anderson7d211e22008-12-31 02:00:25 +0000458 return LiveOut[MBB];
459
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000460 // Check if our block contains any uses or defs.
Owen Anderson7d211e22008-12-31 02:00:25 +0000461 bool ContainsDefs = Defs.count(MBB);
462 bool ContainsUses = Uses.count(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000463
Evan Cheng19a72582009-02-02 18:33:18 +0000464 VNInfo* RetVNI = 0;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000465
466 // Enumerate the cases of use/def contaning blocks.
467 if (!ContainsDefs && !ContainsUses) {
Evan Cheng19a72582009-02-02 18:33:18 +0000468 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
469 NewVNs, LiveOut, Phis,
470 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000471 } else if (ContainsDefs && !ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000472 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000473
474 // Search for the def in this block. If we don't find it before the
475 // instruction we care about, go to the fallback case. Note that that
Owen Anderson7d211e22008-12-31 02:00:25 +0000476 // should never happen: this cannot be intrablock, so use should
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000477 // always be an end() iterator.
Evan Cheng19a72582009-02-02 18:33:18 +0000478 assert(UseI == MBB->end() && "No use marked in intrablock");
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000479
Evan Cheng19a72582009-02-02 18:33:18 +0000480 MachineBasicBlock::iterator Walker = UseI;
481 --Walker;
482 while (Walker != MBB->begin()) {
483 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000484 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000485 --Walker;
486 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000487
488 // Once we've found it, extend its VNInfo to our instruction.
Evan Cheng19a72582009-02-02 18:33:18 +0000489 unsigned DefIndex = LIs->getInstructionIndex(Walker);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000490 DefIndex = LiveIntervals::getDefIndex(DefIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +0000491 unsigned EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000492
Evan Cheng19a72582009-02-02 18:33:18 +0000493 RetVNI = NewVNs[Walker];
494 LI->addRange(LiveRange(DefIndex, EndIndex+1, RetVNI));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000495 } else if (!ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000496 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000497
498 // Search for the use in this block that precedes the instruction we care
Evan Cheng19a72582009-02-02 18:33:18 +0000499 // about, going to the fallback case if we don't find it.
500 if (UseI == MBB->begin())
501 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
502 Uses, NewVNs, LiveOut, Phis,
503 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000504
Evan Cheng19a72582009-02-02 18:33:18 +0000505 MachineBasicBlock::iterator Walker = UseI;
506 --Walker;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000507 bool found = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000508 while (Walker != MBB->begin()) {
509 if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000510 found = true;
511 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000512 }
513 --Walker;
514 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000515
516 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000517 if (!found) {
Evan Cheng19a72582009-02-02 18:33:18 +0000518 if (BlockUses.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000519 found = true;
520 else
Evan Cheng19a72582009-02-02 18:33:18 +0000521 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
522 Uses, NewVNs, LiveOut, Phis,
523 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000524 }
525
Evan Cheng19a72582009-02-02 18:33:18 +0000526 unsigned UseIndex = LIs->getInstructionIndex(Walker);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000527 UseIndex = LiveIntervals::getUseIndex(UseIndex);
528 unsigned EndIndex = 0;
Evan Cheng19a72582009-02-02 18:33:18 +0000529 if (IsIntraBlock) {
530 EndIndex = LIs->getInstructionIndex(UseI);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000531 EndIndex = LiveIntervals::getUseIndex(EndIndex);
532 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000533 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000534
535 // Now, recursively phi construct the VNInfo for the use we found,
536 // and then extend it to include the instruction we care about
Evan Cheng19a72582009-02-02 18:33:18 +0000537 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
538 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000539
Evan Cheng19a72582009-02-02 18:33:18 +0000540 LI->addRange(LiveRange(UseIndex, EndIndex+1, RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000541
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000542 // FIXME: Need to set kills properly for inter-block stuff.
Evan Cheng19a72582009-02-02 18:33:18 +0000543 if (LI->isKill(RetVNI, UseIndex)) LI->removeKill(RetVNI, UseIndex);
544 if (IsIntraBlock)
545 LI->addKill(RetVNI, EndIndex);
546 } else if (ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000547 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
548 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000549
550 // This case is basically a merging of the two preceding case, with the
551 // special note that checking for defs must take precedence over checking
552 // for uses, because of two-address instructions.
553
Evan Cheng19a72582009-02-02 18:33:18 +0000554 if (UseI == MBB->begin())
555 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs, Uses,
556 NewVNs, LiveOut, Phis,
557 IsTopLevel, IsIntraBlock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000558
Evan Cheng19a72582009-02-02 18:33:18 +0000559 MachineBasicBlock::iterator Walker = UseI;
560 --Walker;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000561 bool foundDef = false;
562 bool foundUse = false;
Evan Cheng19a72582009-02-02 18:33:18 +0000563 while (Walker != MBB->begin()) {
564 if (BlockDefs.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000565 foundDef = true;
566 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000567 } else if (BlockUses.count(Walker)) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000568 foundUse = true;
569 break;
Evan Cheng19a72582009-02-02 18:33:18 +0000570 }
571 --Walker;
572 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000573
574 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000575 if (!foundDef && !foundUse) {
Evan Cheng19a72582009-02-02 18:33:18 +0000576 if (BlockDefs.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000577 foundDef = true;
Evan Cheng19a72582009-02-02 18:33:18 +0000578 else if (BlockUses.count(Walker))
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000579 foundUse = true;
580 else
Evan Cheng19a72582009-02-02 18:33:18 +0000581 return PerformPHIConstructionFallBack(UseI, MBB, LI, Visited, Defs,
582 Uses, NewVNs, LiveOut, Phis,
583 IsTopLevel, IsIntraBlock);
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000584 }
585
Evan Cheng19a72582009-02-02 18:33:18 +0000586 unsigned StartIndex = LIs->getInstructionIndex(Walker);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000587 StartIndex = foundDef ? LiveIntervals::getDefIndex(StartIndex) :
588 LiveIntervals::getUseIndex(StartIndex);
589 unsigned EndIndex = 0;
Evan Cheng19a72582009-02-02 18:33:18 +0000590 if (IsIntraBlock) {
591 EndIndex = LIs->getInstructionIndex(UseI);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000592 EndIndex = LiveIntervals::getUseIndex(EndIndex);
593 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000594 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000595
596 if (foundDef)
Evan Cheng19a72582009-02-02 18:33:18 +0000597 RetVNI = NewVNs[Walker];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000598 else
Evan Cheng19a72582009-02-02 18:33:18 +0000599 RetVNI = PerformPHIConstruction(Walker, MBB, LI, Visited, Defs, Uses,
600 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000601
Evan Cheng19a72582009-02-02 18:33:18 +0000602 LI->addRange(LiveRange(StartIndex, EndIndex+1, RetVNI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000603
Evan Cheng19a72582009-02-02 18:33:18 +0000604 if (foundUse && LI->isKill(RetVNI, StartIndex))
605 LI->removeKill(RetVNI, StartIndex);
606 if (IsIntraBlock) {
607 LI->addKill(RetVNI, EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000608 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000609 }
610
611 // Memoize results so we don't have to recompute them.
Evan Cheng19a72582009-02-02 18:33:18 +0000612 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000613 else {
Evan Cheng19a72582009-02-02 18:33:18 +0000614 if (!NewVNs.count(UseI))
615 NewVNs[UseI] = RetVNI;
616 Visited.insert(UseI);
Owen Anderson200ee7f2009-01-06 07:53:32 +0000617 }
618
Evan Cheng19a72582009-02-02 18:33:18 +0000619 return RetVNI;
620}
621
622/// PerformPHIConstructionFallBack - PerformPHIConstruction fall back path.
623///
624VNInfo*
625PreAllocSplitting::PerformPHIConstructionFallBack(MachineBasicBlock::iterator UseI,
626 MachineBasicBlock* MBB, LiveInterval* LI,
627 SmallPtrSet<MachineInstr*, 4>& Visited,
628 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
629 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
630 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
631 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
632 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
633 bool IsTopLevel, bool IsIntraBlock) {
634 // NOTE: Because this is the fallback case from other cases, we do NOT
635 // assume that we are not intrablock here.
636 if (Phis.count(MBB)) return Phis[MBB];
637
638 unsigned StartIndex = LIs->getMBBStartIdx(MBB);
639 VNInfo *RetVNI = Phis[MBB] = LI->getNextValue(~0U, /*FIXME*/ 0,
640 LIs->getVNInfoAllocator());
641 if (!IsIntraBlock) LiveOut[MBB] = RetVNI;
642
643 // If there are no uses or defs between our starting point and the
644 // beginning of the block, then recursive perform phi construction
645 // on our predecessors.
646 DenseMap<MachineBasicBlock*, VNInfo*> IncomingVNs;
647 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
648 PE = MBB->pred_end(); PI != PE; ++PI) {
649 VNInfo* Incoming = PerformPHIConstruction((*PI)->end(), *PI, LI,
650 Visited, Defs, Uses, NewVNs,
651 LiveOut, Phis, false, false);
652 if (Incoming != 0)
653 IncomingVNs[*PI] = Incoming;
654 }
655
656 if (MBB->pred_size() == 1 && !RetVNI->hasPHIKill) {
657 LI->MergeValueNumberInto(RetVNI, IncomingVNs.begin()->second);
658 Phis[MBB] = RetVNI = IncomingVNs.begin()->second;
659 } else {
660 // Otherwise, merge the incoming VNInfos with a phi join. Create a new
661 // VNInfo to represent the joined value.
662 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator I =
663 IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) {
664 I->second->hasPHIKill = true;
665 unsigned KillIndex = LIs->getMBBEndIdx(I->first);
666 if (!LiveInterval::isKill(I->second, KillIndex))
667 LI->addKill(I->second, KillIndex);
668 }
669 }
670
671 unsigned EndIndex = 0;
672 if (IsIntraBlock) {
673 EndIndex = LIs->getInstructionIndex(UseI);
674 EndIndex = LiveIntervals::getUseIndex(EndIndex);
675 } else
676 EndIndex = LIs->getMBBEndIdx(MBB);
677 LI->addRange(LiveRange(StartIndex, EndIndex+1, RetVNI));
678 if (IsIntraBlock)
679 LI->addKill(RetVNI, EndIndex);
680
681 // Memoize results so we don't have to recompute them.
682 if (!IsIntraBlock)
683 LiveOut[MBB] = RetVNI;
684 else {
685 if (!NewVNs.count(UseI))
686 NewVNs[UseI] = RetVNI;
687 Visited.insert(UseI);
688 }
689
690 return RetVNI;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000691}
692
693/// ReconstructLiveInterval - Recompute a live interval from scratch.
694void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) {
695 BumpPtrAllocator& Alloc = LIs->getVNInfoAllocator();
696
697 // Clear the old ranges and valnos;
698 LI->clear();
699
700 // Cache the uses and defs of the register
701 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
702 RegMap Defs, Uses;
703
704 // Keep track of the new VNs we're creating.
705 DenseMap<MachineInstr*, VNInfo*> NewVNs;
706 SmallPtrSet<VNInfo*, 2> PhiVNs;
707
708 // Cache defs, and create a new VNInfo for each def.
709 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
710 DE = MRI->def_end(); DI != DE; ++DI) {
711 Defs[(*DI).getParent()].insert(&*DI);
712
713 unsigned DefIdx = LIs->getInstructionIndex(&*DI);
714 DefIdx = LiveIntervals::getDefIndex(DefIdx);
715
Owen Anderson200ee7f2009-01-06 07:53:32 +0000716 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, Alloc);
717
718 // If the def is a move, set the copy field.
Evan Cheng04ee5a12009-01-20 19:12:24 +0000719 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
720 if (TII->isMoveInstr(*DI, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
721 if (DstReg == LI->reg)
Owen Anderson200ee7f2009-01-06 07:53:32 +0000722 NewVN->copy = &*DI;
723
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000724 NewVNs[&*DI] = NewVN;
725 }
726
727 // Cache uses as a separate pass from actually processing them.
728 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
729 UE = MRI->use_end(); UI != UE; ++UI)
730 Uses[(*UI).getParent()].insert(&*UI);
731
732 // Now, actually process every use and use a phi construction algorithm
733 // to walk from it to its reaching definitions, building VNInfos along
734 // the way.
Owen Anderson7d211e22008-12-31 02:00:25 +0000735 DenseMap<MachineBasicBlock*, VNInfo*> LiveOut;
736 DenseMap<MachineBasicBlock*, VNInfo*> Phis;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000737 SmallPtrSet<MachineInstr*, 4> Visited;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000738 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
739 UE = MRI->use_end(); UI != UE; ++UI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000740 PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000741 Uses, NewVNs, LiveOut, Phis, true, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000742 }
Owen Andersond4f6fe52008-12-28 23:35:13 +0000743
744 // Add ranges for dead defs
745 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
746 DE = MRI->def_end(); DI != DE; ++DI) {
747 unsigned DefIdx = LIs->getInstructionIndex(&*DI);
748 DefIdx = LiveIntervals::getDefIndex(DefIdx);
Owen Andersond4f6fe52008-12-28 23:35:13 +0000749
750 if (LI->liveAt(DefIdx)) continue;
751
752 VNInfo* DeadVN = NewVNs[&*DI];
Owen Anderson7d211e22008-12-31 02:00:25 +0000753 LI->addRange(LiveRange(DefIdx, DefIdx+1, DeadVN));
Owen Andersond4f6fe52008-12-28 23:35:13 +0000754 LI->addKill(DeadVN, DefIdx);
755 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000756}
757
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000758/// RenumberValno - Split the given valno out into a new vreg, allowing it to
759/// be allocated to a different register. This function creates a new vreg,
760/// copies the valno and its live ranges over to the new vreg's interval,
761/// removes them from the old interval, and rewrites all uses and defs of
762/// the original reg to the new vreg within those ranges.
763void PreAllocSplitting::RenumberValno(VNInfo* VN) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000764 SmallVector<VNInfo*, 4> Stack;
765 SmallVector<VNInfo*, 4> VNsToCopy;
766 Stack.push_back(VN);
767
768 // Walk through and copy the valno we care about, and any other valnos
769 // that are two-address redefinitions of the one we care about. These
770 // will need to be rewritten as well. We also check for safety of the
771 // renumbering here, by making sure that none of the valno involved has
772 // phi kills.
773 while (!Stack.empty()) {
774 VNInfo* OldVN = Stack.back();
775 Stack.pop_back();
776
777 // Bail out if we ever encounter a valno that has a PHI kill. We can't
778 // renumber these.
779 if (OldVN->hasPHIKill) return;
780
781 VNsToCopy.push_back(OldVN);
782
783 // Locate two-address redefinitions
784 for (SmallVector<unsigned, 4>::iterator KI = OldVN->kills.begin(),
785 KE = OldVN->kills.end(); KI != KE; ++KI) {
786 MachineInstr* MI = LIs->getInstructionFromIndex(*KI);
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000787 unsigned DefIdx = MI->findRegisterDefOperandIdx(CurrLI->reg);
788 if (DefIdx == ~0U) continue;
789 if (MI->isRegReDefinedByTwoAddr(DefIdx)) {
790 VNInfo* NextVN =
791 CurrLI->findDefinedVNInfo(LiveIntervals::getDefIndex(*KI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000792 if (NextVN == OldVN) continue;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000793 Stack.push_back(NextVN);
794 }
795 }
796 }
797
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000798 // Create the new vreg
799 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
800
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000801 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000802 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000803
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000804 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
805 VNsToCopy.end(); OI != OE; ++OI) {
806 VNInfo* OldVN = *OI;
807
808 // Copy the valno over
809 VNInfo* NewVN = NewLI.getNextValue(OldVN->def, OldVN->copy,
810 LIs->getVNInfoAllocator());
811 NewLI.copyValNumInfo(NewVN, OldVN);
812 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
813
814 // Remove the valno from the old interval
815 CurrLI->removeValNo(OldVN);
816 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000817
818 // Rewrite defs and uses. This is done in two stages to avoid invalidating
819 // the reg_iterator.
820 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
821
822 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
823 E = MRI->reg_end(); I != E; ++I) {
824 MachineOperand& MO = I.getOperand();
825 unsigned InstrIdx = LIs->getInstructionIndex(&*I);
826
827 if ((MO.isUse() && NewLI.liveAt(LiveIntervals::getUseIndex(InstrIdx))) ||
828 (MO.isDef() && NewLI.liveAt(LiveIntervals::getDefIndex(InstrIdx))))
829 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
830 }
831
832 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
833 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
834 MachineInstr* Inst = I->first;
835 unsigned OpIdx = I->second;
836 MachineOperand& MO = Inst->getOperand(OpIdx);
837 MO.setReg(NewVReg);
838 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000839
Owen Anderson45e68552009-01-29 05:28:55 +0000840 // The renumbered vreg shares a stack slot with the old register.
841 if (IntervalSSMap.count(CurrLI->reg))
842 IntervalSSMap[NewVReg] = IntervalSSMap[CurrLI->reg];
843
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000844 NumRenumbers++;
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000845}
846
Owen Anderson6002e992008-12-04 21:20:30 +0000847bool PreAllocSplitting::Rematerialize(unsigned vreg, VNInfo* ValNo,
848 MachineInstr* DefMI,
849 MachineBasicBlock::iterator RestorePt,
850 unsigned RestoreIdx,
851 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
852 MachineBasicBlock& MBB = *RestorePt->getParent();
853
854 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
855 unsigned KillIdx = 0;
856 if (ValNo->def == ~0U || DefMI->getParent() == BarrierMBB)
857 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, KillIdx);
858 else
859 KillPt = findNextEmptySlot(DefMI->getParent(), DefMI, KillIdx);
860
861 if (KillPt == DefMI->getParent()->end())
862 return false;
863
864 TII->reMaterialize(MBB, RestorePt, vreg, DefMI);
865 LIs->InsertMachineInstrInMaps(prior(RestorePt), RestoreIdx);
866
Owen Andersonb4b84362009-01-26 21:57:31 +0000867 ReconstructLiveInterval(CurrLI);
868 unsigned RematIdx = LIs->getInstructionIndex(prior(RestorePt));
869 RematIdx = LiveIntervals::getDefIndex(RematIdx);
870 RenumberValno(CurrLI->findDefinedVNInfo(RematIdx));
Owen Andersone1762c92009-01-12 03:10:40 +0000871
Owen Anderson75fa96b2008-11-19 04:28:29 +0000872 ++NumSplits;
873 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000874 return true;
875}
876
877MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
878 const TargetRegisterClass* RC,
879 MachineInstr* DefMI,
880 MachineInstr* Barrier,
881 MachineBasicBlock* MBB,
882 int& SS,
883 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
884 MachineBasicBlock::iterator Pt = MBB->begin();
885
886 // Go top down if RefsInMBB is empty.
887 if (RefsInMBB.empty())
888 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000889
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000890 MachineBasicBlock::iterator FoldPt = Barrier;
891 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
892 !RefsInMBB.count(FoldPt))
893 --FoldPt;
894
895 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg, false);
896 if (OpIdx == -1)
897 return 0;
898
899 SmallVector<unsigned, 1> Ops;
900 Ops.push_back(OpIdx);
901
902 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
903 return 0;
904
905 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
906 if (I != IntervalSSMap.end()) {
907 SS = I->second;
908 } else {
909 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
910
911 }
912
913 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
914 FoldPt, Ops, SS);
915
916 if (FMI) {
917 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
918 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
919 ++NumFolds;
920
921 IntervalSSMap[vreg] = SS;
922 CurrSLI = &LSs->getOrCreateInterval(SS);
923 if (CurrSLI->hasAtLeastOneValue())
924 CurrSValNo = CurrSLI->getValNumInfo(0);
925 else
926 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
927 }
928
929 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000930}
931
Evan Chengf5cd4f02008-10-23 20:43:13 +0000932/// SplitRegLiveInterval - Split (spill and restore) the given live interval
933/// so it would not cross the barrier that's being processed. Shrink wrap
934/// (minimize) the live interval to the last uses.
935bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
936 CurrLI = LI;
937
938 // Find live range where current interval cross the barrier.
939 LiveInterval::iterator LR =
940 CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx));
941 VNInfo *ValNo = LR->valno;
942
943 if (ValNo->def == ~1U) {
944 // Defined by a dead def? How can this be?
945 assert(0 && "Val# is defined by a dead def?");
946 abort();
947 }
948
Evan Cheng06587492008-10-24 02:05:00 +0000949 MachineInstr *DefMI = (ValNo->def != ~0U)
950 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
Evan Cheng06587492008-10-24 02:05:00 +0000951
Owen Andersond3be4622009-01-21 08:18:03 +0000952 // If this would create a new join point, do not split.
953 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent()))
954 return false;
955
Evan Chengf5cd4f02008-10-23 20:43:13 +0000956 // Find all references in the barrier mbb.
957 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
958 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
959 E = MRI->reg_end(); I != E; ++I) {
960 MachineInstr *RefMI = &*I;
961 if (RefMI->getParent() == BarrierMBB)
962 RefsInMBB.insert(RefMI);
963 }
964
965 // Find a point to restore the value after the barrier.
Evan Chengb964f332009-01-26 18:33:51 +0000966 unsigned RestoreIndex = 0;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000967 MachineBasicBlock::iterator RestorePt =
Evan Chengf62ce372008-10-28 00:47:49 +0000968 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000969 if (RestorePt == BarrierMBB->end())
970 return false;
971
Owen Anderson75fa96b2008-11-19 04:28:29 +0000972 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
973 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt,
974 RestoreIndex, RefsInMBB))
975 return true;
976
Evan Chengf5cd4f02008-10-23 20:43:13 +0000977 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +0000978 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000979 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000980 unsigned SpillIndex = 0;
Evan Cheng06587492008-10-24 02:05:00 +0000981 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +0000982 int SS = -1;
Evan Cheng78dfef72008-10-25 00:52:41 +0000983 if (ValNo->def == ~0U) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000984 // If it's defined by a phi, we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000985 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
986 BarrierMBB, SS, RefsInMBB))) {
987 SpillIndex = LIs->getInstructionIndex(SpillMI);
988 } else {
989 MachineBasicBlock::iterator SpillPt =
990 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex);
991 if (SpillPt == BarrierMBB->begin())
992 return false; // No gap to insert spill.
993 // Add spill.
994
995 SS = CreateSpillStackSlot(CurrLI->reg, RC);
996 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
997 SpillMI = prior(SpillPt);
998 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
999 }
Evan Cheng54898932008-10-29 08:39:34 +00001000 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
1001 RestoreIndex, SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +00001002 // If it's already split, just restore the value. There is no need to spill
1003 // the def again.
Evan Chengd0e32c52008-10-29 05:06:14 +00001004 if (!DefMI)
1005 return false; // Def is dead. Do nothing.
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001006
1007 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
1008 BarrierMBB, SS, RefsInMBB))) {
1009 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001010 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +00001011 // Check if it's possible to insert a spill after the def MI.
1012 MachineBasicBlock::iterator SpillPt;
1013 if (DefMBB == BarrierMBB) {
1014 // Add spill after the def and the last use before the barrier.
1015 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
1016 RefsInMBB, SpillIndex);
1017 if (SpillPt == DefMBB->begin())
1018 return false; // No gap to insert spill.
1019 } else {
1020 SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex);
1021 if (SpillPt == DefMBB->end())
1022 return false; // No gap to insert spill.
1023 }
1024 // Add spill. The store instruction kills the register if def is before
1025 // the barrier in the barrier block.
1026 SS = CreateSpillStackSlot(CurrLI->reg, RC);
1027 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg,
1028 DefMBB == BarrierMBB, SS, RC);
1029 SpillMI = prior(SpillPt);
1030 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Cheng1f08cc22008-10-28 05:28:21 +00001031 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001032 }
1033
Evan Cheng54898932008-10-29 08:39:34 +00001034 // Remember def instruction index to spill index mapping.
1035 if (DefMI && SpillMI)
1036 Def2SpillMap[ValNo->def] = SpillIndex;
1037
Evan Chengf5cd4f02008-10-23 20:43:13 +00001038 // Add restore.
Evan Chengf5cd4f02008-10-23 20:43:13 +00001039 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
1040 MachineInstr *LoadMI = prior(RestorePt);
1041 LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex);
1042
Evan Chengd0e32c52008-10-29 05:06:14 +00001043 // Update spill stack slot live interval.
Evan Cheng54898932008-10-29 08:39:34 +00001044 UpdateSpillSlotInterval(ValNo, LIs->getUseIndex(SpillIndex)+1,
1045 LIs->getDefIndex(RestoreIndex));
Evan Chengd0e32c52008-10-29 05:06:14 +00001046
Owen Andersonb4b84362009-01-26 21:57:31 +00001047 ReconstructLiveInterval(CurrLI);
1048 unsigned RestoreIdx = LIs->getInstructionIndex(prior(RestorePt));
1049 RestoreIdx = LiveIntervals::getDefIndex(RestoreIdx);
1050 RenumberValno(CurrLI->findDefinedVNInfo(RestoreIdx));
Owen Anderson7d211e22008-12-31 02:00:25 +00001051
Evan Chengae7fa5b2008-10-28 01:48:24 +00001052 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001053 return true;
1054}
1055
1056/// SplitRegLiveIntervals - Split all register live intervals that cross the
1057/// barrier that's being processed.
1058bool
Owen Anderson956ec272009-01-23 00:23:32 +00001059PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs,
1060 SmallPtrSet<LiveInterval*, 8>& Split) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001061 // First find all the virtual registers whose live intervals are intercepted
1062 // by the current barrier.
1063 SmallVector<LiveInterval*, 8> Intervals;
1064 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng23066282008-10-27 07:14:50 +00001065 if (TII->IgnoreRegisterClassBarriers(*RC))
1066 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001067 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
1068 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1069 unsigned Reg = VRs[i];
1070 if (!LIs->hasInterval(Reg))
1071 continue;
1072 LiveInterval *LI = &LIs->getInterval(Reg);
1073 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1074 // Virtual register live interval is intercepted by the barrier. We
1075 // should split and shrink wrap its interval if possible.
1076 Intervals.push_back(LI);
1077 }
1078 }
1079
1080 // Process the affected live intervals.
1081 bool Change = false;
1082 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001083 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1084 break;
Owen Andersone1762c92009-01-12 03:10:40 +00001085 else if (NumSplits == 4)
1086 Change |= Change;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001087 LiveInterval *LI = Intervals.back();
1088 Intervals.pop_back();
Owen Anderson956ec272009-01-23 00:23:32 +00001089 bool result = SplitRegLiveInterval(LI);
1090 if (result) Split.insert(LI);
1091 Change |= result;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001092 }
1093
1094 return Change;
1095}
1096
Owen Anderson45e68552009-01-29 05:28:55 +00001097unsigned PreAllocSplitting::getNumberOfNonSpills(
Owen Anderson32ca8652009-01-24 10:07:43 +00001098 SmallPtrSet<MachineInstr*, 4>& MIs,
Owen Anderson45e68552009-01-29 05:28:55 +00001099 unsigned Reg, int FrameIndex,
1100 bool& FeedsTwoAddr) {
1101 unsigned NonSpills = 0;
Owen Anderson32ca8652009-01-24 10:07:43 +00001102 for (SmallPtrSet<MachineInstr*, 4>::iterator UI = MIs.begin(), UE = MIs.end();
Owen Anderson45e68552009-01-29 05:28:55 +00001103 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001104 int StoreFrameIndex;
1105 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001106 if (StoreVReg != Reg || StoreFrameIndex != FrameIndex)
1107 NonSpills++;
1108
1109 int DefIdx = (*UI)->findRegisterDefOperandIdx(Reg);
1110 if (DefIdx != -1 && (*UI)->isRegReDefinedByTwoAddr(DefIdx))
1111 FeedsTwoAddr = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001112 }
1113
Owen Anderson45e68552009-01-29 05:28:55 +00001114 return NonSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001115}
1116
Owen Anderson956ec272009-01-23 00:23:32 +00001117/// removeDeadSpills - After doing splitting, filter through all intervals we've
1118/// split, and see if any of the spills are unnecessary. If so, remove them.
1119bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) {
1120 bool changed = false;
1121
Owen Anderson4bfc2092009-01-29 05:41:02 +00001122 // Walk over all of the live intervals that were touched by the splitter,
1123 // and see if we can do any DCE and/or folding.
Owen Anderson956ec272009-01-23 00:23:32 +00001124 for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(),
1125 LE = split.end(); LI != LE; ++LI) {
Owen Anderson9ce499a2009-01-23 03:28:53 +00001126 DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> > VNUseCount;
Owen Anderson956ec272009-01-23 00:23:32 +00001127
Owen Anderson4bfc2092009-01-29 05:41:02 +00001128 // First, collect all the uses of the vreg, and sort them by their
1129 // reaching definition (VNInfo).
Owen Anderson956ec272009-01-23 00:23:32 +00001130 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg),
1131 UE = MRI->use_end(); UI != UE; ++UI) {
1132 unsigned index = LIs->getInstructionIndex(&*UI);
1133 index = LiveIntervals::getUseIndex(index);
1134
1135 const LiveRange* LR = (*LI)->getLiveRangeContaining(index);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001136 VNUseCount[LR->valno].insert(&*UI);
Owen Anderson956ec272009-01-23 00:23:32 +00001137 }
1138
Owen Anderson4bfc2092009-01-29 05:41:02 +00001139 // Now, take the definitions (VNInfo's) one at a time and try to DCE
1140 // and/or fold them away.
Owen Anderson956ec272009-01-23 00:23:32 +00001141 for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(),
1142 VE = (*LI)->vni_end(); VI != VE; ++VI) {
Owen Anderson45e68552009-01-29 05:28:55 +00001143
1144 if (DeadSplitLimit != -1 && (int)NumDeadSpills == DeadSplitLimit)
1145 return changed;
1146
Owen Anderson956ec272009-01-23 00:23:32 +00001147 VNInfo* CurrVN = *VI;
Owen Anderson4bfc2092009-01-29 05:41:02 +00001148
1149 // We don't currently try to handle definitions with PHI kills, because
1150 // it would involve processing more than one VNInfo at once.
Owen Anderson956ec272009-01-23 00:23:32 +00001151 if (CurrVN->hasPHIKill) continue;
Owen Anderson956ec272009-01-23 00:23:32 +00001152
Owen Anderson4bfc2092009-01-29 05:41:02 +00001153 // We also don't try to handle the results of PHI joins, since there's
1154 // no defining instruction to analyze.
Owen Anderson956ec272009-01-23 00:23:32 +00001155 unsigned DefIdx = CurrVN->def;
1156 if (DefIdx == ~0U || DefIdx == ~1U) continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001157
Owen Anderson4bfc2092009-01-29 05:41:02 +00001158 // We're only interested in eliminating cruft introduced by the splitter,
1159 // is of the form load-use or load-use-store. First, check that the
1160 // definition is a load, and remember what stack slot we loaded it from.
Owen Anderson956ec272009-01-23 00:23:32 +00001161 MachineInstr* DefMI = LIs->getInstructionFromIndex(DefIdx);
1162 int FrameIndex;
1163 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
1164
Owen Anderson4bfc2092009-01-29 05:41:02 +00001165 // If the definition has no uses at all, just DCE it.
Owen Anderson9ce499a2009-01-23 03:28:53 +00001166 if (VNUseCount[CurrVN].size() == 0) {
1167 LIs->RemoveMachineInstrFromMaps(DefMI);
1168 (*LI)->removeValNo(CurrVN);
1169 DefMI->eraseFromParent();
Owen Andersonc0f3a032009-01-29 08:22:06 +00001170 VNUseCount.erase(CurrVN);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001171 NumDeadSpills++;
1172 changed = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001173 continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001174 }
Owen Anderson32ca8652009-01-24 10:07:43 +00001175
Owen Anderson4bfc2092009-01-29 05:41:02 +00001176 // Second, get the number of non-store uses of the definition, as well as
1177 // a flag indicating whether it feeds into a later two-address definition.
Owen Anderson45e68552009-01-29 05:28:55 +00001178 bool FeedsTwoAddr = false;
1179 unsigned NonSpillCount = getNumberOfNonSpills(VNUseCount[CurrVN],
1180 (*LI)->reg, FrameIndex,
1181 FeedsTwoAddr);
1182
Owen Anderson4bfc2092009-01-29 05:41:02 +00001183 // If there's one non-store use and it doesn't feed a two-addr, then
1184 // this is a load-use-store case that we can try to fold.
Owen Anderson45e68552009-01-29 05:28:55 +00001185 if (NonSpillCount == 1 && !FeedsTwoAddr) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001186 // Start by finding the non-store use MachineInstr.
Owen Anderson45e68552009-01-29 05:28:55 +00001187 SmallPtrSet<MachineInstr*, 4>::iterator UI = VNUseCount[CurrVN].begin();
1188 int StoreFrameIndex;
1189 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1190 while (UI != VNUseCount[CurrVN].end() &&
1191 (StoreVReg == (*LI)->reg && StoreFrameIndex == FrameIndex)) {
1192 ++UI;
1193 if (UI != VNUseCount[CurrVN].end())
1194 StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1195 }
Owen Anderson45e68552009-01-29 05:28:55 +00001196 if (UI == VNUseCount[CurrVN].end()) continue;
1197
1198 MachineInstr* use = *UI;
1199
Owen Anderson4bfc2092009-01-29 05:41:02 +00001200 // Attempt to fold it away!
Owen Anderson45e68552009-01-29 05:28:55 +00001201 int OpIdx = use->findRegisterUseOperandIdx((*LI)->reg, false);
1202 if (OpIdx == -1) continue;
Owen Anderson45e68552009-01-29 05:28:55 +00001203 SmallVector<unsigned, 1> Ops;
1204 Ops.push_back(OpIdx);
Owen Anderson45e68552009-01-29 05:28:55 +00001205 if (!TII->canFoldMemoryOperand(use, Ops)) continue;
1206
1207 MachineInstr* NewMI =
1208 TII->foldMemoryOperand(*use->getParent()->getParent(),
1209 use, Ops, FrameIndex);
1210
1211 if (!NewMI) continue;
1212
Owen Anderson4bfc2092009-01-29 05:41:02 +00001213 // Update relevant analyses.
Owen Anderson45e68552009-01-29 05:28:55 +00001214 LIs->RemoveMachineInstrFromMaps(DefMI);
1215 LIs->ReplaceMachineInstrInMaps(use, NewMI);
1216 (*LI)->removeValNo(CurrVN);
1217
1218 DefMI->eraseFromParent();
1219 MachineBasicBlock* MBB = use->getParent();
1220 NewMI = MBB->insert(MBB->erase(use), NewMI);
1221 VNUseCount[CurrVN].erase(use);
1222
Owen Anderson4bfc2092009-01-29 05:41:02 +00001223 // Remove deleted instructions. Note that we need to remove them from
1224 // the VNInfo->use map as well, just to be safe.
Owen Anderson45e68552009-01-29 05:28:55 +00001225 for (SmallPtrSet<MachineInstr*, 4>::iterator II =
1226 VNUseCount[CurrVN].begin(), IE = VNUseCount[CurrVN].end();
1227 II != IE; ++II) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001228 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
Owen Andersonc0f3a032009-01-29 08:22:06 +00001229 VNI = VNUseCount.begin(), VNE = VNUseCount.end(); VNI != VNE;
1230 ++VNI)
1231 if (VNI->first != CurrVN)
1232 VNI->second.erase(*II);
Owen Anderson45e68552009-01-29 05:28:55 +00001233 LIs->RemoveMachineInstrFromMaps(*II);
1234 (*II)->eraseFromParent();
1235 }
Owen Andersonc0f3a032009-01-29 08:22:06 +00001236
1237 VNUseCount.erase(CurrVN);
Owen Anderson45e68552009-01-29 05:28:55 +00001238
1239 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
1240 VI = VNUseCount.begin(), VE = VNUseCount.end(); VI != VE; ++VI)
1241 if (VI->second.erase(use))
1242 VI->second.insert(NewMI);
1243
1244 NumDeadSpills++;
1245 changed = true;
1246 continue;
1247 }
1248
Owen Anderson4bfc2092009-01-29 05:41:02 +00001249 // If there's more than one non-store instruction, we can't profitably
1250 // fold it, so bail.
Owen Anderson45e68552009-01-29 05:28:55 +00001251 if (NonSpillCount) continue;
Owen Anderson32ca8652009-01-24 10:07:43 +00001252
Owen Anderson4bfc2092009-01-29 05:41:02 +00001253 // Otherwise, this is a load-store case, so DCE them.
Owen Anderson32ca8652009-01-24 10:07:43 +00001254 for (SmallPtrSet<MachineInstr*, 4>::iterator UI =
1255 VNUseCount[CurrVN].begin(), UE = VNUseCount[CurrVN].end();
1256 UI != UI; ++UI) {
1257 LIs->RemoveMachineInstrFromMaps(*UI);
1258 (*UI)->eraseFromParent();
1259 }
1260
Owen Andersonc0f3a032009-01-29 08:22:06 +00001261 VNUseCount.erase(CurrVN);
1262
Owen Anderson32ca8652009-01-24 10:07:43 +00001263 LIs->RemoveMachineInstrFromMaps(DefMI);
1264 (*LI)->removeValNo(CurrVN);
1265 DefMI->eraseFromParent();
1266 NumDeadSpills++;
1267 changed = true;
Owen Anderson956ec272009-01-23 00:23:32 +00001268 }
1269 }
1270
1271 return changed;
1272}
1273
Owen Andersonf1f75b12008-11-04 22:22:41 +00001274bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1275 MachineBasicBlock* DefMBB,
1276 MachineBasicBlock* BarrierMBB) {
1277 if (DefMBB == BarrierMBB)
1278 return false;
1279
1280 if (LR->valno->hasPHIKill)
1281 return false;
1282
1283 unsigned MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
1284 if (LR->end < MBBEnd)
1285 return false;
1286
1287 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1288 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1289 return true;
1290
1291 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1292 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1293 typedef std::pair<MachineBasicBlock*,
1294 MachineBasicBlock::succ_iterator> ItPair;
1295 SmallVector<ItPair, 4> Stack;
1296 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1297
1298 while (!Stack.empty()) {
1299 ItPair P = Stack.back();
1300 Stack.pop_back();
1301
1302 MachineBasicBlock* PredMBB = P.first;
1303 MachineBasicBlock::succ_iterator S = P.second;
1304
1305 if (S == PredMBB->succ_end())
1306 continue;
1307 else if (Visited.count(*S)) {
1308 Stack.push_back(std::make_pair(PredMBB, ++S));
1309 continue;
1310 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001311 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001312
1313 MachineBasicBlock* MBB = *S;
1314 Visited.insert(MBB);
1315
1316 if (MBB == BarrierMBB)
1317 return true;
1318
1319 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1320 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1321 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1322 while (MDTN) {
1323 if (MDTN == DefMDTN)
1324 return true;
1325 else if (MDTN == BarrierMDTN)
1326 break;
1327 MDTN = MDTN->getIDom();
1328 }
1329
1330 MBBEnd = LIs->getMBBEndIdx(MBB);
1331 if (LR->end > MBBEnd)
1332 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1333 }
1334
1335 return false;
1336}
1337
1338
Evan Cheng09e8ca82008-10-20 21:44:59 +00001339bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001340 CurrMF = &MF;
1341 TM = &MF.getTarget();
Owen Anderson3ef45492009-01-29 22:13:06 +00001342 TRI = TM->getRegisterInfo();
Evan Chengd0e32c52008-10-29 05:06:14 +00001343 TII = TM->getInstrInfo();
1344 MFI = MF.getFrameInfo();
1345 MRI = &MF.getRegInfo();
1346 LIs = &getAnalysis<LiveIntervals>();
1347 LSs = &getAnalysis<LiveStacks>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001348
1349 bool MadeChange = false;
1350
1351 // Make sure blocks are numbered in order.
1352 MF.RenumberBlocks();
1353
Evan Cheng54898932008-10-29 08:39:34 +00001354 MachineBasicBlock *Entry = MF.begin();
1355 SmallPtrSet<MachineBasicBlock*,16> Visited;
1356
Owen Anderson956ec272009-01-23 00:23:32 +00001357 SmallPtrSet<LiveInterval*, 8> Split;
1358
Evan Cheng54898932008-10-29 08:39:34 +00001359 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1360 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1361 DFI != E; ++DFI) {
1362 BarrierMBB = *DFI;
1363 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1364 E = BarrierMBB->end(); I != E; ++I) {
1365 Barrier = &*I;
1366 const TargetRegisterClass **BarrierRCs =
1367 Barrier->getDesc().getRegClassBarriers();
1368 if (!BarrierRCs)
1369 continue;
1370 BarrierIdx = LIs->getInstructionIndex(Barrier);
Owen Anderson956ec272009-01-23 00:23:32 +00001371 MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split);
Evan Cheng54898932008-10-29 08:39:34 +00001372 }
1373 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001374
Owen Anderson956ec272009-01-23 00:23:32 +00001375 MadeChange |= removeDeadSpills(Split);
1376
Evan Chengf5cd4f02008-10-23 20:43:13 +00001377 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001378}