blob: 4061859f740c23371d52392a9ad7991c7554800d [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);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000167 VNInfo* PerformPHIConstruction(MachineBasicBlock::iterator use,
Owen Anderson7d211e22008-12-31 02:00:25 +0000168 MachineBasicBlock* MBB,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000169 LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000170 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000171 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
172 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
173 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000174 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
175 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
176 bool toplevel, bool intrablock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000177};
Evan Cheng09e8ca82008-10-20 21:44:59 +0000178} // end anonymous namespace
179
180char PreAllocSplitting::ID = 0;
181
182static RegisterPass<PreAllocSplitting>
183X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting");
184
185const PassInfo *const llvm::PreAllocSplittingID = &X;
186
Evan Chengf5cd4f02008-10-23 20:43:13 +0000187
188/// findNextEmptySlot - Find a gap after the given machine instruction in the
189/// instruction index map. If there isn't one, return end().
190MachineBasicBlock::iterator
191PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI,
192 unsigned &SpotIndex) {
193 MachineBasicBlock::iterator MII = MI;
194 if (++MII != MBB->end()) {
195 unsigned Index = LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII));
196 if (Index) {
197 SpotIndex = Index;
198 return MII;
199 }
200 }
201 return MBB->end();
202}
203
204/// findSpillPoint - Find a gap as far away from the given MI that's suitable
205/// for spilling the current live interval. The index must be before any
206/// defs and uses of the live interval register in the mbb. Return begin() if
207/// none is found.
208MachineBasicBlock::iterator
209PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000210 MachineInstr *DefMI,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000211 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
212 unsigned &SpillIndex) {
213 MachineBasicBlock::iterator Pt = MBB->begin();
214
215 // Go top down if RefsInMBB is empty.
Evan Cheng1f08cc22008-10-28 05:28:21 +0000216 if (RefsInMBB.empty() && !DefMI) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000217 MachineBasicBlock::iterator MII = MBB->begin();
218 MachineBasicBlock::iterator EndPt = MI;
219 do {
220 ++MII;
221 unsigned Index = LIs->getInstructionIndex(MII);
222 unsigned Gap = LIs->findGapBeforeInstr(Index);
223 if (Gap) {
224 Pt = MII;
225 SpillIndex = Gap;
226 break;
Owen Anderson3ef45492009-01-29 22:13:06 +0000227
228 // We can't insert the spill between the barrier (a call), and its
229 // corresponding call frame setup.
230 } else if (prior(MII)->getOpcode() == TRI->getCallFrameSetupOpcode() &&
231 MII == MachineBasicBlock::iterator(MI))
232 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000233 } while (MII != EndPt);
234 } else {
235 MachineBasicBlock::iterator MII = MI;
Evan Cheng1f08cc22008-10-28 05:28:21 +0000236 MachineBasicBlock::iterator EndPt = DefMI
237 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
Owen Anderson3ef45492009-01-29 22:13:06 +0000238
239 // We can't insert the spill between the barrier (a call), and its
240 // corresponding call frame setup.
241 if (prior(MII)->getOpcode() == TRI->getCallFrameSetupOpcode()) --MII;
Evan Cheng1f08cc22008-10-28 05:28:21 +0000242 while (MII != EndPt && !RefsInMBB.count(MII)) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000243 unsigned Index = LIs->getInstructionIndex(MII);
244 if (LIs->hasGapBeforeInstr(Index)) {
245 Pt = MII;
246 SpillIndex = LIs->findGapBeforeInstr(Index, true);
247 }
248 --MII;
249 }
250 }
251
252 return Pt;
253}
254
255/// findRestorePoint - Find a gap in the instruction index map that's suitable
256/// for restoring the current live interval value. The index must be before any
257/// uses of the live interval register in the mbb. Return end() if none is
258/// found.
259MachineBasicBlock::iterator
260PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Chengf62ce372008-10-28 00:47:49 +0000261 unsigned LastIdx,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000262 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
263 unsigned &RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000264 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
265 // begin index accordingly.
Owen Anderson5a92d4e2008-11-18 20:53:59 +0000266 MachineBasicBlock::iterator Pt = MBB->end();
Evan Chengf62ce372008-10-28 00:47:49 +0000267 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000268
Evan Chengf62ce372008-10-28 00:47:49 +0000269 // Go bottom up if RefsInMBB is empty and the end of the mbb isn't beyond
270 // the last index in the live range.
271 if (RefsInMBB.empty() && LastIdx >= EndIdx) {
Owen Anderson711fd3d2008-11-13 21:53:14 +0000272 MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000273 MachineBasicBlock::iterator EndPt = MI;
Evan Cheng54898932008-10-29 08:39:34 +0000274 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000275 do {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000276 unsigned Index = LIs->getInstructionIndex(MII);
Evan Cheng56ab0de2008-10-24 18:46:44 +0000277 unsigned Gap = LIs->findGapBeforeInstr(Index);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000278 if (Gap) {
279 Pt = MII;
280 RestoreIndex = Gap;
281 break;
Owen Anderson3ef45492009-01-29 22:13:06 +0000282
283 // We can't insert a restore between the barrier (a call) and its
284 // corresponding call frame teardown.
285 } else if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode() &&
286 prior(MII) == MachineBasicBlock::iterator(MI))
287 break;
Evan Cheng54898932008-10-29 08:39:34 +0000288 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000289 } while (MII != EndPt);
290 } else {
291 MachineBasicBlock::iterator MII = MI;
292 MII = ++MII;
Owen Anderson3ef45492009-01-29 22:13:06 +0000293 // We can't insert a restore between the barrier (a call) and its
294 // corresponding call frame teardown.
295 if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode())
296 ++MII;
297
Evan Chengf62ce372008-10-28 00:47:49 +0000298 // FIXME: Limit the number of instructions to examine to reduce
299 // compile time?
Owen Andersonc0f3a032009-01-29 08:22:06 +0000300 while (MII != MBB->getFirstTerminator()) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000301 unsigned Index = LIs->getInstructionIndex(MII);
Evan Chengf62ce372008-10-28 00:47:49 +0000302 if (Index > LastIdx)
303 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000304 unsigned Gap = LIs->findGapBeforeInstr(Index);
305 if (Gap) {
306 Pt = MII;
307 RestoreIndex = Gap;
308 }
309 if (RefsInMBB.count(MII))
310 break;
311 ++MII;
312 }
313 }
314
315 return Pt;
316}
317
Evan Chengd0e32c52008-10-29 05:06:14 +0000318/// CreateSpillStackSlot - Create a stack slot for the live interval being
319/// split. If the live interval was previously split, just reuse the same
320/// slot.
321int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
322 const TargetRegisterClass *RC) {
323 int SS;
324 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
325 if (I != IntervalSSMap.end()) {
326 SS = I->second;
327 } else {
328 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
329 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000330 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000331
332 // Create live interval for stack slot.
333 CurrSLI = &LSs->getOrCreateInterval(SS);
Evan Cheng54898932008-10-29 08:39:34 +0000334 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000335 CurrSValNo = CurrSLI->getValNumInfo(0);
336 else
337 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
338 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000339}
340
Evan Chengd0e32c52008-10-29 05:06:14 +0000341/// IsAvailableInStack - Return true if register is available in a split stack
342/// slot at the specified index.
343bool
Evan Cheng54898932008-10-29 08:39:34 +0000344PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
345 unsigned Reg, unsigned DefIndex,
346 unsigned RestoreIndex, unsigned &SpillIndex,
347 int& SS) const {
348 if (!DefMBB)
349 return false;
350
Evan Chengd0e32c52008-10-29 05:06:14 +0000351 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
352 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000353 return false;
Evan Cheng54898932008-10-29 08:39:34 +0000354 DenseMap<unsigned, unsigned>::iterator II = Def2SpillMap.find(DefIndex);
355 if (II == Def2SpillMap.end())
356 return false;
357
358 // If last spill of def is in the same mbb as barrier mbb (where restore will
359 // be), make sure it's not below the intended restore index.
360 // FIXME: Undo the previous spill?
361 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
362 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
363 return false;
364
365 SS = I->second;
366 SpillIndex = II->second;
367 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000368}
369
Evan Chengd0e32c52008-10-29 05:06:14 +0000370/// UpdateSpillSlotInterval - Given the specified val# of the register live
371/// interval being split, and the spill and restore indicies, update the live
372/// interval of the spill stack slot.
373void
374PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, unsigned SpillIndex,
375 unsigned RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000376 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
377 "Expect restore in the barrier mbb");
378
379 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
380 if (MBB == BarrierMBB) {
381 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000382 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
383 CurrSLI->addRange(SLR);
384 return;
385 }
386
Evan Cheng54898932008-10-29 08:39:34 +0000387 SmallPtrSet<MachineBasicBlock*, 4> Processed;
388 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
389 LiveRange SLR(SpillIndex, EndIdx+1, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000390 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000391 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000392
393 // Start from the spill mbb, figure out the extend of the spill slot's
394 // live interval.
395 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000396 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
397 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000398 // If live range extend beyond end of mbb, add successors to work list.
399 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
400 SE = MBB->succ_end(); SI != SE; ++SI)
401 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000402
403 while (!WorkList.empty()) {
404 MachineBasicBlock *MBB = WorkList.back();
405 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000406 if (Processed.count(MBB))
407 continue;
Evan Chengd0e32c52008-10-29 05:06:14 +0000408 unsigned Idx = LIs->getMBBStartIdx(MBB);
409 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000410 if (LR && LR->valno == ValNo) {
411 EndIdx = LIs->getMBBEndIdx(MBB);
412 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000413 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000414 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000415 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000416 } else if (LR->end > EndIdx) {
417 // Live range extends beyond end of mbb, process successors.
418 LiveRange SLR(Idx, EndIdx+1, CurrSValNo);
419 CurrSLI->addRange(SLR);
420 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
421 SE = MBB->succ_end(); SI != SE; ++SI)
422 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000423 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000424 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000425 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000426 }
Evan Cheng54898932008-10-29 08:39:34 +0000427 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000428 }
429 }
430}
431
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000432/// PerformPHIConstruction - From properly set up use and def lists, use a PHI
433/// construction algorithm to compute the ranges and valnos for an interval.
434VNInfo* PreAllocSplitting::PerformPHIConstruction(
435 MachineBasicBlock::iterator use,
Owen Anderson7d211e22008-12-31 02:00:25 +0000436 MachineBasicBlock* MBB,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000437 LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000438 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000439 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
440 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
441 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000442 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
443 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
444 bool toplevel, bool intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000445 // Return memoized result if it's available.
Owen Anderson200ee7f2009-01-06 07:53:32 +0000446 if (toplevel && Visited.count(use) && NewVNs.count(use))
447 return NewVNs[use];
448 else if (!toplevel && intrablock && NewVNs.count(use))
Owen Anderson7d211e22008-12-31 02:00:25 +0000449 return NewVNs[use];
450 else if (!intrablock && LiveOut.count(MBB))
451 return LiveOut[MBB];
452
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000453 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
454
455 // Check if our block contains any uses or defs.
Owen Anderson7d211e22008-12-31 02:00:25 +0000456 bool ContainsDefs = Defs.count(MBB);
457 bool ContainsUses = Uses.count(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000458
459 VNInfo* ret = 0;
460
461 // Enumerate the cases of use/def contaning blocks.
462 if (!ContainsDefs && !ContainsUses) {
463 Fallback:
464 // NOTE: Because this is the fallback case from other cases, we do NOT
Owen Anderson7d211e22008-12-31 02:00:25 +0000465 // assume that we are not intrablock here.
466 if (Phis.count(MBB)) return Phis[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000467
Owen Anderson7d211e22008-12-31 02:00:25 +0000468 unsigned StartIndex = LIs->getMBBStartIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000469
Owen Anderson5caedc02009-02-01 07:06:00 +0000470 Phis[MBB] = ret = LI->getNextValue(~0U, /*FIXME*/ 0,
Owen Anderson7d211e22008-12-31 02:00:25 +0000471 LIs->getVNInfoAllocator());
Owen Anderson5caedc02009-02-01 07:06:00 +0000472 if (!intrablock) LiveOut[MBB] = ret;
Owen Anderson7d211e22008-12-31 02:00:25 +0000473
Owen Anderson5caedc02009-02-01 07:06:00 +0000474 // If there are no uses or defs between our starting point and the
475 // beginning of the block, then recursive perform phi construction
476 // on our predecessors.
477 DenseMap<MachineBasicBlock*, VNInfo*> IncomingVNs;
478 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
479 PE = MBB->pred_end(); PI != PE; ++PI) {
480 VNInfo* Incoming = PerformPHIConstruction((*PI)->end(), *PI, LI,
481 Visited, Defs, Uses, NewVNs,
482 LiveOut, Phis, false, false);
483 if (Incoming != 0)
484 IncomingVNs[*PI] = Incoming;
485 }
Owen Anderson7d211e22008-12-31 02:00:25 +0000486
Owen Anderson5caedc02009-02-01 07:06:00 +0000487 if (MBB->pred_size() == 1 && !ret->hasPHIKill) {
488 LI->MergeValueNumberInto(ret, IncomingVNs.begin()->second);
Owen Anderson4a6d13e2009-02-01 08:41:54 +0000489 Phis[MBB] = ret = IncomingVNs.begin()->second;
Owen Anderson5caedc02009-02-01 07:06:00 +0000490 } else {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000491 // Otherwise, merge the incoming VNInfos with a phi join. Create a new
492 // VNInfo to represent the joined value.
493 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator I =
494 IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) {
495 I->second->hasPHIKill = true;
496 unsigned KillIndex = LIs->getMBBEndIdx(I->first);
Owen Andersonc0f3a032009-01-29 08:22:06 +0000497 if (!LiveInterval::isKill(I->second, KillIndex))
498 LI->addKill(I->second, KillIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000499 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000500 }
Owen Anderson5caedc02009-02-01 07:06:00 +0000501
502 unsigned EndIndex = 0;
503 if (intrablock) {
504 EndIndex = LIs->getInstructionIndex(use);
505 EndIndex = LiveIntervals::getUseIndex(EndIndex);
506 } else
507 EndIndex = LIs->getMBBEndIdx(MBB);
508 LI->addRange(LiveRange(StartIndex, EndIndex+1, ret));
509 if (intrablock)
510 LI->addKill(ret, EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000511 } else if (ContainsDefs && !ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000512 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000513
514 // Search for the def in this block. If we don't find it before the
515 // instruction we care about, go to the fallback case. Note that that
Owen Anderson7d211e22008-12-31 02:00:25 +0000516 // should never happen: this cannot be intrablock, so use should
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000517 // always be an end() iterator.
Owen Anderson7d211e22008-12-31 02:00:25 +0000518 assert(use == MBB->end() && "No use marked in intrablock");
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000519
520 MachineBasicBlock::iterator walker = use;
521 --walker;
Owen Anderson7d211e22008-12-31 02:00:25 +0000522 while (walker != MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000523 if (BlockDefs.count(walker)) {
524 break;
525 } else
526 --walker;
527
528 // Once we've found it, extend its VNInfo to our instruction.
529 unsigned DefIndex = LIs->getInstructionIndex(walker);
530 DefIndex = LiveIntervals::getDefIndex(DefIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +0000531 unsigned EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000532
533 ret = NewVNs[walker];
Owen Anderson7d211e22008-12-31 02:00:25 +0000534 LI->addRange(LiveRange(DefIndex, EndIndex+1, ret));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000535 } else if (!ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000536 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000537
538 // Search for the use in this block that precedes the instruction we care
539 // about, going to the fallback case if we don't find it.
540
Owen Anderson7d211e22008-12-31 02:00:25 +0000541 if (use == MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000542 goto Fallback;
543
544 MachineBasicBlock::iterator walker = use;
545 --walker;
546 bool found = false;
Owen Anderson7d211e22008-12-31 02:00:25 +0000547 while (walker != MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000548 if (BlockUses.count(walker)) {
549 found = true;
550 break;
551 } else
552 --walker;
553
554 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000555 if (!found) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000556 if (BlockUses.count(walker))
557 found = true;
558 else
559 goto Fallback;
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000560 }
561
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000562 unsigned UseIndex = LIs->getInstructionIndex(walker);
563 UseIndex = LiveIntervals::getUseIndex(UseIndex);
564 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000565 if (intrablock) {
566 EndIndex = LIs->getInstructionIndex(use);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000567 EndIndex = LiveIntervals::getUseIndex(EndIndex);
568 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000569 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000570
571 // Now, recursively phi construct the VNInfo for the use we found,
572 // and then extend it to include the instruction we care about
Owen Anderson200ee7f2009-01-06 07:53:32 +0000573 ret = PerformPHIConstruction(walker, MBB, LI, Visited, Defs, Uses,
Owen Anderson7d211e22008-12-31 02:00:25 +0000574 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000575
Owen Andersonb4b84362009-01-26 21:57:31 +0000576 LI->addRange(LiveRange(UseIndex, EndIndex+1, ret));
577
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000578 // FIXME: Need to set kills properly for inter-block stuff.
Owen Andersond4f6fe52008-12-28 23:35:13 +0000579 if (LI->isKill(ret, UseIndex)) LI->removeKill(ret, UseIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +0000580 if (intrablock)
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000581 LI->addKill(ret, EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000582 } else if (ContainsDefs && ContainsUses){
Owen Anderson7d211e22008-12-31 02:00:25 +0000583 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
584 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000585
586 // This case is basically a merging of the two preceding case, with the
587 // special note that checking for defs must take precedence over checking
588 // for uses, because of two-address instructions.
589
Owen Anderson7d211e22008-12-31 02:00:25 +0000590 if (use == MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000591 goto Fallback;
592
593 MachineBasicBlock::iterator walker = use;
594 --walker;
595 bool foundDef = false;
596 bool foundUse = false;
Owen Anderson7d211e22008-12-31 02:00:25 +0000597 while (walker != MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000598 if (BlockDefs.count(walker)) {
599 foundDef = true;
600 break;
601 } else if (BlockUses.count(walker)) {
602 foundUse = true;
603 break;
604 } else
605 --walker;
606
607 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000608 if (!foundDef && !foundUse) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000609 if (BlockDefs.count(walker))
610 foundDef = true;
611 else if (BlockUses.count(walker))
612 foundUse = true;
613 else
614 goto Fallback;
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000615 }
616
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000617 unsigned StartIndex = LIs->getInstructionIndex(walker);
618 StartIndex = foundDef ? LiveIntervals::getDefIndex(StartIndex) :
619 LiveIntervals::getUseIndex(StartIndex);
620 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000621 if (intrablock) {
622 EndIndex = LIs->getInstructionIndex(use);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000623 EndIndex = LiveIntervals::getUseIndex(EndIndex);
624 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000625 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000626
627 if (foundDef)
628 ret = NewVNs[walker];
629 else
Owen Anderson200ee7f2009-01-06 07:53:32 +0000630 ret = PerformPHIConstruction(walker, MBB, LI, Visited, Defs, Uses,
Owen Anderson7d211e22008-12-31 02:00:25 +0000631 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000632
Owen Andersonb4b84362009-01-26 21:57:31 +0000633 LI->addRange(LiveRange(StartIndex, EndIndex+1, ret));
634
Owen Andersond4f6fe52008-12-28 23:35:13 +0000635 if (foundUse && LI->isKill(ret, StartIndex))
636 LI->removeKill(ret, StartIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +0000637 if (intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000638 LI->addKill(ret, EndIndex);
639 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000640 }
641
642 // Memoize results so we don't have to recompute them.
Owen Anderson7d211e22008-12-31 02:00:25 +0000643 if (!intrablock) LiveOut[MBB] = ret;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000644 else {
Owen Andersone1762c92009-01-12 03:10:40 +0000645 if (!NewVNs.count(use))
646 NewVNs[use] = ret;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000647 Visited.insert(use);
648 }
649
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000650 return ret;
651}
652
653/// ReconstructLiveInterval - Recompute a live interval from scratch.
654void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) {
655 BumpPtrAllocator& Alloc = LIs->getVNInfoAllocator();
656
657 // Clear the old ranges and valnos;
658 LI->clear();
659
660 // Cache the uses and defs of the register
661 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
662 RegMap Defs, Uses;
663
664 // Keep track of the new VNs we're creating.
665 DenseMap<MachineInstr*, VNInfo*> NewVNs;
666 SmallPtrSet<VNInfo*, 2> PhiVNs;
667
668 // Cache defs, and create a new VNInfo for each def.
669 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
670 DE = MRI->def_end(); DI != DE; ++DI) {
671 Defs[(*DI).getParent()].insert(&*DI);
672
673 unsigned DefIdx = LIs->getInstructionIndex(&*DI);
674 DefIdx = LiveIntervals::getDefIndex(DefIdx);
675
Owen Anderson200ee7f2009-01-06 07:53:32 +0000676 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, Alloc);
677
678 // If the def is a move, set the copy field.
Evan Cheng04ee5a12009-01-20 19:12:24 +0000679 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
680 if (TII->isMoveInstr(*DI, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
681 if (DstReg == LI->reg)
Owen Anderson200ee7f2009-01-06 07:53:32 +0000682 NewVN->copy = &*DI;
683
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000684 NewVNs[&*DI] = NewVN;
685 }
686
687 // Cache uses as a separate pass from actually processing them.
688 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
689 UE = MRI->use_end(); UI != UE; ++UI)
690 Uses[(*UI).getParent()].insert(&*UI);
691
692 // Now, actually process every use and use a phi construction algorithm
693 // to walk from it to its reaching definitions, building VNInfos along
694 // the way.
Owen Anderson7d211e22008-12-31 02:00:25 +0000695 DenseMap<MachineBasicBlock*, VNInfo*> LiveOut;
696 DenseMap<MachineBasicBlock*, VNInfo*> Phis;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000697 SmallPtrSet<MachineInstr*, 4> Visited;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000698 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
699 UE = MRI->use_end(); UI != UE; ++UI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000700 PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000701 Uses, NewVNs, LiveOut, Phis, true, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000702 }
Owen Andersond4f6fe52008-12-28 23:35:13 +0000703
704 // Add ranges for dead defs
705 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
706 DE = MRI->def_end(); DI != DE; ++DI) {
707 unsigned DefIdx = LIs->getInstructionIndex(&*DI);
708 DefIdx = LiveIntervals::getDefIndex(DefIdx);
Owen Andersond4f6fe52008-12-28 23:35:13 +0000709
710 if (LI->liveAt(DefIdx)) continue;
711
712 VNInfo* DeadVN = NewVNs[&*DI];
Owen Anderson7d211e22008-12-31 02:00:25 +0000713 LI->addRange(LiveRange(DefIdx, DefIdx+1, DeadVN));
Owen Andersond4f6fe52008-12-28 23:35:13 +0000714 LI->addKill(DeadVN, DefIdx);
715 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000716}
717
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000718/// RenumberValno - Split the given valno out into a new vreg, allowing it to
719/// be allocated to a different register. This function creates a new vreg,
720/// copies the valno and its live ranges over to the new vreg's interval,
721/// removes them from the old interval, and rewrites all uses and defs of
722/// the original reg to the new vreg within those ranges.
723void PreAllocSplitting::RenumberValno(VNInfo* VN) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000724 SmallVector<VNInfo*, 4> Stack;
725 SmallVector<VNInfo*, 4> VNsToCopy;
726 Stack.push_back(VN);
727
728 // Walk through and copy the valno we care about, and any other valnos
729 // that are two-address redefinitions of the one we care about. These
730 // will need to be rewritten as well. We also check for safety of the
731 // renumbering here, by making sure that none of the valno involved has
732 // phi kills.
733 while (!Stack.empty()) {
734 VNInfo* OldVN = Stack.back();
735 Stack.pop_back();
736
737 // Bail out if we ever encounter a valno that has a PHI kill. We can't
738 // renumber these.
739 if (OldVN->hasPHIKill) return;
740
741 VNsToCopy.push_back(OldVN);
742
743 // Locate two-address redefinitions
744 for (SmallVector<unsigned, 4>::iterator KI = OldVN->kills.begin(),
745 KE = OldVN->kills.end(); KI != KE; ++KI) {
746 MachineInstr* MI = LIs->getInstructionFromIndex(*KI);
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000747 unsigned DefIdx = MI->findRegisterDefOperandIdx(CurrLI->reg);
748 if (DefIdx == ~0U) continue;
749 if (MI->isRegReDefinedByTwoAddr(DefIdx)) {
750 VNInfo* NextVN =
751 CurrLI->findDefinedVNInfo(LiveIntervals::getDefIndex(*KI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000752 if (NextVN == OldVN) continue;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000753 Stack.push_back(NextVN);
754 }
755 }
756 }
757
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000758 // Create the new vreg
759 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
760
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000761 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000762 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000763
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000764 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
765 VNsToCopy.end(); OI != OE; ++OI) {
766 VNInfo* OldVN = *OI;
767
768 // Copy the valno over
769 VNInfo* NewVN = NewLI.getNextValue(OldVN->def, OldVN->copy,
770 LIs->getVNInfoAllocator());
771 NewLI.copyValNumInfo(NewVN, OldVN);
772 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
773
774 // Remove the valno from the old interval
775 CurrLI->removeValNo(OldVN);
776 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000777
778 // Rewrite defs and uses. This is done in two stages to avoid invalidating
779 // the reg_iterator.
780 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
781
782 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
783 E = MRI->reg_end(); I != E; ++I) {
784 MachineOperand& MO = I.getOperand();
785 unsigned InstrIdx = LIs->getInstructionIndex(&*I);
786
787 if ((MO.isUse() && NewLI.liveAt(LiveIntervals::getUseIndex(InstrIdx))) ||
788 (MO.isDef() && NewLI.liveAt(LiveIntervals::getDefIndex(InstrIdx))))
789 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
790 }
791
792 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
793 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
794 MachineInstr* Inst = I->first;
795 unsigned OpIdx = I->second;
796 MachineOperand& MO = Inst->getOperand(OpIdx);
797 MO.setReg(NewVReg);
798 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000799
Owen Anderson45e68552009-01-29 05:28:55 +0000800 // The renumbered vreg shares a stack slot with the old register.
801 if (IntervalSSMap.count(CurrLI->reg))
802 IntervalSSMap[NewVReg] = IntervalSSMap[CurrLI->reg];
803
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000804 NumRenumbers++;
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000805}
806
Owen Anderson6002e992008-12-04 21:20:30 +0000807bool PreAllocSplitting::Rematerialize(unsigned vreg, VNInfo* ValNo,
808 MachineInstr* DefMI,
809 MachineBasicBlock::iterator RestorePt,
810 unsigned RestoreIdx,
811 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
812 MachineBasicBlock& MBB = *RestorePt->getParent();
813
814 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
815 unsigned KillIdx = 0;
816 if (ValNo->def == ~0U || DefMI->getParent() == BarrierMBB)
817 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, KillIdx);
818 else
819 KillPt = findNextEmptySlot(DefMI->getParent(), DefMI, KillIdx);
820
821 if (KillPt == DefMI->getParent()->end())
822 return false;
823
824 TII->reMaterialize(MBB, RestorePt, vreg, DefMI);
825 LIs->InsertMachineInstrInMaps(prior(RestorePt), RestoreIdx);
826
Owen Andersonb4b84362009-01-26 21:57:31 +0000827 ReconstructLiveInterval(CurrLI);
828 unsigned RematIdx = LIs->getInstructionIndex(prior(RestorePt));
829 RematIdx = LiveIntervals::getDefIndex(RematIdx);
830 RenumberValno(CurrLI->findDefinedVNInfo(RematIdx));
Owen Andersone1762c92009-01-12 03:10:40 +0000831
Owen Anderson75fa96b2008-11-19 04:28:29 +0000832 ++NumSplits;
833 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000834 return true;
835}
836
837MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
838 const TargetRegisterClass* RC,
839 MachineInstr* DefMI,
840 MachineInstr* Barrier,
841 MachineBasicBlock* MBB,
842 int& SS,
843 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
844 MachineBasicBlock::iterator Pt = MBB->begin();
845
846 // Go top down if RefsInMBB is empty.
847 if (RefsInMBB.empty())
848 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000849
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000850 MachineBasicBlock::iterator FoldPt = Barrier;
851 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
852 !RefsInMBB.count(FoldPt))
853 --FoldPt;
854
855 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg, false);
856 if (OpIdx == -1)
857 return 0;
858
859 SmallVector<unsigned, 1> Ops;
860 Ops.push_back(OpIdx);
861
862 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
863 return 0;
864
865 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
866 if (I != IntervalSSMap.end()) {
867 SS = I->second;
868 } else {
869 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
870
871 }
872
873 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
874 FoldPt, Ops, SS);
875
876 if (FMI) {
877 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
878 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
879 ++NumFolds;
880
881 IntervalSSMap[vreg] = SS;
882 CurrSLI = &LSs->getOrCreateInterval(SS);
883 if (CurrSLI->hasAtLeastOneValue())
884 CurrSValNo = CurrSLI->getValNumInfo(0);
885 else
886 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
887 }
888
889 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000890}
891
Evan Chengf5cd4f02008-10-23 20:43:13 +0000892/// SplitRegLiveInterval - Split (spill and restore) the given live interval
893/// so it would not cross the barrier that's being processed. Shrink wrap
894/// (minimize) the live interval to the last uses.
895bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
896 CurrLI = LI;
897
898 // Find live range where current interval cross the barrier.
899 LiveInterval::iterator LR =
900 CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx));
901 VNInfo *ValNo = LR->valno;
902
903 if (ValNo->def == ~1U) {
904 // Defined by a dead def? How can this be?
905 assert(0 && "Val# is defined by a dead def?");
906 abort();
907 }
908
Evan Cheng06587492008-10-24 02:05:00 +0000909 MachineInstr *DefMI = (ValNo->def != ~0U)
910 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
Evan Cheng06587492008-10-24 02:05:00 +0000911
Owen Andersond3be4622009-01-21 08:18:03 +0000912 // If this would create a new join point, do not split.
913 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent()))
914 return false;
915
Evan Chengf5cd4f02008-10-23 20:43:13 +0000916 // Find all references in the barrier mbb.
917 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
918 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
919 E = MRI->reg_end(); I != E; ++I) {
920 MachineInstr *RefMI = &*I;
921 if (RefMI->getParent() == BarrierMBB)
922 RefsInMBB.insert(RefMI);
923 }
924
925 // Find a point to restore the value after the barrier.
Evan Chengb964f332009-01-26 18:33:51 +0000926 unsigned RestoreIndex = 0;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000927 MachineBasicBlock::iterator RestorePt =
Evan Chengf62ce372008-10-28 00:47:49 +0000928 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000929 if (RestorePt == BarrierMBB->end())
930 return false;
931
Owen Anderson75fa96b2008-11-19 04:28:29 +0000932 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
933 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt,
934 RestoreIndex, RefsInMBB))
935 return true;
936
Evan Chengf5cd4f02008-10-23 20:43:13 +0000937 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +0000938 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000939 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000940 unsigned SpillIndex = 0;
Evan Cheng06587492008-10-24 02:05:00 +0000941 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +0000942 int SS = -1;
Evan Cheng78dfef72008-10-25 00:52:41 +0000943 if (ValNo->def == ~0U) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000944 // If it's defined by a phi, we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000945 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
946 BarrierMBB, SS, RefsInMBB))) {
947 SpillIndex = LIs->getInstructionIndex(SpillMI);
948 } else {
949 MachineBasicBlock::iterator SpillPt =
950 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex);
951 if (SpillPt == BarrierMBB->begin())
952 return false; // No gap to insert spill.
953 // Add spill.
954
955 SS = CreateSpillStackSlot(CurrLI->reg, RC);
956 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
957 SpillMI = prior(SpillPt);
958 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
959 }
Evan Cheng54898932008-10-29 08:39:34 +0000960 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
961 RestoreIndex, SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +0000962 // If it's already split, just restore the value. There is no need to spill
963 // the def again.
Evan Chengd0e32c52008-10-29 05:06:14 +0000964 if (!DefMI)
965 return false; // Def is dead. Do nothing.
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000966
967 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
968 BarrierMBB, SS, RefsInMBB))) {
969 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +0000970 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000971 // Check if it's possible to insert a spill after the def MI.
972 MachineBasicBlock::iterator SpillPt;
973 if (DefMBB == BarrierMBB) {
974 // Add spill after the def and the last use before the barrier.
975 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
976 RefsInMBB, SpillIndex);
977 if (SpillPt == DefMBB->begin())
978 return false; // No gap to insert spill.
979 } else {
980 SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex);
981 if (SpillPt == DefMBB->end())
982 return false; // No gap to insert spill.
983 }
984 // Add spill. The store instruction kills the register if def is before
985 // the barrier in the barrier block.
986 SS = CreateSpillStackSlot(CurrLI->reg, RC);
987 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg,
988 DefMBB == BarrierMBB, SS, RC);
989 SpillMI = prior(SpillPt);
990 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Cheng1f08cc22008-10-28 05:28:21 +0000991 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000992 }
993
Evan Cheng54898932008-10-29 08:39:34 +0000994 // Remember def instruction index to spill index mapping.
995 if (DefMI && SpillMI)
996 Def2SpillMap[ValNo->def] = SpillIndex;
997
Evan Chengf5cd4f02008-10-23 20:43:13 +0000998 // Add restore.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000999 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
1000 MachineInstr *LoadMI = prior(RestorePt);
1001 LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex);
1002
Evan Chengd0e32c52008-10-29 05:06:14 +00001003 // Update spill stack slot live interval.
Evan Cheng54898932008-10-29 08:39:34 +00001004 UpdateSpillSlotInterval(ValNo, LIs->getUseIndex(SpillIndex)+1,
1005 LIs->getDefIndex(RestoreIndex));
Evan Chengd0e32c52008-10-29 05:06:14 +00001006
Owen Andersonb4b84362009-01-26 21:57:31 +00001007 ReconstructLiveInterval(CurrLI);
1008 unsigned RestoreIdx = LIs->getInstructionIndex(prior(RestorePt));
1009 RestoreIdx = LiveIntervals::getDefIndex(RestoreIdx);
1010 RenumberValno(CurrLI->findDefinedVNInfo(RestoreIdx));
Owen Anderson7d211e22008-12-31 02:00:25 +00001011
Evan Chengae7fa5b2008-10-28 01:48:24 +00001012 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001013 return true;
1014}
1015
1016/// SplitRegLiveIntervals - Split all register live intervals that cross the
1017/// barrier that's being processed.
1018bool
Owen Anderson956ec272009-01-23 00:23:32 +00001019PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs,
1020 SmallPtrSet<LiveInterval*, 8>& Split) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001021 // First find all the virtual registers whose live intervals are intercepted
1022 // by the current barrier.
1023 SmallVector<LiveInterval*, 8> Intervals;
1024 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng23066282008-10-27 07:14:50 +00001025 if (TII->IgnoreRegisterClassBarriers(*RC))
1026 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001027 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
1028 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1029 unsigned Reg = VRs[i];
1030 if (!LIs->hasInterval(Reg))
1031 continue;
1032 LiveInterval *LI = &LIs->getInterval(Reg);
1033 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1034 // Virtual register live interval is intercepted by the barrier. We
1035 // should split and shrink wrap its interval if possible.
1036 Intervals.push_back(LI);
1037 }
1038 }
1039
1040 // Process the affected live intervals.
1041 bool Change = false;
1042 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001043 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1044 break;
Owen Andersone1762c92009-01-12 03:10:40 +00001045 else if (NumSplits == 4)
1046 Change |= Change;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001047 LiveInterval *LI = Intervals.back();
1048 Intervals.pop_back();
Owen Anderson956ec272009-01-23 00:23:32 +00001049 bool result = SplitRegLiveInterval(LI);
1050 if (result) Split.insert(LI);
1051 Change |= result;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001052 }
1053
1054 return Change;
1055}
1056
Owen Anderson45e68552009-01-29 05:28:55 +00001057unsigned PreAllocSplitting::getNumberOfNonSpills(
Owen Anderson32ca8652009-01-24 10:07:43 +00001058 SmallPtrSet<MachineInstr*, 4>& MIs,
Owen Anderson45e68552009-01-29 05:28:55 +00001059 unsigned Reg, int FrameIndex,
1060 bool& FeedsTwoAddr) {
1061 unsigned NonSpills = 0;
Owen Anderson32ca8652009-01-24 10:07:43 +00001062 for (SmallPtrSet<MachineInstr*, 4>::iterator UI = MIs.begin(), UE = MIs.end();
Owen Anderson45e68552009-01-29 05:28:55 +00001063 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001064 int StoreFrameIndex;
1065 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001066 if (StoreVReg != Reg || StoreFrameIndex != FrameIndex)
1067 NonSpills++;
1068
1069 int DefIdx = (*UI)->findRegisterDefOperandIdx(Reg);
1070 if (DefIdx != -1 && (*UI)->isRegReDefinedByTwoAddr(DefIdx))
1071 FeedsTwoAddr = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001072 }
1073
Owen Anderson45e68552009-01-29 05:28:55 +00001074 return NonSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001075}
1076
Owen Anderson956ec272009-01-23 00:23:32 +00001077/// removeDeadSpills - After doing splitting, filter through all intervals we've
1078/// split, and see if any of the spills are unnecessary. If so, remove them.
1079bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) {
1080 bool changed = false;
1081
Owen Anderson4bfc2092009-01-29 05:41:02 +00001082 // Walk over all of the live intervals that were touched by the splitter,
1083 // and see if we can do any DCE and/or folding.
Owen Anderson956ec272009-01-23 00:23:32 +00001084 for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(),
1085 LE = split.end(); LI != LE; ++LI) {
Owen Anderson9ce499a2009-01-23 03:28:53 +00001086 DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> > VNUseCount;
Owen Anderson956ec272009-01-23 00:23:32 +00001087
Owen Anderson4bfc2092009-01-29 05:41:02 +00001088 // First, collect all the uses of the vreg, and sort them by their
1089 // reaching definition (VNInfo).
Owen Anderson956ec272009-01-23 00:23:32 +00001090 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg),
1091 UE = MRI->use_end(); UI != UE; ++UI) {
1092 unsigned index = LIs->getInstructionIndex(&*UI);
1093 index = LiveIntervals::getUseIndex(index);
1094
1095 const LiveRange* LR = (*LI)->getLiveRangeContaining(index);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001096 VNUseCount[LR->valno].insert(&*UI);
Owen Anderson956ec272009-01-23 00:23:32 +00001097 }
1098
Owen Anderson4bfc2092009-01-29 05:41:02 +00001099 // Now, take the definitions (VNInfo's) one at a time and try to DCE
1100 // and/or fold them away.
Owen Anderson956ec272009-01-23 00:23:32 +00001101 for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(),
1102 VE = (*LI)->vni_end(); VI != VE; ++VI) {
Owen Anderson45e68552009-01-29 05:28:55 +00001103
1104 if (DeadSplitLimit != -1 && (int)NumDeadSpills == DeadSplitLimit)
1105 return changed;
1106
Owen Anderson956ec272009-01-23 00:23:32 +00001107 VNInfo* CurrVN = *VI;
Owen Anderson4bfc2092009-01-29 05:41:02 +00001108
1109 // We don't currently try to handle definitions with PHI kills, because
1110 // it would involve processing more than one VNInfo at once.
Owen Anderson956ec272009-01-23 00:23:32 +00001111 if (CurrVN->hasPHIKill) continue;
Owen Anderson956ec272009-01-23 00:23:32 +00001112
Owen Anderson4bfc2092009-01-29 05:41:02 +00001113 // We also don't try to handle the results of PHI joins, since there's
1114 // no defining instruction to analyze.
Owen Anderson956ec272009-01-23 00:23:32 +00001115 unsigned DefIdx = CurrVN->def;
1116 if (DefIdx == ~0U || DefIdx == ~1U) continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001117
Owen Anderson4bfc2092009-01-29 05:41:02 +00001118 // We're only interested in eliminating cruft introduced by the splitter,
1119 // is of the form load-use or load-use-store. First, check that the
1120 // definition is a load, and remember what stack slot we loaded it from.
Owen Anderson956ec272009-01-23 00:23:32 +00001121 MachineInstr* DefMI = LIs->getInstructionFromIndex(DefIdx);
1122 int FrameIndex;
1123 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
1124
Owen Anderson4bfc2092009-01-29 05:41:02 +00001125 // If the definition has no uses at all, just DCE it.
Owen Anderson9ce499a2009-01-23 03:28:53 +00001126 if (VNUseCount[CurrVN].size() == 0) {
1127 LIs->RemoveMachineInstrFromMaps(DefMI);
1128 (*LI)->removeValNo(CurrVN);
1129 DefMI->eraseFromParent();
Owen Andersonc0f3a032009-01-29 08:22:06 +00001130 VNUseCount.erase(CurrVN);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001131 NumDeadSpills++;
1132 changed = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001133 continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001134 }
Owen Anderson32ca8652009-01-24 10:07:43 +00001135
Owen Anderson4bfc2092009-01-29 05:41:02 +00001136 // Second, get the number of non-store uses of the definition, as well as
1137 // a flag indicating whether it feeds into a later two-address definition.
Owen Anderson45e68552009-01-29 05:28:55 +00001138 bool FeedsTwoAddr = false;
1139 unsigned NonSpillCount = getNumberOfNonSpills(VNUseCount[CurrVN],
1140 (*LI)->reg, FrameIndex,
1141 FeedsTwoAddr);
1142
Owen Anderson4bfc2092009-01-29 05:41:02 +00001143 // If there's one non-store use and it doesn't feed a two-addr, then
1144 // this is a load-use-store case that we can try to fold.
Owen Anderson45e68552009-01-29 05:28:55 +00001145 if (NonSpillCount == 1 && !FeedsTwoAddr) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001146 // Start by finding the non-store use MachineInstr.
Owen Anderson45e68552009-01-29 05:28:55 +00001147 SmallPtrSet<MachineInstr*, 4>::iterator UI = VNUseCount[CurrVN].begin();
1148 int StoreFrameIndex;
1149 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1150 while (UI != VNUseCount[CurrVN].end() &&
1151 (StoreVReg == (*LI)->reg && StoreFrameIndex == FrameIndex)) {
1152 ++UI;
1153 if (UI != VNUseCount[CurrVN].end())
1154 StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1155 }
Owen Anderson45e68552009-01-29 05:28:55 +00001156 if (UI == VNUseCount[CurrVN].end()) continue;
1157
1158 MachineInstr* use = *UI;
1159
Owen Anderson4bfc2092009-01-29 05:41:02 +00001160 // Attempt to fold it away!
Owen Anderson45e68552009-01-29 05:28:55 +00001161 int OpIdx = use->findRegisterUseOperandIdx((*LI)->reg, false);
1162 if (OpIdx == -1) continue;
Owen Anderson45e68552009-01-29 05:28:55 +00001163 SmallVector<unsigned, 1> Ops;
1164 Ops.push_back(OpIdx);
Owen Anderson45e68552009-01-29 05:28:55 +00001165 if (!TII->canFoldMemoryOperand(use, Ops)) continue;
1166
1167 MachineInstr* NewMI =
1168 TII->foldMemoryOperand(*use->getParent()->getParent(),
1169 use, Ops, FrameIndex);
1170
1171 if (!NewMI) continue;
1172
Owen Anderson4bfc2092009-01-29 05:41:02 +00001173 // Update relevant analyses.
Owen Anderson45e68552009-01-29 05:28:55 +00001174 LIs->RemoveMachineInstrFromMaps(DefMI);
1175 LIs->ReplaceMachineInstrInMaps(use, NewMI);
1176 (*LI)->removeValNo(CurrVN);
1177
1178 DefMI->eraseFromParent();
1179 MachineBasicBlock* MBB = use->getParent();
1180 NewMI = MBB->insert(MBB->erase(use), NewMI);
1181 VNUseCount[CurrVN].erase(use);
1182
Owen Anderson4bfc2092009-01-29 05:41:02 +00001183 // Remove deleted instructions. Note that we need to remove them from
1184 // the VNInfo->use map as well, just to be safe.
Owen Anderson45e68552009-01-29 05:28:55 +00001185 for (SmallPtrSet<MachineInstr*, 4>::iterator II =
1186 VNUseCount[CurrVN].begin(), IE = VNUseCount[CurrVN].end();
1187 II != IE; ++II) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001188 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
Owen Andersonc0f3a032009-01-29 08:22:06 +00001189 VNI = VNUseCount.begin(), VNE = VNUseCount.end(); VNI != VNE;
1190 ++VNI)
1191 if (VNI->first != CurrVN)
1192 VNI->second.erase(*II);
Owen Anderson45e68552009-01-29 05:28:55 +00001193 LIs->RemoveMachineInstrFromMaps(*II);
1194 (*II)->eraseFromParent();
1195 }
Owen Andersonc0f3a032009-01-29 08:22:06 +00001196
1197 VNUseCount.erase(CurrVN);
Owen Anderson45e68552009-01-29 05:28:55 +00001198
1199 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
1200 VI = VNUseCount.begin(), VE = VNUseCount.end(); VI != VE; ++VI)
1201 if (VI->second.erase(use))
1202 VI->second.insert(NewMI);
1203
1204 NumDeadSpills++;
1205 changed = true;
1206 continue;
1207 }
1208
Owen Anderson4bfc2092009-01-29 05:41:02 +00001209 // If there's more than one non-store instruction, we can't profitably
1210 // fold it, so bail.
Owen Anderson45e68552009-01-29 05:28:55 +00001211 if (NonSpillCount) continue;
Owen Anderson32ca8652009-01-24 10:07:43 +00001212
Owen Anderson4bfc2092009-01-29 05:41:02 +00001213 // Otherwise, this is a load-store case, so DCE them.
Owen Anderson32ca8652009-01-24 10:07:43 +00001214 for (SmallPtrSet<MachineInstr*, 4>::iterator UI =
1215 VNUseCount[CurrVN].begin(), UE = VNUseCount[CurrVN].end();
1216 UI != UI; ++UI) {
1217 LIs->RemoveMachineInstrFromMaps(*UI);
1218 (*UI)->eraseFromParent();
1219 }
1220
Owen Andersonc0f3a032009-01-29 08:22:06 +00001221 VNUseCount.erase(CurrVN);
1222
Owen Anderson32ca8652009-01-24 10:07:43 +00001223 LIs->RemoveMachineInstrFromMaps(DefMI);
1224 (*LI)->removeValNo(CurrVN);
1225 DefMI->eraseFromParent();
1226 NumDeadSpills++;
1227 changed = true;
Owen Anderson956ec272009-01-23 00:23:32 +00001228 }
1229 }
1230
1231 return changed;
1232}
1233
Owen Andersonf1f75b12008-11-04 22:22:41 +00001234bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1235 MachineBasicBlock* DefMBB,
1236 MachineBasicBlock* BarrierMBB) {
1237 if (DefMBB == BarrierMBB)
1238 return false;
1239
1240 if (LR->valno->hasPHIKill)
1241 return false;
1242
1243 unsigned MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
1244 if (LR->end < MBBEnd)
1245 return false;
1246
1247 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1248 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1249 return true;
1250
1251 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1252 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1253 typedef std::pair<MachineBasicBlock*,
1254 MachineBasicBlock::succ_iterator> ItPair;
1255 SmallVector<ItPair, 4> Stack;
1256 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1257
1258 while (!Stack.empty()) {
1259 ItPair P = Stack.back();
1260 Stack.pop_back();
1261
1262 MachineBasicBlock* PredMBB = P.first;
1263 MachineBasicBlock::succ_iterator S = P.second;
1264
1265 if (S == PredMBB->succ_end())
1266 continue;
1267 else if (Visited.count(*S)) {
1268 Stack.push_back(std::make_pair(PredMBB, ++S));
1269 continue;
1270 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001271 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001272
1273 MachineBasicBlock* MBB = *S;
1274 Visited.insert(MBB);
1275
1276 if (MBB == BarrierMBB)
1277 return true;
1278
1279 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1280 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1281 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1282 while (MDTN) {
1283 if (MDTN == DefMDTN)
1284 return true;
1285 else if (MDTN == BarrierMDTN)
1286 break;
1287 MDTN = MDTN->getIDom();
1288 }
1289
1290 MBBEnd = LIs->getMBBEndIdx(MBB);
1291 if (LR->end > MBBEnd)
1292 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1293 }
1294
1295 return false;
1296}
1297
1298
Evan Cheng09e8ca82008-10-20 21:44:59 +00001299bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001300 CurrMF = &MF;
1301 TM = &MF.getTarget();
Owen Anderson3ef45492009-01-29 22:13:06 +00001302 TRI = TM->getRegisterInfo();
Evan Chengd0e32c52008-10-29 05:06:14 +00001303 TII = TM->getInstrInfo();
1304 MFI = MF.getFrameInfo();
1305 MRI = &MF.getRegInfo();
1306 LIs = &getAnalysis<LiveIntervals>();
1307 LSs = &getAnalysis<LiveStacks>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001308
1309 bool MadeChange = false;
1310
1311 // Make sure blocks are numbered in order.
1312 MF.RenumberBlocks();
1313
Evan Cheng54898932008-10-29 08:39:34 +00001314 MachineBasicBlock *Entry = MF.begin();
1315 SmallPtrSet<MachineBasicBlock*,16> Visited;
1316
Owen Anderson956ec272009-01-23 00:23:32 +00001317 SmallPtrSet<LiveInterval*, 8> Split;
1318
Evan Cheng54898932008-10-29 08:39:34 +00001319 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1320 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1321 DFI != E; ++DFI) {
1322 BarrierMBB = *DFI;
1323 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1324 E = BarrierMBB->end(); I != E; ++I) {
1325 Barrier = &*I;
1326 const TargetRegisterClass **BarrierRCs =
1327 Barrier->getDesc().getRegClassBarriers();
1328 if (!BarrierRCs)
1329 continue;
1330 BarrierIdx = LIs->getInstructionIndex(Barrier);
Owen Anderson956ec272009-01-23 00:23:32 +00001331 MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split);
Evan Cheng54898932008-10-29 08:39:34 +00001332 }
1333 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001334
Owen Anderson956ec272009-01-23 00:23:32 +00001335 MadeChange |= removeDeadSpills(Split);
1336
Evan Chengf5cd4f02008-10-23 20:43:13 +00001337 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001338}