blob: 70352fc1782209416fc010cdf6b1107043f66ee9 [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 Olesend68f4582010-10-28 20:34:50 +000021#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000023#include "llvm/CodeGen/MachineLoopInfo.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000025#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000028#include "llvm/Target/TargetInstrInfo.h"
29#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000030
31using namespace llvm;
32
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000033static cl::opt<bool>
34AllowSplit("spiller-splits-edges",
35 cl::desc("Allow critical edge splitting during spilling"));
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000036
37//===----------------------------------------------------------------------===//
38// Split Analysis
39//===----------------------------------------------------------------------===//
40
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000041SplitAnalysis::SplitAnalysis(const MachineFunction &mf,
42 const LiveIntervals &lis,
43 const MachineLoopInfo &mli)
44 : mf_(mf),
45 lis_(lis),
46 loops_(mli),
47 tii_(*mf.getTarget().getInstrInfo()),
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000048 curli_(0) {}
49
50void SplitAnalysis::clear() {
51 usingInstrs_.clear();
52 usingBlocks_.clear();
53 usingLoops_.clear();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000054 curli_ = 0;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000055}
56
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000057bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) {
58 MachineBasicBlock *T, *F;
59 SmallVector<MachineOperand, 4> Cond;
60 return !tii_.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond);
61}
62
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +000063/// analyzeUses - Count instructions, basic blocks, and loops using curli.
64void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000065 const MachineRegisterInfo &MRI = mf_.getRegInfo();
66 for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(curli_->reg);
67 MachineInstr *MI = I.skipInstruction();) {
68 if (MI->isDebugValue() || !usingInstrs_.insert(MI))
69 continue;
70 MachineBasicBlock *MBB = MI->getParent();
71 if (usingBlocks_[MBB]++)
72 continue;
Jakob Stoklund Olesen9b90d7e2010-10-05 23:10:12 +000073 for (MachineLoop *Loop = loops_.getLoopFor(MBB); Loop;
74 Loop = Loop->getParentLoop())
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +000075 usingLoops_[Loop]++;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000076 }
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +000077 DEBUG(dbgs() << " counted "
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000078 << usingInstrs_.size() << " instrs, "
79 << usingBlocks_.size() << " blocks, "
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +000080 << usingLoops_.size() << " loops.\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000081}
82
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +000083void SplitAnalysis::print(const BlockPtrSet &B, raw_ostream &OS) const {
84 for (BlockPtrSet::const_iterator I = B.begin(), E = B.end(); I != E; ++I) {
85 unsigned count = usingBlocks_.lookup(*I);
86 OS << " BB#" << (*I)->getNumber();
87 if (count)
88 OS << '(' << count << ')';
89 }
90}
91
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000092// Get three sets of basic blocks surrounding a loop: Blocks inside the loop,
93// predecessor blocks, and exit blocks.
94void SplitAnalysis::getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks) {
95 Blocks.clear();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000096
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000097 // Blocks in the loop.
98 Blocks.Loop.insert(Loop->block_begin(), Loop->block_end());
99
100 // Predecessor blocks.
101 const MachineBasicBlock *Header = Loop->getHeader();
102 for (MachineBasicBlock::const_pred_iterator I = Header->pred_begin(),
103 E = Header->pred_end(); I != E; ++I)
104 if (!Blocks.Loop.count(*I))
105 Blocks.Preds.insert(*I);
106
107 // Exit blocks.
108 for (MachineLoop::block_iterator I = Loop->block_begin(),
109 E = Loop->block_end(); I != E; ++I) {
110 const MachineBasicBlock *MBB = *I;
111 for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
112 SE = MBB->succ_end(); SI != SE; ++SI)
113 if (!Blocks.Loop.count(*SI))
114 Blocks.Exits.insert(*SI);
115 }
116}
117
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000118void SplitAnalysis::print(const LoopBlocks &B, raw_ostream &OS) const {
119 OS << "Loop:";
120 print(B.Loop, OS);
121 OS << ", preds:";
122 print(B.Preds, OS);
123 OS << ", exits:";
124 print(B.Exits, OS);
125}
126
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000127/// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
128/// and around the Loop.
129SplitAnalysis::LoopPeripheralUse SplitAnalysis::
130analyzeLoopPeripheralUse(const SplitAnalysis::LoopBlocks &Blocks) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000131 LoopPeripheralUse use = ContainedInLoop;
132 for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end();
133 I != E; ++I) {
134 const MachineBasicBlock *MBB = I->first;
135 // Is this a peripheral block?
136 if (use < MultiPeripheral &&
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000137 (Blocks.Preds.count(MBB) || Blocks.Exits.count(MBB))) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000138 if (I->second > 1) use = MultiPeripheral;
139 else use = SinglePeripheral;
140 continue;
141 }
142 // Is it a loop block?
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000143 if (Blocks.Loop.count(MBB))
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000144 continue;
145 // It must be an unrelated block.
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000146 DEBUG(dbgs() << ", outside: BB#" << MBB->getNumber());
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000147 return OutsideLoop;
148 }
149 return use;
150}
151
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000152/// getCriticalExits - It may be necessary to partially break critical edges
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000153/// leaving the loop if an exit block has predecessors from outside the loop
154/// periphery.
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000155void SplitAnalysis::getCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
156 BlockPtrSet &CriticalExits) {
157 CriticalExits.clear();
158
Jakob Stoklund Olesen0960a652010-10-27 00:39:05 +0000159 // A critical exit block has curli live-in, and has a predecessor that is not
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000160 // in the loop nor a loop predecessor. For such an exit block, the edges
161 // carrying the new variable must be moved to a new pre-exit block.
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000162 for (BlockPtrSet::iterator I = Blocks.Exits.begin(), E = Blocks.Exits.end();
163 I != E; ++I) {
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000164 const MachineBasicBlock *Exit = *I;
165 // A single-predecessor exit block is definitely not a critical edge.
166 if (Exit->pred_size() == 1)
167 continue;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000168 // This exit may not have curli live in at all. No need to split.
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000169 if (!lis_.isLiveInToMBB(*curli_, Exit))
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000170 continue;
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000171 // Does this exit block have a predecessor that is not a loop block or loop
172 // predecessor?
173 for (MachineBasicBlock::const_pred_iterator PI = Exit->pred_begin(),
174 PE = Exit->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000175 const MachineBasicBlock *Pred = *PI;
176 if (Blocks.Loop.count(Pred) || Blocks.Preds.count(Pred))
177 continue;
178 // This is a critical exit block, and we need to split the exit edge.
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000179 CriticalExits.insert(Exit);
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000180 break;
181 }
182 }
183}
184
Jakob Stoklund Olesen0960a652010-10-27 00:39:05 +0000185void SplitAnalysis::getCriticalPreds(const SplitAnalysis::LoopBlocks &Blocks,
186 BlockPtrSet &CriticalPreds) {
187 CriticalPreds.clear();
188
189 // A critical predecessor block has curli live-out, and has a successor that
190 // has curli live-in and is not in the loop nor a loop exit block. For such a
191 // predecessor block, we must carry the value in both the 'inside' and
192 // 'outside' registers.
193 for (BlockPtrSet::iterator I = Blocks.Preds.begin(), E = Blocks.Preds.end();
194 I != E; ++I) {
195 const MachineBasicBlock *Pred = *I;
196 // Definitely not a critical edge.
197 if (Pred->succ_size() == 1)
198 continue;
199 // This block may not have curli live out at all if there is a PHI.
200 if (!lis_.isLiveOutOfMBB(*curli_, Pred))
201 continue;
202 // Does this block have a successor outside the loop?
203 for (MachineBasicBlock::const_pred_iterator SI = Pred->succ_begin(),
204 SE = Pred->succ_end(); SI != SE; ++SI) {
205 const MachineBasicBlock *Succ = *SI;
206 if (Blocks.Loop.count(Succ) || Blocks.Exits.count(Succ))
207 continue;
208 if (!lis_.isLiveInToMBB(*curli_, Succ))
209 continue;
210 // This is a critical predecessor block.
211 CriticalPreds.insert(Pred);
212 break;
213 }
214 }
215}
216
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000217/// canSplitCriticalExits - Return true if it is possible to insert new exit
218/// blocks before the blocks in CriticalExits.
219bool
220SplitAnalysis::canSplitCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
221 BlockPtrSet &CriticalExits) {
222 // If we don't allow critical edge splitting, require no critical exits.
223 if (!AllowSplit)
224 return CriticalExits.empty();
225
226 for (BlockPtrSet::iterator I = CriticalExits.begin(), E = CriticalExits.end();
227 I != E; ++I) {
228 const MachineBasicBlock *Succ = *I;
229 // We want to insert a new pre-exit MBB before Succ, and change all the
230 // in-loop blocks to branch to the pre-exit instead of Succ.
231 // Check that all the in-loop predecessors can be changed.
232 for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(),
233 PE = Succ->pred_end(); PI != PE; ++PI) {
234 const MachineBasicBlock *Pred = *PI;
235 // The external predecessors won't be altered.
236 if (!Blocks.Loop.count(Pred) && !Blocks.Preds.count(Pred))
237 continue;
238 if (!canAnalyzeBranch(Pred))
239 return false;
240 }
241
242 // If Succ's layout predecessor falls through, that too must be analyzable.
243 // We need to insert the pre-exit block in the gap.
244 MachineFunction::const_iterator MFI = Succ;
245 if (MFI == mf_.begin())
246 continue;
247 if (!canAnalyzeBranch(--MFI))
248 return false;
249 }
250 // No problems found.
251 return true;
252}
253
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000254void SplitAnalysis::analyze(const LiveInterval *li) {
255 clear();
256 curli_ = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000257 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000258}
259
260const MachineLoop *SplitAnalysis::getBestSplitLoop() {
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000261 assert(curli_ && "Call analyze() before getBestSplitLoop");
262 if (usingLoops_.empty())
263 return 0;
264
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000265 LoopPtrSet Loops;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000266 LoopBlocks Blocks;
267 BlockPtrSet CriticalExits;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000268
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000269 // We split around loops where curli is used outside the periphery.
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +0000270 for (LoopCountMap::const_iterator I = usingLoops_.begin(),
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000271 E = usingLoops_.end(); I != E; ++I) {
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +0000272 const MachineLoop *Loop = I->first;
273 getLoopBlocks(Loop, Blocks);
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000274 DEBUG({ dbgs() << " "; print(Blocks, dbgs()); });
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000275
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000276 switch(analyzeLoopPeripheralUse(Blocks)) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000277 case OutsideLoop:
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000278 break;
279 case MultiPeripheral:
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000280 // FIXME: We could split a live range with multiple uses in a peripheral
281 // block and still make progress. However, it is possible that splitting
282 // another live range will insert copies into a peripheral block, and
283 // there is a small chance we can enter an infinity loop, inserting copies
284 // forever.
285 // For safety, stick to splitting live ranges with uses outside the
286 // periphery.
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000287 DEBUG(dbgs() << ": multiple peripheral uses\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000288 break;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000289 case ContainedInLoop:
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000290 DEBUG(dbgs() << ": fully contained\n");
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000291 continue;
292 case SinglePeripheral:
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000293 DEBUG(dbgs() << ": single peripheral use\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000294 continue;
295 }
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000296 // Will it be possible to split around this loop?
297 getCriticalExits(Blocks, CriticalExits);
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000298 DEBUG(dbgs() << ": " << CriticalExits.size() << " critical exits\n");
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000299 if (!canSplitCriticalExits(Blocks, CriticalExits))
300 continue;
301 // This is a possible split.
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000302 Loops.insert(Loop);
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000303 }
304
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000305 DEBUG(dbgs() << " getBestSplitLoop found " << Loops.size()
306 << " candidate loops.\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000307
308 if (Loops.empty())
309 return 0;
310
311 // Pick the earliest loop.
312 // FIXME: Are there other heuristics to consider?
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000313 const MachineLoop *Best = 0;
314 SlotIndex BestIdx;
315 for (LoopPtrSet::const_iterator I = Loops.begin(), E = Loops.end(); I != E;
316 ++I) {
317 SlotIndex Idx = lis_.getMBBStartIdx((*I)->getHeader());
318 if (!Best || Idx < BestIdx)
319 Best = *I, BestIdx = Idx;
320 }
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000321 DEBUG(dbgs() << " getBestSplitLoop found " << *Best);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000322 return Best;
323}
324
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000325//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000326// LiveIntervalMap
327//===----------------------------------------------------------------------===//
328
Jakob Stoklund Olesenb3e96812010-09-13 21:29:45 +0000329// Work around the fact that the std::pair constructors are broken for pointer
330// pairs in some implementations. makeVV(x, 0) works.
331static inline std::pair<const VNInfo*, VNInfo*>
332makeVV(const VNInfo *a, VNInfo *b) {
333 return std::make_pair(a, b);
334}
335
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000336void LiveIntervalMap::reset(LiveInterval *li) {
337 li_ = li;
338 valueMap_.clear();
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000339 liveOutCache_.clear();
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000340}
341
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000342bool LiveIntervalMap::isComplexMapped(const VNInfo *ParentVNI) const {
343 ValueMap::const_iterator i = valueMap_.find(ParentVNI);
344 return i != valueMap_.end() && i->second == 0;
345}
346
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000347// defValue - Introduce a li_ def for ParentVNI that could be later than
348// ParentVNI->def.
349VNInfo *LiveIntervalMap::defValue(const VNInfo *ParentVNI, SlotIndex Idx) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000350 assert(li_ && "call reset first");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000351 assert(ParentVNI && "Mapping NULL value");
352 assert(Idx.isValid() && "Invalid SlotIndex");
353 assert(parentli_.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
354
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000355 // Create a new value.
Lang Hames6e2968c2010-09-25 12:04:16 +0000356 VNInfo *VNI = li_->getNextValue(Idx, 0, lis_.getVNInfoAllocator());
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000357
Jakob Stoklund Olesen79c02622010-10-26 22:36:02 +0000358 // Preserve the PHIDef bit.
359 if (ParentVNI->isPHIDef() && Idx == ParentVNI->def)
360 VNI->setIsPHIDef(true);
361
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000362 // Use insert for lookup, so we can add missing values with a second lookup.
363 std::pair<ValueMap::iterator,bool> InsP =
364 valueMap_.insert(makeVV(ParentVNI, Idx == ParentVNI->def ? VNI : 0));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000365
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000366 // This is now a complex def. Mark with a NULL in valueMap.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000367 if (!InsP.second)
368 InsP.first->second = 0;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000369
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000370 return VNI;
371}
372
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000373
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000374// mapValue - Find the mapped value for ParentVNI at Idx.
375// Potentially create phi-def values.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000376VNInfo *LiveIntervalMap::mapValue(const VNInfo *ParentVNI, SlotIndex Idx,
377 bool *simple) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000378 assert(li_ && "call reset first");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000379 assert(ParentVNI && "Mapping NULL value");
380 assert(Idx.isValid() && "Invalid SlotIndex");
381 assert(parentli_.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
382
383 // Use insert for lookup, so we can add missing values with a second lookup.
384 std::pair<ValueMap::iterator,bool> InsP =
Jakob Stoklund Olesenb3e96812010-09-13 21:29:45 +0000385 valueMap_.insert(makeVV(ParentVNI, 0));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000386
387 // This was an unknown value. Create a simple mapping.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000388 if (InsP.second) {
389 if (simple) *simple = true;
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000390 return InsP.first->second = li_->createValueCopy(ParentVNI,
391 lis_.getVNInfoAllocator());
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000392 }
393
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000394 // This was a simple mapped value.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000395 if (InsP.first->second) {
396 if (simple) *simple = true;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000397 return InsP.first->second;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000398 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000399
400 // This is a complex mapped value. There may be multiple defs, and we may need
401 // to create phi-defs.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000402 if (simple) *simple = false;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000403 MachineBasicBlock *IdxMBB = lis_.getMBBFromIndex(Idx);
404 assert(IdxMBB && "No MBB at Idx");
405
406 // Is there a def in the same MBB we can extend?
407 if (VNInfo *VNI = extendTo(IdxMBB, Idx))
408 return VNI;
409
410 // Now for the fun part. We know that ParentVNI potentially has multiple defs,
411 // and we may need to create even more phi-defs to preserve VNInfo SSA form.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000412 // Perform a search for all predecessor blocks where we know the dominating
413 // VNInfo. Insert phi-def VNInfos along the path back to IdxMBB.
414 DEBUG(dbgs() << "\n Reaching defs for BB#" << IdxMBB->getNumber()
415 << " at " << Idx << " in " << *li_ << '\n');
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000416
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000417 // Blocks where li_ should be live-in.
418 SmallVector<MachineDomTreeNode*, 16> LiveIn;
419 LiveIn.push_back(mdt_[IdxMBB]);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000420
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000421 // Using liveOutCache_ as a visited set, perform a BFS for all reaching defs.
422 for (unsigned i = 0; i != LiveIn.size(); ++i) {
423 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
424 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
425 PE = MBB->pred_end(); PI != PE; ++PI) {
426 MachineBasicBlock *Pred = *PI;
427 // Is this a known live-out block?
428 std::pair<LiveOutMap::iterator,bool> LOIP =
429 liveOutCache_.insert(std::make_pair(Pred, LiveOutPair()));
430 // Yes, we have been here before.
431 if (!LOIP.second) {
432 if (VNInfo *VNI = LOIP.first->second.first) {
433 DEBUG(dbgs() << " known valno #" << VNI->id
434 << " at BB#" << Pred->getNumber() << '\n');
435 }
436 continue;
437 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000438
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000439 // Does Pred provide a live-out value?
440 SlotIndex Last = lis_.getMBBEndIdx(Pred).getPrevSlot();
441 if (VNInfo *VNI = extendTo(Pred, Last)) {
442 MachineBasicBlock *DefMBB = lis_.getMBBFromIndex(VNI->def);
443 DEBUG(dbgs() << " found valno #" << VNI->id
444 << " at BB#" << DefMBB->getNumber() << '\n');
445 LiveOutPair &LOP = LOIP.first->second;
446 LOP.first = VNI;
447 LOP.second = mdt_[lis_.getMBBFromIndex(VNI->def)];
448 continue;
449 }
450 // No, we need a live-in value for Pred as well
451 if (Pred != IdxMBB)
452 LiveIn.push_back(mdt_[Pred]);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000453 }
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000454 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000455
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000456 // We may need to add phi-def values to preserve the SSA form.
457 // This is essentially the same iterative algorithm that SSAUpdater uses,
458 // except we already have a dominator tree, so we don't have to recompute it.
459 VNInfo *IdxVNI = 0;
460 unsigned Changes;
461 do {
462 Changes = 0;
463 DEBUG(dbgs() << " Iterating over " << LiveIn.size() << " blocks.\n");
464 // Propagate live-out values down the dominator tree, inserting phi-defs when
465 // necessary. Since LiveIn was created by a BFS, going backwards makes it more
466 // likely for us to visit immediate dominators before their children.
467 for (unsigned i = LiveIn.size(); i; --i) {
468 MachineDomTreeNode *Node = LiveIn[i-1];
469 MachineBasicBlock *MBB = Node->getBlock();
470 MachineDomTreeNode *IDom = Node->getIDom();
471 LiveOutPair IDomValue;
472 // We need a live-in value to a block with no immediate dominator?
473 // This is probably an unreachable block that has survived somehow.
474 bool needPHI = !IDom;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000475
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000476 // Get the IDom live-out value.
477 if (!needPHI) {
478 LiveOutMap::iterator I = liveOutCache_.find(IDom->getBlock());
479 if (I != liveOutCache_.end())
480 IDomValue = I->second;
481 else
482 // If IDom is outside our set of live-out blocks, there must be new
483 // defs, and we need a phi-def here.
484 needPHI = true;
485 }
Jakob Stoklund Olesen984a7fc2010-10-05 20:36:28 +0000486
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000487 // IDom dominates all of our predecessors, but it may not be the immediate
488 // dominator. Check if any of them have live-out values that are properly
489 // dominated by IDom. If so, we need a phi-def here.
490 if (!needPHI) {
491 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
492 PE = MBB->pred_end(); PI != PE; ++PI) {
493 LiveOutPair Value = liveOutCache_[*PI];
494 if (!Value.first || Value.first == IDomValue.first)
495 continue;
496 // This predecessor is carrying something other than IDomValue.
497 // It could be because IDomValue hasn't propagated yet, or it could be
498 // because MBB is in the dominance frontier of that value.
499 if (mdt_.dominates(IDom, Value.second)) {
500 needPHI = true;
501 break;
502 }
503 }
504 }
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000505
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000506 // Create a phi-def if required.
507 if (needPHI) {
508 ++Changes;
509 SlotIndex Start = lis_.getMBBStartIdx(MBB);
510 VNInfo *VNI = li_->getNextValue(Start, 0, lis_.getVNInfoAllocator());
511 VNI->setIsPHIDef(true);
512 DEBUG(dbgs() << " - BB#" << MBB->getNumber()
513 << " phi-def #" << VNI->id << " at " << Start << '\n');
514 // We no longer need li_ to be live-in.
515 LiveIn.erase(LiveIn.begin()+(i-1));
516 // Blocks in LiveIn are either IdxMBB, or have a value live-through.
517 if (MBB == IdxMBB)
518 IdxVNI = VNI;
519 // Check if we need to update live-out info.
520 LiveOutMap::iterator I = liveOutCache_.find(MBB);
521 if (I == liveOutCache_.end() || I->second.second == Node) {
522 // We already have a live-out defined in MBB, so this must be IdxMBB.
523 assert(MBB == IdxMBB && "Adding phi-def to known live-out");
524 li_->addRange(LiveRange(Start, Idx.getNextSlot(), VNI));
525 } else {
526 // This phi-def is also live-out, so color the whole block.
527 li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI));
528 I->second = LiveOutPair(VNI, Node);
529 }
530 } else if (IDomValue.first) {
531 // No phi-def here. Propagate IDomValue if needed.
532 if (MBB == IdxMBB)
533 IdxVNI = IDomValue.first;
534 LiveOutMap::iterator I = liveOutCache_.find(MBB);
535 if (I != liveOutCache_.end() && I->second.first != IDomValue.first) {
536 ++Changes;
537 I->second = IDomValue;
538 DEBUG(dbgs() << " - BB#" << MBB->getNumber()
539 << " idom valno #" << IDomValue.first->id
540 << " from BB#" << IDom->getBlock()->getNumber() << '\n');
541 }
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000542 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000543 }
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000544 DEBUG(dbgs() << " - made " << Changes << " changes.\n");
545 } while (Changes);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000546
547 assert(IdxVNI && "Didn't find value for Idx");
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000548
549#ifndef NDEBUG
550 // Check the liveOutCache_ invariants.
551 for (LiveOutMap::iterator I = liveOutCache_.begin(), E = liveOutCache_.end();
552 I != E; ++I) {
553 assert(I->first && "Null MBB entry in cache");
554 assert(I->second.first && "Null VNInfo in cache");
555 assert(I->second.second && "Null DomTreeNode in cache");
556 if (I->second.second->getBlock() == I->first)
557 continue;
558 for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(),
559 PE = I->first->pred_end(); PI != PE; ++PI)
560 assert(liveOutCache_.lookup(*PI) == I->second && "Bad invariant");
561 }
562#endif
563
564 // Since we went through the trouble of a full BFS visiting all reaching defs,
565 // the values in LiveIn are now accurate. No more phi-defs are needed
566 // for these blocks, so we can color the live ranges.
567 // This makes the next mapValue call much faster.
568 for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) {
569 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
570 SlotIndex Start = lis_.getMBBStartIdx(MBB);
571 if (MBB == IdxMBB) {
572 li_->addRange(LiveRange(Start, Idx.getNextSlot(), IdxVNI));
573 continue;
574 }
575 // Anything in LiveIn other than IdxMBB is live-through.
576 VNInfo *VNI = liveOutCache_.lookup(MBB).first;
577 assert(VNI && "Missing block value");
578 li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI));
579 }
580
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000581 return IdxVNI;
582}
583
584// extendTo - Find the last li_ value defined in MBB at or before Idx. The
585// parentli_ is assumed to be live at Idx. Extend the live range to Idx.
586// Return the found VNInfo, or NULL.
Jakob Stoklund Olesenc95c1462010-10-27 00:39:07 +0000587VNInfo *LiveIntervalMap::extendTo(const MachineBasicBlock *MBB, SlotIndex Idx) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000588 assert(li_ && "call reset first");
589 LiveInterval::iterator I = std::upper_bound(li_->begin(), li_->end(), Idx);
590 if (I == li_->begin())
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000591 return 0;
592 --I;
Jakob Stoklund Olesenfc60d772010-10-05 20:36:25 +0000593 if (I->end <= lis_.getMBBStartIdx(MBB))
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000594 return 0;
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000595 if (I->end <= Idx)
596 I->end = Idx.getNextSlot();
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000597 return I->valno;
598}
599
600// addSimpleRange - Add a simple range from parentli_ to li_.
601// ParentVNI must be live in the [Start;End) interval.
602void LiveIntervalMap::addSimpleRange(SlotIndex Start, SlotIndex End,
603 const VNInfo *ParentVNI) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000604 assert(li_ && "call reset first");
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000605 bool simple;
606 VNInfo *VNI = mapValue(ParentVNI, Start, &simple);
607 // A simple mapping is easy.
608 if (simple) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000609 li_->addRange(LiveRange(Start, End, VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000610 return;
611 }
612
613 // ParentVNI is a complex value. We must map per MBB.
614 MachineFunction::iterator MBB = lis_.getMBBFromIndex(Start);
Jakob Stoklund Olesendbc36092010-10-05 22:19:29 +0000615 MachineFunction::iterator MBBE = lis_.getMBBFromIndex(End.getPrevSlot());
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000616
617 if (MBB == MBBE) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000618 li_->addRange(LiveRange(Start, End, VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000619 return;
620 }
621
622 // First block.
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000623 li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000624
625 // Run sequence of full blocks.
626 for (++MBB; MBB != MBBE; ++MBB) {
627 Start = lis_.getMBBStartIdx(MBB);
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000628 li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB),
629 mapValue(ParentVNI, Start)));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000630 }
631
632 // Final block.
633 Start = lis_.getMBBStartIdx(MBB);
634 if (Start != End)
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000635 li_->addRange(LiveRange(Start, End, mapValue(ParentVNI, Start)));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000636}
637
638/// addRange - Add live ranges to li_ where [Start;End) intersects parentli_.
639/// All needed values whose def is not inside [Start;End) must be defined
640/// beforehand so mapValue will work.
641void LiveIntervalMap::addRange(SlotIndex Start, SlotIndex End) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000642 assert(li_ && "call reset first");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000643 LiveInterval::const_iterator B = parentli_.begin(), E = parentli_.end();
644 LiveInterval::const_iterator I = std::lower_bound(B, E, Start);
645
646 // Check if --I begins before Start and overlaps.
647 if (I != B) {
648 --I;
649 if (I->end > Start)
650 addSimpleRange(Start, std::min(End, I->end), I->valno);
651 ++I;
652 }
653
654 // The remaining ranges begin after Start.
655 for (;I != E && I->start < End; ++I)
656 addSimpleRange(I->start, std::min(End, I->end), I->valno);
657}
658
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000659VNInfo *LiveIntervalMap::defByCopyFrom(unsigned Reg,
660 const VNInfo *ParentVNI,
661 MachineBasicBlock &MBB,
662 MachineBasicBlock::iterator I) {
663 const TargetInstrDesc &TID = MBB.getParent()->getTarget().getInstrInfo()->
664 get(TargetOpcode::COPY);
665 MachineInstr *MI = BuildMI(MBB, I, DebugLoc(), TID, li_->reg).addReg(Reg);
666 SlotIndex DefIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex();
667 VNInfo *VNI = defValue(ParentVNI, DefIdx);
668 VNI->setCopy(MI);
669 li_->addRange(LiveRange(DefIdx, DefIdx.getNextSlot(), VNI));
670 return VNI;
671}
672
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000673//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000674// Split Editor
675//===----------------------------------------------------------------------===//
676
677/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000678SplitEditor::SplitEditor(SplitAnalysis &sa,
679 LiveIntervals &lis,
680 VirtRegMap &vrm,
681 MachineDominatorTree &mdt,
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000682 LiveRangeEdit &edit)
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000683 : sa_(sa), lis_(lis), vrm_(vrm),
684 mri_(vrm.getMachineFunction().getRegInfo()),
685 tii_(*vrm.getMachineFunction().getTarget().getInstrInfo()),
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000686 edit_(edit),
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000687 dupli_(lis_, mdt, edit.getParent()),
688 openli_(lis_, mdt, edit.getParent())
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000689{
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000690}
691
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000692bool SplitEditor::intervalsLiveAt(SlotIndex Idx) const {
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000693 for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I)
694 if (*I != dupli_.getLI() && (*I)->liveAt(Idx))
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000695 return true;
696 return false;
697}
698
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000699/// Create a new virtual register and live interval.
700void SplitEditor::openIntv() {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000701 assert(!openli_.getLI() && "Previous LI not closed before openIntv");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000702
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000703 if (!dupli_.getLI())
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000704 dupli_.reset(&edit_.create(mri_, lis_, vrm_));
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000705
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000706 openli_.reset(&edit_.create(mri_, lis_, vrm_));
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000707}
708
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000709/// enterIntvBefore - Enter openli before the instruction at Idx. If curli is
710/// not live before Idx, a COPY is not inserted.
711void SplitEditor::enterIntvBefore(SlotIndex Idx) {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000712 assert(openli_.getLI() && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000713 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000714 VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Idx.getUseIndex());
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000715 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000716 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000717 return;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000718 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000719 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000720 truncatedValues.insert(ParentVNI);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000721 MachineInstr *MI = lis_.getInstructionFromIndex(Idx);
722 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000723 VNInfo *VNI = openli_.defByCopyFrom(edit_.getReg(), ParentVNI,
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000724 *MI->getParent(), MI);
725 openli_.getLI()->addRange(LiveRange(VNI->def, Idx.getDefIndex(), VNI));
726 DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000727}
728
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000729/// enterIntvAtEnd - Enter openli at the end of MBB.
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000730void SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000731 assert(openli_.getLI() && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000732 SlotIndex End = lis_.getMBBEndIdx(&MBB);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000733 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << End);
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000734 VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(End.getPrevSlot());
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000735 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000736 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000737 return;
738 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000739 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000740 truncatedValues.insert(ParentVNI);
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000741 VNInfo *VNI = openli_.defByCopyFrom(edit_.getReg(), ParentVNI,
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000742 MBB, MBB.getFirstTerminator());
743 // Make sure openli is live out of MBB.
744 openli_.getLI()->addRange(LiveRange(VNI->def, End, VNI));
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000745 DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000746}
747
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000748/// useIntv - indicate that all instructions in MBB should use openli.
749void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
750 useIntv(lis_.getMBBStartIdx(&MBB), lis_.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000751}
752
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000753void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000754 assert(openli_.getLI() && "openIntv not called before useIntv");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000755 openli_.addRange(Start, End);
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000756 DEBUG(dbgs() << " use [" << Start << ';' << End << "): "
757 << *openli_.getLI() << '\n');
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000758}
759
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000760/// leaveIntvAfter - Leave openli after the instruction at Idx.
761void SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000762 assert(openli_.getLI() && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000763 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000764
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000765 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000766 VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Idx.getBoundaryIndex());
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000767 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000768 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000769 return;
770 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000771 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000772
Jakob Stoklund Olesenfc60d772010-10-05 20:36:25 +0000773 MachineBasicBlock::iterator MII = lis_.getInstructionFromIndex(Idx);
774 MachineBasicBlock *MBB = MII->getParent();
775 VNInfo *VNI = dupli_.defByCopyFrom(openli_.getLI()->reg, ParentVNI, *MBB,
776 llvm::next(MII));
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000777
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000778 // Finally we must make sure that openli is properly extended from Idx to the
779 // new copy.
Jakob Stoklund Olesenfc60d772010-10-05 20:36:25 +0000780 openli_.addSimpleRange(Idx.getBoundaryIndex(), VNI->def, ParentVNI);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000781 DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000782}
783
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000784/// leaveIntvAtTop - Leave the interval at the top of MBB.
785/// Currently, only one value can leave the interval.
786void SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000787 assert(openli_.getLI() && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000788 SlotIndex Start = lis_.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000789 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000790
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000791 VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Start);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000792 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000793 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000794 return;
795 }
796
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000797 // We are going to insert a back copy, so we must have a dupli_.
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000798 VNInfo *VNI = dupli_.defByCopyFrom(openli_.getLI()->reg, ParentVNI,
799 MBB, MBB.begin());
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000800
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000801 // Finally we must make sure that openli is properly extended from Start to
802 // the new copy.
Jakob Stoklund Olesenfc60d772010-10-05 20:36:25 +0000803 openli_.addSimpleRange(Start, VNI->def, ParentVNI);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000804 DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000805}
806
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000807/// closeIntv - Indicate that we are done editing the currently open
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000808/// LiveInterval, and ranges can be trimmed.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000809void SplitEditor::closeIntv() {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000810 assert(openli_.getLI() && "openIntv not called before closeIntv");
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000811
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000812 DEBUG(dbgs() << " closeIntv cleaning up\n");
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000813 DEBUG(dbgs() << " open " << *openli_.getLI() << '\n');
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000814 openli_.reset(0);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000815}
816
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000817/// rewrite - Rewrite all uses of reg to use the new registers.
818void SplitEditor::rewrite(unsigned reg) {
819 for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(reg),
820 RE = mri_.reg_end(); RI != RE;) {
821 MachineOperand &MO = RI.getOperand();
822 MachineInstr *MI = MO.getParent();
823 ++RI;
824 if (MI->isDebugValue()) {
825 DEBUG(dbgs() << "Zapping " << *MI);
826 // FIXME: We can do much better with debug values.
827 MO.setReg(0);
828 continue;
829 }
830 SlotIndex Idx = lis_.getInstructionIndex(MI);
831 Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
832 LiveInterval *LI = 0;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000833 for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E;
834 ++I) {
835 LiveInterval *testli = *I;
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000836 if (testli->liveAt(Idx)) {
837 LI = testli;
838 break;
839 }
840 }
Jakob Stoklund Olesen9d999772010-10-21 18:47:08 +0000841 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'<< Idx);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000842 assert(LI && "No register was live at use");
843 MO.setReg(LI->reg);
Jakob Stoklund Olesen9d999772010-10-21 18:47:08 +0000844 DEBUG(dbgs() << '\t' << *MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000845 }
846}
847
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000848void
849SplitEditor::addTruncSimpleRange(SlotIndex Start, SlotIndex End, VNInfo *VNI) {
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000850 // Build vector of iterator pairs from the intervals.
851 typedef std::pair<LiveInterval::const_iterator,
852 LiveInterval::const_iterator> IIPair;
853 SmallVector<IIPair, 8> Iters;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000854 for (LiveRangeEdit::iterator LI = edit_.begin(), LE = edit_.end(); LI != LE;
855 ++LI) {
Jakob Stoklund Olesen9d999772010-10-21 18:47:08 +0000856 if (*LI == dupli_.getLI())
857 continue;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000858 LiveInterval::const_iterator I = (*LI)->find(Start);
859 LiveInterval::const_iterator E = (*LI)->end();
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000860 if (I != E)
861 Iters.push_back(std::make_pair(I, E));
862 }
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000863
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000864 SlotIndex sidx = Start;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000865 // Break [Start;End) into segments that don't overlap any intervals.
866 for (;;) {
867 SlotIndex next = sidx, eidx = End;
868 // Find overlapping intervals.
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000869 for (unsigned i = 0; i != Iters.size() && sidx < eidx; ++i) {
870 LiveInterval::const_iterator I = Iters[i].first;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000871 // Interval I is overlapping [sidx;eidx). Trim sidx.
872 if (I->start <= sidx) {
873 sidx = I->end;
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000874 // Move to the next run, remove iters when all are consumed.
875 I = ++Iters[i].first;
876 if (I == Iters[i].second) {
877 Iters.erase(Iters.begin() + i);
878 --i;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000879 continue;
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000880 }
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000881 }
882 // Trim eidx too if needed.
883 if (I->start >= eidx)
884 continue;
885 eidx = I->start;
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000886 next = I->end;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000887 }
888 // Now, [sidx;eidx) doesn't overlap anything in intervals_.
889 if (sidx < eidx)
890 dupli_.addSimpleRange(sidx, eidx, VNI);
891 // If the interval end was truncated, we can try again from next.
892 if (next <= sidx)
893 break;
894 sidx = next;
895 }
896}
897
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000898void SplitEditor::computeRemainder() {
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000899 // First we need to fill in the live ranges in dupli.
900 // If values were redefined, we need a full recoloring with SSA update.
901 // If values were truncated, we only need to truncate the ranges.
902 // If values were partially rematted, we should shrink to uses.
903 // If values were fully rematted, they should be omitted.
904 // FIXME: If a single value is redefined, just move the def and truncate.
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000905 LiveInterval &parent = edit_.getParent();
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000906
907 // Values that are fully contained in the split intervals.
908 SmallPtrSet<const VNInfo*, 8> deadValues;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000909 // Map all curli values that should have live defs in dupli.
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000910 for (LiveInterval::const_vni_iterator I = parent.vni_begin(),
911 E = parent.vni_end(); I != E; ++I) {
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000912 const VNInfo *VNI = *I;
913 // Original def is contained in the split intervals.
914 if (intervalsLiveAt(VNI->def)) {
915 // Did this value escape?
916 if (dupli_.isMapped(VNI))
917 truncatedValues.insert(VNI);
918 else
919 deadValues.insert(VNI);
920 continue;
921 }
922 // Add minimal live range at the definition.
923 VNInfo *DVNI = dupli_.defValue(VNI, VNI->def);
924 dupli_.getLI()->addRange(LiveRange(VNI->def, VNI->def.getNextSlot(), DVNI));
925 }
926
927 // Add all ranges to dupli.
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000928 for (LiveInterval::const_iterator I = parent.begin(), E = parent.end();
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000929 I != E; ++I) {
930 const LiveRange &LR = *I;
931 if (truncatedValues.count(LR.valno)) {
932 // recolor after removing intervals_.
933 addTruncSimpleRange(LR.start, LR.end, LR.valno);
934 } else if (!deadValues.count(LR.valno)) {
935 // recolor without truncation.
936 dupli_.addSimpleRange(LR.start, LR.end, LR.valno);
937 }
938 }
Jakob Stoklund Olesenc95c1462010-10-27 00:39:07 +0000939
940 // Extend dupli_ to be live out of any critical loop predecessors.
941 // This means we have multiple registers live out of those blocks.
942 // The alternative would be to split the critical edges.
943 if (criticalPreds_.empty())
944 return;
945 for (SplitAnalysis::BlockPtrSet::iterator I = criticalPreds_.begin(),
946 E = criticalPreds_.end(); I != E; ++I)
947 dupli_.extendTo(*I, lis_.getMBBEndIdx(*I).getPrevSlot());
948 criticalPreds_.clear();
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000949}
950
951void SplitEditor::finish() {
952 assert(!openli_.getLI() && "Previous LI not closed before rewrite");
953 assert(dupli_.getLI() && "No dupli for rewrite. Noop spilt?");
954
955 // Complete dupli liveness.
956 computeRemainder();
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000957
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000958 // Get rid of unused values and set phi-kill flags.
Jakob Stoklund Olesenf1354ae2010-10-26 22:36:05 +0000959 for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I)
960 (*I)->RenumberValues(lis_);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000961
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000962 // Rewrite instructions.
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000963 rewrite(edit_.getReg());
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000964
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000965 // Now check if any registers were separated into multiple components.
966 ConnectedVNInfoEqClasses ConEQ(lis_);
967 for (unsigned i = 0, e = edit_.size(); i != e; ++i) {
968 // Don't use iterators, they are invalidated by create() below.
969 LiveInterval *li = edit_.get(i);
970 unsigned NumComp = ConEQ.Classify(li);
971 if (NumComp <= 1)
972 continue;
973 DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n');
974 SmallVector<LiveInterval*, 8> dups;
975 dups.push_back(li);
976 for (unsigned i = 1; i != NumComp; ++i)
977 dups.push_back(&edit_.create(mri_, lis_, vrm_));
978 ConEQ.Distribute(&dups[0]);
979 // Rewrite uses to the new regs.
980 rewrite(li->reg);
981 }
982
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000983 // Calculate spill weight and allocation hints for new intervals.
984 VirtRegAuxInfo vrai(vrm_.getMachineFunction(), lis_, sa_.loops_);
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000985 for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I){
986 LiveInterval &li = **I;
Jakob Stoklund Olesen9db3ea42010-08-10 18:37:40 +0000987 vrai.CalculateRegClass(li.reg);
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000988 vrai.CalculateWeightAndHint(li);
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000989 DEBUG(dbgs() << " new interval " << mri_.getRegClass(li.reg)->getName()
990 << ":" << li << '\n');
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000991 }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000992}
993
994
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000995//===----------------------------------------------------------------------===//
996// Loop Splitting
997//===----------------------------------------------------------------------===//
998
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000999void SplitEditor::splitAroundLoop(const MachineLoop *Loop) {
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001000 SplitAnalysis::LoopBlocks Blocks;
1001 sa_.getLoopBlocks(Loop, Blocks);
1002
Jakob Stoklund Olesen452a9fd2010-10-07 18:47:07 +00001003 DEBUG({
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +00001004 dbgs() << " splitAround"; sa_.print(Blocks, dbgs()); dbgs() << '\n';
Jakob Stoklund Olesen452a9fd2010-10-07 18:47:07 +00001005 });
1006
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001007 // Break critical edges as needed.
1008 SplitAnalysis::BlockPtrSet CriticalExits;
1009 sa_.getCriticalExits(Blocks, CriticalExits);
1010 assert(CriticalExits.empty() && "Cannot break critical exits yet");
1011
Jakob Stoklund Olesenc95c1462010-10-27 00:39:07 +00001012 // Get critical predecessors so computeRemainder can deal with them.
1013 sa_.getCriticalPreds(Blocks, criticalPreds_);
1014
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001015 // Create new live interval for the loop.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +00001016 openIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001017
1018 // Insert copies in the predecessors.
1019 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Preds.begin(),
1020 E = Blocks.Preds.end(); I != E; ++I) {
1021 MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +00001022 enterIntvAtEnd(MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001023 }
1024
1025 // Switch all loop blocks.
1026 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Loop.begin(),
1027 E = Blocks.Loop.end(); I != E; ++I)
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +00001028 useIntv(**I);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001029
1030 // Insert back copies in the exit blocks.
1031 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Exits.begin(),
1032 E = Blocks.Exits.end(); I != E; ++I) {
1033 MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +00001034 leaveIntvAtTop(MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001035 }
1036
1037 // Done.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +00001038 closeIntv();
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001039 finish();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +00001040}
1041
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001042
1043//===----------------------------------------------------------------------===//
1044// Single Block Splitting
1045//===----------------------------------------------------------------------===//
1046
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +00001047/// getMultiUseBlocks - if curli has more than one use in a basic block, it
1048/// may be an advantage to split curli for the duration of the block.
1049bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) {
1050 // If curli is local to one block, there is no point to splitting it.
1051 if (usingBlocks_.size() <= 1)
1052 return false;
1053 // Add blocks with multiple uses.
1054 for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end();
1055 I != E; ++I)
1056 switch (I->second) {
1057 case 0:
1058 case 1:
1059 continue;
1060 case 2: {
1061 // When there are only two uses and curli is both live in and live out,
1062 // we don't really win anything by isolating the block since we would be
1063 // inserting two copies.
1064 // The remaing register would still have two uses in the block. (Unless it
1065 // separates into disconnected components).
1066 if (lis_.isLiveInToMBB(*curli_, I->first) &&
1067 lis_.isLiveOutOfMBB(*curli_, I->first))
1068 continue;
1069 } // Fall through.
1070 default:
1071 Blocks.insert(I->first);
1072 }
1073 return !Blocks.empty();
1074}
1075
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001076/// splitSingleBlocks - Split curli into a separate live interval inside each
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +00001077/// basic block in Blocks.
1078void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) {
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +00001079 DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n");
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001080 // Determine the first and last instruction using curli in each block.
1081 typedef std::pair<SlotIndex,SlotIndex> IndexPair;
1082 typedef DenseMap<const MachineBasicBlock*,IndexPair> IndexPairMap;
1083 IndexPairMap MBBRange;
1084 for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(),
1085 E = sa_.usingInstrs_.end(); I != E; ++I) {
1086 const MachineBasicBlock *MBB = (*I)->getParent();
1087 if (!Blocks.count(MBB))
1088 continue;
1089 SlotIndex Idx = lis_.getInstructionIndex(*I);
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +00001090 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '\t' << Idx << '\t' << **I);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001091 IndexPair &IP = MBBRange[MBB];
1092 if (!IP.first.isValid() || Idx < IP.first)
1093 IP.first = Idx;
1094 if (!IP.second.isValid() || Idx > IP.second)
1095 IP.second = Idx;
1096 }
1097
1098 // Create a new interval for each block.
1099 for (SplitAnalysis::BlockPtrSet::const_iterator I = Blocks.begin(),
1100 E = Blocks.end(); I != E; ++I) {
1101 IndexPair &IP = MBBRange[*I];
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +00001102 DEBUG(dbgs() << " splitting for BB#" << (*I)->getNumber() << ": ["
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001103 << IP.first << ';' << IP.second << ")\n");
1104 assert(IP.first.isValid() && IP.second.isValid());
1105
1106 openIntv();
1107 enterIntvBefore(IP.first);
1108 useIntv(IP.first.getBaseIndex(), IP.second.getBoundaryIndex());
1109 leaveIntvAfter(IP.second);
1110 closeIntv();
1111 }
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001112 finish();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001113}
1114
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001115
1116//===----------------------------------------------------------------------===//
1117// Sub Block Splitting
1118//===----------------------------------------------------------------------===//
1119
1120/// getBlockForInsideSplit - If curli is contained inside a single basic block,
1121/// and it wou pay to subdivide the interval inside that block, return it.
1122/// Otherwise return NULL. The returned block can be passed to
1123/// SplitEditor::splitInsideBlock.
1124const MachineBasicBlock *SplitAnalysis::getBlockForInsideSplit() {
1125 // The interval must be exclusive to one block.
1126 if (usingBlocks_.size() != 1)
1127 return 0;
1128 // Don't to this for less than 4 instructions. We want to be sure that
1129 // splitting actually reduces the instruction count per interval.
1130 if (usingInstrs_.size() < 4)
1131 return 0;
1132 return usingBlocks_.begin()->first;
1133}
1134
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +00001135/// splitInsideBlock - Split curli into multiple intervals inside MBB.
1136void SplitEditor::splitInsideBlock(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001137 SmallVector<SlotIndex, 32> Uses;
1138 Uses.reserve(sa_.usingInstrs_.size());
1139 for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(),
1140 E = sa_.usingInstrs_.end(); I != E; ++I)
1141 if ((*I)->getParent() == MBB)
1142 Uses.push_back(lis_.getInstructionIndex(*I));
1143 DEBUG(dbgs() << " splitInsideBlock BB#" << MBB->getNumber() << " for "
1144 << Uses.size() << " instructions.\n");
1145 assert(Uses.size() >= 3 && "Need at least 3 instructions");
1146 array_pod_sort(Uses.begin(), Uses.end());
1147
1148 // Simple algorithm: Find the largest gap between uses as determined by slot
1149 // indices. Create new intervals for instructions before the gap and after the
1150 // gap.
1151 unsigned bestPos = 0;
1152 int bestGap = 0;
1153 DEBUG(dbgs() << " dist (" << Uses[0]);
1154 for (unsigned i = 1, e = Uses.size(); i != e; ++i) {
1155 int g = Uses[i-1].distance(Uses[i]);
1156 DEBUG(dbgs() << ") -" << g << "- (" << Uses[i]);
1157 if (g > bestGap)
1158 bestPos = i, bestGap = g;
1159 }
1160 DEBUG(dbgs() << "), best: -" << bestGap << "-\n");
1161
1162 // bestPos points to the first use after the best gap.
1163 assert(bestPos > 0 && "Invalid gap");
1164
1165 // FIXME: Don't create intervals for low densities.
1166
1167 // First interval before the gap. Don't create single-instr intervals.
1168 if (bestPos > 1) {
1169 openIntv();
1170 enterIntvBefore(Uses.front());
1171 useIntv(Uses.front().getBaseIndex(), Uses[bestPos-1].getBoundaryIndex());
1172 leaveIntvAfter(Uses[bestPos-1]);
1173 closeIntv();
1174 }
1175
1176 // Second interval after the gap.
1177 if (bestPos < Uses.size()-1) {
1178 openIntv();
1179 enterIntvBefore(Uses[bestPos]);
1180 useIntv(Uses[bestPos].getBaseIndex(), Uses.back().getBoundaryIndex());
1181 leaveIntvAfter(Uses.back());
1182 closeIntv();
1183 }
1184
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001185 finish();
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001186}