blob: 0ec4edd8066c136e0b379845b90c18c371efaa3d [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);
40
41STATISTIC(NumSplits, "Number of intervals split");
Owen Anderson75fa96b2008-11-19 04:28:29 +000042STATISTIC(NumRemats, "Number of intervals split by rematerialization");
Owen Anderson7b9d67c2008-12-02 18:53:47 +000043STATISTIC(NumFolds, "Number of intervals split with spill folding");
Evan Chengf5cd4f02008-10-23 20:43:13 +000044
Evan Cheng09e8ca82008-10-20 21:44:59 +000045namespace {
46 class VISIBILITY_HIDDEN PreAllocSplitting : public MachineFunctionPass {
Evan Chengd0e32c52008-10-29 05:06:14 +000047 MachineFunction *CurrMF;
Evan Chengf5cd4f02008-10-23 20:43:13 +000048 const TargetMachine *TM;
49 const TargetInstrInfo *TII;
50 MachineFrameInfo *MFI;
51 MachineRegisterInfo *MRI;
52 LiveIntervals *LIs;
Evan Chengd0e32c52008-10-29 05:06:14 +000053 LiveStacks *LSs;
Evan Cheng09e8ca82008-10-20 21:44:59 +000054
Evan Chengf5cd4f02008-10-23 20:43:13 +000055 // Barrier - Current barrier being processed.
56 MachineInstr *Barrier;
57
58 // BarrierMBB - Basic block where the barrier resides in.
59 MachineBasicBlock *BarrierMBB;
60
61 // Barrier - Current barrier index.
62 unsigned BarrierIdx;
63
64 // CurrLI - Current live interval being split.
65 LiveInterval *CurrLI;
66
Evan Chengd0e32c52008-10-29 05:06:14 +000067 // CurrSLI - Current stack slot live interval.
68 LiveInterval *CurrSLI;
69
70 // CurrSValNo - Current val# for the stack slot live interval.
71 VNInfo *CurrSValNo;
72
73 // IntervalSSMap - A map from live interval to spill slots.
74 DenseMap<unsigned, int> IntervalSSMap;
Evan Chengf5cd4f02008-10-23 20:43:13 +000075
Evan Cheng54898932008-10-29 08:39:34 +000076 // Def2SpillMap - A map from a def instruction index to spill index.
77 DenseMap<unsigned, unsigned> Def2SpillMap;
Evan Cheng06587492008-10-24 02:05:00 +000078
Evan Cheng09e8ca82008-10-20 21:44:59 +000079 public:
80 static char ID;
81 PreAllocSplitting() : MachineFunctionPass(&ID) {}
82
83 virtual bool runOnMachineFunction(MachineFunction &MF);
84
85 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
86 AU.addRequired<LiveIntervals>();
87 AU.addPreserved<LiveIntervals>();
Evan Chengd0e32c52008-10-29 05:06:14 +000088 AU.addRequired<LiveStacks>();
89 AU.addPreserved<LiveStacks>();
Evan Cheng09e8ca82008-10-20 21:44:59 +000090 AU.addPreserved<RegisterCoalescer>();
91 if (StrongPHIElim)
92 AU.addPreservedID(StrongPHIEliminationID);
93 else
94 AU.addPreservedID(PHIEliminationID);
Owen Andersonf1f75b12008-11-04 22:22:41 +000095 AU.addRequired<MachineDominatorTree>();
96 AU.addRequired<MachineLoopInfo>();
97 AU.addPreserved<MachineDominatorTree>();
98 AU.addPreserved<MachineLoopInfo>();
Evan Cheng09e8ca82008-10-20 21:44:59 +000099 MachineFunctionPass::getAnalysisUsage(AU);
100 }
101
102 virtual void releaseMemory() {
Evan Chengd0e32c52008-10-29 05:06:14 +0000103 IntervalSSMap.clear();
Evan Cheng54898932008-10-29 08:39:34 +0000104 Def2SpillMap.clear();
Evan Cheng09e8ca82008-10-20 21:44:59 +0000105 }
106
107 virtual const char *getPassName() const {
108 return "Pre-Register Allocaton Live Interval Splitting";
109 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000110
111 /// print - Implement the dump method.
112 virtual void print(std::ostream &O, const Module* M = 0) const {
113 LIs->print(O, M);
114 }
115
116 void print(std::ostream *O, const Module* M = 0) const {
117 if (O) print(*O, M);
118 }
119
120 private:
121 MachineBasicBlock::iterator
122 findNextEmptySlot(MachineBasicBlock*, MachineInstr*,
123 unsigned&);
124
125 MachineBasicBlock::iterator
Evan Cheng1f08cc22008-10-28 05:28:21 +0000126 findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000127 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
128
129 MachineBasicBlock::iterator
Evan Chengf62ce372008-10-28 00:47:49 +0000130 findRestorePoint(MachineBasicBlock*, MachineInstr*, unsigned,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000131 SmallPtrSet<MachineInstr*, 4>&, unsigned&);
132
Evan Chengd0e32c52008-10-29 05:06:14 +0000133 int CreateSpillStackSlot(unsigned, const TargetRegisterClass *);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000134
Evan Cheng54898932008-10-29 08:39:34 +0000135 bool IsAvailableInStack(MachineBasicBlock*, unsigned, unsigned, unsigned,
136 unsigned&, int&) const;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000137
Evan Chengd0e32c52008-10-29 05:06:14 +0000138 void UpdateSpillSlotInterval(VNInfo*, unsigned, unsigned);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000139
Evan Chengd0e32c52008-10-29 05:06:14 +0000140 void UpdateRegisterInterval(VNInfo*, unsigned, unsigned);
141
142 bool ShrinkWrapToLastUse(MachineBasicBlock*, VNInfo*,
Evan Cheng06587492008-10-24 02:05:00 +0000143 SmallVector<MachineOperand*, 4>&,
144 SmallPtrSet<MachineInstr*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000145
Evan Chengaaf510c2008-10-26 07:49:03 +0000146 void ShrinkWrapLiveInterval(VNInfo*, MachineBasicBlock*, MachineBasicBlock*,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000147 MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 8>&,
Evan Cheng06587492008-10-24 02:05:00 +0000148 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >&,
149 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >&,
150 SmallVector<MachineBasicBlock*, 4>&);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000151
152 bool SplitRegLiveInterval(LiveInterval*);
153
154 bool SplitRegLiveIntervals(const TargetRegisterClass **);
Owen Andersonf1f75b12008-11-04 22:22:41 +0000155
156 bool createsNewJoin(LiveRange* LR, MachineBasicBlock* DefMBB,
157 MachineBasicBlock* BarrierMBB);
Owen Anderson75fa96b2008-11-19 04:28:29 +0000158 bool Rematerialize(unsigned vreg, VNInfo* ValNo,
159 MachineInstr* DefMI,
160 MachineBasicBlock::iterator RestorePt,
161 unsigned RestoreIdx,
162 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000163 MachineInstr* FoldSpill(unsigned vreg, const TargetRegisterClass* RC,
164 MachineInstr* DefMI,
165 MachineInstr* Barrier,
166 MachineBasicBlock* MBB,
167 int& SS,
168 SmallPtrSet<MachineInstr*, 4>& RefsInMBB);
Evan Cheng09e8ca82008-10-20 21:44:59 +0000169 };
170} // end anonymous namespace
171
172char PreAllocSplitting::ID = 0;
173
174static RegisterPass<PreAllocSplitting>
175X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting");
176
177const PassInfo *const llvm::PreAllocSplittingID = &X;
178
Evan Chengf5cd4f02008-10-23 20:43:13 +0000179
180/// findNextEmptySlot - Find a gap after the given machine instruction in the
181/// instruction index map. If there isn't one, return end().
182MachineBasicBlock::iterator
183PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI,
184 unsigned &SpotIndex) {
185 MachineBasicBlock::iterator MII = MI;
186 if (++MII != MBB->end()) {
187 unsigned Index = LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII));
188 if (Index) {
189 SpotIndex = Index;
190 return MII;
191 }
192 }
193 return MBB->end();
194}
195
196/// findSpillPoint - Find a gap as far away from the given MI that's suitable
197/// for spilling the current live interval. The index must be before any
198/// defs and uses of the live interval register in the mbb. Return begin() if
199/// none is found.
200MachineBasicBlock::iterator
201PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Cheng1f08cc22008-10-28 05:28:21 +0000202 MachineInstr *DefMI,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000203 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
204 unsigned &SpillIndex) {
205 MachineBasicBlock::iterator Pt = MBB->begin();
206
207 // Go top down if RefsInMBB is empty.
Evan Cheng1f08cc22008-10-28 05:28:21 +0000208 if (RefsInMBB.empty() && !DefMI) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000209 MachineBasicBlock::iterator MII = MBB->begin();
210 MachineBasicBlock::iterator EndPt = MI;
211 do {
212 ++MII;
213 unsigned Index = LIs->getInstructionIndex(MII);
214 unsigned Gap = LIs->findGapBeforeInstr(Index);
215 if (Gap) {
216 Pt = MII;
217 SpillIndex = Gap;
218 break;
219 }
220 } while (MII != EndPt);
221 } else {
222 MachineBasicBlock::iterator MII = MI;
Evan Cheng1f08cc22008-10-28 05:28:21 +0000223 MachineBasicBlock::iterator EndPt = DefMI
224 ? MachineBasicBlock::iterator(DefMI) : MBB->begin();
225 while (MII != EndPt && !RefsInMBB.count(MII)) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000226 unsigned Index = LIs->getInstructionIndex(MII);
227 if (LIs->hasGapBeforeInstr(Index)) {
228 Pt = MII;
229 SpillIndex = LIs->findGapBeforeInstr(Index, true);
230 }
231 --MII;
232 }
233 }
234
235 return Pt;
236}
237
238/// findRestorePoint - Find a gap in the instruction index map that's suitable
239/// for restoring the current live interval value. The index must be before any
240/// uses of the live interval register in the mbb. Return end() if none is
241/// found.
242MachineBasicBlock::iterator
243PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI,
Evan Chengf62ce372008-10-28 00:47:49 +0000244 unsigned LastIdx,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000245 SmallPtrSet<MachineInstr*, 4> &RefsInMBB,
246 unsigned &RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000247 // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb
248 // begin index accordingly.
Owen Anderson5a92d4e2008-11-18 20:53:59 +0000249 MachineBasicBlock::iterator Pt = MBB->end();
Evan Chengf62ce372008-10-28 00:47:49 +0000250 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000251
Evan Chengf62ce372008-10-28 00:47:49 +0000252 // Go bottom up if RefsInMBB is empty and the end of the mbb isn't beyond
253 // the last index in the live range.
254 if (RefsInMBB.empty() && LastIdx >= EndIdx) {
Owen Anderson711fd3d2008-11-13 21:53:14 +0000255 MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000256 MachineBasicBlock::iterator EndPt = MI;
Evan Cheng54898932008-10-29 08:39:34 +0000257 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000258 do {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000259 unsigned Index = LIs->getInstructionIndex(MII);
Evan Cheng56ab0de2008-10-24 18:46:44 +0000260 unsigned Gap = LIs->findGapBeforeInstr(Index);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000261 if (Gap) {
262 Pt = MII;
263 RestoreIndex = Gap;
264 break;
265 }
Evan Cheng54898932008-10-29 08:39:34 +0000266 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000267 } while (MII != EndPt);
268 } else {
269 MachineBasicBlock::iterator MII = MI;
270 MII = ++MII;
Evan Chengf62ce372008-10-28 00:47:49 +0000271 // FIXME: Limit the number of instructions to examine to reduce
272 // compile time?
Evan Chengf5cd4f02008-10-23 20:43:13 +0000273 while (MII != MBB->end()) {
274 unsigned Index = LIs->getInstructionIndex(MII);
Evan Chengf62ce372008-10-28 00:47:49 +0000275 if (Index > LastIdx)
276 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000277 unsigned Gap = LIs->findGapBeforeInstr(Index);
278 if (Gap) {
279 Pt = MII;
280 RestoreIndex = Gap;
281 }
282 if (RefsInMBB.count(MII))
283 break;
284 ++MII;
285 }
286 }
287
288 return Pt;
289}
290
Evan Chengd0e32c52008-10-29 05:06:14 +0000291/// CreateSpillStackSlot - Create a stack slot for the live interval being
292/// split. If the live interval was previously split, just reuse the same
293/// slot.
294int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg,
295 const TargetRegisterClass *RC) {
296 int SS;
297 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
298 if (I != IntervalSSMap.end()) {
299 SS = I->second;
300 } else {
301 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
302 IntervalSSMap[Reg] = SS;
Evan Cheng06587492008-10-24 02:05:00 +0000303 }
Evan Chengd0e32c52008-10-29 05:06:14 +0000304
305 // Create live interval for stack slot.
306 CurrSLI = &LSs->getOrCreateInterval(SS);
Evan Cheng54898932008-10-29 08:39:34 +0000307 if (CurrSLI->hasAtLeastOneValue())
Evan Chengd0e32c52008-10-29 05:06:14 +0000308 CurrSValNo = CurrSLI->getValNumInfo(0);
309 else
310 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
311 return SS;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000312}
313
Evan Chengd0e32c52008-10-29 05:06:14 +0000314/// IsAvailableInStack - Return true if register is available in a split stack
315/// slot at the specified index.
316bool
Evan Cheng54898932008-10-29 08:39:34 +0000317PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB,
318 unsigned Reg, unsigned DefIndex,
319 unsigned RestoreIndex, unsigned &SpillIndex,
320 int& SS) const {
321 if (!DefMBB)
322 return false;
323
Evan Chengd0e32c52008-10-29 05:06:14 +0000324 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg);
325 if (I == IntervalSSMap.end())
Evan Chengf5cd4f02008-10-23 20:43:13 +0000326 return false;
Evan Cheng54898932008-10-29 08:39:34 +0000327 DenseMap<unsigned, unsigned>::iterator II = Def2SpillMap.find(DefIndex);
328 if (II == Def2SpillMap.end())
329 return false;
330
331 // If last spill of def is in the same mbb as barrier mbb (where restore will
332 // be), make sure it's not below the intended restore index.
333 // FIXME: Undo the previous spill?
334 assert(LIs->getMBBFromIndex(II->second) == DefMBB);
335 if (DefMBB == BarrierMBB && II->second >= RestoreIndex)
336 return false;
337
338 SS = I->second;
339 SpillIndex = II->second;
340 return true;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000341}
342
Evan Chengd0e32c52008-10-29 05:06:14 +0000343/// UpdateSpillSlotInterval - Given the specified val# of the register live
344/// interval being split, and the spill and restore indicies, update the live
345/// interval of the spill stack slot.
346void
347PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, unsigned SpillIndex,
348 unsigned RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000349 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
350 "Expect restore in the barrier mbb");
351
352 MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex);
353 if (MBB == BarrierMBB) {
354 // Intra-block spill + restore. We are done.
Evan Chengd0e32c52008-10-29 05:06:14 +0000355 LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo);
356 CurrSLI->addRange(SLR);
357 return;
358 }
359
Evan Cheng54898932008-10-29 08:39:34 +0000360 SmallPtrSet<MachineBasicBlock*, 4> Processed;
361 unsigned EndIdx = LIs->getMBBEndIdx(MBB);
362 LiveRange SLR(SpillIndex, EndIdx+1, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000363 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000364 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000365
366 // Start from the spill mbb, figure out the extend of the spill slot's
367 // live interval.
368 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000369 const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex);
370 if (LR->end > EndIdx)
Evan Chengd0e32c52008-10-29 05:06:14 +0000371 // If live range extend beyond end of mbb, add successors to work list.
372 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
373 SE = MBB->succ_end(); SI != SE; ++SI)
374 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000375
376 while (!WorkList.empty()) {
377 MachineBasicBlock *MBB = WorkList.back();
378 WorkList.pop_back();
Evan Cheng54898932008-10-29 08:39:34 +0000379 if (Processed.count(MBB))
380 continue;
Evan Chengd0e32c52008-10-29 05:06:14 +0000381 unsigned Idx = LIs->getMBBStartIdx(MBB);
382 LR = CurrLI->getLiveRangeContaining(Idx);
Evan Cheng54898932008-10-29 08:39:34 +0000383 if (LR && LR->valno == ValNo) {
384 EndIdx = LIs->getMBBEndIdx(MBB);
385 if (Idx <= RestoreIndex && RestoreIndex < EndIdx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000386 // Spill slot live interval stops at the restore.
Evan Cheng54898932008-10-29 08:39:34 +0000387 LiveRange SLR(Idx, RestoreIndex, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000388 CurrSLI->addRange(SLR);
Evan Cheng54898932008-10-29 08:39:34 +0000389 } else if (LR->end > EndIdx) {
390 // Live range extends beyond end of mbb, process successors.
391 LiveRange SLR(Idx, EndIdx+1, CurrSValNo);
392 CurrSLI->addRange(SLR);
393 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
394 SE = MBB->succ_end(); SI != SE; ++SI)
395 WorkList.push_back(*SI);
Evan Chengd0e32c52008-10-29 05:06:14 +0000396 } else {
Evan Cheng54898932008-10-29 08:39:34 +0000397 LiveRange SLR(Idx, LR->end, CurrSValNo);
Evan Chengd0e32c52008-10-29 05:06:14 +0000398 CurrSLI->addRange(SLR);
Evan Chengd0e32c52008-10-29 05:06:14 +0000399 }
Evan Cheng54898932008-10-29 08:39:34 +0000400 Processed.insert(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000401 }
402 }
403}
404
405/// UpdateRegisterInterval - Given the specified val# of the current live
406/// interval is being split, and the spill and restore indices, update the live
Evan Chengf5cd4f02008-10-23 20:43:13 +0000407/// interval accordingly.
408void
Evan Chengd0e32c52008-10-29 05:06:14 +0000409PreAllocSplitting::UpdateRegisterInterval(VNInfo *ValNo, unsigned SpillIndex,
410 unsigned RestoreIndex) {
Evan Cheng54898932008-10-29 08:39:34 +0000411 assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB &&
412 "Expect restore in the barrier mbb");
413
Evan Chengf5cd4f02008-10-23 20:43:13 +0000414 SmallVector<std::pair<unsigned,unsigned>, 4> Before;
415 SmallVector<std::pair<unsigned,unsigned>, 4> After;
416 SmallVector<unsigned, 4> BeforeKills;
417 SmallVector<unsigned, 4> AfterKills;
418 SmallPtrSet<const LiveRange*, 4> Processed;
419
420 // First, let's figure out which parts of the live interval is now defined
421 // by the restore, which are defined by the original definition.
Evan Chengd0e32c52008-10-29 05:06:14 +0000422 const LiveRange *LR = CurrLI->getLiveRangeContaining(RestoreIndex);
423 After.push_back(std::make_pair(RestoreIndex, LR->end));
Evan Cheng06587492008-10-24 02:05:00 +0000424 if (CurrLI->isKill(ValNo, LR->end))
425 AfterKills.push_back(LR->end);
426
Evan Chengd0e32c52008-10-29 05:06:14 +0000427 assert(LR->contains(SpillIndex));
428 if (SpillIndex > LR->start) {
429 Before.push_back(std::make_pair(LR->start, SpillIndex));
430 BeforeKills.push_back(SpillIndex);
Evan Cheng06587492008-10-24 02:05:00 +0000431 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000432 Processed.insert(LR);
433
Evan Chengd0e32c52008-10-29 05:06:14 +0000434 // Start from the restore mbb, figure out what part of the live interval
435 // are defined by the restore.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000436 SmallVector<MachineBasicBlock*, 4> WorkList;
Evan Cheng54898932008-10-29 08:39:34 +0000437 MachineBasicBlock *MBB = BarrierMBB;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000438 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
439 SE = MBB->succ_end(); SI != SE; ++SI)
440 WorkList.push_back(*SI);
441
442 while (!WorkList.empty()) {
443 MBB = WorkList.back();
444 WorkList.pop_back();
445 unsigned Idx = LIs->getMBBStartIdx(MBB);
446 LR = CurrLI->getLiveRangeContaining(Idx);
447 if (LR && LR->valno == ValNo && !Processed.count(LR)) {
448 After.push_back(std::make_pair(LR->start, LR->end));
449 if (CurrLI->isKill(ValNo, LR->end))
450 AfterKills.push_back(LR->end);
451 Idx = LIs->getMBBEndIdx(MBB);
452 if (LR->end > Idx) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000453 // Live range extend beyond at least one mbb. Let's see what other
454 // mbbs it reaches.
455 LIs->findReachableMBBs(LR->start, LR->end, WorkList);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000456 }
457 Processed.insert(LR);
458 }
459 }
460
461 for (LiveInterval::iterator I = CurrLI->begin(), E = CurrLI->end();
462 I != E; ++I) {
463 LiveRange *LR = I;
464 if (LR->valno == ValNo && !Processed.count(LR)) {
465 Before.push_back(std::make_pair(LR->start, LR->end));
466 if (CurrLI->isKill(ValNo, LR->end))
467 BeforeKills.push_back(LR->end);
468 }
469 }
470
471 // Now create new val#s to represent the live ranges defined by the old def
472 // those defined by the restore.
473 unsigned AfterDef = ValNo->def;
474 MachineInstr *AfterCopy = ValNo->copy;
475 bool HasPHIKill = ValNo->hasPHIKill;
476 CurrLI->removeValNo(ValNo);
Evan Cheng06587492008-10-24 02:05:00 +0000477 VNInfo *BValNo = (Before.empty())
478 ? NULL
479 : CurrLI->getNextValue(AfterDef, AfterCopy, LIs->getVNInfoAllocator());
480 if (BValNo)
481 CurrLI->addKills(BValNo, BeforeKills);
482
483 VNInfo *AValNo = (After.empty())
484 ? NULL
Evan Chengd0e32c52008-10-29 05:06:14 +0000485 : CurrLI->getNextValue(RestoreIndex, 0, LIs->getVNInfoAllocator());
Evan Cheng06587492008-10-24 02:05:00 +0000486 if (AValNo) {
487 AValNo->hasPHIKill = HasPHIKill;
488 CurrLI->addKills(AValNo, AfterKills);
489 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000490
491 for (unsigned i = 0, e = Before.size(); i != e; ++i) {
492 unsigned Start = Before[i].first;
493 unsigned End = Before[i].second;
494 CurrLI->addRange(LiveRange(Start, End, BValNo));
495 }
496 for (unsigned i = 0, e = After.size(); i != e; ++i) {
497 unsigned Start = After[i].first;
498 unsigned End = After[i].second;
499 CurrLI->addRange(LiveRange(Start, End, AValNo));
500 }
501}
502
503/// ShrinkWrapToLastUse - There are uses of the current live interval in the
504/// given block, shrink wrap the live interval to the last use (i.e. remove
505/// from last use to the end of the mbb). In case mbb is the where the barrier
506/// is, remove from the last use to the barrier.
507bool
Evan Chengd0e32c52008-10-29 05:06:14 +0000508PreAllocSplitting::ShrinkWrapToLastUse(MachineBasicBlock *MBB, VNInfo *ValNo,
Evan Cheng06587492008-10-24 02:05:00 +0000509 SmallVector<MachineOperand*, 4> &Uses,
510 SmallPtrSet<MachineInstr*, 4> &UseMIs) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000511 MachineOperand *LastMO = 0;
512 MachineInstr *LastMI = 0;
513 if (MBB != BarrierMBB && Uses.size() == 1) {
514 // Single use, no need to traverse the block. We can't assume this for the
515 // barrier bb though since the use is probably below the barrier.
516 LastMO = Uses[0];
517 LastMI = LastMO->getParent();
518 } else {
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000519 MachineBasicBlock::iterator MEE = MBB->begin();
Evan Chengf5cd4f02008-10-23 20:43:13 +0000520 MachineBasicBlock::iterator MII;
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000521 if (MBB == BarrierMBB)
Evan Chengf5cd4f02008-10-23 20:43:13 +0000522 MII = Barrier;
Evan Cheng2efe3fd2008-10-24 05:53:44 +0000523 else
Evan Chengf5cd4f02008-10-23 20:43:13 +0000524 MII = MBB->end();
Evan Chengd0e32c52008-10-29 05:06:14 +0000525 while (MII != MEE) {
526 --MII;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000527 MachineInstr *UseMI = &*MII;
528 if (!UseMIs.count(UseMI))
529 continue;
530 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
531 MachineOperand &MO = UseMI->getOperand(i);
532 if (MO.isReg() && MO.getReg() == CurrLI->reg) {
533 LastMO = &MO;
534 break;
535 }
536 }
537 LastMI = UseMI;
538 break;
539 }
540 }
541
542 // Cut off live range from last use (or beginning of the mbb if there
543 // are no uses in it) to the end of the mbb.
544 unsigned RangeStart, RangeEnd = LIs->getMBBEndIdx(MBB)+1;
545 if (LastMI) {
546 RangeStart = LIs->getUseIndex(LIs->getInstructionIndex(LastMI))+1;
547 assert(!LastMO->isKill() && "Last use already terminates the interval?");
548 LastMO->setIsKill();
549 } else {
550 assert(MBB == BarrierMBB);
551 RangeStart = LIs->getMBBStartIdx(MBB);
552 }
553 if (MBB == BarrierMBB)
Evan Cheng06587492008-10-24 02:05:00 +0000554 RangeEnd = LIs->getUseIndex(BarrierIdx)+1;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000555 CurrLI->removeRange(RangeStart, RangeEnd);
Evan Chengd0e32c52008-10-29 05:06:14 +0000556 if (LastMI)
557 CurrLI->addKill(ValNo, RangeStart);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000558
559 // Return true if the last use becomes a new kill.
560 return LastMI;
561}
562
563/// ShrinkWrapLiveInterval - Recursively traverse the predecessor
564/// chain to find the new 'kills' and shrink wrap the live interval to the
565/// new kill indices.
566void
Evan Chengaaf510c2008-10-26 07:49:03 +0000567PreAllocSplitting::ShrinkWrapLiveInterval(VNInfo *ValNo, MachineBasicBlock *MBB,
568 MachineBasicBlock *SuccMBB, MachineBasicBlock *DefMBB,
Evan Cheng06587492008-10-24 02:05:00 +0000569 SmallPtrSet<MachineBasicBlock*, 8> &Visited,
570 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > &Uses,
571 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > &UseMIs,
572 SmallVector<MachineBasicBlock*, 4> &UseMBBs) {
Evan Chengaaf510c2008-10-26 07:49:03 +0000573 if (Visited.count(MBB))
Evan Chengf5cd4f02008-10-23 20:43:13 +0000574 return;
575
Evan Chengaaf510c2008-10-26 07:49:03 +0000576 // If live interval is live in another successor path, then we can't process
577 // this block. But we may able to do so after all the successors have been
578 // processed.
Evan Chengf62ce372008-10-28 00:47:49 +0000579 if (MBB != BarrierMBB) {
580 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
581 SE = MBB->succ_end(); SI != SE; ++SI) {
582 MachineBasicBlock *SMBB = *SI;
583 if (SMBB == SuccMBB)
584 continue;
585 if (CurrLI->liveAt(LIs->getMBBStartIdx(SMBB)))
586 return;
587 }
Evan Chengaaf510c2008-10-26 07:49:03 +0000588 }
589
590 Visited.insert(MBB);
591
Evan Cheng06587492008-10-24 02:05:00 +0000592 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
593 UMII = Uses.find(MBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000594 if (UMII != Uses.end()) {
595 // At least one use in this mbb, lets look for the kill.
Evan Cheng06587492008-10-24 02:05:00 +0000596 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
597 UMII2 = UseMIs.find(MBB);
Evan Chengd0e32c52008-10-29 05:06:14 +0000598 if (ShrinkWrapToLastUse(MBB, ValNo, UMII->second, UMII2->second))
Evan Chengf5cd4f02008-10-23 20:43:13 +0000599 // Found a kill, shrink wrapping of this path ends here.
600 return;
Evan Cheng06587492008-10-24 02:05:00 +0000601 } else if (MBB == DefMBB) {
Evan Cheng06587492008-10-24 02:05:00 +0000602 // There are no uses after the def.
603 MachineInstr *DefMI = LIs->getInstructionFromIndex(ValNo->def);
Evan Cheng06587492008-10-24 02:05:00 +0000604 if (UseMBBs.empty()) {
605 // The only use must be below barrier in the barrier block. It's safe to
606 // remove the def.
607 LIs->RemoveMachineInstrFromMaps(DefMI);
608 DefMI->eraseFromParent();
609 CurrLI->removeRange(ValNo->def, LIs->getMBBEndIdx(MBB)+1);
610 }
Evan Cheng79d5b5a2008-10-25 23:49:39 +0000611 } else if (MBB == BarrierMBB) {
612 // Remove entire live range from start of mbb to barrier.
613 CurrLI->removeRange(LIs->getMBBStartIdx(MBB),
614 LIs->getUseIndex(BarrierIdx)+1);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000615 } else {
Evan Cheng79d5b5a2008-10-25 23:49:39 +0000616 // Remove entire live range of the mbb out of the live interval.
Evan Cheng06587492008-10-24 02:05:00 +0000617 CurrLI->removeRange(LIs->getMBBStartIdx(MBB), LIs->getMBBEndIdx(MBB)+1);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000618 }
619
620 if (MBB == DefMBB)
621 // Reached the def mbb, stop traversing this path further.
622 return;
623
624 // Traverse the pathes up the predecessor chains further.
625 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
626 PE = MBB->pred_end(); PI != PE; ++PI) {
627 MachineBasicBlock *Pred = *PI;
628 if (Pred == MBB)
629 continue;
630 if (Pred == DefMBB && ValNo->hasPHIKill)
631 // Pred is the def bb and the def reaches other val#s, we must
632 // allow the value to be live out of the bb.
633 continue;
Owen Anderson80fe8732008-11-11 22:11:27 +0000634 if (!CurrLI->liveAt(LIs->getMBBEndIdx(Pred)-1))
635 return;
Evan Chengaaf510c2008-10-26 07:49:03 +0000636 ShrinkWrapLiveInterval(ValNo, Pred, MBB, DefMBB, Visited,
637 Uses, UseMIs, UseMBBs);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000638 }
639
640 return;
641}
642
Owen Anderson75fa96b2008-11-19 04:28:29 +0000643bool PreAllocSplitting::Rematerialize(unsigned vreg, VNInfo* ValNo,
644 MachineInstr* DefMI,
645 MachineBasicBlock::iterator RestorePt,
646 unsigned RestoreIdx,
647 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
648 MachineBasicBlock& MBB = *RestorePt->getParent();
649
650 MachineBasicBlock::iterator KillPt = BarrierMBB->end();
651 unsigned KillIdx = 0;
652 if (ValNo->def == ~0U || DefMI->getParent() == BarrierMBB)
653 KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, KillIdx);
654 else
655 KillPt = findNextEmptySlot(DefMI->getParent(), DefMI, KillIdx);
656
657 if (KillPt == DefMI->getParent()->end())
658 return false;
659
660 TII->reMaterialize(MBB, RestorePt, vreg, DefMI);
661 LIs->InsertMachineInstrInMaps(prior(RestorePt), RestoreIdx);
662
663 if (KillPt->getParent() == BarrierMBB) {
664 UpdateRegisterInterval(ValNo, LIs->getUseIndex(KillIdx)+1,
665 LIs->getDefIndex(RestoreIdx));
666
667 ++NumSplits;
668 ++NumRemats;
669 return true;
670 }
671
672 // Shrink wrap the live interval by walking up the CFG and find the
673 // new kills.
674 // Now let's find all the uses of the val#.
675 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > Uses;
676 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > UseMIs;
677 SmallPtrSet<MachineBasicBlock*, 4> Seen;
678 SmallVector<MachineBasicBlock*, 4> UseMBBs;
679 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(CurrLI->reg),
680 UE = MRI->use_end(); UI != UE; ++UI) {
681 MachineOperand &UseMO = UI.getOperand();
682 MachineInstr *UseMI = UseMO.getParent();
683 unsigned UseIdx = LIs->getInstructionIndex(UseMI);
684 LiveInterval::iterator ULR = CurrLI->FindLiveRangeContaining(UseIdx);
685 if (ULR->valno != ValNo)
686 continue;
687 MachineBasicBlock *UseMBB = UseMI->getParent();
688 // Remember which other mbb's use this val#.
689 if (Seen.insert(UseMBB) && UseMBB != BarrierMBB)
690 UseMBBs.push_back(UseMBB);
691 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
692 UMII = Uses.find(UseMBB);
693 if (UMII != Uses.end()) {
694 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
695 UMII2 = UseMIs.find(UseMBB);
696 UMII->second.push_back(&UseMO);
697 UMII2->second.insert(UseMI);
698 } else {
699 SmallVector<MachineOperand*, 4> Ops;
700 Ops.push_back(&UseMO);
701 Uses.insert(std::make_pair(UseMBB, Ops));
702 SmallPtrSet<MachineInstr*, 4> MIs;
703 MIs.insert(UseMI);
704 UseMIs.insert(std::make_pair(UseMBB, MIs));
705 }
706 }
707
708 // Walk up the predecessor chains.
709 SmallPtrSet<MachineBasicBlock*, 8> Visited;
710 ShrinkWrapLiveInterval(ValNo, BarrierMBB, NULL, DefMI->getParent(), Visited,
711 Uses, UseMIs, UseMBBs);
712
713 // FIXME: If ValNo->hasPHIKill is false, then renumber the val# by
714 // the restore.
715
716 // Remove live range from barrier to the restore. FIXME: Find a better
717 // point to re-start the live interval.
718 UpdateRegisterInterval(ValNo, LIs->getUseIndex(BarrierIdx)+1,
719 LIs->getDefIndex(RestoreIdx));
720
721 ++NumSplits;
722 ++NumRemats;
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000723 return true;
724}
725
726MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg,
727 const TargetRegisterClass* RC,
728 MachineInstr* DefMI,
729 MachineInstr* Barrier,
730 MachineBasicBlock* MBB,
731 int& SS,
732 SmallPtrSet<MachineInstr*, 4>& RefsInMBB) {
733 MachineBasicBlock::iterator Pt = MBB->begin();
734
735 // Go top down if RefsInMBB is empty.
736 if (RefsInMBB.empty())
737 return 0;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000738
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000739 MachineBasicBlock::iterator FoldPt = Barrier;
740 while (&*FoldPt != DefMI && FoldPt != MBB->begin() &&
741 !RefsInMBB.count(FoldPt))
742 --FoldPt;
743
744 int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg, false);
745 if (OpIdx == -1)
746 return 0;
747
748 SmallVector<unsigned, 1> Ops;
749 Ops.push_back(OpIdx);
750
751 if (!TII->canFoldMemoryOperand(FoldPt, Ops))
752 return 0;
753
754 DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg);
755 if (I != IntervalSSMap.end()) {
756 SS = I->second;
757 } else {
758 SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment());
759
760 }
761
762 MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(),
763 FoldPt, Ops, SS);
764
765 if (FMI) {
766 LIs->ReplaceMachineInstrInMaps(FoldPt, FMI);
767 FMI = MBB->insert(MBB->erase(FoldPt), FMI);
768 ++NumFolds;
769
770 IntervalSSMap[vreg] = SS;
771 CurrSLI = &LSs->getOrCreateInterval(SS);
772 if (CurrSLI->hasAtLeastOneValue())
773 CurrSValNo = CurrSLI->getValNumInfo(0);
774 else
775 CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator());
776 }
777
778 return FMI;
Owen Anderson75fa96b2008-11-19 04:28:29 +0000779}
780
Evan Chengf5cd4f02008-10-23 20:43:13 +0000781/// SplitRegLiveInterval - Split (spill and restore) the given live interval
782/// so it would not cross the barrier that's being processed. Shrink wrap
783/// (minimize) the live interval to the last uses.
784bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) {
785 CurrLI = LI;
786
787 // Find live range where current interval cross the barrier.
788 LiveInterval::iterator LR =
789 CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx));
790 VNInfo *ValNo = LR->valno;
791
792 if (ValNo->def == ~1U) {
793 // Defined by a dead def? How can this be?
794 assert(0 && "Val# is defined by a dead def?");
795 abort();
796 }
797
Evan Cheng06587492008-10-24 02:05:00 +0000798 MachineInstr *DefMI = (ValNo->def != ~0U)
799 ? LIs->getInstructionFromIndex(ValNo->def) : NULL;
Evan Cheng06587492008-10-24 02:05:00 +0000800
Owen Andersonb214c692008-11-05 00:32:13 +0000801 // If this would create a new join point, do not split.
802 if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent()))
803 return false;
804
Evan Chengf5cd4f02008-10-23 20:43:13 +0000805 // Find all references in the barrier mbb.
806 SmallPtrSet<MachineInstr*, 4> RefsInMBB;
807 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg),
808 E = MRI->reg_end(); I != E; ++I) {
809 MachineInstr *RefMI = &*I;
810 if (RefMI->getParent() == BarrierMBB)
811 RefsInMBB.insert(RefMI);
812 }
813
814 // Find a point to restore the value after the barrier.
815 unsigned RestoreIndex;
816 MachineBasicBlock::iterator RestorePt =
Evan Chengf62ce372008-10-28 00:47:49 +0000817 findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000818 if (RestorePt == BarrierMBB->end())
819 return false;
820
Owen Anderson75fa96b2008-11-19 04:28:29 +0000821 if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI))
822 if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt,
823 RestoreIndex, RefsInMBB))
824 return true;
825
Evan Chengf5cd4f02008-10-23 20:43:13 +0000826 // Add a spill either before the barrier or after the definition.
Evan Cheng06587492008-10-24 02:05:00 +0000827 MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000828 const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000829 unsigned SpillIndex = 0;
Evan Cheng06587492008-10-24 02:05:00 +0000830 MachineInstr *SpillMI = NULL;
Evan Cheng985921e2008-10-27 23:29:28 +0000831 int SS = -1;
Evan Cheng78dfef72008-10-25 00:52:41 +0000832 if (ValNo->def == ~0U) {
Evan Chengf5cd4f02008-10-23 20:43:13 +0000833 // If it's defined by a phi, we must split just before the barrier.
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000834 if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier,
835 BarrierMBB, SS, RefsInMBB))) {
836 SpillIndex = LIs->getInstructionIndex(SpillMI);
837 } else {
838 MachineBasicBlock::iterator SpillPt =
839 findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex);
840 if (SpillPt == BarrierMBB->begin())
841 return false; // No gap to insert spill.
842 // Add spill.
843
844 SS = CreateSpillStackSlot(CurrLI->reg, RC);
845 TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC);
846 SpillMI = prior(SpillPt);
847 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
848 }
Evan Cheng54898932008-10-29 08:39:34 +0000849 } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def,
850 RestoreIndex, SpillIndex, SS)) {
Evan Cheng78dfef72008-10-25 00:52:41 +0000851 // If it's already split, just restore the value. There is no need to spill
852 // the def again.
Evan Chengd0e32c52008-10-29 05:06:14 +0000853 if (!DefMI)
854 return false; // Def is dead. Do nothing.
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000855
856 if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier,
857 BarrierMBB, SS, RefsInMBB))) {
858 SpillIndex = LIs->getInstructionIndex(SpillMI);
Evan Cheng1f08cc22008-10-28 05:28:21 +0000859 } else {
Owen Anderson7b9d67c2008-12-02 18:53:47 +0000860 // Check if it's possible to insert a spill after the def MI.
861 MachineBasicBlock::iterator SpillPt;
862 if (DefMBB == BarrierMBB) {
863 // Add spill after the def and the last use before the barrier.
864 SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI,
865 RefsInMBB, SpillIndex);
866 if (SpillPt == DefMBB->begin())
867 return false; // No gap to insert spill.
868 } else {
869 SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex);
870 if (SpillPt == DefMBB->end())
871 return false; // No gap to insert spill.
872 }
873 // Add spill. The store instruction kills the register if def is before
874 // the barrier in the barrier block.
875 SS = CreateSpillStackSlot(CurrLI->reg, RC);
876 TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg,
877 DefMBB == BarrierMBB, SS, RC);
878 SpillMI = prior(SpillPt);
879 LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex);
Evan Cheng1f08cc22008-10-28 05:28:21 +0000880 }
Evan Chengf5cd4f02008-10-23 20:43:13 +0000881 }
882
Evan Cheng54898932008-10-29 08:39:34 +0000883 // Remember def instruction index to spill index mapping.
884 if (DefMI && SpillMI)
885 Def2SpillMap[ValNo->def] = SpillIndex;
886
Evan Chengf5cd4f02008-10-23 20:43:13 +0000887 // Add restore.
Evan Chengf5cd4f02008-10-23 20:43:13 +0000888 TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC);
889 MachineInstr *LoadMI = prior(RestorePt);
890 LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex);
891
892 // If live interval is spilled in the same block as the barrier, just
893 // create a hole in the interval.
894 if (!DefMBB ||
Evan Cheng78dfef72008-10-25 00:52:41 +0000895 (SpillMI && SpillMI->getParent() == BarrierMBB)) {
Evan Chengd0e32c52008-10-29 05:06:14 +0000896 // Update spill stack slot live interval.
897 UpdateSpillSlotInterval(ValNo, LIs->getUseIndex(SpillIndex)+1,
898 LIs->getDefIndex(RestoreIndex));
Evan Chengf5cd4f02008-10-23 20:43:13 +0000899
Evan Chengd0e32c52008-10-29 05:06:14 +0000900 UpdateRegisterInterval(ValNo, LIs->getUseIndex(SpillIndex)+1,
901 LIs->getDefIndex(RestoreIndex));
Evan Chengf5cd4f02008-10-23 20:43:13 +0000902
Evan Chengae7fa5b2008-10-28 01:48:24 +0000903 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000904 return true;
905 }
906
Evan Chengd0e32c52008-10-29 05:06:14 +0000907 // Update spill stack slot live interval.
Evan Cheng54898932008-10-29 08:39:34 +0000908 UpdateSpillSlotInterval(ValNo, LIs->getUseIndex(SpillIndex)+1,
909 LIs->getDefIndex(RestoreIndex));
Evan Chengd0e32c52008-10-29 05:06:14 +0000910
Evan Chengf5cd4f02008-10-23 20:43:13 +0000911 // Shrink wrap the live interval by walking up the CFG and find the
912 // new kills.
913 // Now let's find all the uses of the val#.
Evan Cheng06587492008-10-24 02:05:00 +0000914 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > Uses;
915 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > UseMIs;
916 SmallPtrSet<MachineBasicBlock*, 4> Seen;
917 SmallVector<MachineBasicBlock*, 4> UseMBBs;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000918 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(CurrLI->reg),
919 UE = MRI->use_end(); UI != UE; ++UI) {
920 MachineOperand &UseMO = UI.getOperand();
921 MachineInstr *UseMI = UseMO.getParent();
922 unsigned UseIdx = LIs->getInstructionIndex(UseMI);
923 LiveInterval::iterator ULR = CurrLI->FindLiveRangeContaining(UseIdx);
924 if (ULR->valno != ValNo)
925 continue;
926 MachineBasicBlock *UseMBB = UseMI->getParent();
Evan Cheng06587492008-10-24 02:05:00 +0000927 // Remember which other mbb's use this val#.
928 if (Seen.insert(UseMBB) && UseMBB != BarrierMBB)
929 UseMBBs.push_back(UseMBB);
930 DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator
931 UMII = Uses.find(UseMBB);
932 if (UMII != Uses.end()) {
933 DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator
934 UMII2 = UseMIs.find(UseMBB);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000935 UMII->second.push_back(&UseMO);
Evan Cheng06587492008-10-24 02:05:00 +0000936 UMII2->second.insert(UseMI);
937 } else {
938 SmallVector<MachineOperand*, 4> Ops;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000939 Ops.push_back(&UseMO);
Evan Cheng06587492008-10-24 02:05:00 +0000940 Uses.insert(std::make_pair(UseMBB, Ops));
941 SmallPtrSet<MachineInstr*, 4> MIs;
942 MIs.insert(UseMI);
943 UseMIs.insert(std::make_pair(UseMBB, MIs));
Evan Chengf5cd4f02008-10-23 20:43:13 +0000944 }
945 }
946
947 // Walk up the predecessor chains.
948 SmallPtrSet<MachineBasicBlock*, 8> Visited;
Evan Chengaaf510c2008-10-26 07:49:03 +0000949 ShrinkWrapLiveInterval(ValNo, BarrierMBB, NULL, DefMBB, Visited,
Evan Cheng06587492008-10-24 02:05:00 +0000950 Uses, UseMIs, UseMBBs);
Evan Chengf5cd4f02008-10-23 20:43:13 +0000951
Evan Cheng36f3adf2008-10-31 16:41:59 +0000952 // FIXME: If ValNo->hasPHIKill is false, then renumber the val# by
953 // the restore.
954
Evan Chengf5cd4f02008-10-23 20:43:13 +0000955 // Remove live range from barrier to the restore. FIXME: Find a better
956 // point to re-start the live interval.
Evan Chengd0e32c52008-10-29 05:06:14 +0000957 UpdateRegisterInterval(ValNo, LIs->getUseIndex(BarrierIdx)+1,
Evan Chengf5cd4f02008-10-23 20:43:13 +0000958 LIs->getDefIndex(RestoreIndex));
Evan Chengf5cd4f02008-10-23 20:43:13 +0000959
Evan Chengae7fa5b2008-10-28 01:48:24 +0000960 ++NumSplits;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000961 return true;
962}
963
964/// SplitRegLiveIntervals - Split all register live intervals that cross the
965/// barrier that's being processed.
966bool
967PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs) {
968 // First find all the virtual registers whose live intervals are intercepted
969 // by the current barrier.
970 SmallVector<LiveInterval*, 8> Intervals;
971 for (const TargetRegisterClass **RC = RCs; *RC; ++RC) {
Evan Cheng23066282008-10-27 07:14:50 +0000972 if (TII->IgnoreRegisterClassBarriers(*RC))
973 continue;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000974 std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC);
975 for (unsigned i = 0, e = VRs.size(); i != e; ++i) {
976 unsigned Reg = VRs[i];
977 if (!LIs->hasInterval(Reg))
978 continue;
979 LiveInterval *LI = &LIs->getInterval(Reg);
980 if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg))
981 // Virtual register live interval is intercepted by the barrier. We
982 // should split and shrink wrap its interval if possible.
983 Intervals.push_back(LI);
984 }
985 }
986
987 // Process the affected live intervals.
988 bool Change = false;
989 while (!Intervals.empty()) {
Evan Chengae7fa5b2008-10-28 01:48:24 +0000990 if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit)
991 break;
Evan Chengf5cd4f02008-10-23 20:43:13 +0000992 LiveInterval *LI = Intervals.back();
993 Intervals.pop_back();
994 Change |= SplitRegLiveInterval(LI);
995 }
996
997 return Change;
998}
999
Owen Andersonf1f75b12008-11-04 22:22:41 +00001000bool PreAllocSplitting::createsNewJoin(LiveRange* LR,
1001 MachineBasicBlock* DefMBB,
1002 MachineBasicBlock* BarrierMBB) {
1003 if (DefMBB == BarrierMBB)
1004 return false;
1005
1006 if (LR->valno->hasPHIKill)
1007 return false;
1008
1009 unsigned MBBEnd = LIs->getMBBEndIdx(BarrierMBB);
1010 if (LR->end < MBBEnd)
1011 return false;
1012
1013 MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>();
1014 if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB))
1015 return true;
1016
1017 MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>();
1018 SmallPtrSet<MachineBasicBlock*, 4> Visited;
1019 typedef std::pair<MachineBasicBlock*,
1020 MachineBasicBlock::succ_iterator> ItPair;
1021 SmallVector<ItPair, 4> Stack;
1022 Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin()));
1023
1024 while (!Stack.empty()) {
1025 ItPair P = Stack.back();
1026 Stack.pop_back();
1027
1028 MachineBasicBlock* PredMBB = P.first;
1029 MachineBasicBlock::succ_iterator S = P.second;
1030
1031 if (S == PredMBB->succ_end())
1032 continue;
1033 else if (Visited.count(*S)) {
1034 Stack.push_back(std::make_pair(PredMBB, ++S));
1035 continue;
1036 } else
Owen Andersonb214c692008-11-05 00:32:13 +00001037 Stack.push_back(std::make_pair(PredMBB, S+1));
Owen Andersonf1f75b12008-11-04 22:22:41 +00001038
1039 MachineBasicBlock* MBB = *S;
1040 Visited.insert(MBB);
1041
1042 if (MBB == BarrierMBB)
1043 return true;
1044
1045 MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB);
1046 MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB);
1047 MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom();
1048 while (MDTN) {
1049 if (MDTN == DefMDTN)
1050 return true;
1051 else if (MDTN == BarrierMDTN)
1052 break;
1053 MDTN = MDTN->getIDom();
1054 }
1055
1056 MBBEnd = LIs->getMBBEndIdx(MBB);
1057 if (LR->end > MBBEnd)
1058 Stack.push_back(std::make_pair(MBB, MBB->succ_begin()));
1059 }
1060
1061 return false;
1062}
1063
1064
Evan Cheng09e8ca82008-10-20 21:44:59 +00001065bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd0e32c52008-10-29 05:06:14 +00001066 CurrMF = &MF;
1067 TM = &MF.getTarget();
1068 TII = TM->getInstrInfo();
1069 MFI = MF.getFrameInfo();
1070 MRI = &MF.getRegInfo();
1071 LIs = &getAnalysis<LiveIntervals>();
1072 LSs = &getAnalysis<LiveStacks>();
Evan Chengf5cd4f02008-10-23 20:43:13 +00001073
1074 bool MadeChange = false;
1075
1076 // Make sure blocks are numbered in order.
1077 MF.RenumberBlocks();
1078
Evan Cheng54898932008-10-29 08:39:34 +00001079#if 0
1080 // FIXME: Go top down.
1081 MachineBasicBlock *Entry = MF.begin();
1082 SmallPtrSet<MachineBasicBlock*,16> Visited;
1083
1084 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
1085 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
1086 DFI != E; ++DFI) {
1087 BarrierMBB = *DFI;
1088 for (MachineBasicBlock::iterator I = BarrierMBB->begin(),
1089 E = BarrierMBB->end(); I != E; ++I) {
1090 Barrier = &*I;
1091 const TargetRegisterClass **BarrierRCs =
1092 Barrier->getDesc().getRegClassBarriers();
1093 if (!BarrierRCs)
1094 continue;
1095 BarrierIdx = LIs->getInstructionIndex(Barrier);
1096 MadeChange |= SplitRegLiveIntervals(BarrierRCs);
1097 }
1098 }
1099#else
Evan Chengf5cd4f02008-10-23 20:43:13 +00001100 for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
1101 I != E; ++I) {
1102 BarrierMBB = &*I;
1103 for (MachineBasicBlock::reverse_iterator II = BarrierMBB->rbegin(),
1104 EE = BarrierMBB->rend(); II != EE; ++II) {
1105 Barrier = &*II;
1106 const TargetRegisterClass **BarrierRCs =
1107 Barrier->getDesc().getRegClassBarriers();
1108 if (!BarrierRCs)
1109 continue;
1110 BarrierIdx = LIs->getInstructionIndex(Barrier);
1111 MadeChange |= SplitRegLiveIntervals(BarrierRCs);
1112 }
1113 }
Evan Cheng54898932008-10-29 08:39:34 +00001114#endif
Evan Chengf5cd4f02008-10-23 20:43:13 +00001115
1116 return MadeChange;
Evan Cheng09e8ca82008-10-20 21:44:59 +00001117}