blob: 7b02b2662329491f4aa604bf31cf931ccd579342 [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/MachineRegisterInfo.h"
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000024#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000025#include "llvm/Support/Debug.h"
26#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000027#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000029
30using namespace llvm;
31
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000032static cl::opt<bool>
33AllowSplit("spiller-splits-edges",
34 cl::desc("Allow critical edge splitting during spilling"));
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000035
36//===----------------------------------------------------------------------===//
37// Split Analysis
38//===----------------------------------------------------------------------===//
39
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000040SplitAnalysis::SplitAnalysis(const MachineFunction &mf,
41 const LiveIntervals &lis,
42 const MachineLoopInfo &mli)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000043 : MF(mf),
44 LIS(lis),
45 Loops(mli),
46 TII(*mf.getTarget().getInstrInfo()),
47 CurLI(0) {}
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000048
49void SplitAnalysis::clear() {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000050 UseSlots.clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000051 UsingInstrs.clear();
52 UsingBlocks.clear();
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +000053 LiveBlocks.clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000054 CurLI = 0;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000055}
56
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000057bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) {
58 MachineBasicBlock *T, *F;
59 SmallVector<MachineOperand, 4> Cond;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000060 return !TII.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond);
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000061}
62
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000063/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +000064void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000065 const MachineRegisterInfo &MRI = MF.getRegInfo();
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +000066 for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(CurLI->reg),
67 E = MRI.reg_end(); I != E; ++I) {
68 MachineOperand &MO = I.getOperand();
69 if (MO.isUse() && MO.isUndef())
70 continue;
71 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000072 if (MI->isDebugValue() || !UsingInstrs.insert(MI))
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000073 continue;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000074 UseSlots.push_back(LIS.getInstructionIndex(MI).getDefIndex());
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000075 MachineBasicBlock *MBB = MI->getParent();
Jakob Stoklund Olesen4f5c9d22011-02-09 23:56:18 +000076 UsingBlocks[MBB]++;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000077 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000078 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +000079 calcLiveBlockInfo();
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +000080 DEBUG(dbgs() << " counted "
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000081 << UsingInstrs.size() << " instrs, "
Jakob Stoklund Olesen4f5c9d22011-02-09 23:56:18 +000082 << UsingBlocks.size() << " blocks.\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000083}
84
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +000085/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
86/// where CurLI is live.
87void SplitAnalysis::calcLiveBlockInfo() {
88 if (CurLI->empty())
89 return;
90
91 LiveInterval::const_iterator LVI = CurLI->begin();
92 LiveInterval::const_iterator LVE = CurLI->end();
93
94 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
95 UseI = UseSlots.begin();
96 UseE = UseSlots.end();
97
98 // Loop over basic blocks where CurLI is live.
99 MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start);
100 for (;;) {
101 BlockInfo BI;
102 BI.MBB = MFI;
103 SlotIndex Start, Stop;
104 tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
105
106 // The last split point is the latest possible insertion point that dominates
107 // all successor blocks. If interference reaches LastSplitPoint, it is not
108 // possible to insert a split or reload that makes CurLI live in the
109 // outgoing bundle.
110 MachineBasicBlock::iterator LSP = LIS.getLastSplitPoint(*CurLI, BI.MBB);
111 if (LSP == BI.MBB->end())
112 BI.LastSplitPoint = Stop;
113 else
114 BI.LastSplitPoint = LIS.getInstructionIndex(LSP);
115
116 // LVI is the first live segment overlapping MBB.
117 BI.LiveIn = LVI->start <= Start;
118 if (!BI.LiveIn)
119 BI.Def = LVI->start;
120
121 // Find the first and last uses in the block.
122 BI.Uses = hasUses(MFI);
123 if (BI.Uses && UseI != UseE) {
124 BI.FirstUse = *UseI;
125 assert(BI.FirstUse >= Start);
126 do ++UseI;
127 while (UseI != UseE && *UseI < Stop);
128 BI.LastUse = UseI[-1];
129 assert(BI.LastUse < Stop);
130 }
131
132 // Look for gaps in the live range.
133 bool hasGap = false;
134 BI.LiveOut = true;
135 while (LVI->end < Stop) {
136 SlotIndex LastStop = LVI->end;
137 if (++LVI == LVE || LVI->start >= Stop) {
138 BI.Kill = LastStop;
139 BI.LiveOut = false;
140 break;
141 }
142 if (LastStop < LVI->start) {
143 hasGap = true;
144 BI.Kill = LastStop;
145 BI.Def = LVI->start;
146 }
147 }
148
149 // Don't set LiveThrough when the block has a gap.
150 BI.LiveThrough = !hasGap && BI.LiveIn && BI.LiveOut;
151 LiveBlocks.push_back(BI);
152
153 // LVI is now at LVE or LVI->end >= Stop.
154 if (LVI == LVE)
155 break;
156
157 // Live segment ends exactly at Stop. Move to the next segment.
158 if (LVI->end == Stop && ++LVI == LVE)
159 break;
160
161 // Pick the next basic block.
162 if (LVI->start < Stop)
163 ++MFI;
164 else
165 MFI = LIS.getMBBFromIndex(LVI->start);
166 }
167}
168
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000169void SplitAnalysis::print(const BlockPtrSet &B, raw_ostream &OS) const {
170 for (BlockPtrSet::const_iterator I = B.begin(), E = B.end(); I != E; ++I) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000171 unsigned count = UsingBlocks.lookup(*I);
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000172 OS << " BB#" << (*I)->getNumber();
173 if (count)
174 OS << '(' << count << ')';
175 }
176}
177
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000178void SplitAnalysis::analyze(const LiveInterval *li) {
179 clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000180 CurLI = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000181 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000182}
183
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000184
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000185//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000186// LiveIntervalMap
187//===----------------------------------------------------------------------===//
188
Jakob Stoklund Olesenb3e96812010-09-13 21:29:45 +0000189// Work around the fact that the std::pair constructors are broken for pointer
190// pairs in some implementations. makeVV(x, 0) works.
191static inline std::pair<const VNInfo*, VNInfo*>
192makeVV(const VNInfo *a, VNInfo *b) {
193 return std::make_pair(a, b);
194}
195
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000196void LiveIntervalMap::reset(LiveInterval *li) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000197 LI = li;
198 Values.clear();
199 LiveOutCache.clear();
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000200}
201
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000202bool LiveIntervalMap::isComplexMapped(const VNInfo *ParentVNI) const {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000203 ValueMap::const_iterator i = Values.find(ParentVNI);
204 return i != Values.end() && i->second == 0;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000205}
206
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000207// defValue - Introduce a LI def for ParentVNI that could be later than
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000208// ParentVNI->def.
209VNInfo *LiveIntervalMap::defValue(const VNInfo *ParentVNI, SlotIndex Idx) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000210 assert(LI && "call reset first");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000211 assert(ParentVNI && "Mapping NULL value");
212 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000213 assert(ParentLI.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000214
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000215 // Create a new value.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000216 VNInfo *VNI = LI->getNextValue(Idx, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000217
Jakob Stoklund Olesen79c02622010-10-26 22:36:02 +0000218 // Preserve the PHIDef bit.
219 if (ParentVNI->isPHIDef() && Idx == ParentVNI->def)
220 VNI->setIsPHIDef(true);
221
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000222 // Use insert for lookup, so we can add missing values with a second lookup.
223 std::pair<ValueMap::iterator,bool> InsP =
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000224 Values.insert(makeVV(ParentVNI, Idx == ParentVNI->def ? VNI : 0));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000225
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000226 // This is now a complex def. Mark with a NULL in valueMap.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000227 if (!InsP.second)
228 InsP.first->second = 0;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000229
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000230 return VNI;
231}
232
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000233
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000234// mapValue - Find the mapped value for ParentVNI at Idx.
235// Potentially create phi-def values.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000236VNInfo *LiveIntervalMap::mapValue(const VNInfo *ParentVNI, SlotIndex Idx,
237 bool *simple) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000238 assert(LI && "call reset first");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000239 assert(ParentVNI && "Mapping NULL value");
240 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000241 assert(ParentLI.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000242
243 // Use insert for lookup, so we can add missing values with a second lookup.
244 std::pair<ValueMap::iterator,bool> InsP =
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000245 Values.insert(makeVV(ParentVNI, 0));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000246
247 // This was an unknown value. Create a simple mapping.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000248 if (InsP.second) {
249 if (simple) *simple = true;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000250 return InsP.first->second = LI->createValueCopy(ParentVNI,
251 LIS.getVNInfoAllocator());
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000252 }
253
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000254 // This was a simple mapped value.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000255 if (InsP.first->second) {
256 if (simple) *simple = true;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000257 return InsP.first->second;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000258 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000259
260 // This is a complex mapped value. There may be multiple defs, and we may need
261 // to create phi-defs.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000262 if (simple) *simple = false;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000263 MachineBasicBlock *IdxMBB = LIS.getMBBFromIndex(Idx);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000264 assert(IdxMBB && "No MBB at Idx");
265
266 // Is there a def in the same MBB we can extend?
267 if (VNInfo *VNI = extendTo(IdxMBB, Idx))
268 return VNI;
269
270 // Now for the fun part. We know that ParentVNI potentially has multiple defs,
271 // and we may need to create even more phi-defs to preserve VNInfo SSA form.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000272 // Perform a search for all predecessor blocks where we know the dominating
273 // VNInfo. Insert phi-def VNInfos along the path back to IdxMBB.
274 DEBUG(dbgs() << "\n Reaching defs for BB#" << IdxMBB->getNumber()
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000275 << " at " << Idx << " in " << *LI << '\n');
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000276
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000277 // Blocks where LI should be live-in.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000278 SmallVector<MachineDomTreeNode*, 16> LiveIn;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000279 LiveIn.push_back(MDT[IdxMBB]);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000280
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000281 // Using LiveOutCache as a visited set, perform a BFS for all reaching defs.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000282 for (unsigned i = 0; i != LiveIn.size(); ++i) {
283 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
284 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
285 PE = MBB->pred_end(); PI != PE; ++PI) {
286 MachineBasicBlock *Pred = *PI;
287 // Is this a known live-out block?
288 std::pair<LiveOutMap::iterator,bool> LOIP =
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000289 LiveOutCache.insert(std::make_pair(Pred, LiveOutPair()));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000290 // Yes, we have been here before.
291 if (!LOIP.second) {
Benjamin Kramer8a8e26f2010-10-29 17:40:05 +0000292 DEBUG(if (VNInfo *VNI = LOIP.first->second.first)
293 dbgs() << " known valno #" << VNI->id
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000294 << " at BB#" << Pred->getNumber() << '\n');
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000295 continue;
296 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000297
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000298 // Does Pred provide a live-out value?
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000299 SlotIndex Last = LIS.getMBBEndIdx(Pred).getPrevSlot();
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000300 if (VNInfo *VNI = extendTo(Pred, Last)) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000301 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(VNI->def);
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000302 DEBUG(dbgs() << " found valno #" << VNI->id
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000303 << " from BB#" << DefMBB->getNumber()
304 << " at BB#" << Pred->getNumber() << '\n');
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000305 LiveOutPair &LOP = LOIP.first->second;
306 LOP.first = VNI;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000307 LOP.second = MDT[DefMBB];
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000308 continue;
309 }
310 // No, we need a live-in value for Pred as well
311 if (Pred != IdxMBB)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000312 LiveIn.push_back(MDT[Pred]);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000313 }
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000314 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000315
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000316 // We may need to add phi-def values to preserve the SSA form.
317 // This is essentially the same iterative algorithm that SSAUpdater uses,
318 // except we already have a dominator tree, so we don't have to recompute it.
319 VNInfo *IdxVNI = 0;
320 unsigned Changes;
321 do {
322 Changes = 0;
323 DEBUG(dbgs() << " Iterating over " << LiveIn.size() << " blocks.\n");
324 // Propagate live-out values down the dominator tree, inserting phi-defs when
325 // necessary. Since LiveIn was created by a BFS, going backwards makes it more
326 // likely for us to visit immediate dominators before their children.
327 for (unsigned i = LiveIn.size(); i; --i) {
328 MachineDomTreeNode *Node = LiveIn[i-1];
329 MachineBasicBlock *MBB = Node->getBlock();
330 MachineDomTreeNode *IDom = Node->getIDom();
331 LiveOutPair IDomValue;
332 // We need a live-in value to a block with no immediate dominator?
333 // This is probably an unreachable block that has survived somehow.
334 bool needPHI = !IDom;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000335
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000336 // Get the IDom live-out value.
337 if (!needPHI) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000338 LiveOutMap::iterator I = LiveOutCache.find(IDom->getBlock());
339 if (I != LiveOutCache.end())
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000340 IDomValue = I->second;
341 else
342 // If IDom is outside our set of live-out blocks, there must be new
343 // defs, and we need a phi-def here.
344 needPHI = true;
345 }
Jakob Stoklund Olesen984a7fc2010-10-05 20:36:28 +0000346
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000347 // IDom dominates all of our predecessors, but it may not be the immediate
348 // dominator. Check if any of them have live-out values that are properly
349 // dominated by IDom. If so, we need a phi-def here.
350 if (!needPHI) {
351 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
352 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000353 LiveOutPair Value = LiveOutCache[*PI];
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000354 if (!Value.first || Value.first == IDomValue.first)
355 continue;
356 // This predecessor is carrying something other than IDomValue.
357 // It could be because IDomValue hasn't propagated yet, or it could be
358 // because MBB is in the dominance frontier of that value.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000359 if (MDT.dominates(IDom, Value.second)) {
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000360 needPHI = true;
361 break;
362 }
363 }
364 }
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000365
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000366 // Create a phi-def if required.
367 if (needPHI) {
368 ++Changes;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000369 SlotIndex Start = LIS.getMBBStartIdx(MBB);
370 VNInfo *VNI = LI->getNextValue(Start, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000371 VNI->setIsPHIDef(true);
372 DEBUG(dbgs() << " - BB#" << MBB->getNumber()
373 << " phi-def #" << VNI->id << " at " << Start << '\n');
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000374 // We no longer need LI to be live-in.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000375 LiveIn.erase(LiveIn.begin()+(i-1));
376 // Blocks in LiveIn are either IdxMBB, or have a value live-through.
377 if (MBB == IdxMBB)
378 IdxVNI = VNI;
379 // Check if we need to update live-out info.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000380 LiveOutMap::iterator I = LiveOutCache.find(MBB);
381 if (I == LiveOutCache.end() || I->second.second == Node) {
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000382 // We already have a live-out defined in MBB, so this must be IdxMBB.
383 assert(MBB == IdxMBB && "Adding phi-def to known live-out");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000384 LI->addRange(LiveRange(Start, Idx.getNextSlot(), VNI));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000385 } else {
386 // This phi-def is also live-out, so color the whole block.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000387 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000388 I->second = LiveOutPair(VNI, Node);
389 }
390 } else if (IDomValue.first) {
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000391 // No phi-def here. Remember incoming value for IdxMBB.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000392 if (MBB == IdxMBB)
393 IdxVNI = IDomValue.first;
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000394 // Propagate IDomValue if needed:
395 // MBB is live-out and doesn't define its own value.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000396 LiveOutMap::iterator I = LiveOutCache.find(MBB);
397 if (I != LiveOutCache.end() && I->second.second != Node &&
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000398 I->second.first != IDomValue.first) {
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000399 ++Changes;
400 I->second = IDomValue;
401 DEBUG(dbgs() << " - BB#" << MBB->getNumber()
402 << " idom valno #" << IDomValue.first->id
403 << " from BB#" << IDom->getBlock()->getNumber() << '\n');
404 }
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000405 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000406 }
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000407 DEBUG(dbgs() << " - made " << Changes << " changes.\n");
408 } while (Changes);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000409
410 assert(IdxVNI && "Didn't find value for Idx");
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000411
412#ifndef NDEBUG
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000413 // Check the LiveOutCache invariants.
414 for (LiveOutMap::iterator I = LiveOutCache.begin(), E = LiveOutCache.end();
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000415 I != E; ++I) {
416 assert(I->first && "Null MBB entry in cache");
417 assert(I->second.first && "Null VNInfo in cache");
418 assert(I->second.second && "Null DomTreeNode in cache");
419 if (I->second.second->getBlock() == I->first)
420 continue;
421 for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(),
422 PE = I->first->pred_end(); PI != PE; ++PI)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000423 assert(LiveOutCache.lookup(*PI) == I->second && "Bad invariant");
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000424 }
425#endif
426
427 // Since we went through the trouble of a full BFS visiting all reaching defs,
428 // the values in LiveIn are now accurate. No more phi-defs are needed
429 // for these blocks, so we can color the live ranges.
430 // This makes the next mapValue call much faster.
431 for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) {
432 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000433 SlotIndex Start = LIS.getMBBStartIdx(MBB);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000434 VNInfo *VNI = LiveOutCache.lookup(MBB).first;
Jakob Stoklund Olesen08eb8dd2011-02-03 20:29:36 +0000435
436 // Anything in LiveIn other than IdxMBB is live-through.
437 // In IdxMBB, we should stop at Idx unless the same value is live-out.
438 if (MBB == IdxMBB && IdxVNI != VNI)
439 LI->addRange(LiveRange(Start, Idx.getNextSlot(), IdxVNI));
440 else
441 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000442 }
443
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000444 return IdxVNI;
445}
446
Jakob Stoklund Olesend7ca5772011-01-20 17:45:20 +0000447#ifndef NDEBUG
448void LiveIntervalMap::dumpCache() {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000449 for (LiveOutMap::iterator I = LiveOutCache.begin(), E = LiveOutCache.end();
Jakob Stoklund Olesend7ca5772011-01-20 17:45:20 +0000450 I != E; ++I) {
451 assert(I->first && "Null MBB entry in cache");
452 assert(I->second.first && "Null VNInfo in cache");
453 assert(I->second.second && "Null DomTreeNode in cache");
454 dbgs() << " cache: BB#" << I->first->getNumber()
455 << " has valno #" << I->second.first->id << " from BB#"
456 << I->second.second->getBlock()->getNumber() << ", preds";
457 for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(),
458 PE = I->first->pred_end(); PI != PE; ++PI)
459 dbgs() << " BB#" << (*PI)->getNumber();
460 dbgs() << '\n';
461 }
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000462 dbgs() << " cache: " << LiveOutCache.size() << " entries.\n";
Jakob Stoklund Olesend7ca5772011-01-20 17:45:20 +0000463}
464#endif
465
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000466// extendTo - Find the last LI value defined in MBB at or before Idx. The
467// ParentLI is assumed to be live at Idx. Extend the live range to Idx.
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000468// Return the found VNInfo, or NULL.
Jakob Stoklund Olesenc95c1462010-10-27 00:39:07 +0000469VNInfo *LiveIntervalMap::extendTo(const MachineBasicBlock *MBB, SlotIndex Idx) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000470 assert(LI && "call reset first");
471 LiveInterval::iterator I = std::upper_bound(LI->begin(), LI->end(), Idx);
472 if (I == LI->begin())
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000473 return 0;
474 --I;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000475 if (I->end <= LIS.getMBBStartIdx(MBB))
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000476 return 0;
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000477 if (I->end <= Idx)
478 I->end = Idx.getNextSlot();
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000479 return I->valno;
480}
481
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000482// addSimpleRange - Add a simple range from ParentLI to LI.
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000483// ParentVNI must be live in the [Start;End) interval.
484void LiveIntervalMap::addSimpleRange(SlotIndex Start, SlotIndex End,
485 const VNInfo *ParentVNI) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000486 assert(LI && "call reset first");
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000487 bool simple;
488 VNInfo *VNI = mapValue(ParentVNI, Start, &simple);
489 // A simple mapping is easy.
490 if (simple) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000491 LI->addRange(LiveRange(Start, End, VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000492 return;
493 }
494
495 // ParentVNI is a complex value. We must map per MBB.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000496 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
497 MachineFunction::iterator MBBE = LIS.getMBBFromIndex(End.getPrevSlot());
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000498
499 if (MBB == MBBE) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000500 LI->addRange(LiveRange(Start, End, VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000501 return;
502 }
503
504 // First block.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000505 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000506
507 // Run sequence of full blocks.
508 for (++MBB; MBB != MBBE; ++MBB) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000509 Start = LIS.getMBBStartIdx(MBB);
510 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB),
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000511 mapValue(ParentVNI, Start)));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000512 }
513
514 // Final block.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000515 Start = LIS.getMBBStartIdx(MBB);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000516 if (Start != End)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000517 LI->addRange(LiveRange(Start, End, mapValue(ParentVNI, Start)));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000518}
519
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000520/// addRange - Add live ranges to LI where [Start;End) intersects ParentLI.
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000521/// All needed values whose def is not inside [Start;End) must be defined
522/// beforehand so mapValue will work.
523void LiveIntervalMap::addRange(SlotIndex Start, SlotIndex End) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000524 assert(LI && "call reset first");
525 LiveInterval::const_iterator B = ParentLI.begin(), E = ParentLI.end();
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000526 LiveInterval::const_iterator I = std::lower_bound(B, E, Start);
527
528 // Check if --I begins before Start and overlaps.
529 if (I != B) {
530 --I;
531 if (I->end > Start)
532 addSimpleRange(Start, std::min(End, I->end), I->valno);
533 ++I;
534 }
535
536 // The remaining ranges begin after Start.
537 for (;I != E && I->start < End; ++I)
538 addSimpleRange(I->start, std::min(End, I->end), I->valno);
539}
540
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000541
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000542//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000543// Split Editor
544//===----------------------------------------------------------------------===//
545
546/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000547SplitEditor::SplitEditor(SplitAnalysis &sa,
548 LiveIntervals &lis,
549 VirtRegMap &vrm,
550 MachineDominatorTree &mdt,
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000551 LiveRangeEdit &edit)
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000552 : SA(sa), LIS(lis), VRM(vrm),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000553 MRI(vrm.getMachineFunction().getRegInfo()),
Eric Christopher0f438112011-02-03 06:18:29 +0000554 MDT(mdt),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000555 TII(*vrm.getMachineFunction().getTarget().getInstrInfo()),
556 TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()),
557 Edit(edit),
Eric Christopher0f438112011-02-03 06:18:29 +0000558 OpenIdx(0),
559 RegAssign(Allocator)
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000560{
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000561 // We don't need an AliasAnalysis since we will only be performing
562 // cheap-as-a-copy remats anyway.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000563 Edit.anyRematerializable(LIS, TII, 0);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000564}
565
Eric Christopher0f438112011-02-03 06:18:29 +0000566void SplitEditor::dump() const {
567 if (RegAssign.empty()) {
568 dbgs() << " empty\n";
569 return;
570 }
571
572 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
573 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
574 dbgs() << '\n';
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000575}
576
Eric Christopher0f438112011-02-03 06:18:29 +0000577VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000578 VNInfo *ParentVNI,
579 SlotIndex UseIdx,
580 MachineBasicBlock &MBB,
581 MachineBasicBlock::iterator I) {
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000582 MachineInstr *CopyMI = 0;
583 SlotIndex Def;
Eric Christopher0f438112011-02-03 06:18:29 +0000584 LiveInterval *LI = Edit.get(RegIdx);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000585
586 // Attempt cheap-as-a-copy rematerialization.
587 LiveRangeEdit::Remat RM(ParentVNI);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000588 if (Edit.canRematerializeAt(RM, UseIdx, true, LIS)) {
Eric Christopher0f438112011-02-03 06:18:29 +0000589 Def = Edit.rematerializeAt(MBB, I, LI->reg, RM, LIS, TII, TRI);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000590 } else {
591 // Can't remat, just insert a copy from parent.
Eric Christopher0f438112011-02-03 06:18:29 +0000592 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
593 .addReg(Edit.getReg());
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000594 Def = LIS.InsertMachineInstrInMaps(CopyMI).getDefIndex();
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000595 }
596
597 // Define the value in Reg.
Eric Christopher0f438112011-02-03 06:18:29 +0000598 VNInfo *VNI = LIMappers[RegIdx].defValue(ParentVNI, Def);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000599 VNI->setCopy(CopyMI);
600
601 // Add minimal liveness for the new value.
Eric Christopher0f438112011-02-03 06:18:29 +0000602 Edit.get(RegIdx)->addRange(LiveRange(Def, Def.getNextSlot(), VNI));
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000603 return VNI;
604}
605
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000606/// Create a new virtual register and live interval.
607void SplitEditor::openIntv() {
Eric Christopher0f438112011-02-03 06:18:29 +0000608 assert(!OpenIdx && "Previous LI not closed before openIntv");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000609
Eric Christopher0f438112011-02-03 06:18:29 +0000610 // Create the complement as index 0.
611 if (Edit.empty()) {
612 Edit.create(MRI, LIS, VRM);
613 LIMappers.push_back(LiveIntervalMap(LIS, MDT, Edit.getParent()));
614 LIMappers.back().reset(Edit.get(0));
615 }
616
617 // Create the open interval.
618 OpenIdx = Edit.size();
619 Edit.create(MRI, LIS, VRM);
620 LIMappers.push_back(LiveIntervalMap(LIS, MDT, Edit.getParent()));
621 LIMappers[OpenIdx].reset(Edit.get(OpenIdx));
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000622}
623
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000624SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000625 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000626 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000627 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000628 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000629 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000630 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000631 return Idx;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000632 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000633 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000634 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000635 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000636
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000637 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
638 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000639}
640
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000641SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000642 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000643 SlotIndex End = LIS.getMBBEndIdx(&MBB);
644 SlotIndex Last = End.getPrevSlot();
645 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
646 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Last);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000647 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000648 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000649 return End;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000650 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000651 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000652 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesencb640472011-02-04 19:33:11 +0000653 LIS.getLastSplitPoint(Edit.getParent(), &MBB));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000654 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopher0f438112011-02-03 06:18:29 +0000655 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000656 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000657}
658
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000659/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000660void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000661 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000662}
663
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000664void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopher0f438112011-02-03 06:18:29 +0000665 assert(OpenIdx && "openIntv not called before useIntv");
666 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
667 RegAssign.insert(Start, End, OpenIdx);
668 DEBUG(dump());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000669}
670
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000671SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000672 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000673 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000674
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000675 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000676 Idx = Idx.getBoundaryIndex();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000677 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000678 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000679 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000680 return Idx.getNextSlot();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000681 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000682 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000683
Jakob Stoklund Olesen01cb34b2011-02-08 18:50:18 +0000684 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
685 assert(MI && "No instruction at index");
686 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(),
687 llvm::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000688 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000689}
690
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000691SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
692 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
693 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
694
695 // The interval must be live into the instruction at Idx.
696 Idx = Idx.getBoundaryIndex();
697 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx);
698 if (!ParentVNI) {
699 DEBUG(dbgs() << ": not live\n");
700 return Idx.getNextSlot();
701 }
702 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
703
704 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
705 assert(MI && "No instruction at index");
706 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
707 return VNI->def;
708}
709
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000710SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000711 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000712 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000713 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000714
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000715 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Start);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000716 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000717 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000718 return Start;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000719 }
720
Eric Christopher0f438112011-02-03 06:18:29 +0000721 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000722 MBB.SkipPHIsAndLabels(MBB.begin()));
Eric Christopher0f438112011-02-03 06:18:29 +0000723 RegAssign.insert(Start, VNI->def, OpenIdx);
724 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000725 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000726}
727
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000728void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
729 assert(OpenIdx && "openIntv not called before overlapIntv");
730 assert(Edit.getParent().getVNInfoAt(Start) ==
731 Edit.getParent().getVNInfoAt(End.getPrevSlot()) &&
732 "Parent changes value in extended range");
733 assert(Edit.get(0)->getVNInfoAt(Start) && "Start must come from leaveIntv*");
734 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
735 "Range cannot span basic blocks");
736
737 // Treat this as useIntv() for now. The complement interval will be extended
738 // as needed by mapValue().
739 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
740 RegAssign.insert(Start, End, OpenIdx);
741 DEBUG(dump());
742}
743
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000744/// closeIntv - Indicate that we are done editing the currently open
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000745/// LiveInterval, and ranges can be trimmed.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000746void SplitEditor::closeIntv() {
Eric Christopher0f438112011-02-03 06:18:29 +0000747 assert(OpenIdx && "openIntv not called before closeIntv");
748 OpenIdx = 0;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000749}
750
Eric Christopher0f438112011-02-03 06:18:29 +0000751/// rewriteAssigned - Rewrite all uses of Edit.getReg().
752void SplitEditor::rewriteAssigned() {
753 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit.getReg()),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000754 RE = MRI.reg_end(); RI != RE;) {
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000755 MachineOperand &MO = RI.getOperand();
756 MachineInstr *MI = MO.getParent();
757 ++RI;
Eric Christopher0f438112011-02-03 06:18:29 +0000758 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000759 if (MI->isDebugValue()) {
760 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000761 MO.setReg(0);
762 continue;
763 }
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +0000764
765 // <undef> operands don't really read the register, so just assign them to
766 // the complement.
767 if (MO.isUse() && MO.isUndef()) {
768 MO.setReg(Edit.get(0)->reg);
769 continue;
770 }
771
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000772 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000773 Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
Eric Christopher0f438112011-02-03 06:18:29 +0000774
775 // Rewrite to the mapped register at Idx.
776 unsigned RegIdx = RegAssign.lookup(Idx);
777 MO.setReg(Edit.get(RegIdx)->reg);
778 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
779 << Idx << ':' << RegIdx << '\t' << *MI);
780
781 // Extend liveness to Idx.
782 const VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx);
783 LIMappers[RegIdx].mapValue(ParentVNI, Idx);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000784 }
785}
786
Eric Christopher0f438112011-02-03 06:18:29 +0000787/// rewriteSplit - Rewrite uses of Intvs[0] according to the ConEQ mapping.
788void SplitEditor::rewriteComponents(const SmallVectorImpl<LiveInterval*> &Intvs,
789 const ConnectedVNInfoEqClasses &ConEq) {
790 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Intvs[0]->reg),
791 RE = MRI.reg_end(); RI != RE;) {
792 MachineOperand &MO = RI.getOperand();
793 MachineInstr *MI = MO.getParent();
794 ++RI;
795 if (MO.isUse() && MO.isUndef())
Jakob Stoklund Olesen9d999772010-10-21 18:47:08 +0000796 continue;
Eric Christopher0f438112011-02-03 06:18:29 +0000797 // DBG_VALUE instructions should have been eliminated earlier.
798 SlotIndex Idx = LIS.getInstructionIndex(MI);
799 Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
800 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
801 << Idx << ':');
802 const VNInfo *VNI = Intvs[0]->getVNInfoAt(Idx);
803 assert(VNI && "Interval not live at use.");
804 MO.setReg(Intvs[ConEq.getEqClass(VNI)]->reg);
805 DEBUG(dbgs() << VNI->id << '\t' << *MI);
Eric Christopher463a2972011-02-03 05:40:54 +0000806 }
Eric Christopher463a2972011-02-03 05:40:54 +0000807}
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000808
Eric Christopher463a2972011-02-03 05:40:54 +0000809void SplitEditor::finish() {
Eric Christopher0f438112011-02-03 06:18:29 +0000810 assert(OpenIdx == 0 && "Previous LI not closed before rewrite");
Eric Christopher463a2972011-02-03 05:40:54 +0000811
Eric Christopher0f438112011-02-03 06:18:29 +0000812 // At this point, the live intervals in Edit contain VNInfos corresponding to
813 // the inserted copies.
814
815 // Add the original defs from the parent interval.
816 for (LiveInterval::const_vni_iterator I = Edit.getParent().vni_begin(),
817 E = Edit.getParent().vni_end(); I != E; ++I) {
818 const VNInfo *ParentVNI = *I;
Jakob Stoklund Olesen9ecd1e72011-02-04 00:59:23 +0000819 if (ParentVNI->isUnused())
820 continue;
Eric Christopher0f438112011-02-03 06:18:29 +0000821 LiveIntervalMap &LIM = LIMappers[RegAssign.lookup(ParentVNI->def)];
822 VNInfo *VNI = LIM.defValue(ParentVNI, ParentVNI->def);
823 LIM.getLI()->addRange(LiveRange(ParentVNI->def,
824 ParentVNI->def.getNextSlot(), VNI));
825 // Mark all values as complex to force liveness computation.
826 // This should really only be necessary for remat victims, but we are lazy.
827 LIM.markComplexMapped(ParentVNI);
828 }
829
830#ifndef NDEBUG
831 // Every new interval must have a def by now, otherwise the split is bogus.
832 for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I)
833 assert((*I)->hasAtLeastOneValue() && "Split interval has no value");
834#endif
835
836 // FIXME: Don't recompute the liveness of all values, infer it from the
837 // overlaps between the parent live interval and RegAssign.
838 // The mapValue algorithm is only necessary when:
839 // - The parent value maps to multiple defs, and new phis are needed, or
840 // - The value has been rematerialized before some uses, and we want to
841 // minimize the live range so it only reaches the remaining uses.
842 // All other values have simple liveness that can be computed from RegAssign
843 // and the parent live interval.
844
845 // Extend live ranges to be live-out for successor PHI values.
846 for (LiveInterval::const_vni_iterator I = Edit.getParent().vni_begin(),
847 E = Edit.getParent().vni_end(); I != E; ++I) {
848 const VNInfo *PHIVNI = *I;
Jakob Stoklund Olesen9ecd1e72011-02-04 00:59:23 +0000849 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
Eric Christopher0f438112011-02-03 06:18:29 +0000850 continue;
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000851 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
852 LiveIntervalMap &LIM = LIMappers[RegIdx];
Eric Christopher0f438112011-02-03 06:18:29 +0000853 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000854 DEBUG(dbgs() << " map phi in BB#" << MBB->getNumber() << '@' << PHIVNI->def
855 << " -> " << RegIdx << '\n');
Eric Christopher0f438112011-02-03 06:18:29 +0000856 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
857 PE = MBB->pred_end(); PI != PE; ++PI) {
858 SlotIndex End = LIS.getMBBEndIdx(*PI).getPrevSlot();
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000859 DEBUG(dbgs() << " pred BB#" << (*PI)->getNumber() << '@' << End);
Eric Christopher0f438112011-02-03 06:18:29 +0000860 // The predecessor may not have a live-out value. That is OK, like an
861 // undef PHI operand.
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000862 if (VNInfo *VNI = Edit.getParent().getVNInfoAt(End)) {
863 DEBUG(dbgs() << " has parent valno #" << VNI->id << " live out\n");
864 assert(RegAssign.lookup(End) == RegIdx &&
865 "Different register assignment in phi predecessor");
Eric Christopher0f438112011-02-03 06:18:29 +0000866 LIM.mapValue(VNI, End);
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000867 }
868 else
869 DEBUG(dbgs() << " is not live-out\n");
Eric Christopher0f438112011-02-03 06:18:29 +0000870 }
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000871 DEBUG(dbgs() << " " << *LIM.getLI() << '\n');
Eric Christopher0f438112011-02-03 06:18:29 +0000872 }
873
874 // Rewrite instructions.
875 rewriteAssigned();
876
877 // FIXME: Delete defs that were rematted everywhere.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000878
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000879 // Get rid of unused values and set phi-kill flags.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000880 for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I)
881 (*I)->RenumberValues(LIS);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000882
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000883 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000884 ConnectedVNInfoEqClasses ConEQ(LIS);
885 for (unsigned i = 0, e = Edit.size(); i != e; ++i) {
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000886 // Don't use iterators, they are invalidated by create() below.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000887 LiveInterval *li = Edit.get(i);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000888 unsigned NumComp = ConEQ.Classify(li);
889 if (NumComp <= 1)
890 continue;
891 DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n');
892 SmallVector<LiveInterval*, 8> dups;
893 dups.push_back(li);
894 for (unsigned i = 1; i != NumComp; ++i)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000895 dups.push_back(&Edit.create(MRI, LIS, VRM));
Eric Christopher0f438112011-02-03 06:18:29 +0000896 rewriteComponents(dups, ConEQ);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000897 ConEQ.Distribute(&dups[0]);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000898 }
899
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000900 // Calculate spill weight and allocation hints for new intervals.
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000901 VirtRegAuxInfo vrai(VRM.getMachineFunction(), LIS, SA.Loops);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000902 for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I){
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000903 LiveInterval &li = **I;
Jakob Stoklund Olesen9db3ea42010-08-10 18:37:40 +0000904 vrai.CalculateRegClass(li.reg);
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000905 vrai.CalculateWeightAndHint(li);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000906 DEBUG(dbgs() << " new interval " << MRI.getRegClass(li.reg)->getName()
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000907 << ":" << li << '\n');
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000908 }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000909}
910
911
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000912//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000913// Single Block Splitting
914//===----------------------------------------------------------------------===//
915
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000916/// getMultiUseBlocks - if CurLI has more than one use in a basic block, it
917/// may be an advantage to split CurLI for the duration of the block.
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000918bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000919 // If CurLI is local to one block, there is no point to splitting it.
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000920 if (LiveBlocks.size() <= 1)
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000921 return false;
922 // Add blocks with multiple uses.
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000923 for (unsigned i = 0, e = LiveBlocks.size(); i != e; ++i) {
924 const BlockInfo &BI = LiveBlocks[i];
925 if (!BI.Uses)
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000926 continue;
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000927 unsigned Instrs = UsingBlocks.lookup(BI.MBB);
928 if (Instrs <= 1)
929 continue;
930 if (Instrs == 2 && BI.LiveIn && BI.LiveOut && !BI.LiveThrough)
931 continue;
932 Blocks.insert(BI.MBB);
933 }
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000934 return !Blocks.empty();
935}
936
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000937/// splitSingleBlocks - Split CurLI into a separate live interval inside each
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000938/// basic block in Blocks.
939void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) {
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000940 DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n");
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000941
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000942 for (unsigned i = 0, e = SA.LiveBlocks.size(); i != e; ++i) {
943 const SplitAnalysis::BlockInfo &BI = SA.LiveBlocks[i];
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000944 if (!BI.Uses || !Blocks.count(BI.MBB))
945 continue;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000946
947 openIntv();
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000948 SlotIndex SegStart = enterIntvBefore(BI.FirstUse);
949 if (BI.LastUse < BI.LastSplitPoint) {
950 useIntv(SegStart, leaveIntvAfter(BI.LastUse));
951 } else {
952 // THe last use os after tha last valid split point.
953 SlotIndex SegStop = leaveIntvBefore(BI.LastSplitPoint);
954 useIntv(SegStart, SegStop);
955 overlapIntv(SegStop, BI.LastUse);
956 }
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000957 closeIntv();
958 }
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000959 finish();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000960}
961
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000962
963//===----------------------------------------------------------------------===//
964// Sub Block Splitting
965//===----------------------------------------------------------------------===//
966
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000967/// getBlockForInsideSplit - If CurLI is contained inside a single basic block,
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000968/// and it wou pay to subdivide the interval inside that block, return it.
969/// Otherwise return NULL. The returned block can be passed to
970/// SplitEditor::splitInsideBlock.
971const MachineBasicBlock *SplitAnalysis::getBlockForInsideSplit() {
972 // The interval must be exclusive to one block.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000973 if (UsingBlocks.size() != 1)
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000974 return 0;
975 // Don't to this for less than 4 instructions. We want to be sure that
976 // splitting actually reduces the instruction count per interval.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000977 if (UsingInstrs.size() < 4)
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000978 return 0;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000979 return UsingBlocks.begin()->first;
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000980}
981
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000982/// splitInsideBlock - Split CurLI into multiple intervals inside MBB.
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000983void SplitEditor::splitInsideBlock(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000984 SmallVector<SlotIndex, 32> Uses;
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000985 Uses.reserve(SA.UsingInstrs.size());
986 for (SplitAnalysis::InstrPtrSet::const_iterator I = SA.UsingInstrs.begin(),
987 E = SA.UsingInstrs.end(); I != E; ++I)
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000988 if ((*I)->getParent() == MBB)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000989 Uses.push_back(LIS.getInstructionIndex(*I));
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000990 DEBUG(dbgs() << " splitInsideBlock BB#" << MBB->getNumber() << " for "
991 << Uses.size() << " instructions.\n");
992 assert(Uses.size() >= 3 && "Need at least 3 instructions");
993 array_pod_sort(Uses.begin(), Uses.end());
994
995 // Simple algorithm: Find the largest gap between uses as determined by slot
996 // indices. Create new intervals for instructions before the gap and after the
997 // gap.
998 unsigned bestPos = 0;
999 int bestGap = 0;
1000 DEBUG(dbgs() << " dist (" << Uses[0]);
1001 for (unsigned i = 1, e = Uses.size(); i != e; ++i) {
1002 int g = Uses[i-1].distance(Uses[i]);
1003 DEBUG(dbgs() << ") -" << g << "- (" << Uses[i]);
1004 if (g > bestGap)
1005 bestPos = i, bestGap = g;
1006 }
1007 DEBUG(dbgs() << "), best: -" << bestGap << "-\n");
1008
1009 // bestPos points to the first use after the best gap.
1010 assert(bestPos > 0 && "Invalid gap");
1011
1012 // FIXME: Don't create intervals for low densities.
1013
1014 // First interval before the gap. Don't create single-instr intervals.
1015 if (bestPos > 1) {
1016 openIntv();
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +00001017 useIntv(enterIntvBefore(Uses.front()), leaveIntvAfter(Uses[bestPos-1]));
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001018 closeIntv();
1019 }
1020
1021 // Second interval after the gap.
1022 if (bestPos < Uses.size()-1) {
1023 openIntv();
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +00001024 useIntv(enterIntvBefore(Uses[bestPos]), leaveIntvAfter(Uses.back()));
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001025 closeIntv();
1026 }
1027
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001028 finish();
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001029}