blob: e6a7b70c7d6372a01213f7c30107b4cfc1c8cfcc [file] [log] [blame]
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +00001//===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===//
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 contains the SplitAnalysis class as well as mutator functions for
11// live range splitting.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "splitter"
16#include "SplitKit.h"
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000017#include "VirtRegMap.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000018#include "llvm/CodeGen/LiveIntervalAnalysis.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000021#include "llvm/CodeGen/MachineLoopInfo.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000023#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000026#include "llvm/Target/TargetInstrInfo.h"
27#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000028
29using namespace llvm;
30
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000031static cl::opt<bool>
32AllowSplit("spiller-splits-edges",
33 cl::desc("Allow critical edge splitting during spilling"));
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000034
35//===----------------------------------------------------------------------===//
36// Split Analysis
37//===----------------------------------------------------------------------===//
38
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000039SplitAnalysis::SplitAnalysis(const MachineFunction &mf,
40 const LiveIntervals &lis,
41 const MachineLoopInfo &mli)
42 : mf_(mf),
43 lis_(lis),
44 loops_(mli),
45 tii_(*mf.getTarget().getInstrInfo()),
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000046 curli_(0) {}
47
48void SplitAnalysis::clear() {
49 usingInstrs_.clear();
50 usingBlocks_.clear();
51 usingLoops_.clear();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000052 curli_ = 0;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000053}
54
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000055bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) {
56 MachineBasicBlock *T, *F;
57 SmallVector<MachineOperand, 4> Cond;
58 return !tii_.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond);
59}
60
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +000061/// analyzeUses - Count instructions, basic blocks, and loops using curli.
62void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000063 const MachineRegisterInfo &MRI = mf_.getRegInfo();
64 for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(curli_->reg);
65 MachineInstr *MI = I.skipInstruction();) {
66 if (MI->isDebugValue() || !usingInstrs_.insert(MI))
67 continue;
68 MachineBasicBlock *MBB = MI->getParent();
69 if (usingBlocks_[MBB]++)
70 continue;
71 if (MachineLoop *Loop = loops_.getLoopFor(MBB))
72 usingLoops_.insert(Loop);
73 }
74 DEBUG(dbgs() << "Counted "
75 << usingInstrs_.size() << " instrs, "
76 << usingBlocks_.size() << " blocks, "
77 << usingLoops_.size() << " loops in "
78 << *curli_ << "\n");
79}
80
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000081// Get three sets of basic blocks surrounding a loop: Blocks inside the loop,
82// predecessor blocks, and exit blocks.
83void SplitAnalysis::getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks) {
84 Blocks.clear();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000085
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000086 // Blocks in the loop.
87 Blocks.Loop.insert(Loop->block_begin(), Loop->block_end());
88
89 // Predecessor blocks.
90 const MachineBasicBlock *Header = Loop->getHeader();
91 for (MachineBasicBlock::const_pred_iterator I = Header->pred_begin(),
92 E = Header->pred_end(); I != E; ++I)
93 if (!Blocks.Loop.count(*I))
94 Blocks.Preds.insert(*I);
95
96 // Exit blocks.
97 for (MachineLoop::block_iterator I = Loop->block_begin(),
98 E = Loop->block_end(); I != E; ++I) {
99 const MachineBasicBlock *MBB = *I;
100 for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
101 SE = MBB->succ_end(); SI != SE; ++SI)
102 if (!Blocks.Loop.count(*SI))
103 Blocks.Exits.insert(*SI);
104 }
105}
106
107/// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
108/// and around the Loop.
109SplitAnalysis::LoopPeripheralUse SplitAnalysis::
110analyzeLoopPeripheralUse(const SplitAnalysis::LoopBlocks &Blocks) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000111 LoopPeripheralUse use = ContainedInLoop;
112 for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end();
113 I != E; ++I) {
114 const MachineBasicBlock *MBB = I->first;
115 // Is this a peripheral block?
116 if (use < MultiPeripheral &&
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000117 (Blocks.Preds.count(MBB) || Blocks.Exits.count(MBB))) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000118 if (I->second > 1) use = MultiPeripheral;
119 else use = SinglePeripheral;
120 continue;
121 }
122 // Is it a loop block?
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000123 if (Blocks.Loop.count(MBB))
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000124 continue;
125 // It must be an unrelated block.
126 return OutsideLoop;
127 }
128 return use;
129}
130
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000131/// getCriticalExits - It may be necessary to partially break critical edges
132/// leaving the loop if an exit block has phi uses of curli. Collect the exit
133/// blocks that need special treatment into CriticalExits.
134void SplitAnalysis::getCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
135 BlockPtrSet &CriticalExits) {
136 CriticalExits.clear();
137
138 // A critical exit block contains a phi def of curli, and has a predecessor
139 // that is not in the loop nor a loop predecessor.
140 // For such an exit block, the edges carrying the new variable must be moved
141 // to a new pre-exit block.
142 for (BlockPtrSet::iterator I = Blocks.Exits.begin(), E = Blocks.Exits.end();
143 I != E; ++I) {
144 const MachineBasicBlock *Succ = *I;
145 SlotIndex SuccIdx = lis_.getMBBStartIdx(Succ);
146 VNInfo *SuccVNI = curli_->getVNInfoAt(SuccIdx);
147 // This exit may not have curli live in at all. No need to split.
148 if (!SuccVNI)
149 continue;
150 // If this is not a PHI def, it is either using a value from before the
151 // loop, or a value defined inside the loop. Both are safe.
152 if (!SuccVNI->isPHIDef() || SuccVNI->def.getBaseIndex() != SuccIdx)
153 continue;
154 // This exit block does have a PHI. Does it also have a predecessor that is
155 // not a loop block or loop predecessor?
156 for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(),
157 PE = Succ->pred_end(); PI != PE; ++PI) {
158 const MachineBasicBlock *Pred = *PI;
159 if (Blocks.Loop.count(Pred) || Blocks.Preds.count(Pred))
160 continue;
161 // This is a critical exit block, and we need to split the exit edge.
162 CriticalExits.insert(Succ);
163 break;
164 }
165 }
166}
167
168/// canSplitCriticalExits - Return true if it is possible to insert new exit
169/// blocks before the blocks in CriticalExits.
170bool
171SplitAnalysis::canSplitCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
172 BlockPtrSet &CriticalExits) {
173 // If we don't allow critical edge splitting, require no critical exits.
174 if (!AllowSplit)
175 return CriticalExits.empty();
176
177 for (BlockPtrSet::iterator I = CriticalExits.begin(), E = CriticalExits.end();
178 I != E; ++I) {
179 const MachineBasicBlock *Succ = *I;
180 // We want to insert a new pre-exit MBB before Succ, and change all the
181 // in-loop blocks to branch to the pre-exit instead of Succ.
182 // Check that all the in-loop predecessors can be changed.
183 for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(),
184 PE = Succ->pred_end(); PI != PE; ++PI) {
185 const MachineBasicBlock *Pred = *PI;
186 // The external predecessors won't be altered.
187 if (!Blocks.Loop.count(Pred) && !Blocks.Preds.count(Pred))
188 continue;
189 if (!canAnalyzeBranch(Pred))
190 return false;
191 }
192
193 // If Succ's layout predecessor falls through, that too must be analyzable.
194 // We need to insert the pre-exit block in the gap.
195 MachineFunction::const_iterator MFI = Succ;
196 if (MFI == mf_.begin())
197 continue;
198 if (!canAnalyzeBranch(--MFI))
199 return false;
200 }
201 // No problems found.
202 return true;
203}
204
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000205void SplitAnalysis::analyze(const LiveInterval *li) {
206 clear();
207 curli_ = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000208 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000209}
210
211const MachineLoop *SplitAnalysis::getBestSplitLoop() {
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000212 assert(curli_ && "Call analyze() before getBestSplitLoop");
213 if (usingLoops_.empty())
214 return 0;
215
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000216 LoopPtrSet Loops, SecondLoops;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000217 LoopBlocks Blocks;
218 BlockPtrSet CriticalExits;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000219
220 // Find first-class and second class candidate loops.
221 // We prefer to split around loops where curli is used outside the periphery.
222 for (LoopPtrSet::const_iterator I = usingLoops_.begin(),
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000223 E = usingLoops_.end(); I != E; ++I) {
224 getLoopBlocks(*I, Blocks);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000225
226 // FIXME: We need an SSA updater to properly handle multiple exit blocks.
227 if (Blocks.Exits.size() > 1) {
228 DEBUG(dbgs() << "MultipleExits: " << **I);
229 continue;
230 }
231
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000232 LoopPtrSet *LPS = 0;
233 switch(analyzeLoopPeripheralUse(Blocks)) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000234 case OutsideLoop:
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000235 LPS = &Loops;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000236 break;
237 case MultiPeripheral:
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000238 LPS = &SecondLoops;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000239 break;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000240 case ContainedInLoop:
241 DEBUG(dbgs() << "ContainedInLoop: " << **I);
242 continue;
243 case SinglePeripheral:
244 DEBUG(dbgs() << "SinglePeripheral: " << **I);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000245 continue;
246 }
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000247 // Will it be possible to split around this loop?
248 getCriticalExits(Blocks, CriticalExits);
249 DEBUG(dbgs() << CriticalExits.size() << " critical exits: " << **I);
250 if (!canSplitCriticalExits(Blocks, CriticalExits))
251 continue;
252 // This is a possible split.
253 assert(LPS);
254 LPS->insert(*I);
255 }
256
257 DEBUG(dbgs() << "Got " << Loops.size() << " + " << SecondLoops.size()
258 << " candidate loops\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000259
260 // If there are no first class loops available, look at second class loops.
261 if (Loops.empty())
262 Loops = SecondLoops;
263
264 if (Loops.empty())
265 return 0;
266
267 // Pick the earliest loop.
268 // FIXME: Are there other heuristics to consider?
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000269 const MachineLoop *Best = 0;
270 SlotIndex BestIdx;
271 for (LoopPtrSet::const_iterator I = Loops.begin(), E = Loops.end(); I != E;
272 ++I) {
273 SlotIndex Idx = lis_.getMBBStartIdx((*I)->getHeader());
274 if (!Best || Idx < BestIdx)
275 Best = *I, BestIdx = Idx;
276 }
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000277 DEBUG(dbgs() << "Best: " << *Best);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000278 return Best;
279}
280
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000281
282//===----------------------------------------------------------------------===//
283// Split Editor
284//===----------------------------------------------------------------------===//
285
286/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000287SplitEditor::SplitEditor(SplitAnalysis &sa, LiveIntervals &lis, VirtRegMap &vrm,
288 std::vector<LiveInterval*> &intervals)
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000289 : sa_(sa), lis_(lis), vrm_(vrm),
290 mri_(vrm.getMachineFunction().getRegInfo()),
291 tii_(*vrm.getMachineFunction().getTarget().getInstrInfo()),
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000292 curli_(sa_.getCurLI()),
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000293 dupli_(0), openli_(0),
294 intervals_(intervals),
295 firstInterval(intervals_.size())
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000296{
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000297 assert(curli_ && "SplitEditor created from empty SplitAnalysis");
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000298
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000299 // Make sure curli_ is assigned a stack slot, so all our intervals get the
300 // same slot as curli_.
301 if (vrm_.getStackSlot(curli_->reg) == VirtRegMap::NO_STACK_SLOT)
302 vrm_.assignVirt2StackSlot(curli_->reg);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000303
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000304}
305
306LiveInterval *SplitEditor::createInterval() {
307 unsigned curli = sa_.getCurLI()->reg;
308 unsigned Reg = mri_.createVirtualRegister(mri_.getRegClass(curli));
309 LiveInterval &Intv = lis_.getOrCreateInterval(Reg);
310 vrm_.grow();
311 vrm_.assignVirt2StackSlot(Reg, vrm_.getStackSlot(curli));
312 return &Intv;
313}
314
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000315LiveInterval *SplitEditor::getDupLI() {
316 if (!dupli_) {
317 // Create an interval for dupli that is a copy of curli.
318 dupli_ = createInterval();
319 dupli_->Copy(*curli_, &mri_, lis_.getVNInfoAllocator());
320 DEBUG(dbgs() << "SplitEditor DupLI: " << *dupli_ << '\n');
321 }
322 return dupli_;
323}
324
325VNInfo *SplitEditor::mapValue(const VNInfo *curliVNI) {
326 VNInfo *&VNI = valueMap_[curliVNI];
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000327 if (!VNI)
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000328 VNI = openli_->createValueCopy(curliVNI, lis_.getVNInfoAllocator());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000329 return VNI;
330}
331
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000332/// Insert a COPY instruction curli -> li. Allocate a new value from li
333/// defined by the COPY. Note that rewrite() will deal with the curli
334/// register, so this function can be used to copy from any interval - openli,
335/// curli, or dupli.
336VNInfo *SplitEditor::insertCopy(LiveInterval &LI,
337 MachineBasicBlock &MBB,
338 MachineBasicBlock::iterator I) {
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000339 MachineInstr *MI = BuildMI(MBB, I, DebugLoc(), tii_.get(TargetOpcode::COPY),
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000340 LI.reg).addReg(curli_->reg);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000341 SlotIndex DefIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex();
342 return LI.getNextValue(DefIdx, MI, true, lis_.getVNInfoAllocator());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000343}
344
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000345/// Create a new virtual register and live interval.
346void SplitEditor::openIntv() {
347 assert(!openli_ && "Previous LI not closed before openIntv");
348 openli_ = createInterval();
349 intervals_.push_back(openli_);
350 liveThrough_ = false;
351}
352
353/// enterIntvAtEnd - Enter openli at the end of MBB.
354/// PhiMBB is a successor inside openli where a PHI value is created.
355/// Currently, all entries must share the same PhiMBB.
356void SplitEditor::enterIntvAtEnd(MachineBasicBlock &A, MachineBasicBlock &B) {
357 assert(openli_ && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000358
359 SlotIndex EndA = lis_.getMBBEndIdx(&A);
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000360 VNInfo *CurVNIA = curli_->getVNInfoAt(EndA.getPrevIndex());
361 if (!CurVNIA) {
362 DEBUG(dbgs() << " ignoring enterIntvAtEnd, curli not live out of BB#"
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000363 << A.getNumber() << ".\n");
364 return;
365 }
366
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000367 // Add a phi kill value and live range out of A.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000368 VNInfo *VNIA = insertCopy(*openli_, A, A.getFirstTerminator());
369 openli_->addRange(LiveRange(VNIA->def, EndA, VNIA));
370
371 // FIXME: If this is the only entry edge, we don't need the extra PHI value.
372 // FIXME: If there are multiple entry blocks (so not a loop), we need proper
373 // SSA update.
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000374
375 // Now look at the start of B.
376 SlotIndex StartB = lis_.getMBBStartIdx(&B);
377 SlotIndex EndB = lis_.getMBBEndIdx(&B);
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000378 const LiveRange *CurB = curli_->getLiveRangeContaining(StartB);
379 if (!CurB) {
380 DEBUG(dbgs() << " enterIntvAtEnd: curli not live in to BB#"
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000381 << B.getNumber() << ".\n");
382 return;
383 }
384
385 VNInfo *VNIB = openli_->getVNInfoAt(StartB);
386 if (!VNIB) {
387 // Create a phi value.
388 VNIB = openli_->getNextValue(SlotIndex(StartB, true), 0, false,
389 lis_.getVNInfoAllocator());
390 VNIB->setIsPHIDef(true);
391 // Add a minimal range for the new value.
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000392 openli_->addRange(LiveRange(VNIB->def, std::min(EndB, CurB->end), VNIB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000393
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000394 VNInfo *&mapVNI = valueMap_[CurB->valno];
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000395 if (mapVNI) {
396 // Multiple copies - must create PHI value.
397 abort();
398 } else {
399 // This is the first copy of dupLR. Mark the mapping.
400 mapVNI = VNIB;
401 }
402
403 }
404
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000405 DEBUG(dbgs() << " enterIntvAtEnd: " << *openli_ << '\n');
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000406}
407
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000408/// useIntv - indicate that all instructions in MBB should use openli.
409void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
410 useIntv(lis_.getMBBStartIdx(&MBB), lis_.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000411}
412
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000413void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
414 assert(openli_ && "openIntv not called before useIntv");
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000415
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000416 // Map the curli values from the interval into openli_
417 LiveInterval::const_iterator B = curli_->begin(), E = curli_->end();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000418 LiveInterval::const_iterator I = std::lower_bound(B, E, Start);
419
420 if (I != B) {
421 --I;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000422 // I begins before Start, but overlaps. openli may already have a value.
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000423 if (I->end > Start && !openli_->liveAt(Start))
424 openli_->addRange(LiveRange(Start, std::min(End, I->end),
425 mapValue(I->valno)));
426 ++I;
427 }
428
429 // The remaining ranges begin after Start.
430 for (;I != E && I->start < End; ++I)
431 openli_->addRange(LiveRange(I->start, std::min(End, I->end),
432 mapValue(I->valno)));
433 DEBUG(dbgs() << " added range [" << Start << ';' << End << "): " << *openli_
434 << '\n');
435}
436
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000437/// leaveIntvAtTop - Leave the interval at the top of MBB.
438/// Currently, only one value can leave the interval.
439void SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
440 assert(openli_ && "openIntv not called before leaveIntvAtTop");
441
442 SlotIndex Start = lis_.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000443 const LiveRange *CurLR = curli_->getLiveRangeContaining(Start);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000444
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000445 // Is curli even live-in to MBB?
446 if (!CurLR) {
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000447 DEBUG(dbgs() << " leaveIntvAtTop at " << Start << ": not live\n");
448 return;
449 }
450
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000451 // Is curli defined by PHI at the beginning of MBB?
452 bool isPHIDef = CurLR->valno->isPHIDef() &&
453 CurLR->valno->def.getBaseIndex() == Start;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000454
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000455 // If MBB is using a value of curli that was defined outside the openli range,
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000456 // we don't want to copy it back here.
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000457 if (!isPHIDef && !openli_->liveAt(CurLR->valno->def)) {
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000458 DEBUG(dbgs() << " leaveIntvAtTop at " << Start
459 << ": using external value\n");
460 liveThrough_ = true;
461 return;
462 }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000463
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000464 // We are going to insert a back copy, so we must have a dupli_.
465 LiveRange *DupLR = getDupLI()->getLiveRangeContaining(Start);
466 assert(DupLR && "dupli not live into black, but curli is?");
467
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000468 // Insert the COPY instruction.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000469 MachineInstr *MI = BuildMI(MBB, MBB.begin(), DebugLoc(),
Jakob Stoklund Olesenb85f5382010-08-06 18:04:17 +0000470 tii_.get(TargetOpcode::COPY), dupli_->reg)
471 .addReg(openli_->reg);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000472 SlotIndex Idx = lis_.InsertMachineInstrInMaps(MI).getDefIndex();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000473
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000474 // Adjust dupli and openli values.
475 if (isPHIDef) {
476 // dupli was already a PHI on entry to MBB. Simply insert an openli PHI,
477 // and shift the dupli def down to the COPY.
478 VNInfo *VNI = openli_->getNextValue(SlotIndex(Start, true), 0, false,
479 lis_.getVNInfoAllocator());
480 VNI->setIsPHIDef(true);
481 openli_->addRange(LiveRange(VNI->def, Idx, VNI));
482
483 dupli_->removeRange(Start, Idx);
484 DupLR->valno->def = Idx;
485 DupLR->valno->setIsPHIDef(false);
486 } else {
487 // The dupli value was defined somewhere inside the openli range.
488 DEBUG(dbgs() << " leaveIntvAtTop source value defined at "
489 << DupLR->valno->def << "\n");
490 // FIXME: We may not need a PHI here if all predecessors have the same
491 // value.
492 VNInfo *VNI = openli_->getNextValue(SlotIndex(Start, true), 0, false,
493 lis_.getVNInfoAllocator());
494 VNI->setIsPHIDef(true);
495 openli_->addRange(LiveRange(VNI->def, Idx, VNI));
496
497 // FIXME: What if DupLR->valno is used by multiple exits? SSA Update.
498
499 // closeIntv is going to remove the superfluous live ranges.
500 DupLR->valno->def = Idx;
501 DupLR->valno->setIsPHIDef(false);
502 }
503
504 DEBUG(dbgs() << " leaveIntvAtTop at " << Idx << ": " << *openli_ << '\n');
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000505}
506
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000507/// closeIntv - Indicate that we are done editing the currently open
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000508/// LiveInterval, and ranges can be trimmed.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000509void SplitEditor::closeIntv() {
510 assert(openli_ && "openIntv not called before closeIntv");
511
512 DEBUG(dbgs() << " closeIntv cleaning up\n");
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000513 DEBUG(dbgs() << " open " << *openli_ << '\n');
514
515 if (liveThrough_) {
516 DEBUG(dbgs() << " value live through region, leaving dupli as is.\n");
517 } else {
518 // live out with copies inserted, or killed by region. Either way we need to
519 // remove the overlapping region from dupli.
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000520 getDupLI();
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000521 for (LiveInterval::iterator I = openli_->begin(), E = openli_->end();
522 I != E; ++I) {
523 dupli_->removeRange(I->start, I->end);
524 }
525 // FIXME: A block branching to the entry block may also branch elsewhere
526 // curli is live. We need both openli and curli to be live in that case.
527 DEBUG(dbgs() << " dup2 " << *dupli_ << '\n');
528 }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000529 openli_ = 0;
530}
531
532/// rewrite - after all the new live ranges have been created, rewrite
533/// instructions using curli to use the new intervals.
534void SplitEditor::rewrite() {
535 assert(!openli_ && "Previous LI not closed before rewrite");
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000536 const LiveInterval *curli = sa_.getCurLI();
537 for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(curli->reg),
538 RE = mri_.reg_end(); RI != RE;) {
539 MachineOperand &MO = RI.getOperand();
540 MachineInstr *MI = MO.getParent();
541 ++RI;
542 if (MI->isDebugValue()) {
543 DEBUG(dbgs() << "Zapping " << *MI);
544 // FIXME: We can do much better with debug values.
545 MO.setReg(0);
546 continue;
547 }
548 SlotIndex Idx = lis_.getInstructionIndex(MI);
549 Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
550 LiveInterval *LI = dupli_;
551 for (unsigned i = firstInterval, e = intervals_.size(); i != e; ++i) {
552 LiveInterval *testli = intervals_[i];
553 if (testli->liveAt(Idx)) {
554 LI = testli;
555 break;
556 }
557 }
558 if (LI)
559 MO.setReg(LI->reg);
560 DEBUG(dbgs() << "rewrite " << Idx << '\t' << *MI);
561 }
562
563 // dupli_ goes in last, after rewriting.
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000564 if (dupli_) {
565 dupli_->RenumberValues();
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000566 intervals_.push_back(dupli_);
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000567 }
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000568
569 // FIXME: *Calculate spill weights, allocation hints, and register classes for
570 // firstInterval..
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000571}
572
573
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000574//===----------------------------------------------------------------------===//
575// Loop Splitting
576//===----------------------------------------------------------------------===//
577
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000578bool SplitEditor::splitAroundLoop(const MachineLoop *Loop) {
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000579 SplitAnalysis::LoopBlocks Blocks;
580 sa_.getLoopBlocks(Loop, Blocks);
581
582 // Break critical edges as needed.
583 SplitAnalysis::BlockPtrSet CriticalExits;
584 sa_.getCriticalExits(Blocks, CriticalExits);
585 assert(CriticalExits.empty() && "Cannot break critical exits yet");
586
587 // Create new live interval for the loop.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000588 openIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000589
590 // Insert copies in the predecessors.
591 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Preds.begin(),
592 E = Blocks.Preds.end(); I != E; ++I) {
593 MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000594 enterIntvAtEnd(MBB, *Loop->getHeader());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000595 }
596
597 // Switch all loop blocks.
598 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Loop.begin(),
599 E = Blocks.Loop.end(); I != E; ++I)
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000600 useIntv(**I);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000601
602 // Insert back copies in the exit blocks.
603 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Exits.begin(),
604 E = Blocks.Exits.end(); I != E; ++I) {
605 MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000606 leaveIntvAtTop(MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000607 }
608
609 // Done.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000610 closeIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000611 rewrite();
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000612 return dupli_;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000613}
614