blob: a89a977695fd047a062a82990f03a1e615b3b181 [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 Olesena17768f2010-10-14 23:49:52 +000017#include "LiveRangeEdit.h"
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000018#include "VirtRegMap.h"
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +000019#include "llvm/CodeGen/CalcSpillWeights.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000020#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000022#include "llvm/CodeGen/MachineLoopInfo.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000024#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000025#include "llvm/Support/Debug.h"
26#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000027#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000029
30using namespace llvm;
31
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000032static cl::opt<bool>
33AllowSplit("spiller-splits-edges",
34 cl::desc("Allow critical edge splitting during spilling"));
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000035
36//===----------------------------------------------------------------------===//
37// Split Analysis
38//===----------------------------------------------------------------------===//
39
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000040SplitAnalysis::SplitAnalysis(const MachineFunction &mf,
41 const LiveIntervals &lis,
42 const MachineLoopInfo &mli)
43 : mf_(mf),
44 lis_(lis),
45 loops_(mli),
46 tii_(*mf.getTarget().getInstrInfo()),
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000047 curli_(0) {}
48
49void SplitAnalysis::clear() {
50 usingInstrs_.clear();
51 usingBlocks_.clear();
52 usingLoops_.clear();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000053 curli_ = 0;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000054}
55
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000056bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) {
57 MachineBasicBlock *T, *F;
58 SmallVector<MachineOperand, 4> Cond;
59 return !tii_.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond);
60}
61
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +000062/// analyzeUses - Count instructions, basic blocks, and loops using curli.
63void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000064 const MachineRegisterInfo &MRI = mf_.getRegInfo();
65 for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(curli_->reg);
66 MachineInstr *MI = I.skipInstruction();) {
67 if (MI->isDebugValue() || !usingInstrs_.insert(MI))
68 continue;
69 MachineBasicBlock *MBB = MI->getParent();
70 if (usingBlocks_[MBB]++)
71 continue;
Jakob Stoklund Olesen9b90d7e2010-10-05 23:10:12 +000072 for (MachineLoop *Loop = loops_.getLoopFor(MBB); Loop;
73 Loop = Loop->getParentLoop())
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +000074 usingLoops_[Loop]++;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000075 }
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +000076 DEBUG(dbgs() << " counted "
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000077 << usingInstrs_.size() << " instrs, "
78 << usingBlocks_.size() << " blocks, "
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +000079 << usingLoops_.size() << " loops.\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000080}
81
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +000082void SplitAnalysis::print(const BlockPtrSet &B, raw_ostream &OS) const {
83 for (BlockPtrSet::const_iterator I = B.begin(), E = B.end(); I != E; ++I) {
84 unsigned count = usingBlocks_.lookup(*I);
85 OS << " BB#" << (*I)->getNumber();
86 if (count)
87 OS << '(' << count << ')';
88 }
89}
90
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000091// Get three sets of basic blocks surrounding a loop: Blocks inside the loop,
92// predecessor blocks, and exit blocks.
93void SplitAnalysis::getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks) {
94 Blocks.clear();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000095
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000096 // Blocks in the loop.
97 Blocks.Loop.insert(Loop->block_begin(), Loop->block_end());
98
99 // Predecessor blocks.
100 const MachineBasicBlock *Header = Loop->getHeader();
101 for (MachineBasicBlock::const_pred_iterator I = Header->pred_begin(),
102 E = Header->pred_end(); I != E; ++I)
103 if (!Blocks.Loop.count(*I))
104 Blocks.Preds.insert(*I);
105
106 // Exit blocks.
107 for (MachineLoop::block_iterator I = Loop->block_begin(),
108 E = Loop->block_end(); I != E; ++I) {
109 const MachineBasicBlock *MBB = *I;
110 for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
111 SE = MBB->succ_end(); SI != SE; ++SI)
112 if (!Blocks.Loop.count(*SI))
113 Blocks.Exits.insert(*SI);
114 }
115}
116
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000117void SplitAnalysis::print(const LoopBlocks &B, raw_ostream &OS) const {
118 OS << "Loop:";
119 print(B.Loop, OS);
120 OS << ", preds:";
121 print(B.Preds, OS);
122 OS << ", exits:";
123 print(B.Exits, OS);
124}
125
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000126/// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
127/// and around the Loop.
128SplitAnalysis::LoopPeripheralUse SplitAnalysis::
129analyzeLoopPeripheralUse(const SplitAnalysis::LoopBlocks &Blocks) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000130 LoopPeripheralUse use = ContainedInLoop;
131 for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end();
132 I != E; ++I) {
133 const MachineBasicBlock *MBB = I->first;
134 // Is this a peripheral block?
135 if (use < MultiPeripheral &&
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000136 (Blocks.Preds.count(MBB) || Blocks.Exits.count(MBB))) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000137 if (I->second > 1) use = MultiPeripheral;
138 else use = SinglePeripheral;
139 continue;
140 }
141 // Is it a loop block?
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000142 if (Blocks.Loop.count(MBB))
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000143 continue;
144 // It must be an unrelated block.
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000145 DEBUG(dbgs() << ", outside: BB#" << MBB->getNumber());
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000146 return OutsideLoop;
147 }
148 return use;
149}
150
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000151/// getCriticalExits - It may be necessary to partially break critical edges
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000152/// leaving the loop if an exit block has predecessors from outside the loop
153/// periphery.
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000154void SplitAnalysis::getCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
155 BlockPtrSet &CriticalExits) {
156 CriticalExits.clear();
157
Jakob Stoklund Olesen0960a652010-10-27 00:39:05 +0000158 // A critical exit block has curli live-in, and has a predecessor that is not
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000159 // in the loop nor a loop predecessor. For such an exit block, the edges
160 // carrying the new variable must be moved to a new pre-exit block.
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000161 for (BlockPtrSet::iterator I = Blocks.Exits.begin(), E = Blocks.Exits.end();
162 I != E; ++I) {
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000163 const MachineBasicBlock *Exit = *I;
164 // A single-predecessor exit block is definitely not a critical edge.
165 if (Exit->pred_size() == 1)
166 continue;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000167 // This exit may not have curli live in at all. No need to split.
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000168 if (!lis_.isLiveInToMBB(*curli_, Exit))
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000169 continue;
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000170 // Does this exit block have a predecessor that is not a loop block or loop
171 // predecessor?
172 for (MachineBasicBlock::const_pred_iterator PI = Exit->pred_begin(),
173 PE = Exit->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000174 const MachineBasicBlock *Pred = *PI;
175 if (Blocks.Loop.count(Pred) || Blocks.Preds.count(Pred))
176 continue;
177 // This is a critical exit block, and we need to split the exit edge.
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000178 CriticalExits.insert(Exit);
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000179 break;
180 }
181 }
182}
183
Jakob Stoklund Olesen0960a652010-10-27 00:39:05 +0000184void SplitAnalysis::getCriticalPreds(const SplitAnalysis::LoopBlocks &Blocks,
185 BlockPtrSet &CriticalPreds) {
186 CriticalPreds.clear();
187
188 // A critical predecessor block has curli live-out, and has a successor that
189 // has curli live-in and is not in the loop nor a loop exit block. For such a
190 // predecessor block, we must carry the value in both the 'inside' and
191 // 'outside' registers.
192 for (BlockPtrSet::iterator I = Blocks.Preds.begin(), E = Blocks.Preds.end();
193 I != E; ++I) {
194 const MachineBasicBlock *Pred = *I;
195 // Definitely not a critical edge.
196 if (Pred->succ_size() == 1)
197 continue;
198 // This block may not have curli live out at all if there is a PHI.
199 if (!lis_.isLiveOutOfMBB(*curli_, Pred))
200 continue;
201 // Does this block have a successor outside the loop?
202 for (MachineBasicBlock::const_pred_iterator SI = Pred->succ_begin(),
203 SE = Pred->succ_end(); SI != SE; ++SI) {
204 const MachineBasicBlock *Succ = *SI;
205 if (Blocks.Loop.count(Succ) || Blocks.Exits.count(Succ))
206 continue;
207 if (!lis_.isLiveInToMBB(*curli_, Succ))
208 continue;
209 // This is a critical predecessor block.
210 CriticalPreds.insert(Pred);
211 break;
212 }
213 }
214}
215
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000216/// canSplitCriticalExits - Return true if it is possible to insert new exit
217/// blocks before the blocks in CriticalExits.
218bool
219SplitAnalysis::canSplitCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
220 BlockPtrSet &CriticalExits) {
221 // If we don't allow critical edge splitting, require no critical exits.
222 if (!AllowSplit)
223 return CriticalExits.empty();
224
225 for (BlockPtrSet::iterator I = CriticalExits.begin(), E = CriticalExits.end();
226 I != E; ++I) {
227 const MachineBasicBlock *Succ = *I;
228 // We want to insert a new pre-exit MBB before Succ, and change all the
229 // in-loop blocks to branch to the pre-exit instead of Succ.
230 // Check that all the in-loop predecessors can be changed.
231 for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(),
232 PE = Succ->pred_end(); PI != PE; ++PI) {
233 const MachineBasicBlock *Pred = *PI;
234 // The external predecessors won't be altered.
235 if (!Blocks.Loop.count(Pred) && !Blocks.Preds.count(Pred))
236 continue;
237 if (!canAnalyzeBranch(Pred))
238 return false;
239 }
240
241 // If Succ's layout predecessor falls through, that too must be analyzable.
242 // We need to insert the pre-exit block in the gap.
243 MachineFunction::const_iterator MFI = Succ;
244 if (MFI == mf_.begin())
245 continue;
246 if (!canAnalyzeBranch(--MFI))
247 return false;
248 }
249 // No problems found.
250 return true;
251}
252
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000253void SplitAnalysis::analyze(const LiveInterval *li) {
254 clear();
255 curli_ = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000256 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000257}
258
259const MachineLoop *SplitAnalysis::getBestSplitLoop() {
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000260 assert(curli_ && "Call analyze() before getBestSplitLoop");
261 if (usingLoops_.empty())
262 return 0;
263
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000264 LoopPtrSet Loops;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000265 LoopBlocks Blocks;
266 BlockPtrSet CriticalExits;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000267
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000268 // We split around loops where curli is used outside the periphery.
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +0000269 for (LoopCountMap::const_iterator I = usingLoops_.begin(),
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000270 E = usingLoops_.end(); I != E; ++I) {
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +0000271 const MachineLoop *Loop = I->first;
272 getLoopBlocks(Loop, Blocks);
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000273 DEBUG({ dbgs() << " "; print(Blocks, dbgs()); });
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000274
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000275 switch(analyzeLoopPeripheralUse(Blocks)) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000276 case OutsideLoop:
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000277 break;
278 case MultiPeripheral:
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000279 // FIXME: We could split a live range with multiple uses in a peripheral
280 // block and still make progress. However, it is possible that splitting
281 // another live range will insert copies into a peripheral block, and
282 // there is a small chance we can enter an infinity loop, inserting copies
283 // forever.
284 // For safety, stick to splitting live ranges with uses outside the
285 // periphery.
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000286 DEBUG(dbgs() << ": multiple peripheral uses\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000287 break;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000288 case ContainedInLoop:
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000289 DEBUG(dbgs() << ": fully contained\n");
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000290 continue;
291 case SinglePeripheral:
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000292 DEBUG(dbgs() << ": single peripheral use\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000293 continue;
294 }
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000295 // Will it be possible to split around this loop?
296 getCriticalExits(Blocks, CriticalExits);
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000297 DEBUG(dbgs() << ": " << CriticalExits.size() << " critical exits\n");
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000298 if (!canSplitCriticalExits(Blocks, CriticalExits))
299 continue;
300 // This is a possible split.
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000301 Loops.insert(Loop);
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000302 }
303
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000304 DEBUG(dbgs() << " getBestSplitLoop found " << Loops.size()
305 << " candidate loops.\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000306
307 if (Loops.empty())
308 return 0;
309
310 // Pick the earliest loop.
311 // FIXME: Are there other heuristics to consider?
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000312 const MachineLoop *Best = 0;
313 SlotIndex BestIdx;
314 for (LoopPtrSet::const_iterator I = Loops.begin(), E = Loops.end(); I != E;
315 ++I) {
316 SlotIndex Idx = lis_.getMBBStartIdx((*I)->getHeader());
317 if (!Best || Idx < BestIdx)
318 Best = *I, BestIdx = Idx;
319 }
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000320 DEBUG(dbgs() << " getBestSplitLoop found " << *Best);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000321 return Best;
322}
323
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000324//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000325// LiveIntervalMap
326//===----------------------------------------------------------------------===//
327
Jakob Stoklund Olesenb3e96812010-09-13 21:29:45 +0000328// Work around the fact that the std::pair constructors are broken for pointer
329// pairs in some implementations. makeVV(x, 0) works.
330static inline std::pair<const VNInfo*, VNInfo*>
331makeVV(const VNInfo *a, VNInfo *b) {
332 return std::make_pair(a, b);
333}
334
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000335void LiveIntervalMap::reset(LiveInterval *li) {
336 li_ = li;
337 valueMap_.clear();
338}
339
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000340bool LiveIntervalMap::isComplexMapped(const VNInfo *ParentVNI) const {
341 ValueMap::const_iterator i = valueMap_.find(ParentVNI);
342 return i != valueMap_.end() && i->second == 0;
343}
344
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000345// defValue - Introduce a li_ def for ParentVNI that could be later than
346// ParentVNI->def.
347VNInfo *LiveIntervalMap::defValue(const VNInfo *ParentVNI, SlotIndex Idx) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000348 assert(li_ && "call reset first");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000349 assert(ParentVNI && "Mapping NULL value");
350 assert(Idx.isValid() && "Invalid SlotIndex");
351 assert(parentli_.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
352
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000353 // Create a new value.
Lang Hames6e2968c2010-09-25 12:04:16 +0000354 VNInfo *VNI = li_->getNextValue(Idx, 0, lis_.getVNInfoAllocator());
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000355
Jakob Stoklund Olesen79c02622010-10-26 22:36:02 +0000356 // Preserve the PHIDef bit.
357 if (ParentVNI->isPHIDef() && Idx == ParentVNI->def)
358 VNI->setIsPHIDef(true);
359
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000360 // Use insert for lookup, so we can add missing values with a second lookup.
361 std::pair<ValueMap::iterator,bool> InsP =
362 valueMap_.insert(makeVV(ParentVNI, Idx == ParentVNI->def ? VNI : 0));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000363
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000364 // This is now a complex def. Mark with a NULL in valueMap.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000365 if (!InsP.second)
366 InsP.first->second = 0;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000367
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000368 return VNI;
369}
370
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000371
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000372// mapValue - Find the mapped value for ParentVNI at Idx.
373// Potentially create phi-def values.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000374VNInfo *LiveIntervalMap::mapValue(const VNInfo *ParentVNI, SlotIndex Idx,
375 bool *simple) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000376 assert(li_ && "call reset first");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000377 assert(ParentVNI && "Mapping NULL value");
378 assert(Idx.isValid() && "Invalid SlotIndex");
379 assert(parentli_.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
380
381 // Use insert for lookup, so we can add missing values with a second lookup.
382 std::pair<ValueMap::iterator,bool> InsP =
Jakob Stoklund Olesenb3e96812010-09-13 21:29:45 +0000383 valueMap_.insert(makeVV(ParentVNI, 0));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000384
385 // This was an unknown value. Create a simple mapping.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000386 if (InsP.second) {
387 if (simple) *simple = true;
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000388 return InsP.first->second = li_->createValueCopy(ParentVNI,
389 lis_.getVNInfoAllocator());
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000390 }
391
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000392 // This was a simple mapped value.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000393 if (InsP.first->second) {
394 if (simple) *simple = true;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000395 return InsP.first->second;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000396 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000397
398 // This is a complex mapped value. There may be multiple defs, and we may need
399 // to create phi-defs.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000400 if (simple) *simple = false;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000401 MachineBasicBlock *IdxMBB = lis_.getMBBFromIndex(Idx);
402 assert(IdxMBB && "No MBB at Idx");
403
404 // Is there a def in the same MBB we can extend?
405 if (VNInfo *VNI = extendTo(IdxMBB, Idx))
406 return VNI;
407
408 // Now for the fun part. We know that ParentVNI potentially has multiple defs,
409 // and we may need to create even more phi-defs to preserve VNInfo SSA form.
410 // Perform a depth-first search for predecessor blocks where we know the
411 // dominating VNInfo. Insert phi-def VNInfos along the path back to IdxMBB.
412
413 // Track MBBs where we have created or learned the dominating value.
414 // This may change during the DFS as we create new phi-defs.
415 typedef DenseMap<MachineBasicBlock*, VNInfo*> MBBValueMap;
416 MBBValueMap DomValue;
Jakob Stoklund Olesen984a7fc2010-10-05 20:36:28 +0000417 typedef SplitAnalysis::BlockPtrSet BlockPtrSet;
418 BlockPtrSet Visited;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000419
Jakob Stoklund Olesen984a7fc2010-10-05 20:36:28 +0000420 // Iterate over IdxMBB predecessors in a depth-first order.
421 // Skip begin() since that is always IdxMBB.
422 for (idf_ext_iterator<MachineBasicBlock*, BlockPtrSet>
423 IDFI = llvm::next(idf_ext_begin(IdxMBB, Visited)),
424 IDFE = idf_ext_end(IdxMBB, Visited); IDFI != IDFE;) {
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000425 MachineBasicBlock *MBB = *IDFI;
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000426 SlotIndex End = lis_.getMBBEndIdx(MBB).getPrevSlot();
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000427
428 // We are operating on the restricted CFG where ParentVNI is live.
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000429 if (parentli_.getVNInfoAt(End) != ParentVNI) {
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000430 IDFI.skipChildren();
431 continue;
432 }
433
434 // Do we have a dominating value in this block?
435 VNInfo *VNI = extendTo(MBB, End);
436 if (!VNI) {
437 ++IDFI;
438 continue;
439 }
440
Jakob Stoklund Olesen984a7fc2010-10-05 20:36:28 +0000441 // Yes, VNI dominates MBB. Make sure we visit MBB again from other paths.
442 Visited.erase(MBB);
443
444 // Track the path back to IdxMBB, creating phi-defs
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000445 // as needed along the way.
446 for (unsigned PI = IDFI.getPathLength()-1; PI != 0; --PI) {
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000447 // Start from MBB's immediate successor. End at IdxMBB.
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000448 MachineBasicBlock *Succ = IDFI.getPath(PI-1);
449 std::pair<MBBValueMap::iterator, bool> InsP =
450 DomValue.insert(MBBValueMap::value_type(Succ, VNI));
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000451
452 // This is the first time we backtrack to Succ.
453 if (InsP.second)
454 continue;
455
456 // We reached Succ again with the same VNI. Nothing is going to change.
457 VNInfo *OVNI = InsP.first->second;
458 if (OVNI == VNI)
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000459 break;
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000460
461 // Succ already has a phi-def. No need to continue.
462 SlotIndex Start = lis_.getMBBStartIdx(Succ);
463 if (OVNI->def == Start)
464 break;
465
466 // We have a collision between the old and new VNI at Succ. That means
467 // neither dominates and we need a new phi-def.
Lang Hames6e2968c2010-09-25 12:04:16 +0000468 VNI = li_->getNextValue(Start, 0, lis_.getVNInfoAllocator());
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000469 VNI->setIsPHIDef(true);
470 InsP.first->second = VNI;
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000471
472 // Replace OVNI with VNI in the remaining path.
473 for (; PI > 1 ; --PI) {
474 MBBValueMap::iterator I = DomValue.find(IDFI.getPath(PI-2));
475 if (I == DomValue.end() || I->second != OVNI)
476 break;
477 I->second = VNI;
478 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000479 }
480
481 // No need to search the children, we found a dominating value.
Jakob Stoklund Olesencf16bea2010-08-18 20:06:05 +0000482 IDFI.skipChildren();
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000483 }
484
485 // The search should at least find a dominating value for IdxMBB.
486 assert(!DomValue.empty() && "Couldn't find a reaching definition");
487
488 // Since we went through the trouble of a full DFS visiting all reaching defs,
489 // the values in DomValue are now accurate. No more phi-defs are needed for
490 // these blocks, so we can color the live ranges.
491 // This makes the next mapValue call much faster.
492 VNInfo *IdxVNI = 0;
493 for (MBBValueMap::iterator I = DomValue.begin(), E = DomValue.end(); I != E;
494 ++I) {
495 MachineBasicBlock *MBB = I->first;
496 VNInfo *VNI = I->second;
497 SlotIndex Start = lis_.getMBBStartIdx(MBB);
498 if (MBB == IdxMBB) {
499 // Don't add full liveness to IdxMBB, stop at Idx.
500 if (Start != Idx)
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000501 li_->addRange(LiveRange(Start, Idx.getNextSlot(), VNI));
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000502 // The caller had better add some liveness to IdxVNI, or it leaks.
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000503 IdxVNI = VNI;
504 } else
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000505 li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000506 }
507
508 assert(IdxVNI && "Didn't find value for Idx");
509 return IdxVNI;
510}
511
512// extendTo - Find the last li_ value defined in MBB at or before Idx. The
513// parentli_ is assumed to be live at Idx. Extend the live range to Idx.
514// Return the found VNInfo, or NULL.
Jakob Stoklund Olesenc95c1462010-10-27 00:39:07 +0000515VNInfo *LiveIntervalMap::extendTo(const MachineBasicBlock *MBB, SlotIndex Idx) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000516 assert(li_ && "call reset first");
517 LiveInterval::iterator I = std::upper_bound(li_->begin(), li_->end(), Idx);
518 if (I == li_->begin())
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000519 return 0;
520 --I;
Jakob Stoklund Olesenfc60d772010-10-05 20:36:25 +0000521 if (I->end <= lis_.getMBBStartIdx(MBB))
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000522 return 0;
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000523 if (I->end <= Idx)
524 I->end = Idx.getNextSlot();
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000525 return I->valno;
526}
527
528// addSimpleRange - Add a simple range from parentli_ to li_.
529// ParentVNI must be live in the [Start;End) interval.
530void LiveIntervalMap::addSimpleRange(SlotIndex Start, SlotIndex End,
531 const VNInfo *ParentVNI) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000532 assert(li_ && "call reset first");
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000533 bool simple;
534 VNInfo *VNI = mapValue(ParentVNI, Start, &simple);
535 // A simple mapping is easy.
536 if (simple) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000537 li_->addRange(LiveRange(Start, End, VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000538 return;
539 }
540
541 // ParentVNI is a complex value. We must map per MBB.
542 MachineFunction::iterator MBB = lis_.getMBBFromIndex(Start);
Jakob Stoklund Olesendbc36092010-10-05 22:19:29 +0000543 MachineFunction::iterator MBBE = lis_.getMBBFromIndex(End.getPrevSlot());
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000544
545 if (MBB == MBBE) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000546 li_->addRange(LiveRange(Start, End, VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000547 return;
548 }
549
550 // First block.
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000551 li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000552
553 // Run sequence of full blocks.
554 for (++MBB; MBB != MBBE; ++MBB) {
555 Start = lis_.getMBBStartIdx(MBB);
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000556 li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB),
557 mapValue(ParentVNI, Start)));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000558 }
559
560 // Final block.
561 Start = lis_.getMBBStartIdx(MBB);
562 if (Start != End)
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000563 li_->addRange(LiveRange(Start, End, mapValue(ParentVNI, Start)));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000564}
565
566/// addRange - Add live ranges to li_ where [Start;End) intersects parentli_.
567/// All needed values whose def is not inside [Start;End) must be defined
568/// beforehand so mapValue will work.
569void LiveIntervalMap::addRange(SlotIndex Start, SlotIndex End) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000570 assert(li_ && "call reset first");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000571 LiveInterval::const_iterator B = parentli_.begin(), E = parentli_.end();
572 LiveInterval::const_iterator I = std::lower_bound(B, E, Start);
573
574 // Check if --I begins before Start and overlaps.
575 if (I != B) {
576 --I;
577 if (I->end > Start)
578 addSimpleRange(Start, std::min(End, I->end), I->valno);
579 ++I;
580 }
581
582 // The remaining ranges begin after Start.
583 for (;I != E && I->start < End; ++I)
584 addSimpleRange(I->start, std::min(End, I->end), I->valno);
585}
586
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000587VNInfo *LiveIntervalMap::defByCopyFrom(unsigned Reg,
588 const VNInfo *ParentVNI,
589 MachineBasicBlock &MBB,
590 MachineBasicBlock::iterator I) {
591 const TargetInstrDesc &TID = MBB.getParent()->getTarget().getInstrInfo()->
592 get(TargetOpcode::COPY);
593 MachineInstr *MI = BuildMI(MBB, I, DebugLoc(), TID, li_->reg).addReg(Reg);
594 SlotIndex DefIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex();
595 VNInfo *VNI = defValue(ParentVNI, DefIdx);
596 VNI->setCopy(MI);
597 li_->addRange(LiveRange(DefIdx, DefIdx.getNextSlot(), VNI));
598 return VNI;
599}
600
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000601//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000602// Split Editor
603//===----------------------------------------------------------------------===//
604
605/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000606SplitEditor::SplitEditor(SplitAnalysis &sa, LiveIntervals &lis, VirtRegMap &vrm,
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000607 LiveRangeEdit &edit)
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000608 : sa_(sa), lis_(lis), vrm_(vrm),
609 mri_(vrm.getMachineFunction().getRegInfo()),
610 tii_(*vrm.getMachineFunction().getTarget().getInstrInfo()),
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000611 edit_(edit),
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000612 dupli_(lis_, edit.getParent()),
613 openli_(lis_, edit.getParent())
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000614{
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000615}
616
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000617bool SplitEditor::intervalsLiveAt(SlotIndex Idx) const {
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000618 for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I)
619 if (*I != dupli_.getLI() && (*I)->liveAt(Idx))
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000620 return true;
621 return false;
622}
623
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000624/// Create a new virtual register and live interval.
625void SplitEditor::openIntv() {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000626 assert(!openli_.getLI() && "Previous LI not closed before openIntv");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000627
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000628 if (!dupli_.getLI())
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000629 dupli_.reset(&edit_.create(mri_, lis_, vrm_));
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000630
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000631 openli_.reset(&edit_.create(mri_, lis_, vrm_));
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000632}
633
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000634/// enterIntvBefore - Enter openli before the instruction at Idx. If curli is
635/// not live before Idx, a COPY is not inserted.
636void SplitEditor::enterIntvBefore(SlotIndex Idx) {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000637 assert(openli_.getLI() && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000638 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000639 VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Idx.getUseIndex());
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000640 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000641 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000642 return;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000643 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000644 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000645 truncatedValues.insert(ParentVNI);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000646 MachineInstr *MI = lis_.getInstructionFromIndex(Idx);
647 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000648 VNInfo *VNI = openli_.defByCopyFrom(edit_.getReg(), ParentVNI,
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000649 *MI->getParent(), MI);
650 openli_.getLI()->addRange(LiveRange(VNI->def, Idx.getDefIndex(), VNI));
651 DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000652}
653
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000654/// enterIntvAtEnd - Enter openli at the end of MBB.
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000655void SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000656 assert(openli_.getLI() && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000657 SlotIndex End = lis_.getMBBEndIdx(&MBB);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000658 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << End);
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000659 VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(End.getPrevSlot());
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000660 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000661 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000662 return;
663 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000664 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000665 truncatedValues.insert(ParentVNI);
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000666 VNInfo *VNI = openli_.defByCopyFrom(edit_.getReg(), ParentVNI,
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000667 MBB, MBB.getFirstTerminator());
668 // Make sure openli is live out of MBB.
669 openli_.getLI()->addRange(LiveRange(VNI->def, End, VNI));
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000670 DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000671}
672
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000673/// useIntv - indicate that all instructions in MBB should use openli.
674void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
675 useIntv(lis_.getMBBStartIdx(&MBB), lis_.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000676}
677
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000678void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000679 assert(openli_.getLI() && "openIntv not called before useIntv");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000680 openli_.addRange(Start, End);
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000681 DEBUG(dbgs() << " use [" << Start << ';' << End << "): "
682 << *openli_.getLI() << '\n');
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000683}
684
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000685/// leaveIntvAfter - Leave openli after the instruction at Idx.
686void SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000687 assert(openli_.getLI() && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000688 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000689
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000690 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000691 VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Idx.getBoundaryIndex());
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000692 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000693 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000694 return;
695 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000696 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000697
Jakob Stoklund Olesenfc60d772010-10-05 20:36:25 +0000698 MachineBasicBlock::iterator MII = lis_.getInstructionFromIndex(Idx);
699 MachineBasicBlock *MBB = MII->getParent();
700 VNInfo *VNI = dupli_.defByCopyFrom(openli_.getLI()->reg, ParentVNI, *MBB,
701 llvm::next(MII));
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000702
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000703 // Finally we must make sure that openli is properly extended from Idx to the
704 // new copy.
Jakob Stoklund Olesenfc60d772010-10-05 20:36:25 +0000705 openli_.addSimpleRange(Idx.getBoundaryIndex(), VNI->def, ParentVNI);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000706 DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000707}
708
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000709/// leaveIntvAtTop - Leave the interval at the top of MBB.
710/// Currently, only one value can leave the interval.
711void SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000712 assert(openli_.getLI() && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000713 SlotIndex Start = lis_.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000714 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000715
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000716 VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Start);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000717 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000718 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000719 return;
720 }
721
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000722 // We are going to insert a back copy, so we must have a dupli_.
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000723 VNInfo *VNI = dupli_.defByCopyFrom(openli_.getLI()->reg, ParentVNI,
724 MBB, MBB.begin());
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000725
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000726 // Finally we must make sure that openli is properly extended from Start to
727 // the new copy.
Jakob Stoklund Olesenfc60d772010-10-05 20:36:25 +0000728 openli_.addSimpleRange(Start, VNI->def, ParentVNI);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000729 DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000730}
731
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000732/// closeIntv - Indicate that we are done editing the currently open
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000733/// LiveInterval, and ranges can be trimmed.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000734void SplitEditor::closeIntv() {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000735 assert(openli_.getLI() && "openIntv not called before closeIntv");
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000736
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000737 DEBUG(dbgs() << " closeIntv cleaning up\n");
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000738 DEBUG(dbgs() << " open " << *openli_.getLI() << '\n');
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000739 openli_.reset(0);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000740}
741
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000742/// rewrite - Rewrite all uses of reg to use the new registers.
743void SplitEditor::rewrite(unsigned reg) {
744 for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(reg),
745 RE = mri_.reg_end(); RI != RE;) {
746 MachineOperand &MO = RI.getOperand();
747 MachineInstr *MI = MO.getParent();
748 ++RI;
749 if (MI->isDebugValue()) {
750 DEBUG(dbgs() << "Zapping " << *MI);
751 // FIXME: We can do much better with debug values.
752 MO.setReg(0);
753 continue;
754 }
755 SlotIndex Idx = lis_.getInstructionIndex(MI);
756 Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
757 LiveInterval *LI = 0;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000758 for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E;
759 ++I) {
760 LiveInterval *testli = *I;
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000761 if (testli->liveAt(Idx)) {
762 LI = testli;
763 break;
764 }
765 }
Jakob Stoklund Olesen9d999772010-10-21 18:47:08 +0000766 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'<< Idx);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000767 assert(LI && "No register was live at use");
768 MO.setReg(LI->reg);
Jakob Stoklund Olesen9d999772010-10-21 18:47:08 +0000769 DEBUG(dbgs() << '\t' << *MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000770 }
771}
772
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000773void
774SplitEditor::addTruncSimpleRange(SlotIndex Start, SlotIndex End, VNInfo *VNI) {
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000775 // Build vector of iterator pairs from the intervals.
776 typedef std::pair<LiveInterval::const_iterator,
777 LiveInterval::const_iterator> IIPair;
778 SmallVector<IIPair, 8> Iters;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000779 for (LiveRangeEdit::iterator LI = edit_.begin(), LE = edit_.end(); LI != LE;
780 ++LI) {
Jakob Stoklund Olesen9d999772010-10-21 18:47:08 +0000781 if (*LI == dupli_.getLI())
782 continue;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000783 LiveInterval::const_iterator I = (*LI)->find(Start);
784 LiveInterval::const_iterator E = (*LI)->end();
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000785 if (I != E)
786 Iters.push_back(std::make_pair(I, E));
787 }
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000788
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000789 SlotIndex sidx = Start;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000790 // Break [Start;End) into segments that don't overlap any intervals.
791 for (;;) {
792 SlotIndex next = sidx, eidx = End;
793 // Find overlapping intervals.
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000794 for (unsigned i = 0; i != Iters.size() && sidx < eidx; ++i) {
795 LiveInterval::const_iterator I = Iters[i].first;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000796 // Interval I is overlapping [sidx;eidx). Trim sidx.
797 if (I->start <= sidx) {
798 sidx = I->end;
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000799 // Move to the next run, remove iters when all are consumed.
800 I = ++Iters[i].first;
801 if (I == Iters[i].second) {
802 Iters.erase(Iters.begin() + i);
803 --i;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000804 continue;
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000805 }
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000806 }
807 // Trim eidx too if needed.
808 if (I->start >= eidx)
809 continue;
810 eidx = I->start;
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000811 next = I->end;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000812 }
813 // Now, [sidx;eidx) doesn't overlap anything in intervals_.
814 if (sidx < eidx)
815 dupli_.addSimpleRange(sidx, eidx, VNI);
816 // If the interval end was truncated, we can try again from next.
817 if (next <= sidx)
818 break;
819 sidx = next;
820 }
821}
822
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000823void SplitEditor::computeRemainder() {
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000824 // First we need to fill in the live ranges in dupli.
825 // If values were redefined, we need a full recoloring with SSA update.
826 // If values were truncated, we only need to truncate the ranges.
827 // If values were partially rematted, we should shrink to uses.
828 // If values were fully rematted, they should be omitted.
829 // FIXME: If a single value is redefined, just move the def and truncate.
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000830 LiveInterval &parent = edit_.getParent();
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000831
832 // Values that are fully contained in the split intervals.
833 SmallPtrSet<const VNInfo*, 8> deadValues;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000834 // Map all curli values that should have live defs in dupli.
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000835 for (LiveInterval::const_vni_iterator I = parent.vni_begin(),
836 E = parent.vni_end(); I != E; ++I) {
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000837 const VNInfo *VNI = *I;
838 // Original def is contained in the split intervals.
839 if (intervalsLiveAt(VNI->def)) {
840 // Did this value escape?
841 if (dupli_.isMapped(VNI))
842 truncatedValues.insert(VNI);
843 else
844 deadValues.insert(VNI);
845 continue;
846 }
847 // Add minimal live range at the definition.
848 VNInfo *DVNI = dupli_.defValue(VNI, VNI->def);
849 dupli_.getLI()->addRange(LiveRange(VNI->def, VNI->def.getNextSlot(), DVNI));
850 }
851
852 // Add all ranges to dupli.
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000853 for (LiveInterval::const_iterator I = parent.begin(), E = parent.end();
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000854 I != E; ++I) {
855 const LiveRange &LR = *I;
856 if (truncatedValues.count(LR.valno)) {
857 // recolor after removing intervals_.
858 addTruncSimpleRange(LR.start, LR.end, LR.valno);
859 } else if (!deadValues.count(LR.valno)) {
860 // recolor without truncation.
861 dupli_.addSimpleRange(LR.start, LR.end, LR.valno);
862 }
863 }
Jakob Stoklund Olesenc95c1462010-10-27 00:39:07 +0000864
865 // Extend dupli_ to be live out of any critical loop predecessors.
866 // This means we have multiple registers live out of those blocks.
867 // The alternative would be to split the critical edges.
868 if (criticalPreds_.empty())
869 return;
870 for (SplitAnalysis::BlockPtrSet::iterator I = criticalPreds_.begin(),
871 E = criticalPreds_.end(); I != E; ++I)
872 dupli_.extendTo(*I, lis_.getMBBEndIdx(*I).getPrevSlot());
873 criticalPreds_.clear();
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000874}
875
876void SplitEditor::finish() {
877 assert(!openli_.getLI() && "Previous LI not closed before rewrite");
878 assert(dupli_.getLI() && "No dupli for rewrite. Noop spilt?");
879
880 // Complete dupli liveness.
881 computeRemainder();
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000882
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000883 // Get rid of unused values and set phi-kill flags.
Jakob Stoklund Olesenf1354ae2010-10-26 22:36:05 +0000884 for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I)
885 (*I)->RenumberValues(lis_);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000886
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000887 // Rewrite instructions.
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000888 rewrite(edit_.getReg());
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000889
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000890 // Now check if any registers were separated into multiple components.
891 ConnectedVNInfoEqClasses ConEQ(lis_);
892 for (unsigned i = 0, e = edit_.size(); i != e; ++i) {
893 // Don't use iterators, they are invalidated by create() below.
894 LiveInterval *li = edit_.get(i);
895 unsigned NumComp = ConEQ.Classify(li);
896 if (NumComp <= 1)
897 continue;
898 DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n');
899 SmallVector<LiveInterval*, 8> dups;
900 dups.push_back(li);
901 for (unsigned i = 1; i != NumComp; ++i)
902 dups.push_back(&edit_.create(mri_, lis_, vrm_));
903 ConEQ.Distribute(&dups[0]);
904 // Rewrite uses to the new regs.
905 rewrite(li->reg);
906 }
907
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000908 // Calculate spill weight and allocation hints for new intervals.
909 VirtRegAuxInfo vrai(vrm_.getMachineFunction(), lis_, sa_.loops_);
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000910 for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I){
911 LiveInterval &li = **I;
Jakob Stoklund Olesen9db3ea42010-08-10 18:37:40 +0000912 vrai.CalculateRegClass(li.reg);
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000913 vrai.CalculateWeightAndHint(li);
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000914 DEBUG(dbgs() << " new interval " << mri_.getRegClass(li.reg)->getName()
915 << ":" << li << '\n');
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000916 }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000917}
918
919
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000920//===----------------------------------------------------------------------===//
921// Loop Splitting
922//===----------------------------------------------------------------------===//
923
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000924void SplitEditor::splitAroundLoop(const MachineLoop *Loop) {
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000925 SplitAnalysis::LoopBlocks Blocks;
926 sa_.getLoopBlocks(Loop, Blocks);
927
Jakob Stoklund Olesen452a9fd2010-10-07 18:47:07 +0000928 DEBUG({
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000929 dbgs() << " splitAround"; sa_.print(Blocks, dbgs()); dbgs() << '\n';
Jakob Stoklund Olesen452a9fd2010-10-07 18:47:07 +0000930 });
931
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000932 // Break critical edges as needed.
933 SplitAnalysis::BlockPtrSet CriticalExits;
934 sa_.getCriticalExits(Blocks, CriticalExits);
935 assert(CriticalExits.empty() && "Cannot break critical exits yet");
936
Jakob Stoklund Olesenc95c1462010-10-27 00:39:07 +0000937 // Get critical predecessors so computeRemainder can deal with them.
938 sa_.getCriticalPreds(Blocks, criticalPreds_);
939
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000940 // Create new live interval for the loop.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000941 openIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000942
943 // Insert copies in the predecessors.
944 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Preds.begin(),
945 E = Blocks.Preds.end(); I != E; ++I) {
946 MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000947 enterIntvAtEnd(MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000948 }
949
950 // Switch all loop blocks.
951 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Loop.begin(),
952 E = Blocks.Loop.end(); I != E; ++I)
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000953 useIntv(**I);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000954
955 // Insert back copies in the exit blocks.
956 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Exits.begin(),
957 E = Blocks.Exits.end(); I != E; ++I) {
958 MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000959 leaveIntvAtTop(MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000960 }
961
962 // Done.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000963 closeIntv();
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000964 finish();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000965}
966
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000967
968//===----------------------------------------------------------------------===//
969// Single Block Splitting
970//===----------------------------------------------------------------------===//
971
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000972/// getMultiUseBlocks - if curli has more than one use in a basic block, it
973/// may be an advantage to split curli for the duration of the block.
974bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) {
975 // If curli is local to one block, there is no point to splitting it.
976 if (usingBlocks_.size() <= 1)
977 return false;
978 // Add blocks with multiple uses.
979 for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end();
980 I != E; ++I)
981 switch (I->second) {
982 case 0:
983 case 1:
984 continue;
985 case 2: {
986 // When there are only two uses and curli is both live in and live out,
987 // we don't really win anything by isolating the block since we would be
988 // inserting two copies.
989 // The remaing register would still have two uses in the block. (Unless it
990 // separates into disconnected components).
991 if (lis_.isLiveInToMBB(*curli_, I->first) &&
992 lis_.isLiveOutOfMBB(*curli_, I->first))
993 continue;
994 } // Fall through.
995 default:
996 Blocks.insert(I->first);
997 }
998 return !Blocks.empty();
999}
1000
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001001/// splitSingleBlocks - Split curli into a separate live interval inside each
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +00001002/// basic block in Blocks.
1003void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) {
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +00001004 DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n");
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001005 // Determine the first and last instruction using curli in each block.
1006 typedef std::pair<SlotIndex,SlotIndex> IndexPair;
1007 typedef DenseMap<const MachineBasicBlock*,IndexPair> IndexPairMap;
1008 IndexPairMap MBBRange;
1009 for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(),
1010 E = sa_.usingInstrs_.end(); I != E; ++I) {
1011 const MachineBasicBlock *MBB = (*I)->getParent();
1012 if (!Blocks.count(MBB))
1013 continue;
1014 SlotIndex Idx = lis_.getInstructionIndex(*I);
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +00001015 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '\t' << Idx << '\t' << **I);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001016 IndexPair &IP = MBBRange[MBB];
1017 if (!IP.first.isValid() || Idx < IP.first)
1018 IP.first = Idx;
1019 if (!IP.second.isValid() || Idx > IP.second)
1020 IP.second = Idx;
1021 }
1022
1023 // Create a new interval for each block.
1024 for (SplitAnalysis::BlockPtrSet::const_iterator I = Blocks.begin(),
1025 E = Blocks.end(); I != E; ++I) {
1026 IndexPair &IP = MBBRange[*I];
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +00001027 DEBUG(dbgs() << " splitting for BB#" << (*I)->getNumber() << ": ["
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001028 << IP.first << ';' << IP.second << ")\n");
1029 assert(IP.first.isValid() && IP.second.isValid());
1030
1031 openIntv();
1032 enterIntvBefore(IP.first);
1033 useIntv(IP.first.getBaseIndex(), IP.second.getBoundaryIndex());
1034 leaveIntvAfter(IP.second);
1035 closeIntv();
1036 }
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001037 finish();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001038}
1039
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001040
1041//===----------------------------------------------------------------------===//
1042// Sub Block Splitting
1043//===----------------------------------------------------------------------===//
1044
1045/// getBlockForInsideSplit - If curli is contained inside a single basic block,
1046/// and it wou pay to subdivide the interval inside that block, return it.
1047/// Otherwise return NULL. The returned block can be passed to
1048/// SplitEditor::splitInsideBlock.
1049const MachineBasicBlock *SplitAnalysis::getBlockForInsideSplit() {
1050 // The interval must be exclusive to one block.
1051 if (usingBlocks_.size() != 1)
1052 return 0;
1053 // Don't to this for less than 4 instructions. We want to be sure that
1054 // splitting actually reduces the instruction count per interval.
1055 if (usingInstrs_.size() < 4)
1056 return 0;
1057 return usingBlocks_.begin()->first;
1058}
1059
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +00001060/// splitInsideBlock - Split curli into multiple intervals inside MBB.
1061void SplitEditor::splitInsideBlock(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001062 SmallVector<SlotIndex, 32> Uses;
1063 Uses.reserve(sa_.usingInstrs_.size());
1064 for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(),
1065 E = sa_.usingInstrs_.end(); I != E; ++I)
1066 if ((*I)->getParent() == MBB)
1067 Uses.push_back(lis_.getInstructionIndex(*I));
1068 DEBUG(dbgs() << " splitInsideBlock BB#" << MBB->getNumber() << " for "
1069 << Uses.size() << " instructions.\n");
1070 assert(Uses.size() >= 3 && "Need at least 3 instructions");
1071 array_pod_sort(Uses.begin(), Uses.end());
1072
1073 // Simple algorithm: Find the largest gap between uses as determined by slot
1074 // indices. Create new intervals for instructions before the gap and after the
1075 // gap.
1076 unsigned bestPos = 0;
1077 int bestGap = 0;
1078 DEBUG(dbgs() << " dist (" << Uses[0]);
1079 for (unsigned i = 1, e = Uses.size(); i != e; ++i) {
1080 int g = Uses[i-1].distance(Uses[i]);
1081 DEBUG(dbgs() << ") -" << g << "- (" << Uses[i]);
1082 if (g > bestGap)
1083 bestPos = i, bestGap = g;
1084 }
1085 DEBUG(dbgs() << "), best: -" << bestGap << "-\n");
1086
1087 // bestPos points to the first use after the best gap.
1088 assert(bestPos > 0 && "Invalid gap");
1089
1090 // FIXME: Don't create intervals for low densities.
1091
1092 // First interval before the gap. Don't create single-instr intervals.
1093 if (bestPos > 1) {
1094 openIntv();
1095 enterIntvBefore(Uses.front());
1096 useIntv(Uses.front().getBaseIndex(), Uses[bestPos-1].getBoundaryIndex());
1097 leaveIntvAfter(Uses[bestPos-1]);
1098 closeIntv();
1099 }
1100
1101 // Second interval after the gap.
1102 if (bestPos < Uses.size()-1) {
1103 openIntv();
1104 enterIntvBefore(Uses[bestPos]);
1105 useIntv(Uses[bestPos].getBaseIndex(), Uses.back().getBoundaryIndex());
1106 leaveIntvAfter(Uses.back());
1107 closeIntv();
1108 }
1109
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001110 finish();
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001111}