blob: 4dc8b82972a026acf6922aaacd4eda11bf6ee3e8 [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;
53 MachineFrameInfo *MFI;
54 MachineRegisterInfo *MRI;
55 LiveIntervals *LIs;
Evan Chengd0e32c52008-10-29 05:06:14 +000056 LiveStacks *LSs;
Evan Cheng09e8ca82008-10-20 21:44:59 +000057
Evan Chengf5cd4f02008-10-23 20:43:13 +000058 // Barrier - Current barrier being processed.
59 MachineInstr *Barrier;
60
61 // BarrierMBB - Basic block where the barrier resides in.
62 MachineBasicBlock *BarrierMBB;
63
64 // Barrier - Current barrier index.
65 unsigned BarrierIdx;
66
67 // CurrLI - Current live interval being split.
68 LiveInterval *CurrLI;
69
Evan Chengd0e32c52008-10-29 05:06:14 +000070 // CurrSLI - Current stack slot live interval.
71 LiveInterval *CurrSLI;
72
73 // CurrSValNo - Current val# for the stack slot live interval.
74 VNInfo *CurrSValNo;
75
76 // IntervalSSMap - A map from live interval to spill slots.
77 DenseMap<unsigned, int> IntervalSSMap;
Evan Chengf5cd4f02008-10-23 20:43:13 +000078
Evan Cheng54898932008-10-29 08:39:34 +000079 // Def2SpillMap - A map from a def instruction index to spill index.
80 DenseMap<unsigned, unsigned> Def2SpillMap;
Evan Cheng06587492008-10-24 02:05:00 +000081
Evan Cheng09e8ca82008-10-20 21:44:59 +000082 public:
83 static char ID;
84 PreAllocSplitting() : MachineFunctionPass(&ID) {}
85
86 virtual bool runOnMachineFunction(MachineFunction &MF);
87
88 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
89 AU.addRequired<LiveIntervals>();
90 AU.addPreserved<LiveIntervals>();
Evan Chengd0e32c52008-10-29 05:06:14 +000091 AU.addRequired<LiveStacks>();
92 AU.addPreserved<LiveStacks>();
Evan Cheng09e8ca82008-10-20 21:44:59 +000093 AU.addPreserved<RegisterCoalescer>();
94 if (StrongPHIElim)
95 AU.addPreservedID(StrongPHIEliminationID);
96 else
97 AU.addPreservedID(PHIEliminationID);
Owen Andersonf1f75b12008-11-04 22:22:41 +000098 AU.addRequired<MachineDominatorTree>();
99 AU.addRequired<MachineLoopInfo>();
100 AU.addPreserved<MachineDominatorTree>();
101 AU.addPreserved<MachineLoopInfo>();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000102 MachineFunctionPass::getAnalysisUsage(AU);
103 }
104
105 virtual void releaseMemory() {
Evan Chengd0e32c52008-10-29 05:06:14 +0000106 IntervalSSMap.clear();
Evan Cheng54898932008-10-29 08:39:34 +0000107 Def2SpillMap.clear();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000108 }
109
110 virtual const char *getPassName() const {
111 return "Pre-Register Allocaton Live Interval Splitting";
112 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000113
114 /// print - Implement the dump method.
115 virtual void print(std::ostream &O, const Module* M = 0) const {
116 LIs->print(O, M);
117 }
118
119 void print(std::ostream *O, const Module* M = 0) const {
120 if (O) print(*O, M);
121 }
122
123 private:
124 MachineBasicBlock::iterator
125 findNextEmptySlot(MachineBasicBlock*, MachineInstr*,
126 unsigned&);
127
128 MachineBasicBlock::iterator
Evan Cheng1f08cc22008-10-28 05:28:21 +0000129 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000130 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
131
132 MachineBasicBlock::iterator
Evan Chengf62ce372008-10-28 00:47:49 +0000133 findRestorePoint(MachineBasicBlock*, MachineInstr*, unsigned,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000134 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
135
Evan Chengd0e32c52008-10-29 05:06:14 +0000136 int CreateSpillStackSlot(unsigned, const TargetRegisterClass *);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000137
Evan Cheng54898932008-10-29 08:39:34 +0000138 bool IsAvailableInStack(MachineBasicBlock*, unsigned, unsigned, unsigned,
139 unsigned&, int&) const;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000140
Evan Chengd0e32c52008-10-29 05:06:14 +0000141 void UpdateSpillSlotInterval(VNInfo*, unsigned, unsigned);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000142
Evan Chengf5cd4f02008-10-23 20:43:13 +0000143 bool SplitRegLiveInterval(LiveInterval*);
144
Owen Anderson956ec272009-01-23 00:23:32 +0000145 bool SplitRegLiveIntervals(const TargetRegisterClass **,
146 SmallPtrSet<LiveInterval*, 8>&);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000147
148 bool createsNewJoin(LiveRange* LR, MachineBasicBlock* DefMBB,
149 MachineBasicBlock* BarrierMBB);
Owen Anderson75fa96b2008-11-19 04:28:29 +0000150 bool Rematerialize(unsigned vreg, VNInfo* ValNo,
151 MachineInstr* DefMI,
152 MachineBasicBlock::iterator RestorePt,
153 unsigned RestoreIdx,
154 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000155 MachineInstr* FoldSpill(unsigned vreg, const TargetRegisterClass* RC,
156 MachineInstr* DefMI,
157 MachineInstr* Barrier,
158 MachineBasicBlock* MBB,
159 int& SS,
160 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000161 void RenumberValno(VNInfo* VN);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000162 void ReconstructLiveInterval(LiveInterval* LI);
Owen Anderson956ec272009-01-23 00:23:32 +0000163 bool removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split);
Owen Anderson45e68552009-01-29 05:28:55 +0000164 unsigned getNumberOfNonSpills(SmallPtrSet<MachineInstr*, 4>& MIs,
165 unsigned Reg, int FrameIndex, bool& TwoAddr);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000166 VNInfo* PerformPHIConstruction(MachineBasicBlock::iterator use,
Owen Anderson7d211e22008-12-31 02:00:25 +0000167 MachineBasicBlock* MBB,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000168 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,
175 bool toplevel, bool intrablock);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000176};
Evan Cheng09e8ca82008-10-20 21:44:59 +0000177} // end anonymous namespace
178
179char PreAllocSplitting::ID = 0;
180
181static RegisterPass<PreAllocSplitting>
182X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting");
183
184const PassInfo *const llvm::PreAllocSplittingID = &X;
185
Evan Chengf5cd4f02008-10-23 20:43:13 +0000186
187/// findNextEmptySlot - Find a gap after the given machine instruction in the
188/// instruction index map. If there isn't one, return end().
189MachineBasicBlock::iterator
190PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI,
191 unsigned &SpotIndex) {
192 MachineBasicBlock::iterator MII = MI;
193 if (++MII != MBB->end()) {
194 unsigned Index = LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII));
195 if (Index) {
196 SpotIndex = Index;
197 return MII;
198 }
199 }
200 return MBB->end();
201}
202
203/// findSpillPoint - Find a gap as far away from the given MI that's suitable
204/// for spilling the current live interval. The index must be before any
205/// defs and uses of the live interval register in the mbb. Return begin() if
206/// none is found.
207MachineBasicBlock::iterator
208PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000209 MachineInstr *DefMI,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000210 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
211 unsigned &SpillIndex) {
212 MachineBasicBlock::iterator Pt = MBB->begin();
213
214 // Go top down if RefsInMBB is empty.
Evan Cheng1f08cc22008-10-28 05:28:21 +0000215 if (RefsInMBB.empty() && !DefMI) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000216 MachineBasicBlock::iterator MII = MBB->begin();
217 MachineBasicBlock::iterator EndPt = MI;
218 do {
219 ++MII;
220 unsigned Index = LIs->getInstructionIndex(MII);
221 unsigned Gap = LIs->findGapBeforeInstr(Index);
222 if (Gap) {
223 Pt = MII;
224 SpillIndex = Gap;
225 break;
226 }
227 } while (MII != EndPt);
228 } else {
229 MachineBasicBlock::iterator MII = MI;
Evan Cheng1f08cc22008-10-28 05:28:21 +0000230 MachineBasicBlock::iterator EndPt = DefMI
231 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
232 while (MII != EndPt && !RefsInMBB.count(MII)) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000233 unsigned Index = LIs->getInstructionIndex(MII);
234 if (LIs->hasGapBeforeInstr(Index)) {
235 Pt = MII;
236 SpillIndex = LIs->findGapBeforeInstr(Index, true);
237 }
238 --MII;
239 }
240 }
241
242 return Pt;
243}
244
245/// findRestorePoint - Find a gap in the instruction index map that's suitable
246/// for restoring the current live interval value. The index must be before any
247/// uses of the live interval register in the mbb. Return end() if none is
248/// found.
249MachineBasicBlock::iterator
250PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Chengf62ce372008-10-28 00:47:49 +0000251 unsigned LastIdx,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000252 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
253 unsigned &RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000254 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
255 // begin index accordingly.
Owen Anderson5a92d4e2008-11-18 20:53:59 +0000256 MachineBasicBlock::iterator Pt = MBB->end();
Evan Chengf62ce372008-10-28 00:47:49 +0000257 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000258
Evan Chengf62ce372008-10-28 00:47:49 +0000259 // Go bottom up if RefsInMBB is empty and the end of the mbb isn't beyond
260 // the last index in the live range.
261 if (RefsInMBB.empty() && LastIdx >= EndIdx) {
Owen Anderson711fd3d2008-11-13 21:53:14 +0000262 MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000263 MachineBasicBlock::iterator EndPt = MI;
Evan Cheng54898932008-10-29 08:39:34 +0000264 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000265 do {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000266 unsigned Index = LIs->getInstructionIndex(MII);
Evan Cheng56ab0de2008-10-24 18:46:44 +0000267 unsigned Gap = LIs->findGapBeforeInstr(Index);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000268 if (Gap) {
269 Pt = MII;
270 RestoreIndex = Gap;
271 break;
272 }
Evan Cheng54898932008-10-29 08:39:34 +0000273 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000274 } while (MII != EndPt);
275 } else {
276 MachineBasicBlock::iterator MII = MI;
277 MII = ++MII;
Evan Chengf62ce372008-10-28 00:47:49 +0000278 // FIXME: Limit the number of instructions to examine to reduce
279 // compile time?
Owen Andersonc0f3a032009-01-29 08:22:06 +0000280 while (MII != MBB->getFirstTerminator()) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000281 unsigned Index = LIs->getInstructionIndex(MII);
Evan Chengf62ce372008-10-28 00:47:49 +0000282 if (Index > LastIdx)
283 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000284 unsigned Gap = LIs->findGapBeforeInstr(Index);
285 if (Gap) {
286 Pt = MII;
287 RestoreIndex = Gap;
288 }
289 if (RefsInMBB.count(MII))
290 break;
291 ++MII;
292 }
293 }
294
295 return Pt;
296}
297
Evan Chengd0e32c52008-10-29 05:06:14 +0000298/// CreateSpillStackSlot - Create a stack slot for the live interval being
299/// split. If the live interval was previously split, just reuse the same
300/// slot.
301int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
302 const TargetRegisterClass *RC) {
303 int SS;
304 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
305 if (I != IntervalSSMap.end()) {
306 SS = I->second;
307 } else {
308 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
309 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000310 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000311
312 // Create live interval for stack slot.
313 CurrSLI = &LSs->getOrCreateInterval(SS);
Evan Cheng54898932008-10-29 08:39:34 +0000314 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000315 CurrSValNo = CurrSLI->getValNumInfo(0);
316 else
317 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
318 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000319}
320
Evan Chengd0e32c52008-10-29 05:06:14 +0000321/// IsAvailableInStack - Return true if register is available in a split stack
322/// slot at the specified index.
323bool
Evan Cheng54898932008-10-29 08:39:34 +0000324PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
325 unsigned Reg, unsigned DefIndex,
326 unsigned RestoreIndex, unsigned &SpillIndex,
327 int& SS) const {
328 if (!DefMBB)
329 return false;
330
Evan Chengd0e32c52008-10-29 05:06:14 +0000331 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
332 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000333 return false;
Evan Cheng54898932008-10-29 08:39:34 +0000334 DenseMap<unsigned, unsigned>::iterator II = Def2SpillMap.find(DefIndex);
335 if (II == Def2SpillMap.end())
336 return false;
337
338 // If last spill of def is in the same mbb as barrier mbb (where restore will
339 // be), make sure it's not below the intended restore index.
340 // FIXME: Undo the previous spill?
341 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
342 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
343 return false;
344
345 SS = I->second;
346 SpillIndex = II->second;
347 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000348}
349
Evan Chengd0e32c52008-10-29 05:06:14 +0000350/// UpdateSpillSlotInterval - Given the specified val# of the register live
351/// interval being split, and the spill and restore indicies, update the live
352/// interval of the spill stack slot.
353void
354PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, unsigned SpillIndex,
355 unsigned RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000356 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
357 "Expect restore in the barrier mbb");
358
359 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
360 if (MBB == BarrierMBB) {
361 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000362 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
363 CurrSLI->addRange(SLR);
364 return;
365 }
366
Evan Cheng54898932008-10-29 08:39:34 +0000367 SmallPtrSet<MachineBasicBlock*, 4> Processed;
368 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
369 LiveRange SLR(SpillIndex, EndIdx+1, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000370 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000371 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000372
373 // Start from the spill mbb, figure out the extend of the spill slot's
374 // live interval.
375 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000376 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
377 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000378 // If live range extend beyond end of mbb, add successors to work list.
379 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
380 SE = MBB->succ_end(); SI != SE; ++SI)
381 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000382
383 while (!WorkList.empty()) {
384 MachineBasicBlock *MBB = WorkList.back();
385 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000386 if (Processed.count(MBB))
387 continue;
Evan Chengd0e32c52008-10-29 05:06:14 +0000388 unsigned Idx = LIs->getMBBStartIdx(MBB);
389 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000390 if (LR && LR->valno == ValNo) {
391 EndIdx = LIs->getMBBEndIdx(MBB);
392 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000393 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000394 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000395 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000396 } else if (LR->end > EndIdx) {
397 // Live range extends beyond end of mbb, process successors.
398 LiveRange SLR(Idx, EndIdx+1, CurrSValNo);
399 CurrSLI->addRange(SLR);
400 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
401 SE = MBB->succ_end(); SI != SE; ++SI)
402 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000403 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000404 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000405 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000406 }
Evan Cheng54898932008-10-29 08:39:34 +0000407 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000408 }
409 }
410}
411
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000412/// PerformPHIConstruction - From properly set up use and def lists, use a PHI
413/// construction algorithm to compute the ranges and valnos for an interval.
414VNInfo* PreAllocSplitting::PerformPHIConstruction(
415 MachineBasicBlock::iterator use,
Owen Anderson7d211e22008-12-31 02:00:25 +0000416 MachineBasicBlock* MBB,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000417 LiveInterval* LI,
Owen Anderson200ee7f2009-01-06 07:53:32 +0000418 SmallPtrSet<MachineInstr*, 4>& Visited,
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000419 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs,
420 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses,
421 DenseMap<MachineInstr*, VNInfo*>& NewVNs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000422 DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut,
423 DenseMap<MachineBasicBlock*, VNInfo*>& Phis,
424 bool toplevel, bool intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000425 // Return memoized result if it's available.
Owen Anderson200ee7f2009-01-06 07:53:32 +0000426 if (toplevel && Visited.count(use) && NewVNs.count(use))
427 return NewVNs[use];
428 else if (!toplevel && intrablock && NewVNs.count(use))
Owen Anderson7d211e22008-12-31 02:00:25 +0000429 return NewVNs[use];
430 else if (!intrablock && LiveOut.count(MBB))
431 return LiveOut[MBB];
432
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000433 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
434
435 // Check if our block contains any uses or defs.
Owen Anderson7d211e22008-12-31 02:00:25 +0000436 bool ContainsDefs = Defs.count(MBB);
437 bool ContainsUses = Uses.count(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000438
439 VNInfo* ret = 0;
440
441 // Enumerate the cases of use/def contaning blocks.
442 if (!ContainsDefs && !ContainsUses) {
443 Fallback:
444 // NOTE: Because this is the fallback case from other cases, we do NOT
Owen Anderson7d211e22008-12-31 02:00:25 +0000445 // assume that we are not intrablock here.
446 if (Phis.count(MBB)) return Phis[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000447
Owen Anderson7d211e22008-12-31 02:00:25 +0000448 unsigned StartIndex = LIs->getMBBStartIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000449
Owen Anderson7d211e22008-12-31 02:00:25 +0000450 if (MBB->pred_size() == 1) {
451 Phis[MBB] = ret = PerformPHIConstruction((*MBB->pred_begin())->end(),
Owen Anderson200ee7f2009-01-06 07:53:32 +0000452 *(MBB->pred_begin()), LI, Visited,
453 Defs, Uses, NewVNs, LiveOut, Phis,
Owen Anderson7d211e22008-12-31 02:00:25 +0000454 false, false);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000455 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000456 if (intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000457 EndIndex = LIs->getInstructionIndex(use);
458 EndIndex = LiveIntervals::getUseIndex(EndIndex);
459 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000460 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000461
Owen Anderson7d211e22008-12-31 02:00:25 +0000462 LI->addRange(LiveRange(StartIndex, EndIndex+1, ret));
Owen Andersone1762c92009-01-12 03:10:40 +0000463 if (intrablock)
464 LI->addKill(ret, EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000465 } else {
Owen Anderson7d211e22008-12-31 02:00:25 +0000466 Phis[MBB] = ret = LI->getNextValue(~0U, /*FIXME*/ 0,
467 LIs->getVNInfoAllocator());
468 if (!intrablock) LiveOut[MBB] = ret;
469
470 // If there are no uses or defs between our starting point and the
471 // beginning of the block, then recursive perform phi construction
472 // on our predecessors.
473 DenseMap<MachineBasicBlock*, VNInfo*> IncomingVNs;
474 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
475 PE = MBB->pred_end(); PI != PE; ++PI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000476 VNInfo* Incoming = PerformPHIConstruction((*PI)->end(), *PI, LI,
477 Visited, Defs, Uses, NewVNs,
478 LiveOut, Phis, false, false);
Owen Anderson7d211e22008-12-31 02:00:25 +0000479 if (Incoming != 0)
480 IncomingVNs[*PI] = Incoming;
481 }
482
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000483 // Otherwise, merge the incoming VNInfos with a phi join. Create a new
484 // VNInfo to represent the joined value.
485 for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator I =
486 IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) {
487 I->second->hasPHIKill = true;
488 unsigned KillIndex = LIs->getMBBEndIdx(I->first);
Owen Andersonc0f3a032009-01-29 08:22:06 +0000489 if (!LiveInterval::isKill(I->second, KillIndex))
490 LI->addKill(I->second, KillIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000491 }
492
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000493 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000494 if (intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000495 EndIndex = LIs->getInstructionIndex(use);
496 EndIndex = LiveIntervals::getUseIndex(EndIndex);
497 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000498 EndIndex = LIs->getMBBEndIdx(MBB);
499 LI->addRange(LiveRange(StartIndex, EndIndex+1, ret));
Owen Andersone1762c92009-01-12 03:10:40 +0000500 if (intrablock)
501 LI->addKill(ret, EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000502 }
503 } else if (ContainsDefs && !ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000504 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000505
506 // Search for the def in this block. If we don't find it before the
507 // instruction we care about, go to the fallback case. Note that that
Owen Anderson7d211e22008-12-31 02:00:25 +0000508 // should never happen: this cannot be intrablock, so use should
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000509 // always be an end() iterator.
Owen Anderson7d211e22008-12-31 02:00:25 +0000510 assert(use == MBB->end() && "No use marked in intrablock");
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000511
512 MachineBasicBlock::iterator walker = use;
513 --walker;
Owen Anderson7d211e22008-12-31 02:00:25 +0000514 while (walker != MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000515 if (BlockDefs.count(walker)) {
516 break;
517 } else
518 --walker;
519
520 // Once we've found it, extend its VNInfo to our instruction.
521 unsigned DefIndex = LIs->getInstructionIndex(walker);
522 DefIndex = LiveIntervals::getDefIndex(DefIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +0000523 unsigned EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000524
525 ret = NewVNs[walker];
Owen Anderson7d211e22008-12-31 02:00:25 +0000526 LI->addRange(LiveRange(DefIndex, EndIndex+1, ret));
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000527 } else if (!ContainsDefs && ContainsUses) {
Owen Anderson7d211e22008-12-31 02:00:25 +0000528 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000529
530 // Search for the use in this block that precedes the instruction we care
531 // about, going to the fallback case if we don't find it.
532
Owen Anderson7d211e22008-12-31 02:00:25 +0000533 if (use == MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000534 goto Fallback;
535
536 MachineBasicBlock::iterator walker = use;
537 --walker;
538 bool found = false;
Owen Anderson7d211e22008-12-31 02:00:25 +0000539 while (walker != MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000540 if (BlockUses.count(walker)) {
541 found = true;
542 break;
543 } else
544 --walker;
545
546 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000547 if (!found) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000548 if (BlockUses.count(walker))
549 found = true;
550 else
551 goto Fallback;
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000552 }
553
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000554 unsigned UseIndex = LIs->getInstructionIndex(walker);
555 UseIndex = LiveIntervals::getUseIndex(UseIndex);
556 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000557 if (intrablock) {
558 EndIndex = LIs->getInstructionIndex(use);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000559 EndIndex = LiveIntervals::getUseIndex(EndIndex);
560 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000561 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000562
563 // Now, recursively phi construct the VNInfo for the use we found,
564 // and then extend it to include the instruction we care about
Owen Anderson200ee7f2009-01-06 07:53:32 +0000565 ret = PerformPHIConstruction(walker, MBB, LI, Visited, Defs, Uses,
Owen Anderson7d211e22008-12-31 02:00:25 +0000566 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000567
Owen Andersonb4b84362009-01-26 21:57:31 +0000568 LI->addRange(LiveRange(UseIndex, EndIndex+1, ret));
569
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000570 // FIXME: Need to set kills properly for inter-block stuff.
Owen Andersond4f6fe52008-12-28 23:35:13 +0000571 if (LI->isKill(ret, UseIndex)) LI->removeKill(ret, UseIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +0000572 if (intrablock)
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000573 LI->addKill(ret, EndIndex);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000574 } else if (ContainsDefs && ContainsUses){
Owen Anderson7d211e22008-12-31 02:00:25 +0000575 SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB];
576 SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB];
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000577
578 // This case is basically a merging of the two preceding case, with the
579 // special note that checking for defs must take precedence over checking
580 // for uses, because of two-address instructions.
581
Owen Anderson7d211e22008-12-31 02:00:25 +0000582 if (use == MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000583 goto Fallback;
584
585 MachineBasicBlock::iterator walker = use;
586 --walker;
587 bool foundDef = false;
588 bool foundUse = false;
Owen Anderson7d211e22008-12-31 02:00:25 +0000589 while (walker != MBB->begin())
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000590 if (BlockDefs.count(walker)) {
591 foundDef = true;
592 break;
593 } else if (BlockUses.count(walker)) {
594 foundUse = true;
595 break;
596 } else
597 --walker;
598
599 // Must check begin() too.
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000600 if (!foundDef && !foundUse) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000601 if (BlockDefs.count(walker))
602 foundDef = true;
603 else if (BlockUses.count(walker))
604 foundUse = true;
605 else
606 goto Fallback;
Duncan Sands2b7fc1e2008-12-29 08:05:02 +0000607 }
608
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000609 unsigned StartIndex = LIs->getInstructionIndex(walker);
610 StartIndex = foundDef ? LiveIntervals::getDefIndex(StartIndex) :
611 LiveIntervals::getUseIndex(StartIndex);
612 unsigned EndIndex = 0;
Owen Anderson7d211e22008-12-31 02:00:25 +0000613 if (intrablock) {
614 EndIndex = LIs->getInstructionIndex(use);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000615 EndIndex = LiveIntervals::getUseIndex(EndIndex);
616 } else
Owen Anderson7d211e22008-12-31 02:00:25 +0000617 EndIndex = LIs->getMBBEndIdx(MBB);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000618
619 if (foundDef)
620 ret = NewVNs[walker];
621 else
Owen Anderson200ee7f2009-01-06 07:53:32 +0000622 ret = PerformPHIConstruction(walker, MBB, LI, Visited, Defs, Uses,
Owen Anderson7d211e22008-12-31 02:00:25 +0000623 NewVNs, LiveOut, Phis, false, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000624
Owen Andersonb4b84362009-01-26 21:57:31 +0000625 LI->addRange(LiveRange(StartIndex, EndIndex+1, ret));
626
Owen Andersond4f6fe52008-12-28 23:35:13 +0000627 if (foundUse && LI->isKill(ret, StartIndex))
628 LI->removeKill(ret, StartIndex);
Owen Anderson7d211e22008-12-31 02:00:25 +0000629 if (intrablock) {
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000630 LI->addKill(ret, EndIndex);
631 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000632 }
633
634 // Memoize results so we don't have to recompute them.
Owen Anderson7d211e22008-12-31 02:00:25 +0000635 if (!intrablock) LiveOut[MBB] = ret;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000636 else {
Owen Andersone1762c92009-01-12 03:10:40 +0000637 if (!NewVNs.count(use))
638 NewVNs[use] = ret;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000639 Visited.insert(use);
640 }
641
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000642 return ret;
643}
644
645/// ReconstructLiveInterval - Recompute a live interval from scratch.
646void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) {
647 BumpPtrAllocator& Alloc = LIs->getVNInfoAllocator();
648
649 // Clear the old ranges and valnos;
650 LI->clear();
651
652 // Cache the uses and defs of the register
653 typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap;
654 RegMap Defs, Uses;
655
656 // Keep track of the new VNs we're creating.
657 DenseMap<MachineInstr*, VNInfo*> NewVNs;
658 SmallPtrSet<VNInfo*, 2> PhiVNs;
659
660 // Cache defs, and create a new VNInfo for each def.
661 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
662 DE = MRI->def_end(); DI != DE; ++DI) {
663 Defs[(*DI).getParent()].insert(&*DI);
664
665 unsigned DefIdx = LIs->getInstructionIndex(&*DI);
666 DefIdx = LiveIntervals::getDefIndex(DefIdx);
667
Owen Anderson200ee7f2009-01-06 07:53:32 +0000668 VNInfo* NewVN = LI->getNextValue(DefIdx, 0, Alloc);
669
670 // If the def is a move, set the copy field.
Evan Cheng04ee5a12009-01-20 19:12:24 +0000671 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
672 if (TII->isMoveInstr(*DI, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
673 if (DstReg == LI->reg)
Owen Anderson200ee7f2009-01-06 07:53:32 +0000674 NewVN->copy = &*DI;
675
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000676 NewVNs[&*DI] = NewVN;
677 }
678
679 // Cache uses as a separate pass from actually processing them.
680 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
681 UE = MRI->use_end(); UI != UE; ++UI)
682 Uses[(*UI).getParent()].insert(&*UI);
683
684 // Now, actually process every use and use a phi construction algorithm
685 // to walk from it to its reaching definitions, building VNInfos along
686 // the way.
Owen Anderson7d211e22008-12-31 02:00:25 +0000687 DenseMap<MachineBasicBlock*, VNInfo*> LiveOut;
688 DenseMap<MachineBasicBlock*, VNInfo*> Phis;
Owen Anderson200ee7f2009-01-06 07:53:32 +0000689 SmallPtrSet<MachineInstr*, 4> Visited;
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000690 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg),
691 UE = MRI->use_end(); UI != UE; ++UI) {
Owen Anderson200ee7f2009-01-06 07:53:32 +0000692 PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs,
Owen Anderson7d211e22008-12-31 02:00:25 +0000693 Uses, NewVNs, LiveOut, Phis, true, true);
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000694 }
Owen Andersond4f6fe52008-12-28 23:35:13 +0000695
696 // Add ranges for dead defs
697 for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg),
698 DE = MRI->def_end(); DI != DE; ++DI) {
699 unsigned DefIdx = LIs->getInstructionIndex(&*DI);
700 DefIdx = LiveIntervals::getDefIndex(DefIdx);
Owen Andersond4f6fe52008-12-28 23:35:13 +0000701
702 if (LI->liveAt(DefIdx)) continue;
703
704 VNInfo* DeadVN = NewVNs[&*DI];
Owen Anderson7d211e22008-12-31 02:00:25 +0000705 LI->addRange(LiveRange(DefIdx, DefIdx+1, DeadVN));
Owen Andersond4f6fe52008-12-28 23:35:13 +0000706 LI->addKill(DeadVN, DefIdx);
707 }
Owen Anderson60d4f6d2008-12-28 21:48:48 +0000708}
709
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000710/// RenumberValno - Split the given valno out into a new vreg, allowing it to
711/// be allocated to a different register. This function creates a new vreg,
712/// copies the valno and its live ranges over to the new vreg's interval,
713/// removes them from the old interval, and rewrites all uses and defs of
714/// the original reg to the new vreg within those ranges.
715void PreAllocSplitting::RenumberValno(VNInfo* VN) {
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000716 SmallVector<VNInfo*, 4> Stack;
717 SmallVector<VNInfo*, 4> VNsToCopy;
718 Stack.push_back(VN);
719
720 // Walk through and copy the valno we care about, and any other valnos
721 // that are two-address redefinitions of the one we care about. These
722 // will need to be rewritten as well. We also check for safety of the
723 // renumbering here, by making sure that none of the valno involved has
724 // phi kills.
725 while (!Stack.empty()) {
726 VNInfo* OldVN = Stack.back();
727 Stack.pop_back();
728
729 // Bail out if we ever encounter a valno that has a PHI kill. We can't
730 // renumber these.
731 if (OldVN->hasPHIKill) return;
732
733 VNsToCopy.push_back(OldVN);
734
735 // Locate two-address redefinitions
736 for (SmallVector<unsigned, 4>::iterator KI = OldVN->kills.begin(),
737 KE = OldVN->kills.end(); KI != KE; ++KI) {
738 MachineInstr* MI = LIs->getInstructionFromIndex(*KI);
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000739 unsigned DefIdx = MI->findRegisterDefOperandIdx(CurrLI->reg);
740 if (DefIdx == ~0U) continue;
741 if (MI->isRegReDefinedByTwoAddr(DefIdx)) {
742 VNInfo* NextVN =
743 CurrLI->findDefinedVNInfo(LiveIntervals::getDefIndex(*KI));
Owen Andersonb4b84362009-01-26 21:57:31 +0000744 if (NextVN == OldVN) continue;
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000745 Stack.push_back(NextVN);
746 }
747 }
748 }
749
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000750 // Create the new vreg
751 unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg));
752
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000753 // Create the new live interval
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000754 LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg);
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000755
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000756 for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE =
757 VNsToCopy.end(); OI != OE; ++OI) {
758 VNInfo* OldVN = *OI;
759
760 // Copy the valno over
761 VNInfo* NewVN = NewLI.getNextValue(OldVN->def, OldVN->copy,
762 LIs->getVNInfoAllocator());
763 NewLI.copyValNumInfo(NewVN, OldVN);
764 NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN);
765
766 // Remove the valno from the old interval
767 CurrLI->removeValNo(OldVN);
768 }
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000769
770 // Rewrite defs and uses. This is done in two stages to avoid invalidating
771 // the reg_iterator.
772 SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange;
773
774 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
775 E = MRI->reg_end(); I != E; ++I) {
776 MachineOperand& MO = I.getOperand();
777 unsigned InstrIdx = LIs->getInstructionIndex(&*I);
778
779 if ((MO.isUse() && NewLI.liveAt(LiveIntervals::getUseIndex(InstrIdx))) ||
780 (MO.isDef() && NewLI.liveAt(LiveIntervals::getDefIndex(InstrIdx))))
781 OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo()));
782 }
783
784 for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I =
785 OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) {
786 MachineInstr* Inst = I->first;
787 unsigned OpIdx = I->second;
788 MachineOperand& MO = Inst->getOperand(OpIdx);
789 MO.setReg(NewVReg);
790 }
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000791
Owen Anderson45e68552009-01-29 05:28:55 +0000792 // The renumbered vreg shares a stack slot with the old register.
793 if (IntervalSSMap.count(CurrLI->reg))
794 IntervalSSMap[NewVReg] = IntervalSSMap[CurrLI->reg];
795
Owen Anderson2ebf63f2008-12-18 01:27:19 +0000796 NumRenumbers++;
Owen Andersond0b6a0d2008-12-16 21:35:08 +0000797}
798
Owen Anderson6002e992008-12-04 21:20:30 +0000799bool PreAllocSplitting::Rematerialize(unsigned vreg, VNInfo* ValNo,
800 MachineInstr* DefMI,
801 MachineBasicBlock::iterator RestorePt,
802 unsigned RestoreIdx,
803 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
804 MachineBasicBlock& MBB = *RestorePt->getParent();
805
806 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
807 unsigned KillIdx = 0;
808 if (ValNo->def == ~0U || DefMI->getParent() == BarrierMBB)
809 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, KillIdx);
810 else
811 KillPt = findNextEmptySlot(DefMI->getParent(), DefMI, KillIdx);
812
813 if (KillPt == DefMI->getParent()->end())
814 return false;
815
816 TII->reMaterialize(MBB, RestorePt, vreg, DefMI);
817 LIs->InsertMachineInstrInMaps(prior(RestorePt), RestoreIdx);
818
Owen Andersonb4b84362009-01-26 21:57:31 +0000819 ReconstructLiveInterval(CurrLI);
820 unsigned RematIdx = LIs->getInstructionIndex(prior(RestorePt));
821 RematIdx = LiveIntervals::getDefIndex(RematIdx);
822 RenumberValno(CurrLI->findDefinedVNInfo(RematIdx));
Owen Andersone1762c92009-01-12 03:10:40 +0000823
Owen Anderson75fa96b2008-11-19 04:28:29 +0000824 ++NumSplits;
825 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000826 return true;
827}
828
829MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
830 const TargetRegisterClass* RC,
831 MachineInstr* DefMI,
832 MachineInstr* Barrier,
833 MachineBasicBlock* MBB,
834 int& SS,
835 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
836 MachineBasicBlock::iterator Pt = MBB->begin();
837
838 // Go top down if RefsInMBB is empty.
839 if (RefsInMBB.empty())
840 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000841
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000842 MachineBasicBlock::iterator FoldPt = Barrier;
843 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
844 !RefsInMBB.count(FoldPt))
845 --FoldPt;
846
847 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg, false);
848 if (OpIdx == -1)
849 return 0;
850
851 SmallVector<unsigned, 1> Ops;
852 Ops.push_back(OpIdx);
853
854 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
855 return 0;
856
857 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
858 if (I != IntervalSSMap.end()) {
859 SS = I->second;
860 } else {
861 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
862
863 }
864
865 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
866 FoldPt, Ops, SS);
867
868 if (FMI) {
869 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
870 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
871 ++NumFolds;
872
873 IntervalSSMap[vreg] = SS;
874 CurrSLI = &LSs->getOrCreateInterval(SS);
875 if (CurrSLI->hasAtLeastOneValue())
876 CurrSValNo = CurrSLI->getValNumInfo(0);
877 else
878 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
879 }
880
881 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000882}
883
Evan Chengf5cd4f02008-10-23 20:43:13 +0000884/// SplitRegLiveInterval - Split (spill and restore) the given live interval
885/// so it would not cross the barrier that's being processed. Shrink wrap
886/// (minimize) the live interval to the last uses.
887bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
888 CurrLI = LI;
889
890 // Find live range where current interval cross the barrier.
891 LiveInterval::iterator LR =
892 CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx));
893 VNInfo *ValNo = LR->valno;
894
895 if (ValNo->def == ~1U) {
896 // Defined by a dead def? How can this be?
897 assert(0 && "Val# is defined by a dead def?");
898 abort();
899 }
900
Evan Cheng06587492008-10-24 02:05:00 +0000901 MachineInstr *DefMI = (ValNo->def != ~0U)
902 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
Evan Cheng06587492008-10-24 02:05:00 +0000903
Owen Andersond3be4622009-01-21 08:18:03 +0000904 // If this would create a new join point, do not split.
905 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent()))
906 return false;
907
Evan Chengf5cd4f02008-10-23 20:43:13 +0000908 // Find all references in the barrier mbb.
909 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
910 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
911 E = MRI->reg_end(); I != E; ++I) {
912 MachineInstr *RefMI = &*I;
913 if (RefMI->getParent() == BarrierMBB)
914 RefsInMBB.insert(RefMI);
915 }
916
917 // Find a point to restore the value after the barrier.
Evan Chengb964f332009-01-26 18:33:51 +0000918 unsigned RestoreIndex = 0;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000919 MachineBasicBlock::iterator RestorePt =
Evan Chengf62ce372008-10-28 00:47:49 +0000920 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000921 if (RestorePt == BarrierMBB->end())
922 return false;
923
Owen Anderson75fa96b2008-11-19 04:28:29 +0000924 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
925 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt,
926 RestoreIndex, RefsInMBB))
927 return true;
928
Evan Chengf5cd4f02008-10-23 20:43:13 +0000929 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +0000930 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000931 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000932 unsigned SpillIndex = 0;
Evan Cheng06587492008-10-24 02:05:00 +0000933 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +0000934 int SS = -1;
Evan Cheng78dfef72008-10-25 00:52:41 +0000935 if (ValNo->def == ~0U) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000936 // If it's defined by a phi, we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000937 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
938 BarrierMBB, SS, RefsInMBB))) {
939 SpillIndex = LIs->getInstructionIndex(SpillMI);
940 } else {
941 MachineBasicBlock::iterator SpillPt =
942 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex);
943 if (SpillPt == BarrierMBB->begin())
944 return false; // No gap to insert spill.
945 // Add spill.
946
947 SS = CreateSpillStackSlot(CurrLI->reg, RC);
948 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
949 SpillMI = prior(SpillPt);
950 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
951 }
Evan Cheng54898932008-10-29 08:39:34 +0000952 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
953 RestoreIndex, SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +0000954 // If it's already split, just restore the value. There is no need to spill
955 // the def again.
Evan Chengd0e32c52008-10-29 05:06:14 +0000956 if (!DefMI)
957 return false; // Def is dead. Do nothing.
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000958
959 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
960 BarrierMBB, SS, RefsInMBB))) {
961 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +0000962 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000963 // Check if it's possible to insert a spill after the def MI.
964 MachineBasicBlock::iterator SpillPt;
965 if (DefMBB == BarrierMBB) {
966 // Add spill after the def and the last use before the barrier.
967 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
968 RefsInMBB, SpillIndex);
969 if (SpillPt == DefMBB->begin())
970 return false; // No gap to insert spill.
971 } else {
972 SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex);
973 if (SpillPt == DefMBB->end())
974 return false; // No gap to insert spill.
975 }
976 // Add spill. The store instruction kills the register if def is before
977 // the barrier in the barrier block.
978 SS = CreateSpillStackSlot(CurrLI->reg, RC);
979 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg,
980 DefMBB == BarrierMBB, SS, RC);
981 SpillMI = prior(SpillPt);
982 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Cheng1f08cc22008-10-28 05:28:21 +0000983 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000984 }
985
Evan Cheng54898932008-10-29 08:39:34 +0000986 // Remember def instruction index to spill index mapping.
987 if (DefMI && SpillMI)
988 Def2SpillMap[ValNo->def] = SpillIndex;
989
Evan Chengf5cd4f02008-10-23 20:43:13 +0000990 // Add restore.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000991 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
992 MachineInstr *LoadMI = prior(RestorePt);
993 LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex);
994
Evan Chengd0e32c52008-10-29 05:06:14 +0000995 // Update spill stack slot live interval.
Evan Cheng54898932008-10-29 08:39:34 +0000996 UpdateSpillSlotInterval(ValNo, LIs->getUseIndex(SpillIndex)+1,
997 LIs->getDefIndex(RestoreIndex));
Evan Chengd0e32c52008-10-29 05:06:14 +0000998
Owen Andersonb4b84362009-01-26 21:57:31 +0000999 ReconstructLiveInterval(CurrLI);
1000 unsigned RestoreIdx = LIs->getInstructionIndex(prior(RestorePt));
1001 RestoreIdx = LiveIntervals::getDefIndex(RestoreIdx);
1002 RenumberValno(CurrLI->findDefinedVNInfo(RestoreIdx));
Owen Anderson7d211e22008-12-31 02:00:25 +00001003
Evan Chengae7fa5b2008-10-28 01:48:24 +00001004 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001005 return true;
1006}
1007
1008/// SplitRegLiveIntervals - Split all register live intervals that cross the
1009/// barrier that's being processed.
1010bool
Owen Anderson956ec272009-01-23 00:23:32 +00001011PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs,
1012 SmallPtrSet<LiveInterval*, 8>& Split) {
Evan Chengf5cd4f02008-10-23 20:43:13 +00001013 // First find all the virtual registers whose live intervals are intercepted
1014 // by the current barrier.
1015 SmallVector<LiveInterval*, 8> Intervals;
1016 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng23066282008-10-27 07:14:50 +00001017 if (TII->IgnoreRegisterClassBarriers(*RC))
1018 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001019 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
1020 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
1021 unsigned Reg = VRs[i];
1022 if (!LIs->hasInterval(Reg))
1023 continue;
1024 LiveInterval *LI = &LIs->getInterval(Reg);
1025 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
1026 // Virtual register live interval is intercepted by the barrier. We
1027 // should split and shrink wrap its interval if possible.
1028 Intervals.push_back(LI);
1029 }
1030 }
1031
1032 // Process the affected live intervals.
1033 bool Change = false;
1034 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +00001035 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
1036 break;
Owen Andersone1762c92009-01-12 03:10:40 +00001037 else if (NumSplits == 4)
1038 Change |= Change;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001039 LiveInterval *LI = Intervals.back();
1040 Intervals.pop_back();
Owen Anderson956ec272009-01-23 00:23:32 +00001041 bool result = SplitRegLiveInterval(LI);
1042 if (result) Split.insert(LI);
1043 Change |= result;
Evan Chengf5cd4f02008-10-23 20:43:13 +00001044 }
1045
1046 return Change;
1047}
1048
Owen Anderson45e68552009-01-29 05:28:55 +00001049unsigned PreAllocSplitting::getNumberOfNonSpills(
Owen Anderson32ca8652009-01-24 10:07:43 +00001050 SmallPtrSet<MachineInstr*, 4>& MIs,
Owen Anderson45e68552009-01-29 05:28:55 +00001051 unsigned Reg, int FrameIndex,
1052 bool& FeedsTwoAddr) {
1053 unsigned NonSpills = 0;
Owen Anderson32ca8652009-01-24 10:07:43 +00001054 for (SmallPtrSet<MachineInstr*, 4>::iterator UI = MIs.begin(), UE = MIs.end();
Owen Anderson45e68552009-01-29 05:28:55 +00001055 UI != UE; ++UI) {
Owen Anderson32ca8652009-01-24 10:07:43 +00001056 int StoreFrameIndex;
1057 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
Owen Anderson45e68552009-01-29 05:28:55 +00001058 if (StoreVReg != Reg || StoreFrameIndex != FrameIndex)
1059 NonSpills++;
1060
1061 int DefIdx = (*UI)->findRegisterDefOperandIdx(Reg);
1062 if (DefIdx != -1 && (*UI)->isRegReDefinedByTwoAddr(DefIdx))
1063 FeedsTwoAddr = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001064 }
1065
Owen Anderson45e68552009-01-29 05:28:55 +00001066 return NonSpills;
Owen Anderson32ca8652009-01-24 10:07:43 +00001067}
1068
Owen Anderson956ec272009-01-23 00:23:32 +00001069/// removeDeadSpills - After doing splitting, filter through all intervals we've
1070/// split, and see if any of the spills are unnecessary. If so, remove them.
1071bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) {
1072 bool changed = false;
1073
Owen Anderson4bfc2092009-01-29 05:41:02 +00001074 // Walk over all of the live intervals that were touched by the splitter,
1075 // and see if we can do any DCE and/or folding.
Owen Anderson956ec272009-01-23 00:23:32 +00001076 for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(),
1077 LE = split.end(); LI != LE; ++LI) {
Owen Anderson9ce499a2009-01-23 03:28:53 +00001078 DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> > VNUseCount;
Owen Anderson956ec272009-01-23 00:23:32 +00001079
Owen Anderson4bfc2092009-01-29 05:41:02 +00001080 // First, collect all the uses of the vreg, and sort them by their
1081 // reaching definition (VNInfo).
Owen Anderson956ec272009-01-23 00:23:32 +00001082 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg),
1083 UE = MRI->use_end(); UI != UE; ++UI) {
1084 unsigned index = LIs->getInstructionIndex(&*UI);
1085 index = LiveIntervals::getUseIndex(index);
1086
1087 const LiveRange* LR = (*LI)->getLiveRangeContaining(index);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001088 VNUseCount[LR->valno].insert(&*UI);
Owen Anderson956ec272009-01-23 00:23:32 +00001089 }
1090
Owen Anderson4bfc2092009-01-29 05:41:02 +00001091 // Now, take the definitions (VNInfo's) one at a time and try to DCE
1092 // and/or fold them away.
Owen Anderson956ec272009-01-23 00:23:32 +00001093 for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(),
1094 VE = (*LI)->vni_end(); VI != VE; ++VI) {
Owen Anderson45e68552009-01-29 05:28:55 +00001095
1096 if (DeadSplitLimit != -1 && (int)NumDeadSpills == DeadSplitLimit)
1097 return changed;
1098
Owen Anderson956ec272009-01-23 00:23:32 +00001099 VNInfo* CurrVN = *VI;
Owen Anderson4bfc2092009-01-29 05:41:02 +00001100
1101 // We don't currently try to handle definitions with PHI kills, because
1102 // it would involve processing more than one VNInfo at once.
Owen Anderson956ec272009-01-23 00:23:32 +00001103 if (CurrVN->hasPHIKill) continue;
Owen Anderson956ec272009-01-23 00:23:32 +00001104
Owen Anderson4bfc2092009-01-29 05:41:02 +00001105 // We also don't try to handle the results of PHI joins, since there's
1106 // no defining instruction to analyze.
Owen Anderson956ec272009-01-23 00:23:32 +00001107 unsigned DefIdx = CurrVN->def;
1108 if (DefIdx == ~0U || DefIdx == ~1U) continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001109
Owen Anderson4bfc2092009-01-29 05:41:02 +00001110 // We're only interested in eliminating cruft introduced by the splitter,
1111 // is of the form load-use or load-use-store. First, check that the
1112 // definition is a load, and remember what stack slot we loaded it from.
Owen Anderson956ec272009-01-23 00:23:32 +00001113 MachineInstr* DefMI = LIs->getInstructionFromIndex(DefIdx);
1114 int FrameIndex;
1115 if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue;
1116
Owen Anderson4bfc2092009-01-29 05:41:02 +00001117 // If the definition has no uses at all, just DCE it.
Owen Anderson9ce499a2009-01-23 03:28:53 +00001118 if (VNUseCount[CurrVN].size() == 0) {
1119 LIs->RemoveMachineInstrFromMaps(DefMI);
1120 (*LI)->removeValNo(CurrVN);
1121 DefMI->eraseFromParent();
Owen Andersonc0f3a032009-01-29 08:22:06 +00001122 VNUseCount.erase(CurrVN);
Owen Anderson9ce499a2009-01-23 03:28:53 +00001123 NumDeadSpills++;
1124 changed = true;
Owen Anderson32ca8652009-01-24 10:07:43 +00001125 continue;
Owen Anderson9ce499a2009-01-23 03:28:53 +00001126 }
Owen Anderson32ca8652009-01-24 10:07:43 +00001127
Owen Anderson4bfc2092009-01-29 05:41:02 +00001128 // Second, get the number of non-store uses of the definition, as well as
1129 // a flag indicating whether it feeds into a later two-address definition.
Owen Anderson45e68552009-01-29 05:28:55 +00001130 bool FeedsTwoAddr = false;
1131 unsigned NonSpillCount = getNumberOfNonSpills(VNUseCount[CurrVN],
1132 (*LI)->reg, FrameIndex,
1133 FeedsTwoAddr);
1134
Owen Anderson4bfc2092009-01-29 05:41:02 +00001135 // If there's one non-store use and it doesn't feed a two-addr, then
1136 // this is a load-use-store case that we can try to fold.
Owen Anderson45e68552009-01-29 05:28:55 +00001137 if (NonSpillCount == 1 && !FeedsTwoAddr) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001138 // Start by finding the non-store use MachineInstr.
Owen Anderson45e68552009-01-29 05:28:55 +00001139 SmallPtrSet<MachineInstr*, 4>::iterator UI = VNUseCount[CurrVN].begin();
1140 int StoreFrameIndex;
1141 unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1142 while (UI != VNUseCount[CurrVN].end() &&
1143 (StoreVReg == (*LI)->reg && StoreFrameIndex == FrameIndex)) {
1144 ++UI;
1145 if (UI != VNUseCount[CurrVN].end())
1146 StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex);
1147 }
Owen Anderson45e68552009-01-29 05:28:55 +00001148 if (UI == VNUseCount[CurrVN].end()) continue;
1149
1150 MachineInstr* use = *UI;
1151
Owen Anderson4bfc2092009-01-29 05:41:02 +00001152 // Attempt to fold it away!
Owen Anderson45e68552009-01-29 05:28:55 +00001153 int OpIdx = use->findRegisterUseOperandIdx((*LI)->reg, false);
1154 if (OpIdx == -1) continue;
Owen Anderson45e68552009-01-29 05:28:55 +00001155 SmallVector<unsigned, 1> Ops;
1156 Ops.push_back(OpIdx);
Owen Anderson45e68552009-01-29 05:28:55 +00001157 if (!TII->canFoldMemoryOperand(use, Ops)) continue;
1158
1159 MachineInstr* NewMI =
1160 TII->foldMemoryOperand(*use->getParent()->getParent(),
1161 use, Ops, FrameIndex);
1162
1163 if (!NewMI) continue;
1164
Owen Anderson4bfc2092009-01-29 05:41:02 +00001165 // Update relevant analyses.
Owen Anderson45e68552009-01-29 05:28:55 +00001166 LIs->RemoveMachineInstrFromMaps(DefMI);
1167 LIs->ReplaceMachineInstrInMaps(use, NewMI);
1168 (*LI)->removeValNo(CurrVN);
1169
1170 DefMI->eraseFromParent();
1171 MachineBasicBlock* MBB = use->getParent();
1172 NewMI = MBB->insert(MBB->erase(use), NewMI);
1173 VNUseCount[CurrVN].erase(use);
1174
Owen Anderson4bfc2092009-01-29 05:41:02 +00001175 // Remove deleted instructions. Note that we need to remove them from
1176 // the VNInfo->use map as well, just to be safe.
Owen Anderson45e68552009-01-29 05:28:55 +00001177 for (SmallPtrSet<MachineInstr*, 4>::iterator II =
1178 VNUseCount[CurrVN].begin(), IE = VNUseCount[CurrVN].end();
1179 II != IE; ++II) {
Owen Anderson4bfc2092009-01-29 05:41:02 +00001180 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
Owen Andersonc0f3a032009-01-29 08:22:06 +00001181 VNI = VNUseCount.begin(), VNE = VNUseCount.end(); VNI != VNE;
1182 ++VNI)
1183 if (VNI->first != CurrVN)
1184 VNI->second.erase(*II);
Owen Anderson45e68552009-01-29 05:28:55 +00001185 LIs->RemoveMachineInstrFromMaps(*II);
1186 (*II)->eraseFromParent();
1187 }
Owen Andersonc0f3a032009-01-29 08:22:06 +00001188
1189 VNUseCount.erase(CurrVN);
Owen Anderson45e68552009-01-29 05:28:55 +00001190
1191 for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator
1192 VI = VNUseCount.begin(), VE = VNUseCount.end(); VI != VE; ++VI)
1193 if (VI->second.erase(use))
1194 VI->second.insert(NewMI);
1195
1196 NumDeadSpills++;
1197 changed = true;
1198 continue;
1199 }
1200
Owen Anderson4bfc2092009-01-29 05:41:02 +00001201 // If there's more than one non-store instruction, we can't profitably
1202 // fold it, so bail.
Owen Anderson45e68552009-01-29 05:28:55 +00001203 if (NonSpillCount) continue;
Owen Anderson32ca8652009-01-24 10:07:43 +00001204
Owen Anderson4bfc2092009-01-29 05:41:02 +00001205 // Otherwise, this is a load-store case, so DCE them.
Owen Anderson32ca8652009-01-24 10:07:43 +00001206 for (SmallPtrSet<MachineInstr*, 4>::iterator UI =
1207 VNUseCount[CurrVN].begin(), UE = VNUseCount[CurrVN].end();
1208 UI != UI; ++UI) {
1209 LIs->RemoveMachineInstrFromMaps(*UI);
1210 (*UI)->eraseFromParent();
1211 }
1212
Owen Andersonc0f3a032009-01-29 08:22:06 +00001213 VNUseCount.erase(CurrVN);
1214
Owen Anderson32ca8652009-01-24 10:07:43 +00001215 LIs->RemoveMachineInstrFromMaps(DefMI);
1216 (*LI)->removeValNo(CurrVN);
1217 DefMI->eraseFromParent();
1218 NumDeadSpills++;
1219 changed = true;
Owen Anderson956ec272009-01-23 00:23:32 +00001220 }
1221 }
1222
1223 return changed;
1224}
1225
Owen Andersonf1f75b12008-11-04 22:22:41 +00001226bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1227 MachineBasicBlock* DefMBB,
1228 MachineBasicBlock* BarrierMBB) {
1229 if (DefMBB == BarrierMBB)
1230 return false;
1231
1232 if (LR->valno->hasPHIKill)
1233 return false;
1234
1235 unsigned MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
1236 if (LR->end < MBBEnd)
1237 return false;
1238
1239 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1240 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1241 return true;
1242
1243 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1244 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1245 typedef std::pair<MachineBasicBlock*,
1246 MachineBasicBlock::succ_iterator> ItPair;
1247 SmallVector<ItPair, 4> Stack;
1248 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1249
1250 while (!Stack.empty()) {
1251 ItPair P = Stack.back();
1252 Stack.pop_back();
1253
1254 MachineBasicBlock* PredMBB = P.first;
1255 MachineBasicBlock::succ_iterator S = P.second;
1256
1257 if (S == PredMBB->succ_end())
1258 continue;
1259 else if (Visited.count(*S)) {
1260 Stack.push_back(std::make_pair(PredMBB, ++S));
1261 continue;
1262 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001263 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001264
1265 MachineBasicBlock* MBB = *S;
1266 Visited.insert(MBB);
1267
1268 if (MBB == BarrierMBB)
1269 return true;
1270
1271 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1272 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1273 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1274 while (MDTN) {
1275 if (MDTN == DefMDTN)
1276 return true;
1277 else if (MDTN == BarrierMDTN)
1278 break;
1279 MDTN = MDTN->getIDom();
1280 }
1281
1282 MBBEnd = LIs->getMBBEndIdx(MBB);
1283 if (LR->end > MBBEnd)
1284 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1285 }
1286
1287 return false;
1288}
1289
1290
Evan Cheng09e8ca82008-10-20 21:44:59 +00001291bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001292 CurrMF = &MF;
1293 TM = &MF.getTarget();
1294 TII = TM->getInstrInfo();
1295 MFI = MF.getFrameInfo();
1296 MRI = &MF.getRegInfo();
1297 LIs = &getAnalysis<LiveIntervals>();
1298 LSs = &getAnalysis<LiveStacks>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001299
1300 bool MadeChange = false;
1301
1302 // Make sure blocks are numbered in order.
1303 MF.RenumberBlocks();
1304
Evan Cheng54898932008-10-29 08:39:34 +00001305 MachineBasicBlock *Entry = MF.begin();
1306 SmallPtrSet<MachineBasicBlock*,16> Visited;
1307
Owen Anderson956ec272009-01-23 00:23:32 +00001308 SmallPtrSet<LiveInterval*, 8> Split;
1309
Evan Cheng54898932008-10-29 08:39:34 +00001310 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1311 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1312 DFI != E; ++DFI) {
1313 BarrierMBB = *DFI;
1314 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1315 E = BarrierMBB->end(); I != E; ++I) {
1316 Barrier = &*I;
1317 const TargetRegisterClass **BarrierRCs =
1318 Barrier->getDesc().getRegClassBarriers();
1319 if (!BarrierRCs)
1320 continue;
1321 BarrierIdx = LIs->getInstructionIndex(Barrier);
Owen Anderson956ec272009-01-23 00:23:32 +00001322 MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split);
Evan Cheng54898932008-10-29 08:39:34 +00001323 }
1324 }
Evan Chengf5cd4f02008-10-23 20:43:13 +00001325
Owen Anderson956ec272009-01-23 00:23:32 +00001326 MadeChange |= removeDeadSpills(Split);
1327
Evan Chengf5cd4f02008-10-23 20:43:13 +00001328 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001329}