blob: 0ec983ec13f6ea97dca030b43020d27bbf2ac901 [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"
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)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000044 : MF(mf),
45 LIS(lis),
46 Loops(mli),
47 TII(*mf.getTarget().getInstrInfo()),
48 CurLI(0) {}
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000049
50void SplitAnalysis::clear() {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000051 UseSlots.clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000052 UsingInstrs.clear();
53 UsingBlocks.clear();
54 UsingLoops.clear();
55 CurLI = 0;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000056}
57
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000058bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) {
59 MachineBasicBlock *T, *F;
60 SmallVector<MachineOperand, 4> Cond;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000061 return !TII.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond);
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000062}
63
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000064/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +000065void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000066 const MachineRegisterInfo &MRI = MF.getRegInfo();
67 for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(CurLI->reg);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000068 MachineInstr *MI = I.skipInstruction();) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000069 if (MI->isDebugValue() || !UsingInstrs.insert(MI))
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000070 continue;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000071 UseSlots.push_back(LIS.getInstructionIndex(MI).getDefIndex());
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000072 MachineBasicBlock *MBB = MI->getParent();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000073 if (UsingBlocks[MBB]++)
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000074 continue;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000075 for (MachineLoop *Loop = Loops.getLoopFor(MBB); Loop;
Jakob Stoklund Olesen9b90d7e2010-10-05 23:10:12 +000076 Loop = Loop->getParentLoop())
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000077 UsingLoops[Loop]++;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000078 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000079 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +000080 DEBUG(dbgs() << " counted "
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000081 << UsingInstrs.size() << " instrs, "
82 << UsingBlocks.size() << " blocks, "
83 << UsingLoops.size() << " loops.\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000084}
85
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +000086void SplitAnalysis::print(const BlockPtrSet &B, raw_ostream &OS) const {
87 for (BlockPtrSet::const_iterator I = B.begin(), E = B.end(); I != E; ++I) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000088 unsigned count = UsingBlocks.lookup(*I);
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +000089 OS << " BB#" << (*I)->getNumber();
90 if (count)
91 OS << '(' << count << ')';
92 }
93}
94
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000095// Get three sets of basic blocks surrounding a loop: Blocks inside the loop,
96// predecessor blocks, and exit blocks.
97void SplitAnalysis::getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks) {
98 Blocks.clear();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000099
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000100 // Blocks in the loop.
101 Blocks.Loop.insert(Loop->block_begin(), Loop->block_end());
102
103 // Predecessor blocks.
104 const MachineBasicBlock *Header = Loop->getHeader();
105 for (MachineBasicBlock::const_pred_iterator I = Header->pred_begin(),
106 E = Header->pred_end(); I != E; ++I)
107 if (!Blocks.Loop.count(*I))
108 Blocks.Preds.insert(*I);
109
110 // Exit blocks.
111 for (MachineLoop::block_iterator I = Loop->block_begin(),
112 E = Loop->block_end(); I != E; ++I) {
113 const MachineBasicBlock *MBB = *I;
114 for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
115 SE = MBB->succ_end(); SI != SE; ++SI)
116 if (!Blocks.Loop.count(*SI))
117 Blocks.Exits.insert(*SI);
118 }
119}
120
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000121void SplitAnalysis::print(const LoopBlocks &B, raw_ostream &OS) const {
122 OS << "Loop:";
123 print(B.Loop, OS);
124 OS << ", preds:";
125 print(B.Preds, OS);
126 OS << ", exits:";
127 print(B.Exits, OS);
128}
129
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000130/// analyzeLoopPeripheralUse - Return an enum describing how CurLI is used in
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000131/// and around the Loop.
132SplitAnalysis::LoopPeripheralUse SplitAnalysis::
133analyzeLoopPeripheralUse(const SplitAnalysis::LoopBlocks &Blocks) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000134 LoopPeripheralUse use = ContainedInLoop;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000135 for (BlockCountMap::iterator I = UsingBlocks.begin(), E = UsingBlocks.end();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000136 I != E; ++I) {
137 const MachineBasicBlock *MBB = I->first;
138 // Is this a peripheral block?
139 if (use < MultiPeripheral &&
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000140 (Blocks.Preds.count(MBB) || Blocks.Exits.count(MBB))) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000141 if (I->second > 1) use = MultiPeripheral;
142 else use = SinglePeripheral;
143 continue;
144 }
145 // Is it a loop block?
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000146 if (Blocks.Loop.count(MBB))
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000147 continue;
148 // It must be an unrelated block.
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000149 DEBUG(dbgs() << ", outside: BB#" << MBB->getNumber());
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000150 return OutsideLoop;
151 }
152 return use;
153}
154
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000155/// getCriticalExits - It may be necessary to partially break critical edges
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000156/// leaving the loop if an exit block has predecessors from outside the loop
157/// periphery.
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000158void SplitAnalysis::getCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
159 BlockPtrSet &CriticalExits) {
160 CriticalExits.clear();
161
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000162 // A critical exit block has CurLI live-in, and has a predecessor that is not
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000163 // in the loop nor a loop predecessor. For such an exit block, the edges
164 // carrying the new variable must be moved to a new pre-exit block.
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000165 for (BlockPtrSet::iterator I = Blocks.Exits.begin(), E = Blocks.Exits.end();
166 I != E; ++I) {
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000167 const MachineBasicBlock *Exit = *I;
168 // A single-predecessor exit block is definitely not a critical edge.
169 if (Exit->pred_size() == 1)
170 continue;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000171 // This exit may not have CurLI live in at all. No need to split.
172 if (!LIS.isLiveInToMBB(*CurLI, Exit))
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000173 continue;
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000174 // Does this exit block have a predecessor that is not a loop block or loop
175 // predecessor?
176 for (MachineBasicBlock::const_pred_iterator PI = Exit->pred_begin(),
177 PE = Exit->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000178 const MachineBasicBlock *Pred = *PI;
179 if (Blocks.Loop.count(Pred) || Blocks.Preds.count(Pred))
180 continue;
181 // This is a critical exit block, and we need to split the exit edge.
Jakob Stoklund Olesen2c1442e2010-10-22 20:28:23 +0000182 CriticalExits.insert(Exit);
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000183 break;
184 }
185 }
186}
187
Jakob Stoklund Olesen0960a652010-10-27 00:39:05 +0000188void SplitAnalysis::getCriticalPreds(const SplitAnalysis::LoopBlocks &Blocks,
189 BlockPtrSet &CriticalPreds) {
190 CriticalPreds.clear();
191
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000192 // A critical predecessor block has CurLI live-out, and has a successor that
193 // has CurLI live-in and is not in the loop nor a loop exit block. For such a
Jakob Stoklund Olesen0960a652010-10-27 00:39:05 +0000194 // predecessor block, we must carry the value in both the 'inside' and
195 // 'outside' registers.
196 for (BlockPtrSet::iterator I = Blocks.Preds.begin(), E = Blocks.Preds.end();
197 I != E; ++I) {
198 const MachineBasicBlock *Pred = *I;
199 // Definitely not a critical edge.
200 if (Pred->succ_size() == 1)
201 continue;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000202 // This block may not have CurLI live out at all if there is a PHI.
203 if (!LIS.isLiveOutOfMBB(*CurLI, Pred))
Jakob Stoklund Olesen0960a652010-10-27 00:39:05 +0000204 continue;
205 // Does this block have a successor outside the loop?
206 for (MachineBasicBlock::const_pred_iterator SI = Pred->succ_begin(),
207 SE = Pred->succ_end(); SI != SE; ++SI) {
208 const MachineBasicBlock *Succ = *SI;
209 if (Blocks.Loop.count(Succ) || Blocks.Exits.count(Succ))
210 continue;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000211 if (!LIS.isLiveInToMBB(*CurLI, Succ))
Jakob Stoklund Olesen0960a652010-10-27 00:39:05 +0000212 continue;
213 // This is a critical predecessor block.
214 CriticalPreds.insert(Pred);
215 break;
216 }
217 }
218}
219
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000220/// canSplitCriticalExits - Return true if it is possible to insert new exit
221/// blocks before the blocks in CriticalExits.
222bool
223SplitAnalysis::canSplitCriticalExits(const SplitAnalysis::LoopBlocks &Blocks,
224 BlockPtrSet &CriticalExits) {
225 // If we don't allow critical edge splitting, require no critical exits.
226 if (!AllowSplit)
227 return CriticalExits.empty();
228
229 for (BlockPtrSet::iterator I = CriticalExits.begin(), E = CriticalExits.end();
230 I != E; ++I) {
231 const MachineBasicBlock *Succ = *I;
232 // We want to insert a new pre-exit MBB before Succ, and change all the
233 // in-loop blocks to branch to the pre-exit instead of Succ.
234 // Check that all the in-loop predecessors can be changed.
235 for (MachineBasicBlock::const_pred_iterator PI = Succ->pred_begin(),
236 PE = Succ->pred_end(); PI != PE; ++PI) {
237 const MachineBasicBlock *Pred = *PI;
238 // The external predecessors won't be altered.
239 if (!Blocks.Loop.count(Pred) && !Blocks.Preds.count(Pred))
240 continue;
241 if (!canAnalyzeBranch(Pred))
242 return false;
243 }
244
245 // If Succ's layout predecessor falls through, that too must be analyzable.
246 // We need to insert the pre-exit block in the gap.
247 MachineFunction::const_iterator MFI = Succ;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000248 if (MFI == MF.begin())
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000249 continue;
250 if (!canAnalyzeBranch(--MFI))
251 return false;
252 }
253 // No problems found.
254 return true;
255}
256
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000257void SplitAnalysis::analyze(const LiveInterval *li) {
258 clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000259 CurLI = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000260 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000261}
262
Jakob Stoklund Olesen521a4532010-12-15 17:41:19 +0000263void SplitAnalysis::getSplitLoops(LoopPtrSet &Loops) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000264 assert(CurLI && "Call analyze() before getSplitLoops");
265 if (UsingLoops.empty())
Jakob Stoklund Olesen521a4532010-12-15 17:41:19 +0000266 return;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000267
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000268 LoopBlocks Blocks;
269 BlockPtrSet CriticalExits;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000270
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000271 // We split around loops where CurLI is used outside the periphery.
272 for (LoopCountMap::const_iterator I = UsingLoops.begin(),
273 E = UsingLoops.end(); I != E; ++I) {
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +0000274 const MachineLoop *Loop = I->first;
275 getLoopBlocks(Loop, Blocks);
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000276 DEBUG({ dbgs() << " "; print(Blocks, dbgs()); });
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000277
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000278 switch(analyzeLoopPeripheralUse(Blocks)) {
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000279 case OutsideLoop:
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000280 break;
281 case MultiPeripheral:
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000282 // FIXME: We could split a live range with multiple uses in a peripheral
283 // block and still make progress. However, it is possible that splitting
284 // another live range will insert copies into a peripheral block, and
Jakob Stoklund Olesen521a4532010-12-15 17:41:19 +0000285 // there is a small chance we can enter an infinite loop, inserting copies
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000286 // forever.
287 // For safety, stick to splitting live ranges with uses outside the
288 // periphery.
Jakob Stoklund Olesen521a4532010-12-15 17:41:19 +0000289 DEBUG(dbgs() << ": multiple peripheral uses");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000290 break;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000291 case ContainedInLoop:
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000292 DEBUG(dbgs() << ": fully contained\n");
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000293 continue;
294 case SinglePeripheral:
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000295 DEBUG(dbgs() << ": single peripheral use\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000296 continue;
297 }
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000298 // Will it be possible to split around this loop?
299 getCriticalExits(Blocks, CriticalExits);
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000300 DEBUG(dbgs() << ": " << CriticalExits.size() << " critical exits\n");
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000301 if (!canSplitCriticalExits(Blocks, CriticalExits))
302 continue;
303 // This is a possible split.
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000304 Loops.insert(Loop);
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000305 }
306
Jakob Stoklund Olesen521a4532010-12-15 17:41:19 +0000307 DEBUG(dbgs() << " getSplitLoops found " << Loops.size()
Jakob Stoklund Olesenab00e9f2010-10-14 18:26:45 +0000308 << " candidate loops.\n");
Jakob Stoklund Olesen521a4532010-12-15 17:41:19 +0000309}
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000310
Jakob Stoklund Olesen521a4532010-12-15 17:41:19 +0000311const MachineLoop *SplitAnalysis::getBestSplitLoop() {
312 LoopPtrSet Loops;
313 getSplitLoops(Loops);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000314 if (Loops.empty())
315 return 0;
316
317 // Pick the earliest loop.
318 // FIXME: Are there other heuristics to consider?
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000319 const MachineLoop *Best = 0;
320 SlotIndex BestIdx;
321 for (LoopPtrSet::const_iterator I = Loops.begin(), E = Loops.end(); I != E;
322 ++I) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000323 SlotIndex Idx = LIS.getMBBStartIdx((*I)->getHeader());
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000324 if (!Best || Idx < BestIdx)
325 Best = *I, BestIdx = Idx;
326 }
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000327 DEBUG(dbgs() << " getBestSplitLoop found " << *Best);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000328 return Best;
329}
330
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000331/// isBypassLoop - Return true if CurLI is live through Loop and has no uses
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000332/// inside the loop. Bypass loops are candidates for splitting because it can
333/// prevent interference inside the loop.
334bool SplitAnalysis::isBypassLoop(const MachineLoop *Loop) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000335 // If CurLI is live into the loop header and there are no uses in the loop, it
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000336 // must be live in the entire loop and live on at least one exiting edge.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000337 return !UsingLoops.count(Loop) &&
338 LIS.isLiveInToMBB(*CurLI, Loop->getHeader());
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000339}
340
341/// getBypassLoops - Get all the maximal bypass loops. These are the bypass
342/// loops whose parent is not a bypass loop.
343void SplitAnalysis::getBypassLoops(LoopPtrSet &BypassLoops) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000344 SmallVector<MachineLoop*, 8> Todo(Loops.begin(), Loops.end());
Jakob Stoklund Olesen62032952010-12-15 18:07:48 +0000345 while (!Todo.empty()) {
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000346 MachineLoop *Loop = Todo.pop_back_val();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000347 if (!UsingLoops.count(Loop)) {
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000348 // This is either a bypass loop or completely irrelevant.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000349 if (LIS.isLiveInToMBB(*CurLI, Loop->getHeader()))
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000350 BypassLoops.insert(Loop);
351 // Either way, skip the child loops.
352 continue;
353 }
354
355 // The child loops may be bypass loops.
356 Todo.append(Loop->begin(), Loop->end());
357 }
358}
359
360
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000361//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000362// LiveIntervalMap
363//===----------------------------------------------------------------------===//
364
Jakob Stoklund Olesenb3e96812010-09-13 21:29:45 +0000365// Work around the fact that the std::pair constructors are broken for pointer
366// pairs in some implementations. makeVV(x, 0) works.
367static inline std::pair<const VNInfo*, VNInfo*>
368makeVV(const VNInfo *a, VNInfo *b) {
369 return std::make_pair(a, b);
370}
371
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000372void LiveIntervalMap::reset(LiveInterval *li) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000373 LI = li;
374 Values.clear();
375 LiveOutCache.clear();
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000376}
377
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000378bool LiveIntervalMap::isComplexMapped(const VNInfo *ParentVNI) const {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000379 ValueMap::const_iterator i = Values.find(ParentVNI);
380 return i != Values.end() && i->second == 0;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000381}
382
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000383// defValue - Introduce a LI def for ParentVNI that could be later than
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000384// ParentVNI->def.
385VNInfo *LiveIntervalMap::defValue(const VNInfo *ParentVNI, SlotIndex Idx) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000386 assert(LI && "call reset first");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000387 assert(ParentVNI && "Mapping NULL value");
388 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000389 assert(ParentLI.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000390
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000391 // Create a new value.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000392 VNInfo *VNI = LI->getNextValue(Idx, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000393
Jakob Stoklund Olesen79c02622010-10-26 22:36:02 +0000394 // Preserve the PHIDef bit.
395 if (ParentVNI->isPHIDef() && Idx == ParentVNI->def)
396 VNI->setIsPHIDef(true);
397
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000398 // Use insert for lookup, so we can add missing values with a second lookup.
399 std::pair<ValueMap::iterator,bool> InsP =
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000400 Values.insert(makeVV(ParentVNI, Idx == ParentVNI->def ? VNI : 0));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000401
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000402 // This is now a complex def. Mark with a NULL in valueMap.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000403 if (!InsP.second)
404 InsP.first->second = 0;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000405
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000406 return VNI;
407}
408
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000409
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000410// mapValue - Find the mapped value for ParentVNI at Idx.
411// Potentially create phi-def values.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000412VNInfo *LiveIntervalMap::mapValue(const VNInfo *ParentVNI, SlotIndex Idx,
413 bool *simple) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000414 assert(LI && "call reset first");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000415 assert(ParentVNI && "Mapping NULL value");
416 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000417 assert(ParentLI.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000418
419 // Use insert for lookup, so we can add missing values with a second lookup.
420 std::pair<ValueMap::iterator,bool> InsP =
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000421 Values.insert(makeVV(ParentVNI, 0));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000422
423 // This was an unknown value. Create a simple mapping.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000424 if (InsP.second) {
425 if (simple) *simple = true;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000426 return InsP.first->second = LI->createValueCopy(ParentVNI,
427 LIS.getVNInfoAllocator());
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000428 }
429
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000430 // This was a simple mapped value.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000431 if (InsP.first->second) {
432 if (simple) *simple = true;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000433 return InsP.first->second;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000434 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000435
436 // This is a complex mapped value. There may be multiple defs, and we may need
437 // to create phi-defs.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000438 if (simple) *simple = false;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000439 MachineBasicBlock *IdxMBB = LIS.getMBBFromIndex(Idx);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000440 assert(IdxMBB && "No MBB at Idx");
441
442 // Is there a def in the same MBB we can extend?
443 if (VNInfo *VNI = extendTo(IdxMBB, Idx))
444 return VNI;
445
446 // Now for the fun part. We know that ParentVNI potentially has multiple defs,
447 // and we may need to create even more phi-defs to preserve VNInfo SSA form.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000448 // Perform a search for all predecessor blocks where we know the dominating
449 // VNInfo. Insert phi-def VNInfos along the path back to IdxMBB.
450 DEBUG(dbgs() << "\n Reaching defs for BB#" << IdxMBB->getNumber()
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000451 << " at " << Idx << " in " << *LI << '\n');
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000452
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000453 // Blocks where LI should be live-in.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000454 SmallVector<MachineDomTreeNode*, 16> LiveIn;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000455 LiveIn.push_back(MDT[IdxMBB]);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000456
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000457 // Using LiveOutCache as a visited set, perform a BFS for all reaching defs.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000458 for (unsigned i = 0; i != LiveIn.size(); ++i) {
459 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
460 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
461 PE = MBB->pred_end(); PI != PE; ++PI) {
462 MachineBasicBlock *Pred = *PI;
463 // Is this a known live-out block?
464 std::pair<LiveOutMap::iterator,bool> LOIP =
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000465 LiveOutCache.insert(std::make_pair(Pred, LiveOutPair()));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000466 // Yes, we have been here before.
467 if (!LOIP.second) {
Benjamin Kramer8a8e26f2010-10-29 17:40:05 +0000468 DEBUG(if (VNInfo *VNI = LOIP.first->second.first)
469 dbgs() << " known valno #" << VNI->id
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000470 << " at BB#" << Pred->getNumber() << '\n');
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000471 continue;
472 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000473
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000474 // Does Pred provide a live-out value?
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000475 SlotIndex Last = LIS.getMBBEndIdx(Pred).getPrevSlot();
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000476 if (VNInfo *VNI = extendTo(Pred, Last)) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000477 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(VNI->def);
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000478 DEBUG(dbgs() << " found valno #" << VNI->id
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000479 << " from BB#" << DefMBB->getNumber()
480 << " at BB#" << Pred->getNumber() << '\n');
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000481 LiveOutPair &LOP = LOIP.first->second;
482 LOP.first = VNI;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000483 LOP.second = MDT[DefMBB];
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000484 continue;
485 }
486 // No, we need a live-in value for Pred as well
487 if (Pred != IdxMBB)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000488 LiveIn.push_back(MDT[Pred]);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000489 }
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000490 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000491
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000492 // We may need to add phi-def values to preserve the SSA form.
493 // This is essentially the same iterative algorithm that SSAUpdater uses,
494 // except we already have a dominator tree, so we don't have to recompute it.
495 VNInfo *IdxVNI = 0;
496 unsigned Changes;
497 do {
498 Changes = 0;
499 DEBUG(dbgs() << " Iterating over " << LiveIn.size() << " blocks.\n");
500 // Propagate live-out values down the dominator tree, inserting phi-defs when
501 // necessary. Since LiveIn was created by a BFS, going backwards makes it more
502 // likely for us to visit immediate dominators before their children.
503 for (unsigned i = LiveIn.size(); i; --i) {
504 MachineDomTreeNode *Node = LiveIn[i-1];
505 MachineBasicBlock *MBB = Node->getBlock();
506 MachineDomTreeNode *IDom = Node->getIDom();
507 LiveOutPair IDomValue;
508 // We need a live-in value to a block with no immediate dominator?
509 // This is probably an unreachable block that has survived somehow.
510 bool needPHI = !IDom;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000511
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000512 // Get the IDom live-out value.
513 if (!needPHI) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000514 LiveOutMap::iterator I = LiveOutCache.find(IDom->getBlock());
515 if (I != LiveOutCache.end())
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000516 IDomValue = I->second;
517 else
518 // If IDom is outside our set of live-out blocks, there must be new
519 // defs, and we need a phi-def here.
520 needPHI = true;
521 }
Jakob Stoklund Olesen984a7fc2010-10-05 20:36:28 +0000522
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000523 // IDom dominates all of our predecessors, but it may not be the immediate
524 // dominator. Check if any of them have live-out values that are properly
525 // dominated by IDom. If so, we need a phi-def here.
526 if (!needPHI) {
527 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
528 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000529 LiveOutPair Value = LiveOutCache[*PI];
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000530 if (!Value.first || Value.first == IDomValue.first)
531 continue;
532 // This predecessor is carrying something other than IDomValue.
533 // It could be because IDomValue hasn't propagated yet, or it could be
534 // because MBB is in the dominance frontier of that value.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000535 if (MDT.dominates(IDom, Value.second)) {
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000536 needPHI = true;
537 break;
538 }
539 }
540 }
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000541
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000542 // Create a phi-def if required.
543 if (needPHI) {
544 ++Changes;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000545 SlotIndex Start = LIS.getMBBStartIdx(MBB);
546 VNInfo *VNI = LI->getNextValue(Start, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000547 VNI->setIsPHIDef(true);
548 DEBUG(dbgs() << " - BB#" << MBB->getNumber()
549 << " phi-def #" << VNI->id << " at " << Start << '\n');
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000550 // We no longer need LI to be live-in.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000551 LiveIn.erase(LiveIn.begin()+(i-1));
552 // Blocks in LiveIn are either IdxMBB, or have a value live-through.
553 if (MBB == IdxMBB)
554 IdxVNI = VNI;
555 // Check if we need to update live-out info.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000556 LiveOutMap::iterator I = LiveOutCache.find(MBB);
557 if (I == LiveOutCache.end() || I->second.second == Node) {
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000558 // We already have a live-out defined in MBB, so this must be IdxMBB.
559 assert(MBB == IdxMBB && "Adding phi-def to known live-out");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000560 LI->addRange(LiveRange(Start, Idx.getNextSlot(), VNI));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000561 } else {
562 // This phi-def is also live-out, so color the whole block.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000563 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000564 I->second = LiveOutPair(VNI, Node);
565 }
566 } else if (IDomValue.first) {
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000567 // No phi-def here. Remember incoming value for IdxMBB.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000568 if (MBB == IdxMBB)
569 IdxVNI = IDomValue.first;
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000570 // Propagate IDomValue if needed:
571 // MBB is live-out and doesn't define its own value.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000572 LiveOutMap::iterator I = LiveOutCache.find(MBB);
573 if (I != LiveOutCache.end() && I->second.second != Node &&
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000574 I->second.first != IDomValue.first) {
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000575 ++Changes;
576 I->second = IDomValue;
577 DEBUG(dbgs() << " - BB#" << MBB->getNumber()
578 << " idom valno #" << IDomValue.first->id
579 << " from BB#" << IDom->getBlock()->getNumber() << '\n');
580 }
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000581 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000582 }
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000583 DEBUG(dbgs() << " - made " << Changes << " changes.\n");
584 } while (Changes);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000585
586 assert(IdxVNI && "Didn't find value for Idx");
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000587
588#ifndef NDEBUG
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000589 // Check the LiveOutCache invariants.
590 for (LiveOutMap::iterator I = LiveOutCache.begin(), E = LiveOutCache.end();
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000591 I != E; ++I) {
592 assert(I->first && "Null MBB entry in cache");
593 assert(I->second.first && "Null VNInfo in cache");
594 assert(I->second.second && "Null DomTreeNode in cache");
595 if (I->second.second->getBlock() == I->first)
596 continue;
597 for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(),
598 PE = I->first->pred_end(); PI != PE; ++PI)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000599 assert(LiveOutCache.lookup(*PI) == I->second && "Bad invariant");
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000600 }
601#endif
602
603 // Since we went through the trouble of a full BFS visiting all reaching defs,
604 // the values in LiveIn are now accurate. No more phi-defs are needed
605 // for these blocks, so we can color the live ranges.
606 // This makes the next mapValue call much faster.
607 for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) {
608 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000609 SlotIndex Start = LIS.getMBBStartIdx(MBB);
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000610 if (MBB == IdxMBB) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000611 LI->addRange(LiveRange(Start, Idx.getNextSlot(), IdxVNI));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000612 continue;
613 }
614 // Anything in LiveIn other than IdxMBB is live-through.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000615 VNInfo *VNI = LiveOutCache.lookup(MBB).first;
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000616 assert(VNI && "Missing block value");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000617 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000618 }
619
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000620 return IdxVNI;
621}
622
Jakob Stoklund Olesend7ca5772011-01-20 17:45:20 +0000623#ifndef NDEBUG
624void LiveIntervalMap::dumpCache() {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000625 for (LiveOutMap::iterator I = LiveOutCache.begin(), E = LiveOutCache.end();
Jakob Stoklund Olesend7ca5772011-01-20 17:45:20 +0000626 I != E; ++I) {
627 assert(I->first && "Null MBB entry in cache");
628 assert(I->second.first && "Null VNInfo in cache");
629 assert(I->second.second && "Null DomTreeNode in cache");
630 dbgs() << " cache: BB#" << I->first->getNumber()
631 << " has valno #" << I->second.first->id << " from BB#"
632 << I->second.second->getBlock()->getNumber() << ", preds";
633 for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(),
634 PE = I->first->pred_end(); PI != PE; ++PI)
635 dbgs() << " BB#" << (*PI)->getNumber();
636 dbgs() << '\n';
637 }
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000638 dbgs() << " cache: " << LiveOutCache.size() << " entries.\n";
Jakob Stoklund Olesend7ca5772011-01-20 17:45:20 +0000639}
640#endif
641
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000642// extendTo - Find the last LI value defined in MBB at or before Idx. The
643// ParentLI is assumed to be live at Idx. Extend the live range to Idx.
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000644// Return the found VNInfo, or NULL.
Jakob Stoklund Olesenc95c1462010-10-27 00:39:07 +0000645VNInfo *LiveIntervalMap::extendTo(const MachineBasicBlock *MBB, SlotIndex Idx) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000646 assert(LI && "call reset first");
647 LiveInterval::iterator I = std::upper_bound(LI->begin(), LI->end(), Idx);
648 if (I == LI->begin())
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000649 return 0;
650 --I;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000651 if (I->end <= LIS.getMBBStartIdx(MBB))
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000652 return 0;
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000653 if (I->end <= Idx)
654 I->end = Idx.getNextSlot();
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000655 return I->valno;
656}
657
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000658// addSimpleRange - Add a simple range from ParentLI to LI.
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000659// ParentVNI must be live in the [Start;End) interval.
660void LiveIntervalMap::addSimpleRange(SlotIndex Start, SlotIndex End,
661 const VNInfo *ParentVNI) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000662 assert(LI && "call reset first");
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000663 bool simple;
664 VNInfo *VNI = mapValue(ParentVNI, Start, &simple);
665 // A simple mapping is easy.
666 if (simple) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000667 LI->addRange(LiveRange(Start, End, VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000668 return;
669 }
670
671 // ParentVNI is a complex value. We must map per MBB.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000672 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
673 MachineFunction::iterator MBBE = LIS.getMBBFromIndex(End.getPrevSlot());
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000674
675 if (MBB == MBBE) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000676 LI->addRange(LiveRange(Start, End, VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000677 return;
678 }
679
680 // First block.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000681 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000682
683 // Run sequence of full blocks.
684 for (++MBB; MBB != MBBE; ++MBB) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000685 Start = LIS.getMBBStartIdx(MBB);
686 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB),
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000687 mapValue(ParentVNI, Start)));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000688 }
689
690 // Final block.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000691 Start = LIS.getMBBStartIdx(MBB);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000692 if (Start != End)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000693 LI->addRange(LiveRange(Start, End, mapValue(ParentVNI, Start)));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000694}
695
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000696/// addRange - Add live ranges to LI where [Start;End) intersects ParentLI.
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000697/// All needed values whose def is not inside [Start;End) must be defined
698/// beforehand so mapValue will work.
699void LiveIntervalMap::addRange(SlotIndex Start, SlotIndex End) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000700 assert(LI && "call reset first");
701 LiveInterval::const_iterator B = ParentLI.begin(), E = ParentLI.end();
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000702 LiveInterval::const_iterator I = std::lower_bound(B, E, Start);
703
704 // Check if --I begins before Start and overlaps.
705 if (I != B) {
706 --I;
707 if (I->end > Start)
708 addSimpleRange(Start, std::min(End, I->end), I->valno);
709 ++I;
710 }
711
712 // The remaining ranges begin after Start.
713 for (;I != E && I->start < End; ++I)
714 addSimpleRange(I->start, std::min(End, I->end), I->valno);
715}
716
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000717
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000718//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000719// Split Editor
720//===----------------------------------------------------------------------===//
721
722/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000723SplitEditor::SplitEditor(SplitAnalysis &sa,
724 LiveIntervals &lis,
725 VirtRegMap &vrm,
726 MachineDominatorTree &mdt,
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000727 LiveRangeEdit &edit)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000728 : sa_(sa), LIS(lis), VRM(vrm),
729 MRI(vrm.getMachineFunction().getRegInfo()),
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000730 MDT(mdt),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000731 TII(*vrm.getMachineFunction().getTarget().getInstrInfo()),
732 TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()),
733 Edit(edit),
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000734 OpenIdx(0),
735 RegAssign(Allocator)
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000736{
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000737 // We don't need an AliasAnalysis since we will only be performing
738 // cheap-as-a-copy remats anyway.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000739 Edit.anyRematerializable(LIS, TII, 0);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000740}
741
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000742void SplitEditor::dump() const {
743 if (RegAssign.empty()) {
744 dbgs() << " empty\n";
745 return;
746 }
747
748 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
749 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
750 dbgs() << '\n';
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000751}
752
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000753VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000754 VNInfo *ParentVNI,
755 SlotIndex UseIdx,
756 MachineBasicBlock &MBB,
757 MachineBasicBlock::iterator I) {
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000758 MachineInstr *CopyMI = 0;
759 SlotIndex Def;
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000760 LiveInterval *LI = Edit.get(RegIdx);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000761
762 // Attempt cheap-as-a-copy rematerialization.
763 LiveRangeEdit::Remat RM(ParentVNI);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000764 if (Edit.canRematerializeAt(RM, UseIdx, true, LIS)) {
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000765 Def = Edit.rematerializeAt(MBB, I, LI->reg, RM, LIS, TII, TRI);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000766 } else {
767 // Can't remat, just insert a copy from parent.
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000768 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
769 .addReg(Edit.getReg());
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000770 Def = LIS.InsertMachineInstrInMaps(CopyMI).getDefIndex();
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000771 }
772
773 // Define the value in Reg.
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000774 VNInfo *VNI = LIMappers[RegIdx].defValue(ParentVNI, Def);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000775 VNI->setCopy(CopyMI);
776
777 // Add minimal liveness for the new value.
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000778 Edit.get(RegIdx)->addRange(LiveRange(Def, Def.getNextSlot(), VNI));
779
780 if (RegIdx) {
781 if (UseIdx < Def)
782 UseIdx = Def;
783 RegAssign.insert(Def, UseIdx.getNextSlot(), RegIdx);
784 }
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000785 return VNI;
786}
787
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000788/// Create a new virtual register and live interval.
789void SplitEditor::openIntv() {
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000790 assert(!OpenIdx && "Previous LI not closed before openIntv");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000791
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000792 // Create the complement as index 0.
793 if (Edit.empty()) {
794 Edit.create(MRI, LIS, VRM);
795 LIMappers.push_back(LiveIntervalMap(LIS, MDT, Edit.getParent()));
796 LIMappers.back().reset(Edit.get(0));
797 }
798
799 // Create the open interval.
800 OpenIdx = Edit.size();
801 Edit.create(MRI, LIS, VRM);
802 LIMappers.push_back(LiveIntervalMap(LIS, MDT, Edit.getParent()));
803 LIMappers[OpenIdx].reset(Edit.get(OpenIdx));
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000804}
805
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000806/// enterIntvBefore - Enter OpenLI before the instruction at Idx. If CurLI is
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000807/// not live before Idx, a COPY is not inserted.
808void SplitEditor::enterIntvBefore(SlotIndex Idx) {
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000809 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000810 Idx = Idx.getUseIndex();
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000811 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000812 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000813 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000814 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000815 return;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000816 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000817 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000818 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000819 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000820
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000821 defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
822 DEBUG(dump());
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000823}
824
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000825/// enterIntvAtEnd - Enter OpenLI at the end of MBB.
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000826void SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000827 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000828 SlotIndex End = LIS.getMBBEndIdx(&MBB).getPrevSlot();
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000829 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << End);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000830 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(End);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000831 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000832 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000833 return;
834 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000835 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000836 defFromParent(OpenIdx, ParentVNI, End, MBB, MBB.getFirstTerminator());
837 DEBUG(dump());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000838}
839
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000840/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000841void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000842 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000843}
844
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000845void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000846 assert(OpenIdx && "openIntv not called before useIntv");
847 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
848 RegAssign.insert(Start, End, OpenIdx);
849 DEBUG(dump());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000850}
851
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000852/// leaveIntvAfter - Leave OpenLI after the instruction at Idx.
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000853void SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000854 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000855 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000856
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000857 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000858 Idx = Idx.getBoundaryIndex();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000859 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000860 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000861 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000862 return;
863 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000864 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000865
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000866 MachineBasicBlock::iterator MII = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000867 VNInfo *VNI = defFromParent(0, ParentVNI, Idx,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000868 *MII->getParent(), llvm::next(MII));
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000869
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000870 RegAssign.insert(Idx, VNI->def, OpenIdx);
871 DEBUG(dump());
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000872}
873
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000874/// leaveIntvAtTop - Leave the interval at the top of MBB.
875/// Currently, only one value can leave the interval.
876void SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000877 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000878 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000879 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000880
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000881 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Start);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000882 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000883 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000884 return;
885 }
886
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000887 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000888 MBB.SkipPHIsAndLabels(MBB.begin()));
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000889 RegAssign.insert(Start, VNI->def, OpenIdx);
890 DEBUG(dump());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000891}
892
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000893/// closeIntv - Indicate that we are done editing the currently open
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000894/// LiveInterval, and ranges can be trimmed.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000895void SplitEditor::closeIntv() {
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000896 assert(OpenIdx && "openIntv not called before closeIntv");
897 OpenIdx = 0;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000898}
899
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000900/// rewriteAssigned - Rewrite all uses of Edit.getReg().
901void SplitEditor::rewriteAssigned() {
902 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit.getReg()),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000903 RE = MRI.reg_end(); RI != RE;) {
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000904 MachineOperand &MO = RI.getOperand();
905 MachineInstr *MI = MO.getParent();
906 ++RI;
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000907 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000908 if (MI->isDebugValue()) {
909 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000910 MO.setReg(0);
911 continue;
912 }
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000913 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000914 Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000915
916 // Rewrite to the mapped register at Idx.
917 unsigned RegIdx = RegAssign.lookup(Idx);
918 MO.setReg(Edit.get(RegIdx)->reg);
919 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
920 << Idx << ':' << RegIdx << '\t' << *MI);
921
922 // Extend liveness to Idx.
923 const VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx);
924 LIMappers[RegIdx].mapValue(ParentVNI, Idx);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000925 }
926}
927
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000928/// rewriteSplit - Rewrite uses of Intvs[0] according to the ConEQ mapping.
929void SplitEditor::rewriteComponents(const SmallVectorImpl<LiveInterval*> &Intvs,
930 const ConnectedVNInfoEqClasses &ConEq) {
931 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Intvs[0]->reg),
932 RE = MRI.reg_end(); RI != RE;) {
933 MachineOperand &MO = RI.getOperand();
934 MachineInstr *MI = MO.getParent();
935 ++RI;
936 if (MO.isUse() && MO.isUndef())
Jakob Stoklund Olesen9d999772010-10-21 18:47:08 +0000937 continue;
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000938 // DBG_VALUE instructions should have been eliminated earlier.
939 SlotIndex Idx = LIS.getInstructionIndex(MI);
940 Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
941 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
942 << Idx << ':');
943 const VNInfo *VNI = Intvs[0]->getVNInfoAt(Idx);
944 assert(VNI && "Interval not live at use.");
945 MO.setReg(Intvs[ConEq.getEqClass(VNI)]->reg);
946 DEBUG(dbgs() << VNI->id << '\t' << *MI);
Jakob Stoklund Olesen4b3041c2010-10-07 17:56:39 +0000947 }
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000948}
949
950void SplitEditor::finish() {
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000951 assert(OpenIdx == 0 && "Previous LI not closed before rewrite");
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000952
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000953 // At this point, the live intervals in Edit contain VNInfos corresponding to
954 // the inserted copies.
955
956 // Add the original defs from the parent interval.
957 for (LiveInterval::const_vni_iterator I = Edit.getParent().vni_begin(),
958 E = Edit.getParent().vni_end(); I != E; ++I) {
959 const VNInfo *ParentVNI = *I;
960 LiveIntervalMap &LIM = LIMappers[RegAssign.lookup(ParentVNI->def)];
961 VNInfo *VNI = LIM.defValue(ParentVNI, ParentVNI->def);
962 LIM.getLI()->addRange(LiveRange(ParentVNI->def,
963 ParentVNI->def.getNextSlot(), VNI));
964 // Mark all values as complex to force liveness computation.
965 // This should really only be necessary for remat victims, but we are lazy.
966 LIM.markComplexMapped(ParentVNI);
967 }
968
969#ifndef NDEBUG
970 // Every new interval must have a def by now, otherwise the split is bogus.
971 for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I)
972 assert((*I)->hasAtLeastOneValue() && "Split interval has no value");
973#endif
974
975 // FIXME: Don't recompute the liveness of all values, infer it from the
976 // overlaps between the parent live interval and RegAssign.
977 // The mapValue algorithm is only necessary when:
978 // - The parent value maps to multiple defs, and new phis are needed, or
979 // - The value has been rematerialized before some uses, and we want to
980 // minimize the live range so it only reaches the remaining uses.
981 // All other values have simple liveness that can be computed from RegAssign
982 // and the parent live interval.
983
984 // Extend live ranges to be live-out for successor PHI values.
985 for (LiveInterval::const_vni_iterator I = Edit.getParent().vni_begin(),
986 E = Edit.getParent().vni_end(); I != E; ++I) {
987 const VNInfo *PHIVNI = *I;
988 if (!PHIVNI->isPHIDef())
989 continue;
990 LiveIntervalMap &LIM = LIMappers[RegAssign.lookup(PHIVNI->def)];
991 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
992 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
993 PE = MBB->pred_end(); PI != PE; ++PI) {
994 SlotIndex End = LIS.getMBBEndIdx(*PI).getPrevSlot();
995 // The predecessor may not have a live-out value. That is OK, like an
996 // undef PHI operand.
997 if (VNInfo *VNI = Edit.getParent().getVNInfoAt(End))
998 LIM.mapValue(VNI, End);
999 }
1000 }
1001
1002 // Rewrite instructions.
1003 rewriteAssigned();
1004
1005 // FIXME: Delete defs that were rematted everywhere.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001006
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001007 // Get rid of unused values and set phi-kill flags.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001008 for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I)
1009 (*I)->RenumberValues(LIS);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001010
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001011 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001012 ConnectedVNInfoEqClasses ConEQ(LIS);
1013 for (unsigned i = 0, e = Edit.size(); i != e; ++i) {
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001014 // Don't use iterators, they are invalidated by create() below.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001015 LiveInterval *li = Edit.get(i);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001016 unsigned NumComp = ConEQ.Classify(li);
1017 if (NumComp <= 1)
1018 continue;
1019 DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n');
1020 SmallVector<LiveInterval*, 8> dups;
1021 dups.push_back(li);
1022 for (unsigned i = 1; i != NumComp; ++i)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001023 dups.push_back(&Edit.create(MRI, LIS, VRM));
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +00001024 rewriteComponents(dups, ConEQ);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001025 ConEQ.Distribute(&dups[0]);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001026 }
1027
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +00001028 // Calculate spill weight and allocation hints for new intervals.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001029 VirtRegAuxInfo vrai(VRM.getMachineFunction(), LIS, sa_.Loops);
1030 for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I){
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +00001031 LiveInterval &li = **I;
Jakob Stoklund Olesen9db3ea42010-08-10 18:37:40 +00001032 vrai.CalculateRegClass(li.reg);
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +00001033 vrai.CalculateWeightAndHint(li);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001034 DEBUG(dbgs() << " new interval " << MRI.getRegClass(li.reg)->getName()
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +00001035 << ":" << li << '\n');
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +00001036 }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001037}
1038
1039
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +00001040//===----------------------------------------------------------------------===//
1041// Loop Splitting
1042//===----------------------------------------------------------------------===//
1043
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +00001044void SplitEditor::splitAroundLoop(const MachineLoop *Loop) {
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001045 SplitAnalysis::LoopBlocks Blocks;
1046 sa_.getLoopBlocks(Loop, Blocks);
1047
Jakob Stoklund Olesen452a9fd2010-10-07 18:47:07 +00001048 DEBUG({
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +00001049 dbgs() << " splitAround"; sa_.print(Blocks, dbgs()); dbgs() << '\n';
Jakob Stoklund Olesen452a9fd2010-10-07 18:47:07 +00001050 });
1051
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001052 // Break critical edges as needed.
1053 SplitAnalysis::BlockPtrSet CriticalExits;
1054 sa_.getCriticalExits(Blocks, CriticalExits);
1055 assert(CriticalExits.empty() && "Cannot break critical exits yet");
1056
1057 // Create new live interval for the loop.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +00001058 openIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001059
Jakob Stoklund Olesendfe3b6d2010-12-18 01:06:19 +00001060 // Insert copies in the predecessors if live-in to the header.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001061 if (LIS.isLiveInToMBB(Edit.getParent(), Loop->getHeader())) {
Jakob Stoklund Olesendfe3b6d2010-12-18 01:06:19 +00001062 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Preds.begin(),
1063 E = Blocks.Preds.end(); I != E; ++I) {
1064 MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I);
1065 enterIntvAtEnd(MBB);
1066 }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001067 }
1068
1069 // Switch all loop blocks.
1070 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Loop.begin(),
1071 E = Blocks.Loop.end(); I != E; ++I)
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +00001072 useIntv(**I);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001073
1074 // Insert back copies in the exit blocks.
1075 for (SplitAnalysis::BlockPtrSet::iterator I = Blocks.Exits.begin(),
1076 E = Blocks.Exits.end(); I != E; ++I) {
1077 MachineBasicBlock &MBB = const_cast<MachineBasicBlock&>(**I);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +00001078 leaveIntvAtTop(MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001079 }
1080
1081 // Done.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +00001082 closeIntv();
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001083 finish();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +00001084}
1085
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001086
1087//===----------------------------------------------------------------------===//
1088// Single Block Splitting
1089//===----------------------------------------------------------------------===//
1090
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001091/// getMultiUseBlocks - if CurLI has more than one use in a basic block, it
1092/// may be an advantage to split CurLI for the duration of the block.
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +00001093bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001094 // If CurLI is local to one block, there is no point to splitting it.
1095 if (UsingBlocks.size() <= 1)
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +00001096 return false;
1097 // Add blocks with multiple uses.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001098 for (BlockCountMap::iterator I = UsingBlocks.begin(), E = UsingBlocks.end();
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +00001099 I != E; ++I)
1100 switch (I->second) {
1101 case 0:
1102 case 1:
1103 continue;
1104 case 2: {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001105 // When there are only two uses and CurLI is both live in and live out,
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +00001106 // we don't really win anything by isolating the block since we would be
1107 // inserting two copies.
1108 // The remaing register would still have two uses in the block. (Unless it
1109 // separates into disconnected components).
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001110 if (LIS.isLiveInToMBB(*CurLI, I->first) &&
1111 LIS.isLiveOutOfMBB(*CurLI, I->first))
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +00001112 continue;
1113 } // Fall through.
1114 default:
1115 Blocks.insert(I->first);
1116 }
1117 return !Blocks.empty();
1118}
1119
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001120/// splitSingleBlocks - Split CurLI into a separate live interval inside each
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +00001121/// basic block in Blocks.
1122void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) {
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +00001123 DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001124 // Determine the first and last instruction using CurLI in each block.
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001125 typedef std::pair<SlotIndex,SlotIndex> IndexPair;
1126 typedef DenseMap<const MachineBasicBlock*,IndexPair> IndexPairMap;
1127 IndexPairMap MBBRange;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001128 for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.UsingInstrs.begin(),
1129 E = sa_.UsingInstrs.end(); I != E; ++I) {
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001130 const MachineBasicBlock *MBB = (*I)->getParent();
1131 if (!Blocks.count(MBB))
1132 continue;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001133 SlotIndex Idx = LIS.getInstructionIndex(*I);
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +00001134 DEBUG(dbgs() << " BB#" << MBB->getNumber() << '\t' << Idx << '\t' << **I);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001135 IndexPair &IP = MBBRange[MBB];
1136 if (!IP.first.isValid() || Idx < IP.first)
1137 IP.first = Idx;
1138 if (!IP.second.isValid() || Idx > IP.second)
1139 IP.second = Idx;
1140 }
1141
1142 // Create a new interval for each block.
1143 for (SplitAnalysis::BlockPtrSet::const_iterator I = Blocks.begin(),
1144 E = Blocks.end(); I != E; ++I) {
1145 IndexPair &IP = MBBRange[*I];
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +00001146 DEBUG(dbgs() << " splitting for BB#" << (*I)->getNumber() << ": ["
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001147 << IP.first << ';' << IP.second << ")\n");
1148 assert(IP.first.isValid() && IP.second.isValid());
1149
1150 openIntv();
1151 enterIntvBefore(IP.first);
1152 useIntv(IP.first.getBaseIndex(), IP.second.getBoundaryIndex());
1153 leaveIntvAfter(IP.second);
1154 closeIntv();
1155 }
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001156 finish();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001157}
1158
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001159
1160//===----------------------------------------------------------------------===//
1161// Sub Block Splitting
1162//===----------------------------------------------------------------------===//
1163
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001164/// getBlockForInsideSplit - If CurLI is contained inside a single basic block,
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001165/// and it wou pay to subdivide the interval inside that block, return it.
1166/// Otherwise return NULL. The returned block can be passed to
1167/// SplitEditor::splitInsideBlock.
1168const MachineBasicBlock *SplitAnalysis::getBlockForInsideSplit() {
1169 // The interval must be exclusive to one block.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001170 if (UsingBlocks.size() != 1)
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001171 return 0;
1172 // Don't to this for less than 4 instructions. We want to be sure that
1173 // splitting actually reduces the instruction count per interval.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001174 if (UsingInstrs.size() < 4)
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001175 return 0;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001176 return UsingBlocks.begin()->first;
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001177}
1178
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001179/// splitInsideBlock - Split CurLI into multiple intervals inside MBB.
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +00001180void SplitEditor::splitInsideBlock(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001181 SmallVector<SlotIndex, 32> Uses;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001182 Uses.reserve(sa_.UsingInstrs.size());
1183 for (SplitAnalysis::InstrPtrSet::const_iterator I = sa_.UsingInstrs.begin(),
1184 E = sa_.UsingInstrs.end(); I != E; ++I)
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001185 if ((*I)->getParent() == MBB)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001186 Uses.push_back(LIS.getInstructionIndex(*I));
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001187 DEBUG(dbgs() << " splitInsideBlock BB#" << MBB->getNumber() << " for "
1188 << Uses.size() << " instructions.\n");
1189 assert(Uses.size() >= 3 && "Need at least 3 instructions");
1190 array_pod_sort(Uses.begin(), Uses.end());
1191
1192 // Simple algorithm: Find the largest gap between uses as determined by slot
1193 // indices. Create new intervals for instructions before the gap and after the
1194 // gap.
1195 unsigned bestPos = 0;
1196 int bestGap = 0;
1197 DEBUG(dbgs() << " dist (" << Uses[0]);
1198 for (unsigned i = 1, e = Uses.size(); i != e; ++i) {
1199 int g = Uses[i-1].distance(Uses[i]);
1200 DEBUG(dbgs() << ") -" << g << "- (" << Uses[i]);
1201 if (g > bestGap)
1202 bestPos = i, bestGap = g;
1203 }
1204 DEBUG(dbgs() << "), best: -" << bestGap << "-\n");
1205
1206 // bestPos points to the first use after the best gap.
1207 assert(bestPos > 0 && "Invalid gap");
1208
1209 // FIXME: Don't create intervals for low densities.
1210
1211 // First interval before the gap. Don't create single-instr intervals.
1212 if (bestPos > 1) {
1213 openIntv();
1214 enterIntvBefore(Uses.front());
1215 useIntv(Uses.front().getBaseIndex(), Uses[bestPos-1].getBoundaryIndex());
1216 leaveIntvAfter(Uses[bestPos-1]);
1217 closeIntv();
1218 }
1219
1220 // Second interval after the gap.
1221 if (bestPos < Uses.size()-1) {
1222 openIntv();
1223 enterIntvBefore(Uses[bestPos]);
1224 useIntv(Uses[bestPos].getBaseIndex(), Uses.back().getBoundaryIndex());
1225 leaveIntvAfter(Uses.back());
1226 closeIntv();
1227 }
1228
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001229 finish();
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001230}