blob: 7d5eaf4643a82d2d4efdb55bcbea3a629e855f30 [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"
17#include "llvm/CodeGen/LiveIntervalAnalysis.h"
18#include "llvm/CodeGen/MachineFunctionPass.h"
19#include "llvm/CodeGen/MachineLoopInfo.h"
20#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000021#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000022#include "llvm/Support/Debug.h"
23#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000024#include "llvm/Target/TargetInstrInfo.h"
25#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000026
27using namespace llvm;
28
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000029static cl::opt<bool>
30AllowSplit("spiller-splits-edges",
31 cl::desc("Allow critical edge splitting during spilling"));
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000032
33//===----------------------------------------------------------------------===//
34// Split Analysis
35//===----------------------------------------------------------------------===//
36
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000037SplitAnalysis::SplitAnalysis(const MachineFunction &mf,
38 const LiveIntervals &lis,
39 const MachineLoopInfo &mli)
40 : mf_(mf),
41 lis_(lis),
42 loops_(mli),
43 tii_(*mf.getTarget().getInstrInfo()),
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000044 curli_(0) {}
45
46void SplitAnalysis::clear() {
47 usingInstrs_.clear();
48 usingBlocks_.clear();
49 usingLoops_.clear();
50}
51
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000052bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) {
53 MachineBasicBlock *T, *F;
54 SmallVector<MachineOperand, 4> Cond;
55 return !tii_.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond);
56}
57
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +000058/// analyzeUses - Count instructions, basic blocks, and loops using curli.
59void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000060 const MachineRegisterInfo &MRI = mf_.getRegInfo();
61 for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(curli_->reg);
62 MachineInstr *MI = I.skipInstruction();) {
63 if (MI->isDebugValue() || !usingInstrs_.insert(MI))
64 continue;
65 MachineBasicBlock *MBB = MI->getParent();
66 if (usingBlocks_[MBB]++)
67 continue;
68 if (MachineLoop *Loop = loops_.getLoopFor(MBB))
69 usingLoops_.insert(Loop);
70 }
71 DEBUG(dbgs() << "Counted "
72 << usingInstrs_.size() << " instrs, "
73 << usingBlocks_.size() << " blocks, "
74 << usingLoops_.size() << " loops in "
75 << *curli_ << "\n");
76}
77
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000078// Get three sets of basic blocks surrounding a loop: Blocks inside the loop,
79// predecessor blocks, and exit blocks.
80void SplitAnalysis::getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks) {
81 Blocks.clear();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000082
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000083 // Blocks in the loop.
84 Blocks.Loop.insert(Loop->block_begin(), Loop->block_end());
85
86 // Predecessor blocks.
87 const MachineBasicBlock *Header = Loop->getHeader();
88 for (MachineBasicBlock::const_pred_iterator I = Header->pred_begin(),
89 E = Header->pred_end(); I != E; ++I)
90 if (!Blocks.Loop.count(*I))
91 Blocks.Preds.insert(*I);
92
93 // Exit blocks.
94 for (MachineLoop::block_iterator I = Loop->block_begin(),
95 E = Loop->block_end(); I != E; ++I) {
96 const MachineBasicBlock *MBB = *I;
97 for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
98 SE = MBB->succ_end(); SI != SE; ++SI)
99 if (!Blocks.Loop.count(*SI))
100 Blocks.Exits.insert(*SI);
101 }
102}
103
104/// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
105/// and around the Loop.
106SplitAnalysis::LoopPeripheralUse SplitAnalysis::
107analyzeLoopPeripheralUse(const SplitAnalysis::LoopBlocks &Blocks) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000108 LoopPeripheralUse use = ContainedInLoop;
109 for (BlockCountMap::iterator I = usingBlocks_.begin(), E = usingBlocks_.end();
110 I != E; ++I) {
111 const MachineBasicBlock *MBB = I->first;
112 // Is this a peripheral block?
113 if (use < MultiPeripheral &&
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000114 (Blocks.Preds.count(MBB) || Blocks.Exits.count(MBB))) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000115 if (I->second > 1) use = MultiPeripheral;
116 else use = SinglePeripheral;
117 continue;
118 }
119 // Is it a loop block?
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000120 if (Blocks.Loop.count(MBB))
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000121 continue;
122 // It must be an unrelated block.
123 return OutsideLoop;
124 }
125 return use;
126}
127
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000128/// getCriticalExits - It may be necessary to partially break critical edges
129/// leaving the loop if an exit block has phi uses of curli. Collect the exit
130/// blocks that need special treatment into CriticalExits.
131void SplitAnalysis::getCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
132 BlockPtrSet &CriticalExits) {
133 CriticalExits.clear();
134
135 // A critical exit block contains a phi def of curli, and has a predecessor
136 // that is not in the loop nor a loop predecessor.
137 // For such an exit block, the edges carrying the new variable must be moved
138 // to a new pre-exit block.
139 for (BlockPtrSet::iterator I = Blocks.Exits.begin(), E = Blocks.Exits.end();
140 I != E; ++I) {
141 const MachineBasicBlock *Succ = *I;
142 SlotIndex SuccIdx = lis_.getMBBStartIdx(Succ);
143 VNInfo *SuccVNI = curli_->getVNInfoAt(SuccIdx);
144 // This exit may not have curli live in at all. No need to split.
145 if (!SuccVNI)
146 continue;
147 // If this is not a PHI def, it is either using a value from before the
148 // loop, or a value defined inside the loop. Both are safe.
149 if (!SuccVNI->isPHIDef() || SuccVNI->def.getBaseIndex() != SuccIdx)
150 continue;
151 // This exit block does have a PHI. Does it also have a predecessor that is
152 // not a loop block or loop predecessor?
153 for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(),
154 PE = Succ->pred_end(); PI != PE; ++PI) {
155 const MachineBasicBlock *Pred = *PI;
156 if (Blocks.Loop.count(Pred) || Blocks.Preds.count(Pred))
157 continue;
158 // This is a critical exit block, and we need to split the exit edge.
159 CriticalExits.insert(Succ);
160 break;
161 }
162 }
163}
164
165/// canSplitCriticalExits - Return true if it is possible to insert new exit
166/// blocks before the blocks in CriticalExits.
167bool
168SplitAnalysis::canSplitCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
169 BlockPtrSet &CriticalExits) {
170 // If we don't allow critical edge splitting, require no critical exits.
171 if (!AllowSplit)
172 return CriticalExits.empty();
173
174 for (BlockPtrSet::iterator I = CriticalExits.begin(), E = CriticalExits.end();
175 I != E; ++I) {
176 const MachineBasicBlock *Succ = *I;
177 // We want to insert a new pre-exit MBB before Succ, and change all the
178 // in-loop blocks to branch to the pre-exit instead of Succ.
179 // Check that all the in-loop predecessors can be changed.
180 for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(),
181 PE = Succ->pred_end(); PI != PE; ++PI) {
182 const MachineBasicBlock *Pred = *PI;
183 // The external predecessors won't be altered.
184 if (!Blocks.Loop.count(Pred) && !Blocks.Preds.count(Pred))
185 continue;
186 if (!canAnalyzeBranch(Pred))
187 return false;
188 }
189
190 // If Succ's layout predecessor falls through, that too must be analyzable.
191 // We need to insert the pre-exit block in the gap.
192 MachineFunction::const_iterator MFI = Succ;
193 if (MFI == mf_.begin())
194 continue;
195 if (!canAnalyzeBranch(--MFI))
196 return false;
197 }
198 // No problems found.
199 return true;
200}
201
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000202void SplitAnalysis::analyze(const LiveInterval *li) {
203 clear();
204 curli_ = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000205 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000206}
207
208const MachineLoop *SplitAnalysis::getBestSplitLoop() {
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000209 assert(curli_ && "Call analyze() before getBestSplitLoop");
210 if (usingLoops_.empty())
211 return 0;
212
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000213 LoopPtrSet Loops, SecondLoops;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000214 LoopBlocks Blocks;
215 BlockPtrSet CriticalExits;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000216
217 // Find first-class and second class candidate loops.
218 // We prefer to split around loops where curli is used outside the periphery.
219 for (LoopPtrSet::const_iterator I = usingLoops_.begin(),
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000220 E = usingLoops_.end(); I != E; ++I) {
221 getLoopBlocks(*I, Blocks);
222 LoopPtrSet *LPS = 0;
223 switch(analyzeLoopPeripheralUse(Blocks)) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000224 case OutsideLoop:
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000225 LPS = &Loops;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000226 break;
227 case MultiPeripheral:
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000228 LPS = &SecondLoops;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000229 break;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000230 case ContainedInLoop:
231 DEBUG(dbgs() << "ContainedInLoop: " << **I);
232 continue;
233 case SinglePeripheral:
234 DEBUG(dbgs() << "SinglePeripheral: " << **I);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000235 continue;
236 }
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000237 // Will it be possible to split around this loop?
238 getCriticalExits(Blocks, CriticalExits);
239 DEBUG(dbgs() << CriticalExits.size() << " critical exits: " << **I);
240 if (!canSplitCriticalExits(Blocks, CriticalExits))
241 continue;
242 // This is a possible split.
243 assert(LPS);
244 LPS->insert(*I);
245 }
246
247 DEBUG(dbgs() << "Got " << Loops.size() << " + " << SecondLoops.size()
248 << " candidate loops\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000249
250 // If there are no first class loops available, look at second class loops.
251 if (Loops.empty())
252 Loops = SecondLoops;
253
254 if (Loops.empty())
255 return 0;
256
257 // Pick the earliest loop.
258 // FIXME: Are there other heuristics to consider?
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000259 const MachineLoop *Best = 0;
260 SlotIndex BestIdx;
261 for (LoopPtrSet::const_iterator I = Loops.begin(), E = Loops.end(); I != E;
262 ++I) {
263 SlotIndex Idx = lis_.getMBBStartIdx((*I)->getHeader());
264 if (!Best || Idx < BestIdx)
265 Best = *I, BestIdx = Idx;
266 }
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000267 DEBUG(dbgs() << "Best: " << *Best);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000268 return Best;
269}
270
271//===----------------------------------------------------------------------===//
272// Loop Splitting
273//===----------------------------------------------------------------------===//
274
275bool llvm::splitAroundLoop(SplitAnalysis &sa, const MachineLoop *loop) {
276 return false;
277}
278