blob: 38c94698799e2c6a147139556414163e5ae455bb [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
Jakob Stoklund Olesen376dcbd2010-11-03 20:39:23 +000015#define DEBUG_TYPE "regalloc"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000016#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"
Jakob Stoklund Olesen8d0963f2010-12-21 01:50:21 +000027#include "llvm/Support/GraphWriter.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000028#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000029#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000031
32using namespace llvm;
33
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000034static cl::opt<bool>
35AllowSplit("spiller-splits-edges",
36 cl::desc("Allow critical edge splitting during spilling"));
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000037
38//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen8d0963f2010-12-21 01:50:21 +000039// Edge Bundles
40//===----------------------------------------------------------------------===//
41
42/// compute - Compute the edge bundles for MF. Bundles depend only on the CFG.
43void EdgeBundles::compute(const MachineFunction *mf) {
44 MF = mf;
45 EC.clear();
46 EC.grow(2 * MF->size());
47
48 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
49 ++I) {
50 const MachineBasicBlock &MBB = *I;
51 unsigned OutE = 2 * MBB.getNumber() + 1;
52 // Join the outgoing bundle with the ingoing bundles of all successors.
53 for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
54 SE = MBB.succ_end(); SI != SE; ++SI)
55 EC.join(OutE, 2 * (*SI)->getNumber());
56 }
57 EC.compress();
58}
59
60/// view - Visualize the annotated bipartite CFG with Graphviz.
61void EdgeBundles::view() const {
62 ViewGraph(*this, "EdgeBundles");
63}
64
65/// Specialize WriteGraph, the standard implementation won't work.
66raw_ostream &llvm::WriteGraph(raw_ostream &O, const EdgeBundles &G,
67 bool ShortNames,
68 const std::string &Title) {
69 const MachineFunction *MF = G.getMachineFunction();
70
71 O << "digraph {\n";
72 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
73 I != E; ++I) {
74 unsigned BB = I->getNumber();
75 O << "\t\"BB#" << BB << "\" [ shape=box ]\n"
76 << '\t' << G.getBundle(BB, false) << " -> \"BB#" << BB << "\"\n"
77 << "\t\"BB#" << BB << "\" -> " << G.getBundle(BB, true) << '\n';
78 }
79 O << "}\n";
80 return O;
81}
82
83
84//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000085// Split Analysis
86//===----------------------------------------------------------------------===//
87
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000088SplitAnalysis::SplitAnalysis(const MachineFunction &mf,
89 const LiveIntervals &lis,
90 const MachineLoopInfo &mli)
91 : mf_(mf),
92 lis_(lis),
93 loops_(mli),
94 tii_(*mf.getTarget().getInstrInfo()),
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000095 curli_(0) {}
96
97void SplitAnalysis::clear() {
98 usingInstrs_.clear();
99 usingBlocks_.clear();
100 usingLoops_.clear();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000101 curli_ = 0;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000102}
103
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000104bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) {
105 MachineBasicBlock *T, *F;
106 SmallVector<MachineOperand, 4> Cond;
107 return !tii_.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond);
108}
109
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000110/// analyzeUses - Count instructions, basic blocks, and loops using curli.
111void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000112 const MachineRegisterInfo &MRI = mf_.getRegInfo();
113 for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(curli_->reg);
114 MachineInstr *MI = I.skipInstruction();) {
115 if (MI->isDebugValue() || !usingInstrs_.insert(MI))
116 continue;
117 MachineBasicBlock *MBB = MI->getParent();
118 if (usingBlocks_[MBB]++)
119 continue;
Jakob Stoklund Olesen9b90d7e2010-10-05 23:10:12 +0000120 for (MachineLoop *Loop = loops_.getLoopFor(MBB); Loop;
121 Loop = Loop->getParentLoop())
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +0000122 usingLoops_[Loop]++;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000123 }
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000124 DEBUG(dbgs() << " counted "
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000125 << usingInstrs_.size() << " instrs, "
126 << usingBlocks_.size() << " blocks, "
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000127 << usingLoops_.size() << " loops.\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000128}
129
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000130void SplitAnalysis::print(const BlockPtrSet &B, raw_ostream &OS) const {
131 for (BlockPtrSet::const_iterator I = B.begin(), E = B.end(); I != E; ++I) {
132 unsigned count = usingBlocks_.lookup(*I);
133 OS << " BB#" << (*I)->getNumber();
134 if (count)
135 OS << '(' << count << ')';
136 }
137}
138
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000139// Get three sets of basic blocks surrounding a loop: Blocks inside the loop,
140// predecessor blocks, and exit blocks.
141void SplitAnalysis::getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks) {
142 Blocks.clear();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000143
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000144 // Blocks in the loop.
145 Blocks.Loop.insert(Loop->block_begin(), Loop->block_end());
146
147 // Predecessor blocks.
148 const MachineBasicBlock *Header = Loop->getHeader();
149 for (MachineBasicBlock::const_pred_iterator I = Header->pred_begin(),
150 E = Header->pred_end(); I != E; ++I)
151 if (!Blocks.Loop.count(*I))
152 Blocks.Preds.insert(*I);
153
154 // Exit blocks.
155 for (MachineLoop::block_iterator I = Loop->block_begin(),
156 E = Loop->block_end(); I != E; ++I) {
157 const MachineBasicBlock *MBB = *I;
158 for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
159 SE = MBB->succ_end(); SI != SE; ++SI)
160 if (!Blocks.Loop.count(*SI))
161 Blocks.Exits.insert(*SI);
162 }
163}
164
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000165void SplitAnalysis::print(const LoopBlocks &B, raw_ostream &OS) const {
166 OS << "Loop:";
167 print(B.Loop, OS);
168 OS << ", preds:";
169 print(B.Preds, OS);
170 OS << ", exits:";
171 print(B.Exits, OS);
172}
173
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000174/// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
175/// and around the Loop.
176SplitAnalysis::LoopPeripheralUse SplitAnalysis::
177analyzeLoopPeripheralUse(const SplitAnalysis::LoopBlocks &Blocks) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000178 LoopPeripheralUse use = ContainedInLoop;
179 for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end();
180 I != E; ++I) {
181 const MachineBasicBlock *MBB = I->first;
182 // Is this a peripheral block?
183 if (use < MultiPeripheral &&
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000184 (Blocks.Preds.count(MBB) || Blocks.Exits.count(MBB))) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000185 if (I->second > 1) use = MultiPeripheral;
186 else use = SinglePeripheral;
187 continue;
188 }
189 // Is it a loop block?
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000190 if (Blocks.Loop.count(MBB))
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000191 continue;
192 // It must be an unrelated block.
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000193 DEBUG(dbgs() << ", outside: BB#" << MBB->getNumber());
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000194 return OutsideLoop;
195 }
196 return use;
197}
198
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000199/// getCriticalExits - It may be necessary to partially break critical edges
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000200/// leaving the loop if an exit block has predecessors from outside the loop
201/// periphery.
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000202void SplitAnalysis::getCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
203 BlockPtrSet &CriticalExits) {
204 CriticalExits.clear();
205
Jakob Stoklund Olesen0960a652010-10-27 00:39:05 +0000206 // A critical exit block has curli live-in, and has a predecessor that is not
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000207 // in the loop nor a loop predecessor. For such an exit block, the edges
208 // carrying the new variable must be moved to a new pre-exit block.
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000209 for (BlockPtrSet::iterator I = Blocks.Exits.begin(), E = Blocks.Exits.end();
210 I != E; ++I) {
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000211 const MachineBasicBlock *Exit = *I;
212 // A single-predecessor exit block is definitely not a critical edge.
213 if (Exit->pred_size() == 1)
214 continue;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000215 // This exit may not have curli live in at all. No need to split.
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000216 if (!lis_.isLiveInToMBB(*curli_, Exit))
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000217 continue;
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000218 // Does this exit block have a predecessor that is not a loop block or loop
219 // predecessor?
220 for (MachineBasicBlock::const_pred_iterator PI = Exit->pred_begin(),
221 PE = Exit->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000222 const MachineBasicBlock *Pred = *PI;
223 if (Blocks.Loop.count(Pred) || Blocks.Preds.count(Pred))
224 continue;
225 // This is a critical exit block, and we need to split the exit edge.
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000226 CriticalExits.insert(Exit);
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000227 break;
228 }
229 }
230}
231
Jakob Stoklund Olesen0960a652010-10-27 00:39:05 +0000232void SplitAnalysis::getCriticalPreds(const SplitAnalysis::LoopBlocks &Blocks,
233 BlockPtrSet &CriticalPreds) {
234 CriticalPreds.clear();
235
236 // A critical predecessor block has curli live-out, and has a successor that
237 // has curli live-in and is not in the loop nor a loop exit block. For such a
238 // predecessor block, we must carry the value in both the 'inside' and
239 // 'outside' registers.
240 for (BlockPtrSet::iterator I = Blocks.Preds.begin(), E = Blocks.Preds.end();
241 I != E; ++I) {
242 const MachineBasicBlock *Pred = *I;
243 // Definitely not a critical edge.
244 if (Pred->succ_size() == 1)
245 continue;
246 // This block may not have curli live out at all if there is a PHI.
247 if (!lis_.isLiveOutOfMBB(*curli_, Pred))
248 continue;
249 // Does this block have a successor outside the loop?
250 for (MachineBasicBlock::const_pred_iterator SI = Pred->succ_begin(),
251 SE = Pred->succ_end(); SI != SE; ++SI) {
252 const MachineBasicBlock *Succ = *SI;
253 if (Blocks.Loop.count(Succ) || Blocks.Exits.count(Succ))
254 continue;
255 if (!lis_.isLiveInToMBB(*curli_, Succ))
256 continue;
257 // This is a critical predecessor block.
258 CriticalPreds.insert(Pred);
259 break;
260 }
261 }
262}
263
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000264/// canSplitCriticalExits - Return true if it is possible to insert new exit
265/// blocks before the blocks in CriticalExits.
266bool
267SplitAnalysis::canSplitCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
268 BlockPtrSet &CriticalExits) {
269 // If we don't allow critical edge splitting, require no critical exits.
270 if (!AllowSplit)
271 return CriticalExits.empty();
272
273 for (BlockPtrSet::iterator I = CriticalExits.begin(), E = CriticalExits.end();
274 I != E; ++I) {
275 const MachineBasicBlock *Succ = *I;
276 // We want to insert a new pre-exit MBB before Succ, and change all the
277 // in-loop blocks to branch to the pre-exit instead of Succ.
278 // Check that all the in-loop predecessors can be changed.
279 for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(),
280 PE = Succ->pred_end(); PI != PE; ++PI) {
281 const MachineBasicBlock *Pred = *PI;
282 // The external predecessors won't be altered.
283 if (!Blocks.Loop.count(Pred) && !Blocks.Preds.count(Pred))
284 continue;
285 if (!canAnalyzeBranch(Pred))
286 return false;
287 }
288
289 // If Succ's layout predecessor falls through, that too must be analyzable.
290 // We need to insert the pre-exit block in the gap.
291 MachineFunction::const_iterator MFI = Succ;
292 if (MFI == mf_.begin())
293 continue;
294 if (!canAnalyzeBranch(--MFI))
295 return false;
296 }
297 // No problems found.
298 return true;
299}
300
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000301void SplitAnalysis::analyze(const LiveInterval *li) {
302 clear();
303 curli_ = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000304 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000305}
306
Jakob Stoklund Olesen521a4532010-12-15 17:41:19 +0000307void SplitAnalysis::getSplitLoops(LoopPtrSet &Loops) {
308 assert(curli_ && "Call analyze() before getSplitLoops");
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000309 if (usingLoops_.empty())
Jakob Stoklund Olesen521a4532010-12-15 17:41:19 +0000310 return;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000311
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000312 LoopBlocks Blocks;
313 BlockPtrSet CriticalExits;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000314
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000315 // We split around loops where curli is used outside the periphery.
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +0000316 for (LoopCountMap::const_iterator I = usingLoops_.begin(),
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000317 E = usingLoops_.end(); I != E; ++I) {
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +0000318 const MachineLoop *Loop = I->first;
319 getLoopBlocks(Loop, Blocks);
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000320 DEBUG({ dbgs() << " "; print(Blocks, dbgs()); });
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000321
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000322 switch(analyzeLoopPeripheralUse(Blocks)) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000323 case OutsideLoop:
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000324 break;
325 case MultiPeripheral:
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000326 // FIXME: We could split a live range with multiple uses in a peripheral
327 // block and still make progress. However, it is possible that splitting
328 // another live range will insert copies into a peripheral block, and
Jakob Stoklund Olesen521a4532010-12-15 17:41:19 +0000329 // there is a small chance we can enter an infinite loop, inserting copies
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000330 // forever.
331 // For safety, stick to splitting live ranges with uses outside the
332 // periphery.
Jakob Stoklund Olesen521a4532010-12-15 17:41:19 +0000333 DEBUG(dbgs() << ": multiple peripheral uses");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000334 break;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000335 case ContainedInLoop:
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000336 DEBUG(dbgs() << ": fully contained\n");
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000337 continue;
338 case SinglePeripheral:
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000339 DEBUG(dbgs() << ": single peripheral use\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000340 continue;
341 }
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000342 // Will it be possible to split around this loop?
343 getCriticalExits(Blocks, CriticalExits);
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000344 DEBUG(dbgs() << ": " << CriticalExits.size() << " critical exits\n");
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000345 if (!canSplitCriticalExits(Blocks, CriticalExits))
346 continue;
347 // This is a possible split.
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000348 Loops.insert(Loop);
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000349 }
350
Jakob Stoklund Olesen521a4532010-12-15 17:41:19 +0000351 DEBUG(dbgs() << " getSplitLoops found " << Loops.size()
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000352 << " candidate loops.\n");
Jakob Stoklund Olesen521a4532010-12-15 17:41:19 +0000353}
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000354
Jakob Stoklund Olesen521a4532010-12-15 17:41:19 +0000355const MachineLoop *SplitAnalysis::getBestSplitLoop() {
356 LoopPtrSet Loops;
357 getSplitLoops(Loops);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000358 if (Loops.empty())
359 return 0;
360
361 // Pick the earliest loop.
362 // FIXME: Are there other heuristics to consider?
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000363 const MachineLoop *Best = 0;
364 SlotIndex BestIdx;
365 for (LoopPtrSet::const_iterator I = Loops.begin(), E = Loops.end(); I != E;
366 ++I) {
367 SlotIndex Idx = lis_.getMBBStartIdx((*I)->getHeader());
368 if (!Best || Idx < BestIdx)
369 Best = *I, BestIdx = Idx;
370 }
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000371 DEBUG(dbgs() << " getBestSplitLoop found " << *Best);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000372 return Best;
373}
374
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000375/// isBypassLoop - Return true if curli is live through Loop and has no uses
376/// inside the loop. Bypass loops are candidates for splitting because it can
377/// prevent interference inside the loop.
378bool SplitAnalysis::isBypassLoop(const MachineLoop *Loop) {
379 // If curli is live into the loop header and there are no uses in the loop, it
380 // must be live in the entire loop and live on at least one exiting edge.
381 return !usingLoops_.count(Loop) &&
382 lis_.isLiveInToMBB(*curli_, Loop->getHeader());
383}
384
385/// getBypassLoops - Get all the maximal bypass loops. These are the bypass
386/// loops whose parent is not a bypass loop.
387void SplitAnalysis::getBypassLoops(LoopPtrSet &BypassLoops) {
388 SmallVector<MachineLoop*, 8> Todo(loops_.begin(), loops_.end());
Jakob Stoklund Olesen62032952010-12-15 18:07:48 +0000389 while (!Todo.empty()) {
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000390 MachineLoop *Loop = Todo.pop_back_val();
391 if (!usingLoops_.count(Loop)) {
392 // This is either a bypass loop or completely irrelevant.
393 if (lis_.isLiveInToMBB(*curli_, Loop->getHeader()))
394 BypassLoops.insert(Loop);
395 // Either way, skip the child loops.
396 continue;
397 }
398
399 // The child loops may be bypass loops.
400 Todo.append(Loop->begin(), Loop->end());
401 }
402}
403
404
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000405//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000406// LiveIntervalMap
407//===----------------------------------------------------------------------===//
408
Jakob Stoklund Olesenb3e96812010-09-13 21:29:45 +0000409// Work around the fact that the std::pair constructors are broken for pointer
410// pairs in some implementations. makeVV(x, 0) works.
411static inline std::pair<const VNInfo*, VNInfo*>
412makeVV(const VNInfo *a, VNInfo *b) {
413 return std::make_pair(a, b);
414}
415
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000416void LiveIntervalMap::reset(LiveInterval *li) {
417 li_ = li;
418 valueMap_.clear();
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000419 liveOutCache_.clear();
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000420}
421
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000422bool LiveIntervalMap::isComplexMapped(const VNInfo *ParentVNI) const {
423 ValueMap::const_iterator i = valueMap_.find(ParentVNI);
424 return i != valueMap_.end() && i->second == 0;
425}
426
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000427// defValue - Introduce a li_ def for ParentVNI that could be later than
428// ParentVNI->def.
429VNInfo *LiveIntervalMap::defValue(const VNInfo *ParentVNI, SlotIndex Idx) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000430 assert(li_ && "call reset first");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000431 assert(ParentVNI && "Mapping NULL value");
432 assert(Idx.isValid() && "Invalid SlotIndex");
433 assert(parentli_.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
434
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000435 // Create a new value.
Lang Hames6e2968c2010-09-25 12:04:16 +0000436 VNInfo *VNI = li_->getNextValue(Idx, 0, lis_.getVNInfoAllocator());
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000437
Jakob Stoklund Olesen79c02622010-10-26 22:36:02 +0000438 // Preserve the PHIDef bit.
439 if (ParentVNI->isPHIDef() && Idx == ParentVNI->def)
440 VNI->setIsPHIDef(true);
441
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000442 // Use insert for lookup, so we can add missing values with a second lookup.
443 std::pair<ValueMap::iterator,bool> InsP =
444 valueMap_.insert(makeVV(ParentVNI, Idx == ParentVNI->def ? VNI : 0));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000445
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000446 // This is now a complex def. Mark with a NULL in valueMap.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000447 if (!InsP.second)
448 InsP.first->second = 0;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000449
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000450 return VNI;
451}
452
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000453
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000454// mapValue - Find the mapped value for ParentVNI at Idx.
455// Potentially create phi-def values.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000456VNInfo *LiveIntervalMap::mapValue(const VNInfo *ParentVNI, SlotIndex Idx,
457 bool *simple) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000458 assert(li_ && "call reset first");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000459 assert(ParentVNI && "Mapping NULL value");
460 assert(Idx.isValid() && "Invalid SlotIndex");
461 assert(parentli_.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
462
463 // Use insert for lookup, so we can add missing values with a second lookup.
464 std::pair<ValueMap::iterator,bool> InsP =
Jakob Stoklund Olesenb3e96812010-09-13 21:29:45 +0000465 valueMap_.insert(makeVV(ParentVNI, 0));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000466
467 // This was an unknown value. Create a simple mapping.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000468 if (InsP.second) {
469 if (simple) *simple = true;
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000470 return InsP.first->second = li_->createValueCopy(ParentVNI,
471 lis_.getVNInfoAllocator());
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000472 }
473
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000474 // This was a simple mapped value.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000475 if (InsP.first->second) {
476 if (simple) *simple = true;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000477 return InsP.first->second;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000478 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000479
480 // This is a complex mapped value. There may be multiple defs, and we may need
481 // to create phi-defs.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000482 if (simple) *simple = false;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000483 MachineBasicBlock *IdxMBB = lis_.getMBBFromIndex(Idx);
484 assert(IdxMBB && "No MBB at Idx");
485
486 // Is there a def in the same MBB we can extend?
487 if (VNInfo *VNI = extendTo(IdxMBB, Idx))
488 return VNI;
489
490 // Now for the fun part. We know that ParentVNI potentially has multiple defs,
491 // and we may need to create even more phi-defs to preserve VNInfo SSA form.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000492 // Perform a search for all predecessor blocks where we know the dominating
493 // VNInfo. Insert phi-def VNInfos along the path back to IdxMBB.
494 DEBUG(dbgs() << "\n Reaching defs for BB#" << IdxMBB->getNumber()
495 << " at " << Idx << " in " << *li_ << '\n');
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000496
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000497 // Blocks where li_ should be live-in.
498 SmallVector<MachineDomTreeNode*, 16> LiveIn;
499 LiveIn.push_back(mdt_[IdxMBB]);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000500
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000501 // Using liveOutCache_ as a visited set, perform a BFS for all reaching defs.
502 for (unsigned i = 0; i != LiveIn.size(); ++i) {
503 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
504 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
505 PE = MBB->pred_end(); PI != PE; ++PI) {
506 MachineBasicBlock *Pred = *PI;
507 // Is this a known live-out block?
508 std::pair<LiveOutMap::iterator,bool> LOIP =
509 liveOutCache_.insert(std::make_pair(Pred, LiveOutPair()));
510 // Yes, we have been here before.
511 if (!LOIP.second) {
Benjamin Kramer8a8e26f2010-10-29 17:40:05 +0000512 DEBUG(if (VNInfo *VNI = LOIP.first->second.first)
513 dbgs() << " known valno #" << VNI->id
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000514 << " at BB#" << Pred->getNumber() << '\n');
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000515 continue;
516 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000517
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000518 // Does Pred provide a live-out value?
519 SlotIndex Last = lis_.getMBBEndIdx(Pred).getPrevSlot();
520 if (VNInfo *VNI = extendTo(Pred, Last)) {
521 MachineBasicBlock *DefMBB = lis_.getMBBFromIndex(VNI->def);
522 DEBUG(dbgs() << " found valno #" << VNI->id
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000523 << " from BB#" << DefMBB->getNumber()
524 << " at BB#" << Pred->getNumber() << '\n');
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000525 LiveOutPair &LOP = LOIP.first->second;
526 LOP.first = VNI;
Benjamin Kramer8a8e26f2010-10-29 17:40:05 +0000527 LOP.second = mdt_[DefMBB];
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000528 continue;
529 }
530 // No, we need a live-in value for Pred as well
531 if (Pred != IdxMBB)
532 LiveIn.push_back(mdt_[Pred]);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000533 }
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000534 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000535
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000536 // We may need to add phi-def values to preserve the SSA form.
537 // This is essentially the same iterative algorithm that SSAUpdater uses,
538 // except we already have a dominator tree, so we don't have to recompute it.
539 VNInfo *IdxVNI = 0;
540 unsigned Changes;
541 do {
542 Changes = 0;
543 DEBUG(dbgs() << " Iterating over " << LiveIn.size() << " blocks.\n");
544 // Propagate live-out values down the dominator tree, inserting phi-defs when
545 // necessary. Since LiveIn was created by a BFS, going backwards makes it more
546 // likely for us to visit immediate dominators before their children.
547 for (unsigned i = LiveIn.size(); i; --i) {
548 MachineDomTreeNode *Node = LiveIn[i-1];
549 MachineBasicBlock *MBB = Node->getBlock();
550 MachineDomTreeNode *IDom = Node->getIDom();
551 LiveOutPair IDomValue;
552 // We need a live-in value to a block with no immediate dominator?
553 // This is probably an unreachable block that has survived somehow.
554 bool needPHI = !IDom;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000555
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000556 // Get the IDom live-out value.
557 if (!needPHI) {
558 LiveOutMap::iterator I = liveOutCache_.find(IDom->getBlock());
559 if (I != liveOutCache_.end())
560 IDomValue = I->second;
561 else
562 // If IDom is outside our set of live-out blocks, there must be new
563 // defs, and we need a phi-def here.
564 needPHI = true;
565 }
Jakob Stoklund Olesen984a7fc2010-10-05 20:36:28 +0000566
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000567 // IDom dominates all of our predecessors, but it may not be the immediate
568 // dominator. Check if any of them have live-out values that are properly
569 // dominated by IDom. If so, we need a phi-def here.
570 if (!needPHI) {
571 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
572 PE = MBB->pred_end(); PI != PE; ++PI) {
573 LiveOutPair Value = liveOutCache_[*PI];
574 if (!Value.first || Value.first == IDomValue.first)
575 continue;
576 // This predecessor is carrying something other than IDomValue.
577 // It could be because IDomValue hasn't propagated yet, or it could be
578 // because MBB is in the dominance frontier of that value.
579 if (mdt_.dominates(IDom, Value.second)) {
580 needPHI = true;
581 break;
582 }
583 }
584 }
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000585
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000586 // Create a phi-def if required.
587 if (needPHI) {
588 ++Changes;
589 SlotIndex Start = lis_.getMBBStartIdx(MBB);
590 VNInfo *VNI = li_->getNextValue(Start, 0, lis_.getVNInfoAllocator());
591 VNI->setIsPHIDef(true);
592 DEBUG(dbgs() << " - BB#" << MBB->getNumber()
593 << " phi-def #" << VNI->id << " at " << Start << '\n');
594 // We no longer need li_ to be live-in.
595 LiveIn.erase(LiveIn.begin()+(i-1));
596 // Blocks in LiveIn are either IdxMBB, or have a value live-through.
597 if (MBB == IdxMBB)
598 IdxVNI = VNI;
599 // Check if we need to update live-out info.
600 LiveOutMap::iterator I = liveOutCache_.find(MBB);
601 if (I == liveOutCache_.end() || I->second.second == Node) {
602 // We already have a live-out defined in MBB, so this must be IdxMBB.
603 assert(MBB == IdxMBB && "Adding phi-def to known live-out");
604 li_->addRange(LiveRange(Start, Idx.getNextSlot(), VNI));
605 } else {
606 // This phi-def is also live-out, so color the whole block.
607 li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI));
608 I->second = LiveOutPair(VNI, Node);
609 }
610 } else if (IDomValue.first) {
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000611 // No phi-def here. Remember incoming value for IdxMBB.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000612 if (MBB == IdxMBB)
613 IdxVNI = IDomValue.first;
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000614 // Propagate IDomValue if needed:
615 // MBB is live-out and doesn't define its own value.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000616 LiveOutMap::iterator I = liveOutCache_.find(MBB);
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000617 if (I != liveOutCache_.end() && I->second.second != Node &&
618 I->second.first != IDomValue.first) {
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000619 ++Changes;
620 I->second = IDomValue;
621 DEBUG(dbgs() << " - BB#" << MBB->getNumber()
622 << " idom valno #" << IDomValue.first->id
623 << " from BB#" << IDom->getBlock()->getNumber() << '\n');
624 }
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000625 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000626 }
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000627 DEBUG(dbgs() << " - made " << Changes << " changes.\n");
628 } while (Changes);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000629
630 assert(IdxVNI && "Didn't find value for Idx");
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000631
632#ifndef NDEBUG
633 // Check the liveOutCache_ invariants.
634 for (LiveOutMap::iterator I = liveOutCache_.begin(), E = liveOutCache_.end();
635 I != E; ++I) {
636 assert(I->first && "Null MBB entry in cache");
637 assert(I->second.first && "Null VNInfo in cache");
638 assert(I->second.second && "Null DomTreeNode in cache");
639 if (I->second.second->getBlock() == I->first)
640 continue;
641 for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(),
642 PE = I->first->pred_end(); PI != PE; ++PI)
643 assert(liveOutCache_.lookup(*PI) == I->second && "Bad invariant");
644 }
645#endif
646
647 // Since we went through the trouble of a full BFS visiting all reaching defs,
648 // the values in LiveIn are now accurate. No more phi-defs are needed
649 // for these blocks, so we can color the live ranges.
650 // This makes the next mapValue call much faster.
651 for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) {
652 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
653 SlotIndex Start = lis_.getMBBStartIdx(MBB);
654 if (MBB == IdxMBB) {
655 li_->addRange(LiveRange(Start, Idx.getNextSlot(), IdxVNI));
656 continue;
657 }
658 // Anything in LiveIn other than IdxMBB is live-through.
659 VNInfo *VNI = liveOutCache_.lookup(MBB).first;
660 assert(VNI && "Missing block value");
661 li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI));
662 }
663
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000664 return IdxVNI;
665}
666
667// extendTo - Find the last li_ value defined in MBB at or before Idx. The
668// parentli_ is assumed to be live at Idx. Extend the live range to Idx.
669// Return the found VNInfo, or NULL.
Jakob Stoklund Olesenc95c1462010-10-27 00:39:07 +0000670VNInfo *LiveIntervalMap::extendTo(const MachineBasicBlock *MBB, SlotIndex Idx) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000671 assert(li_ && "call reset first");
672 LiveInterval::iterator I = std::upper_bound(li_->begin(), li_->end(), Idx);
673 if (I == li_->begin())
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000674 return 0;
675 --I;
Jakob Stoklund Olesenfc60d772010-10-05 20:36:25 +0000676 if (I->end <= lis_.getMBBStartIdx(MBB))
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000677 return 0;
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000678 if (I->end <= Idx)
679 I->end = Idx.getNextSlot();
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000680 return I->valno;
681}
682
683// addSimpleRange - Add a simple range from parentli_ to li_.
684// ParentVNI must be live in the [Start;End) interval.
685void LiveIntervalMap::addSimpleRange(SlotIndex Start, SlotIndex End,
686 const VNInfo *ParentVNI) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000687 assert(li_ && "call reset first");
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000688 bool simple;
689 VNInfo *VNI = mapValue(ParentVNI, Start, &simple);
690 // A simple mapping is easy.
691 if (simple) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000692 li_->addRange(LiveRange(Start, End, VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000693 return;
694 }
695
696 // ParentVNI is a complex value. We must map per MBB.
697 MachineFunction::iterator MBB = lis_.getMBBFromIndex(Start);
Jakob Stoklund Olesendbc36092010-10-05 22:19:29 +0000698 MachineFunction::iterator MBBE = lis_.getMBBFromIndex(End.getPrevSlot());
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000699
700 if (MBB == MBBE) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000701 li_->addRange(LiveRange(Start, End, VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000702 return;
703 }
704
705 // First block.
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000706 li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000707
708 // Run sequence of full blocks.
709 for (++MBB; MBB != MBBE; ++MBB) {
710 Start = lis_.getMBBStartIdx(MBB);
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000711 li_->addRange(LiveRange(Start, lis_.getMBBEndIdx(MBB),
712 mapValue(ParentVNI, Start)));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000713 }
714
715 // Final block.
716 Start = lis_.getMBBStartIdx(MBB);
717 if (Start != End)
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000718 li_->addRange(LiveRange(Start, End, mapValue(ParentVNI, Start)));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000719}
720
721/// addRange - Add live ranges to li_ where [Start;End) intersects parentli_.
722/// All needed values whose def is not inside [Start;End) must be defined
723/// beforehand so mapValue will work.
724void LiveIntervalMap::addRange(SlotIndex Start, SlotIndex End) {
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000725 assert(li_ && "call reset first");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000726 LiveInterval::const_iterator B = parentli_.begin(), E = parentli_.end();
727 LiveInterval::const_iterator I = std::lower_bound(B, E, Start);
728
729 // Check if --I begins before Start and overlaps.
730 if (I != B) {
731 --I;
732 if (I->end > Start)
733 addSimpleRange(Start, std::min(End, I->end), I->valno);
734 ++I;
735 }
736
737 // The remaining ranges begin after Start.
738 for (;I != E && I->start < End; ++I)
739 addSimpleRange(I->start, std::min(End, I->end), I->valno);
740}
741
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000742
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000743//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000744// Split Editor
745//===----------------------------------------------------------------------===//
746
747/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000748SplitEditor::SplitEditor(SplitAnalysis &sa,
749 LiveIntervals &lis,
750 VirtRegMap &vrm,
751 MachineDominatorTree &mdt,
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000752 LiveRangeEdit &edit)
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000753 : sa_(sa), lis_(lis), vrm_(vrm),
754 mri_(vrm.getMachineFunction().getRegInfo()),
755 tii_(*vrm.getMachineFunction().getTarget().getInstrInfo()),
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000756 tri_(*vrm.getMachineFunction().getTarget().getRegisterInfo()),
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000757 edit_(edit),
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000758 dupli_(lis_, mdt, edit.getParent()),
759 openli_(lis_, mdt, edit.getParent())
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000760{
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000761 // We don't need an AliasAnalysis since we will only be performing
762 // cheap-as-a-copy remats anyway.
763 edit_.anyRematerializable(lis_, tii_, 0);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000764}
765
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000766bool SplitEditor::intervalsLiveAt(SlotIndex Idx) const {
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000767 for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I)
768 if (*I != dupli_.getLI() && (*I)->liveAt(Idx))
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000769 return true;
770 return false;
771}
772
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000773VNInfo *SplitEditor::defFromParent(LiveIntervalMap &Reg,
774 VNInfo *ParentVNI,
775 SlotIndex UseIdx,
776 MachineBasicBlock &MBB,
777 MachineBasicBlock::iterator I) {
778 VNInfo *VNI = 0;
779 MachineInstr *CopyMI = 0;
780 SlotIndex Def;
781
782 // Attempt cheap-as-a-copy rematerialization.
783 LiveRangeEdit::Remat RM(ParentVNI);
784 if (edit_.canRematerializeAt(RM, UseIdx, true, lis_)) {
785 Def = edit_.rematerializeAt(MBB, I, Reg.getLI()->reg, RM,
786 lis_, tii_, tri_);
787 } else {
788 // Can't remat, just insert a copy from parent.
789 CopyMI = BuildMI(MBB, I, DebugLoc(), tii_.get(TargetOpcode::COPY),
790 Reg.getLI()->reg).addReg(edit_.getReg());
791 Def = lis_.InsertMachineInstrInMaps(CopyMI).getDefIndex();
792 }
793
794 // Define the value in Reg.
795 VNI = Reg.defValue(ParentVNI, Def);
796 VNI->setCopy(CopyMI);
797
798 // Add minimal liveness for the new value.
799 if (UseIdx < Def)
800 UseIdx = Def;
801 Reg.getLI()->addRange(LiveRange(Def, UseIdx.getNextSlot(), VNI));
802 return VNI;
803}
804
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000805/// Create a new virtual register and live interval.
806void SplitEditor::openIntv() {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000807 assert(!openli_.getLI() && "Previous LI not closed before openIntv");
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000808 if (!dupli_.getLI())
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000809 dupli_.reset(&edit_.create(mri_, lis_, vrm_));
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000810
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000811 openli_.reset(&edit_.create(mri_, lis_, vrm_));
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000812}
813
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000814/// enterIntvBefore - Enter openli before the instruction at Idx. If curli is
815/// not live before Idx, a COPY is not inserted.
816void SplitEditor::enterIntvBefore(SlotIndex Idx) {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000817 assert(openli_.getLI() && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000818 Idx = Idx.getUseIndex();
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000819 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000820 VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000821 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000822 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000823 return;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000824 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000825 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000826 truncatedValues.insert(ParentVNI);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000827 MachineInstr *MI = lis_.getInstructionFromIndex(Idx);
828 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000829
830 defFromParent(openli_, ParentVNI, Idx, *MI->getParent(), MI);
831
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000832 DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000833}
834
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000835/// enterIntvAtEnd - Enter openli at the end of MBB.
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000836void SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000837 assert(openli_.getLI() && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000838 SlotIndex End = lis_.getMBBEndIdx(&MBB).getPrevSlot();
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000839 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << End);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000840 VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(End);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000841 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000842 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000843 return;
844 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000845 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000846 truncatedValues.insert(ParentVNI);
Jakob Stoklund Olesenb5f327b2010-11-10 23:56:00 +0000847 defFromParent(openli_, ParentVNI, End, MBB, MBB.getFirstTerminator());
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000848 DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000849}
850
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000851/// useIntv - indicate that all instructions in MBB should use openli.
852void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
853 useIntv(lis_.getMBBStartIdx(&MBB), lis_.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000854}
855
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000856void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000857 assert(openli_.getLI() && "openIntv not called before useIntv");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000858 openli_.addRange(Start, End);
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000859 DEBUG(dbgs() << " use [" << Start << ';' << End << "): "
860 << *openli_.getLI() << '\n');
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000861}
862
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000863/// leaveIntvAfter - Leave openli after the instruction at Idx.
864void SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000865 assert(openli_.getLI() && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000866 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000867
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000868 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000869 Idx = Idx.getBoundaryIndex();
870 VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000871 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000872 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000873 return;
874 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000875 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000876
Jakob Stoklund Olesenfc60d772010-10-05 20:36:25 +0000877 MachineBasicBlock::iterator MII = lis_.getInstructionFromIndex(Idx);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000878 VNInfo *VNI = defFromParent(dupli_, ParentVNI, Idx,
879 *MII->getParent(), llvm::next(MII));
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000880
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000881 // Make sure that openli is properly extended from Idx to the new copy.
882 // FIXME: This shouldn't be necessary for remats.
883 openli_.addSimpleRange(Idx, VNI->def, ParentVNI);
884
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000885 DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000886}
887
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000888/// leaveIntvAtTop - Leave the interval at the top of MBB.
889/// Currently, only one value can leave the interval.
890void SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000891 assert(openli_.getLI() && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000892 SlotIndex Start = lis_.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000893 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000894
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +0000895 VNInfo *ParentVNI = edit_.getParent().getVNInfoAt(Start);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000896 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000897 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000898 return;
899 }
900
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000901 VNInfo *VNI = defFromParent(dupli_, ParentVNI, Start, MBB,
902 MBB.SkipPHIsAndLabels(MBB.begin()));
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000903
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000904 // Finally we must make sure that openli is properly extended from Start to
905 // the new copy.
Jakob Stoklund Olesenfc60d772010-10-05 20:36:25 +0000906 openli_.addSimpleRange(Start, VNI->def, ParentVNI);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000907 DEBUG(dbgs() << ": " << *openli_.getLI() << '\n');
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000908}
909
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000910/// closeIntv - Indicate that we are done editing the currently open
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000911/// LiveInterval, and ranges can be trimmed.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000912void SplitEditor::closeIntv() {
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000913 assert(openli_.getLI() && "openIntv not called before closeIntv");
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000914
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000915 DEBUG(dbgs() << " closeIntv cleaning up\n");
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000916 DEBUG(dbgs() << " open " << *openli_.getLI() << '\n');
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000917 openli_.reset(0);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000918}
919
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000920/// rewrite - Rewrite all uses of reg to use the new registers.
921void SplitEditor::rewrite(unsigned reg) {
922 for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(reg),
923 RE = mri_.reg_end(); RI != RE;) {
924 MachineOperand &MO = RI.getOperand();
Jakob Stoklund Olesen79cb53c2010-11-01 21:51:29 +0000925 unsigned OpNum = RI.getOperandNo();
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000926 MachineInstr *MI = MO.getParent();
927 ++RI;
928 if (MI->isDebugValue()) {
929 DEBUG(dbgs() << "Zapping " << *MI);
930 // FIXME: We can do much better with debug values.
931 MO.setReg(0);
932 continue;
933 }
934 SlotIndex Idx = lis_.getInstructionIndex(MI);
935 Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
936 LiveInterval *LI = 0;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000937 for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E;
938 ++I) {
939 LiveInterval *testli = *I;
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000940 if (testli->liveAt(Idx)) {
941 LI = testli;
942 break;
943 }
944 }
Jakob Stoklund Olesen9d999772010-10-21 18:47:08 +0000945 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'<< Idx);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000946 assert(LI && "No register was live at use");
947 MO.setReg(LI->reg);
Jakob Stoklund Olesen79cb53c2010-11-01 21:51:29 +0000948 if (MO.isUse() && !MI->isRegTiedToDefOperand(OpNum))
949 MO.setIsKill(LI->killedAt(Idx.getDefIndex()));
Jakob Stoklund Olesen9d999772010-10-21 18:47:08 +0000950 DEBUG(dbgs() << '\t' << *MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000951 }
952}
953
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000954void
955SplitEditor::addTruncSimpleRange(SlotIndex Start, SlotIndex End, VNInfo *VNI) {
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000956 // Build vector of iterator pairs from the intervals.
957 typedef std::pair<LiveInterval::const_iterator,
958 LiveInterval::const_iterator> IIPair;
959 SmallVector<IIPair, 8> Iters;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000960 for (LiveRangeEdit::iterator LI = edit_.begin(), LE = edit_.end(); LI != LE;
961 ++LI) {
Jakob Stoklund Olesen9d999772010-10-21 18:47:08 +0000962 if (*LI == dupli_.getLI())
963 continue;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000964 LiveInterval::const_iterator I = (*LI)->find(Start);
965 LiveInterval::const_iterator E = (*LI)->end();
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000966 if (I != E)
967 Iters.push_back(std::make_pair(I, E));
968 }
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000969
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000970 SlotIndex sidx = Start;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000971 // Break [Start;End) into segments that don't overlap any intervals.
972 for (;;) {
973 SlotIndex next = sidx, eidx = End;
974 // Find overlapping intervals.
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000975 for (unsigned i = 0; i != Iters.size() && sidx < eidx; ++i) {
976 LiveInterval::const_iterator I = Iters[i].first;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000977 // Interval I is overlapping [sidx;eidx). Trim sidx.
978 if (I->start <= sidx) {
979 sidx = I->end;
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000980 // Move to the next run, remove iters when all are consumed.
981 I = ++Iters[i].first;
982 if (I == Iters[i].second) {
983 Iters.erase(Iters.begin() + i);
984 --i;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000985 continue;
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000986 }
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000987 }
988 // Trim eidx too if needed.
989 if (I->start >= eidx)
990 continue;
991 eidx = I->start;
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000992 next = I->end;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000993 }
994 // Now, [sidx;eidx) doesn't overlap anything in intervals_.
995 if (sidx < eidx)
996 dupli_.addSimpleRange(sidx, eidx, VNI);
997 // If the interval end was truncated, we can try again from next.
998 if (next <= sidx)
999 break;
1000 sidx = next;
1001 }
1002}
1003
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001004void SplitEditor::computeRemainder() {
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001005 // First we need to fill in the live ranges in dupli.
1006 // If values were redefined, we need a full recoloring with SSA update.
1007 // If values were truncated, we only need to truncate the ranges.
1008 // If values were partially rematted, we should shrink to uses.
1009 // If values were fully rematted, they should be omitted.
1010 // FIXME: If a single value is redefined, just move the def and truncate.
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +00001011 LiveInterval &parent = edit_.getParent();
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001012
1013 // Values that are fully contained in the split intervals.
1014 SmallPtrSet<const VNInfo*, 8> deadValues;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001015 // Map all curli values that should have live defs in dupli.
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +00001016 for (LiveInterval::const_vni_iterator I = parent.vni_begin(),
1017 E = parent.vni_end(); I != E; ++I) {
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001018 const VNInfo *VNI = *I;
Jakob Stoklund Olesen3ccfce02010-10-29 17:47:49 +00001019 // Don't transfer unused values to the new intervals.
1020 if (VNI->isUnused())
1021 continue;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001022 // Original def is contained in the split intervals.
1023 if (intervalsLiveAt(VNI->def)) {
1024 // Did this value escape?
1025 if (dupli_.isMapped(VNI))
1026 truncatedValues.insert(VNI);
1027 else
1028 deadValues.insert(VNI);
1029 continue;
1030 }
1031 // Add minimal live range at the definition.
1032 VNInfo *DVNI = dupli_.defValue(VNI, VNI->def);
1033 dupli_.getLI()->addRange(LiveRange(VNI->def, VNI->def.getNextSlot(), DVNI));
1034 }
1035
1036 // Add all ranges to dupli.
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +00001037 for (LiveInterval::const_iterator I = parent.begin(), E = parent.end();
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001038 I != E; ++I) {
1039 const LiveRange &LR = *I;
1040 if (truncatedValues.count(LR.valno)) {
1041 // recolor after removing intervals_.
1042 addTruncSimpleRange(LR.start, LR.end, LR.valno);
1043 } else if (!deadValues.count(LR.valno)) {
1044 // recolor without truncation.
1045 dupli_.addSimpleRange(LR.start, LR.end, LR.valno);
1046 }
1047 }
Jakob Stoklund Olesenc95c1462010-10-27 00:39:07 +00001048
1049 // Extend dupli_ to be live out of any critical loop predecessors.
1050 // This means we have multiple registers live out of those blocks.
1051 // The alternative would be to split the critical edges.
1052 if (criticalPreds_.empty())
1053 return;
1054 for (SplitAnalysis::BlockPtrSet::iterator I = criticalPreds_.begin(),
1055 E = criticalPreds_.end(); I != E; ++I)
1056 dupli_.extendTo(*I, lis_.getMBBEndIdx(*I).getPrevSlot());
1057 criticalPreds_.clear();
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001058}
1059
1060void SplitEditor::finish() {
1061 assert(!openli_.getLI() && "Previous LI not closed before rewrite");
1062 assert(dupli_.getLI() && "No dupli for rewrite. Noop spilt?");
1063
1064 // Complete dupli liveness.
1065 computeRemainder();
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001066
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001067 // Get rid of unused values and set phi-kill flags.
Jakob Stoklund Olesenf1354ae2010-10-26 22:36:05 +00001068 for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I)
1069 (*I)->RenumberValues(lis_);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001070
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001071 // Rewrite instructions.
Jakob Stoklund Olesen9d5d48b2010-10-15 00:34:01 +00001072 rewrite(edit_.getReg());
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +00001073
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001074 // Now check if any registers were separated into multiple components.
1075 ConnectedVNInfoEqClasses ConEQ(lis_);
1076 for (unsigned i = 0, e = edit_.size(); i != e; ++i) {
1077 // Don't use iterators, they are invalidated by create() below.
1078 LiveInterval *li = edit_.get(i);
1079 unsigned NumComp = ConEQ.Classify(li);
1080 if (NumComp <= 1)
1081 continue;
1082 DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n');
1083 SmallVector<LiveInterval*, 8> dups;
1084 dups.push_back(li);
1085 for (unsigned i = 1; i != NumComp; ++i)
1086 dups.push_back(&edit_.create(mri_, lis_, vrm_));
1087 ConEQ.Distribute(&dups[0]);
1088 // Rewrite uses to the new regs.
1089 rewrite(li->reg);
1090 }
1091
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +00001092 // Calculate spill weight and allocation hints for new intervals.
1093 VirtRegAuxInfo vrai(vrm_.getMachineFunction(), lis_, sa_.loops_);
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +00001094 for (LiveRangeEdit::iterator I = edit_.begin(), E = edit_.end(); I != E; ++I){
1095 LiveInterval &li = **I;
Jakob Stoklund Olesen9db3ea42010-08-10 18:37:40 +00001096 vrai.CalculateRegClass(li.reg);
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +00001097 vrai.CalculateWeightAndHint(li);
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +00001098 DEBUG(dbgs() << " new interval " << mri_.getRegClass(li.reg)->getName()
1099 << ":" << li << '\n');
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +00001100 }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001101}
1102
1103
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +00001104//===----------------------------------------------------------------------===//
1105// Loop Splitting
1106//===----------------------------------------------------------------------===//
1107
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +00001108void SplitEditor::splitAroundLoop(const MachineLoop *Loop) {
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001109 SplitAnalysis::LoopBlocks Blocks;
1110 sa_.getLoopBlocks(Loop, Blocks);
1111
Jakob Stoklund Olesen452a9fd2010-10-07 18:47:07 +00001112 DEBUG({
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +00001113 dbgs() << " splitAround"; sa_.print(Blocks, dbgs()); dbgs() << '\n';
Jakob Stoklund Olesen452a9fd2010-10-07 18:47:07 +00001114 });
1115
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001116 // Break critical edges as needed.
1117 SplitAnalysis::BlockPtrSet CriticalExits;
1118 sa_.getCriticalExits(Blocks, CriticalExits);
1119 assert(CriticalExits.empty() && "Cannot break critical exits yet");
1120
Jakob Stoklund Olesenc95c1462010-10-27 00:39:07 +00001121 // Get critical predecessors so computeRemainder can deal with them.
1122 sa_.getCriticalPreds(Blocks, criticalPreds_);
1123
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001124 // Create new live interval for the loop.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +00001125 openIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001126
Jakob Stoklund Olesendfe3b6d2010-12-18 01:06:19 +00001127 // Insert copies in the predecessors if live-in to the header.
1128 if (lis_.isLiveInToMBB(edit_.getParent(), Loop->getHeader())) {
1129 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Preds.begin(),
1130 E = Blocks.Preds.end(); I != E; ++I) {
1131 MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I);
1132 enterIntvAtEnd(MBB);
1133 }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001134 }
1135
1136 // Switch all loop blocks.
1137 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Loop.begin(),
1138 E = Blocks.Loop.end(); I != E; ++I)
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +00001139 useIntv(**I);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001140
1141 // Insert back copies in the exit blocks.
1142 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Exits.begin(),
1143 E = Blocks.Exits.end(); I != E; ++I) {
1144 MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +00001145 leaveIntvAtTop(MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001146 }
1147
1148 // Done.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +00001149 closeIntv();
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001150 finish();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +00001151}
1152
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001153
1154//===----------------------------------------------------------------------===//
1155// Single Block Splitting
1156//===----------------------------------------------------------------------===//
1157
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +00001158/// getMultiUseBlocks - if curli has more than one use in a basic block, it
1159/// may be an advantage to split curli for the duration of the block.
1160bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) {
1161 // If curli is local to one block, there is no point to splitting it.
1162 if (usingBlocks_.size() <= 1)
1163 return false;
1164 // Add blocks with multiple uses.
1165 for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end();
1166 I != E; ++I)
1167 switch (I->second) {
1168 case 0:
1169 case 1:
1170 continue;
1171 case 2: {
1172 // When there are only two uses and curli is both live in and live out,
1173 // we don't really win anything by isolating the block since we would be
1174 // inserting two copies.
1175 // The remaing register would still have two uses in the block. (Unless it
1176 // separates into disconnected components).
1177 if (lis_.isLiveInToMBB(*curli_, I->first) &&
1178 lis_.isLiveOutOfMBB(*curli_, I->first))
1179 continue;
1180 } // Fall through.
1181 default:
1182 Blocks.insert(I->first);
1183 }
1184 return !Blocks.empty();
1185}
1186
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001187/// splitSingleBlocks - Split curli into a separate live interval inside each
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +00001188/// basic block in Blocks.
1189void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) {
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +00001190 DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n");
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001191 // Determine the first and last instruction using curli in each block.
1192 typedef std::pair<SlotIndex,SlotIndex> IndexPair;
1193 typedef DenseMap<const MachineBasicBlock*,IndexPair> IndexPairMap;
1194 IndexPairMap MBBRange;
1195 for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(),
1196 E = sa_.usingInstrs_.end(); I != E; ++I) {
1197 const MachineBasicBlock *MBB = (*I)->getParent();
1198 if (!Blocks.count(MBB))
1199 continue;
1200 SlotIndex Idx = lis_.getInstructionIndex(*I);
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +00001201 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '\t' << Idx << '\t' << **I);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001202 IndexPair &IP = MBBRange[MBB];
1203 if (!IP.first.isValid() || Idx < IP.first)
1204 IP.first = Idx;
1205 if (!IP.second.isValid() || Idx > IP.second)
1206 IP.second = Idx;
1207 }
1208
1209 // Create a new interval for each block.
1210 for (SplitAnalysis::BlockPtrSet::const_iterator I = Blocks.begin(),
1211 E = Blocks.end(); I != E; ++I) {
1212 IndexPair &IP = MBBRange[*I];
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +00001213 DEBUG(dbgs() << " splitting for BB#" << (*I)->getNumber() << ": ["
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001214 << IP.first << ';' << IP.second << ")\n");
1215 assert(IP.first.isValid() && IP.second.isValid());
1216
1217 openIntv();
1218 enterIntvBefore(IP.first);
1219 useIntv(IP.first.getBaseIndex(), IP.second.getBoundaryIndex());
1220 leaveIntvAfter(IP.second);
1221 closeIntv();
1222 }
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001223 finish();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001224}
1225
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001226
1227//===----------------------------------------------------------------------===//
1228// Sub Block Splitting
1229//===----------------------------------------------------------------------===//
1230
1231/// getBlockForInsideSplit - If curli is contained inside a single basic block,
1232/// and it wou pay to subdivide the interval inside that block, return it.
1233/// Otherwise return NULL. The returned block can be passed to
1234/// SplitEditor::splitInsideBlock.
1235const MachineBasicBlock *SplitAnalysis::getBlockForInsideSplit() {
1236 // The interval must be exclusive to one block.
1237 if (usingBlocks_.size() != 1)
1238 return 0;
1239 // Don't to this for less than 4 instructions. We want to be sure that
1240 // splitting actually reduces the instruction count per interval.
1241 if (usingInstrs_.size() < 4)
1242 return 0;
1243 return usingBlocks_.begin()->first;
1244}
1245
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +00001246/// splitInsideBlock - Split curli into multiple intervals inside MBB.
1247void SplitEditor::splitInsideBlock(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001248 SmallVector<SlotIndex, 32> Uses;
1249 Uses.reserve(sa_.usingInstrs_.size());
1250 for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.usingInstrs_.begin(),
1251 E = sa_.usingInstrs_.end(); I != E; ++I)
1252 if ((*I)->getParent() == MBB)
1253 Uses.push_back(lis_.getInstructionIndex(*I));
1254 DEBUG(dbgs() << " splitInsideBlock BB#" << MBB->getNumber() << " for "
1255 << Uses.size() << " instructions.\n");
1256 assert(Uses.size() >= 3 && "Need at least 3 instructions");
1257 array_pod_sort(Uses.begin(), Uses.end());
1258
1259 // Simple algorithm: Find the largest gap between uses as determined by slot
1260 // indices. Create new intervals for instructions before the gap and after the
1261 // gap.
1262 unsigned bestPos = 0;
1263 int bestGap = 0;
1264 DEBUG(dbgs() << " dist (" << Uses[0]);
1265 for (unsigned i = 1, e = Uses.size(); i != e; ++i) {
1266 int g = Uses[i-1].distance(Uses[i]);
1267 DEBUG(dbgs() << ") -" << g << "- (" << Uses[i]);
1268 if (g > bestGap)
1269 bestPos = i, bestGap = g;
1270 }
1271 DEBUG(dbgs() << "), best: -" << bestGap << "-\n");
1272
1273 // bestPos points to the first use after the best gap.
1274 assert(bestPos > 0 && "Invalid gap");
1275
1276 // FIXME: Don't create intervals for low densities.
1277
1278 // First interval before the gap. Don't create single-instr intervals.
1279 if (bestPos > 1) {
1280 openIntv();
1281 enterIntvBefore(Uses.front());
1282 useIntv(Uses.front().getBaseIndex(), Uses[bestPos-1].getBoundaryIndex());
1283 leaveIntvAfter(Uses[bestPos-1]);
1284 closeIntv();
1285 }
1286
1287 // Second interval after the gap.
1288 if (bestPos < Uses.size()-1) {
1289 openIntv();
1290 enterIntvBefore(Uses[bestPos]);
1291 useIntv(Uses[bestPos].getBaseIndex(), Uses.back().getBoundaryIndex());
1292 leaveIntvAfter(Uses.back());
1293 closeIntv();
1294 }
1295
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001296 finish();
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001297}