blob: 5663936bf3aae501fb0512b3e9be4a8f076c8d91 [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 Olesen1b847de2011-02-19 00:53:42 +000040SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm,
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000041 const LiveIntervals &lis,
42 const MachineLoopInfo &mli)
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000043 : MF(vrm.getMachineFunction()),
44 VRM(vrm),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000045 LIS(lis),
46 Loops(mli),
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000047 TII(*MF.getTarget().getInstrInfo()),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000048 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();
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +000054 LiveBlocks.clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000055 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();
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +000067 for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(CurLI->reg),
68 E = MRI.reg_end(); I != E; ++I) {
69 MachineOperand &MO = I.getOperand();
70 if (MO.isUse() && MO.isUndef())
71 continue;
72 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000073 if (MI->isDebugValue() || !UsingInstrs.insert(MI))
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000074 continue;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000075 UseSlots.push_back(LIS.getInstructionIndex(MI).getDefIndex());
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000076 MachineBasicBlock *MBB = MI->getParent();
Jakob Stoklund Olesen4f5c9d22011-02-09 23:56:18 +000077 UsingBlocks[MBB]++;
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 Olesenf0ac26c2011-02-09 22:50:26 +000080 calcLiveBlockInfo();
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +000081 DEBUG(dbgs() << " counted "
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000082 << UsingInstrs.size() << " instrs, "
Jakob Stoklund Olesen4f5c9d22011-02-09 23:56:18 +000083 << UsingBlocks.size() << " blocks.\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000084}
85
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +000086/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
87/// where CurLI is live.
88void SplitAnalysis::calcLiveBlockInfo() {
89 if (CurLI->empty())
90 return;
91
92 LiveInterval::const_iterator LVI = CurLI->begin();
93 LiveInterval::const_iterator LVE = CurLI->end();
94
95 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
96 UseI = UseSlots.begin();
97 UseE = UseSlots.end();
98
99 // Loop over basic blocks where CurLI is live.
100 MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start);
101 for (;;) {
102 BlockInfo BI;
103 BI.MBB = MFI;
104 SlotIndex Start, Stop;
105 tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
106
107 // The last split point is the latest possible insertion point that dominates
108 // all successor blocks. If interference reaches LastSplitPoint, it is not
109 // possible to insert a split or reload that makes CurLI live in the
110 // outgoing bundle.
111 MachineBasicBlock::iterator LSP = LIS.getLastSplitPoint(*CurLI, BI.MBB);
112 if (LSP == BI.MBB->end())
113 BI.LastSplitPoint = Stop;
114 else
115 BI.LastSplitPoint = LIS.getInstructionIndex(LSP);
116
117 // LVI is the first live segment overlapping MBB.
118 BI.LiveIn = LVI->start <= Start;
119 if (!BI.LiveIn)
120 BI.Def = LVI->start;
121
122 // Find the first and last uses in the block.
123 BI.Uses = hasUses(MFI);
124 if (BI.Uses && UseI != UseE) {
125 BI.FirstUse = *UseI;
126 assert(BI.FirstUse >= Start);
127 do ++UseI;
128 while (UseI != UseE && *UseI < Stop);
129 BI.LastUse = UseI[-1];
130 assert(BI.LastUse < Stop);
131 }
132
133 // Look for gaps in the live range.
134 bool hasGap = false;
135 BI.LiveOut = true;
136 while (LVI->end < Stop) {
137 SlotIndex LastStop = LVI->end;
138 if (++LVI == LVE || LVI->start >= Stop) {
139 BI.Kill = LastStop;
140 BI.LiveOut = false;
141 break;
142 }
143 if (LastStop < LVI->start) {
144 hasGap = true;
145 BI.Kill = LastStop;
146 BI.Def = LVI->start;
147 }
148 }
149
150 // Don't set LiveThrough when the block has a gap.
151 BI.LiveThrough = !hasGap && BI.LiveIn && BI.LiveOut;
152 LiveBlocks.push_back(BI);
153
154 // LVI is now at LVE or LVI->end >= Stop.
155 if (LVI == LVE)
156 break;
157
158 // Live segment ends exactly at Stop. Move to the next segment.
159 if (LVI->end == Stop && ++LVI == LVE)
160 break;
161
162 // Pick the next basic block.
163 if (LVI->start < Stop)
164 ++MFI;
165 else
166 MFI = LIS.getMBBFromIndex(LVI->start);
167 }
168}
169
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000170void SplitAnalysis::print(const BlockPtrSet &B, raw_ostream &OS) const {
171 for (BlockPtrSet::const_iterator I = B.begin(), E = B.end(); I != E; ++I) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000172 unsigned count = UsingBlocks.lookup(*I);
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000173 OS << " BB#" << (*I)->getNumber();
174 if (count)
175 OS << '(' << count << ')';
176 }
177}
178
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000179void SplitAnalysis::analyze(const LiveInterval *li) {
180 clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000181 CurLI = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000182 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000183}
184
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000185
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000186//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000187// LiveIntervalMap
188//===----------------------------------------------------------------------===//
189
Jakob Stoklund Olesenb3e96812010-09-13 21:29:45 +0000190// Work around the fact that the std::pair constructors are broken for pointer
191// pairs in some implementations. makeVV(x, 0) works.
192static inline std::pair<const VNInfo*, VNInfo*>
193makeVV(const VNInfo *a, VNInfo *b) {
194 return std::make_pair(a, b);
195}
196
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000197void LiveIntervalMap::reset(LiveInterval *li) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000198 LI = li;
199 Values.clear();
200 LiveOutCache.clear();
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000201}
202
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000203bool LiveIntervalMap::isComplexMapped(const VNInfo *ParentVNI) const {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000204 ValueMap::const_iterator i = Values.find(ParentVNI);
205 return i != Values.end() && i->second == 0;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000206}
207
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000208// defValue - Introduce a LI def for ParentVNI that could be later than
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000209// ParentVNI->def.
210VNInfo *LiveIntervalMap::defValue(const VNInfo *ParentVNI, SlotIndex Idx) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000211 assert(LI && "call reset first");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000212 assert(ParentVNI && "Mapping NULL value");
213 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000214 assert(ParentLI.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000215
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000216 // Create a new value.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000217 VNInfo *VNI = LI->getNextValue(Idx, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000218
Jakob Stoklund Olesen79c02622010-10-26 22:36:02 +0000219 // Preserve the PHIDef bit.
220 if (ParentVNI->isPHIDef() && Idx == ParentVNI->def)
221 VNI->setIsPHIDef(true);
222
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000223 // Use insert for lookup, so we can add missing values with a second lookup.
224 std::pair<ValueMap::iterator,bool> InsP =
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000225 Values.insert(makeVV(ParentVNI, Idx == ParentVNI->def ? VNI : 0));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000226
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000227 // This is now a complex def. Mark with a NULL in valueMap.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000228 if (!InsP.second)
229 InsP.first->second = 0;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000230
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000231 return VNI;
232}
233
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000234
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000235// mapValue - Find the mapped value for ParentVNI at Idx.
236// Potentially create phi-def values.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000237VNInfo *LiveIntervalMap::mapValue(const VNInfo *ParentVNI, SlotIndex Idx,
238 bool *simple) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000239 assert(LI && "call reset first");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000240 assert(ParentVNI && "Mapping NULL value");
241 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000242 assert(ParentLI.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000243
244 // Use insert for lookup, so we can add missing values with a second lookup.
245 std::pair<ValueMap::iterator,bool> InsP =
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000246 Values.insert(makeVV(ParentVNI, 0));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000247
248 // This was an unknown value. Create a simple mapping.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000249 if (InsP.second) {
250 if (simple) *simple = true;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000251 return InsP.first->second = LI->createValueCopy(ParentVNI,
252 LIS.getVNInfoAllocator());
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000253 }
254
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000255 // This was a simple mapped value.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000256 if (InsP.first->second) {
257 if (simple) *simple = true;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000258 return InsP.first->second;
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000259 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000260
261 // This is a complex mapped value. There may be multiple defs, and we may need
262 // to create phi-defs.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000263 if (simple) *simple = false;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000264 MachineBasicBlock *IdxMBB = LIS.getMBBFromIndex(Idx);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000265 assert(IdxMBB && "No MBB at Idx");
266
267 // Is there a def in the same MBB we can extend?
268 if (VNInfo *VNI = extendTo(IdxMBB, Idx))
269 return VNI;
270
271 // Now for the fun part. We know that ParentVNI potentially has multiple defs,
272 // and we may need to create even more phi-defs to preserve VNInfo SSA form.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000273 // Perform a search for all predecessor blocks where we know the dominating
274 // VNInfo. Insert phi-def VNInfos along the path back to IdxMBB.
275 DEBUG(dbgs() << "\n Reaching defs for BB#" << IdxMBB->getNumber()
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000276 << " at " << Idx << " in " << *LI << '\n');
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000277
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000278 // Blocks where LI should be live-in.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000279 SmallVector<MachineDomTreeNode*, 16> LiveIn;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000280 LiveIn.push_back(MDT[IdxMBB]);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000281
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000282 // Using LiveOutCache as a visited set, perform a BFS for all reaching defs.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000283 for (unsigned i = 0; i != LiveIn.size(); ++i) {
284 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
285 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
286 PE = MBB->pred_end(); PI != PE; ++PI) {
287 MachineBasicBlock *Pred = *PI;
288 // Is this a known live-out block?
289 std::pair<LiveOutMap::iterator,bool> LOIP =
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000290 LiveOutCache.insert(std::make_pair(Pred, LiveOutPair()));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000291 // Yes, we have been here before.
292 if (!LOIP.second) {
Benjamin Kramer8a8e26f2010-10-29 17:40:05 +0000293 DEBUG(if (VNInfo *VNI = LOIP.first->second.first)
294 dbgs() << " known valno #" << VNI->id
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000295 << " at BB#" << Pred->getNumber() << '\n');
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000296 continue;
297 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000298
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000299 // Does Pred provide a live-out value?
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000300 SlotIndex Last = LIS.getMBBEndIdx(Pred).getPrevSlot();
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000301 if (VNInfo *VNI = extendTo(Pred, Last)) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000302 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(VNI->def);
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000303 DEBUG(dbgs() << " found valno #" << VNI->id
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000304 << " from BB#" << DefMBB->getNumber()
305 << " at BB#" << Pred->getNumber() << '\n');
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000306 LiveOutPair &LOP = LOIP.first->second;
307 LOP.first = VNI;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000308 LOP.second = MDT[DefMBB];
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000309 continue;
310 }
311 // No, we need a live-in value for Pred as well
312 if (Pred != IdxMBB)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000313 LiveIn.push_back(MDT[Pred]);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000314 }
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000315 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000316
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000317 // We may need to add phi-def values to preserve the SSA form.
318 // This is essentially the same iterative algorithm that SSAUpdater uses,
319 // except we already have a dominator tree, so we don't have to recompute it.
320 VNInfo *IdxVNI = 0;
321 unsigned Changes;
322 do {
323 Changes = 0;
324 DEBUG(dbgs() << " Iterating over " << LiveIn.size() << " blocks.\n");
325 // Propagate live-out values down the dominator tree, inserting phi-defs when
326 // necessary. Since LiveIn was created by a BFS, going backwards makes it more
327 // likely for us to visit immediate dominators before their children.
328 for (unsigned i = LiveIn.size(); i; --i) {
329 MachineDomTreeNode *Node = LiveIn[i-1];
330 MachineBasicBlock *MBB = Node->getBlock();
331 MachineDomTreeNode *IDom = Node->getIDom();
332 LiveOutPair IDomValue;
333 // We need a live-in value to a block with no immediate dominator?
334 // This is probably an unreachable block that has survived somehow.
335 bool needPHI = !IDom;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000336
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000337 // Get the IDom live-out value.
338 if (!needPHI) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000339 LiveOutMap::iterator I = LiveOutCache.find(IDom->getBlock());
340 if (I != LiveOutCache.end())
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000341 IDomValue = I->second;
342 else
343 // If IDom is outside our set of live-out blocks, there must be new
344 // defs, and we need a phi-def here.
345 needPHI = true;
346 }
Jakob Stoklund Olesen984a7fc2010-10-05 20:36:28 +0000347
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000348 // IDom dominates all of our predecessors, but it may not be the immediate
349 // dominator. Check if any of them have live-out values that are properly
350 // dominated by IDom. If so, we need a phi-def here.
351 if (!needPHI) {
352 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
353 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000354 LiveOutPair Value = LiveOutCache[*PI];
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000355 if (!Value.first || Value.first == IDomValue.first)
356 continue;
357 // This predecessor is carrying something other than IDomValue.
358 // It could be because IDomValue hasn't propagated yet, or it could be
359 // because MBB is in the dominance frontier of that value.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000360 if (MDT.dominates(IDom, Value.second)) {
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000361 needPHI = true;
362 break;
363 }
364 }
365 }
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000366
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000367 // Create a phi-def if required.
368 if (needPHI) {
369 ++Changes;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000370 SlotIndex Start = LIS.getMBBStartIdx(MBB);
371 VNInfo *VNI = LI->getNextValue(Start, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000372 VNI->setIsPHIDef(true);
373 DEBUG(dbgs() << " - BB#" << MBB->getNumber()
374 << " phi-def #" << VNI->id << " at " << Start << '\n');
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000375 // We no longer need LI to be live-in.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000376 LiveIn.erase(LiveIn.begin()+(i-1));
377 // Blocks in LiveIn are either IdxMBB, or have a value live-through.
378 if (MBB == IdxMBB)
379 IdxVNI = VNI;
380 // Check if we need to update live-out info.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000381 LiveOutMap::iterator I = LiveOutCache.find(MBB);
382 if (I == LiveOutCache.end() || I->second.second == Node) {
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000383 // We already have a live-out defined in MBB, so this must be IdxMBB.
384 assert(MBB == IdxMBB && "Adding phi-def to known live-out");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000385 LI->addRange(LiveRange(Start, Idx.getNextSlot(), VNI));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000386 } else {
387 // This phi-def is also live-out, so color the whole block.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000388 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000389 I->second = LiveOutPair(VNI, Node);
390 }
391 } else if (IDomValue.first) {
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000392 // No phi-def here. Remember incoming value for IdxMBB.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000393 if (MBB == IdxMBB)
394 IdxVNI = IDomValue.first;
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000395 // Propagate IDomValue if needed:
396 // MBB is live-out and doesn't define its own value.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000397 LiveOutMap::iterator I = LiveOutCache.find(MBB);
398 if (I != LiveOutCache.end() && I->second.second != Node &&
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000399 I->second.first != IDomValue.first) {
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000400 ++Changes;
401 I->second = IDomValue;
402 DEBUG(dbgs() << " - BB#" << MBB->getNumber()
403 << " idom valno #" << IDomValue.first->id
404 << " from BB#" << IDom->getBlock()->getNumber() << '\n');
405 }
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000406 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000407 }
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000408 DEBUG(dbgs() << " - made " << Changes << " changes.\n");
409 } while (Changes);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000410
411 assert(IdxVNI && "Didn't find value for Idx");
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000412
413#ifndef NDEBUG
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000414 // Check the LiveOutCache invariants.
415 for (LiveOutMap::iterator I = LiveOutCache.begin(), E = LiveOutCache.end();
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000416 I != E; ++I) {
417 assert(I->first && "Null MBB entry in cache");
418 assert(I->second.first && "Null VNInfo in cache");
419 assert(I->second.second && "Null DomTreeNode in cache");
420 if (I->second.second->getBlock() == I->first)
421 continue;
422 for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(),
423 PE = I->first->pred_end(); PI != PE; ++PI)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000424 assert(LiveOutCache.lookup(*PI) == I->second && "Bad invariant");
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000425 }
426#endif
427
428 // Since we went through the trouble of a full BFS visiting all reaching defs,
429 // the values in LiveIn are now accurate. No more phi-defs are needed
430 // for these blocks, so we can color the live ranges.
431 // This makes the next mapValue call much faster.
432 for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) {
433 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000434 SlotIndex Start = LIS.getMBBStartIdx(MBB);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000435 VNInfo *VNI = LiveOutCache.lookup(MBB).first;
Jakob Stoklund Olesen08eb8dd2011-02-03 20:29:36 +0000436
437 // Anything in LiveIn other than IdxMBB is live-through.
438 // In IdxMBB, we should stop at Idx unless the same value is live-out.
439 if (MBB == IdxMBB && IdxVNI != VNI)
440 LI->addRange(LiveRange(Start, Idx.getNextSlot(), IdxVNI));
441 else
442 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000443 }
444
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000445 return IdxVNI;
446}
447
Jakob Stoklund Olesend7ca5772011-01-20 17:45:20 +0000448#ifndef NDEBUG
449void LiveIntervalMap::dumpCache() {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000450 for (LiveOutMap::iterator I = LiveOutCache.begin(), E = LiveOutCache.end();
Jakob Stoklund Olesend7ca5772011-01-20 17:45:20 +0000451 I != E; ++I) {
452 assert(I->first && "Null MBB entry in cache");
453 assert(I->second.first && "Null VNInfo in cache");
454 assert(I->second.second && "Null DomTreeNode in cache");
455 dbgs() << " cache: BB#" << I->first->getNumber()
456 << " has valno #" << I->second.first->id << " from BB#"
457 << I->second.second->getBlock()->getNumber() << ", preds";
458 for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(),
459 PE = I->first->pred_end(); PI != PE; ++PI)
460 dbgs() << " BB#" << (*PI)->getNumber();
461 dbgs() << '\n';
462 }
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000463 dbgs() << " cache: " << LiveOutCache.size() << " entries.\n";
Jakob Stoklund Olesend7ca5772011-01-20 17:45:20 +0000464}
465#endif
466
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000467// extendTo - Find the last LI value defined in MBB at or before Idx. The
468// ParentLI is assumed to be live at Idx. Extend the live range to Idx.
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000469// Return the found VNInfo, or NULL.
Jakob Stoklund Olesenc95c1462010-10-27 00:39:07 +0000470VNInfo *LiveIntervalMap::extendTo(const MachineBasicBlock *MBB, SlotIndex Idx) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000471 assert(LI && "call reset first");
472 LiveInterval::iterator I = std::upper_bound(LI->begin(), LI->end(), Idx);
473 if (I == LI->begin())
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000474 return 0;
475 --I;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000476 if (I->end <= LIS.getMBBStartIdx(MBB))
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000477 return 0;
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000478 if (I->end <= Idx)
479 I->end = Idx.getNextSlot();
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000480 return I->valno;
481}
482
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000483// addSimpleRange - Add a simple range from ParentLI to LI.
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000484// ParentVNI must be live in the [Start;End) interval.
485void LiveIntervalMap::addSimpleRange(SlotIndex Start, SlotIndex End,
486 const VNInfo *ParentVNI) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000487 assert(LI && "call reset first");
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000488 bool simple;
489 VNInfo *VNI = mapValue(ParentVNI, Start, &simple);
490 // A simple mapping is easy.
491 if (simple) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000492 LI->addRange(LiveRange(Start, End, VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000493 return;
494 }
495
496 // ParentVNI is a complex value. We must map per MBB.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000497 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
498 MachineFunction::iterator MBBE = LIS.getMBBFromIndex(End.getPrevSlot());
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000499
500 if (MBB == MBBE) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000501 LI->addRange(LiveRange(Start, End, VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000502 return;
503 }
504
505 // First block.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000506 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000507
508 // Run sequence of full blocks.
509 for (++MBB; MBB != MBBE; ++MBB) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000510 Start = LIS.getMBBStartIdx(MBB);
511 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB),
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000512 mapValue(ParentVNI, Start)));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000513 }
514
515 // Final block.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000516 Start = LIS.getMBBStartIdx(MBB);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000517 if (Start != End)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000518 LI->addRange(LiveRange(Start, End, mapValue(ParentVNI, Start)));
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000519}
520
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000521/// addRange - Add live ranges to LI where [Start;End) intersects ParentLI.
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000522/// All needed values whose def is not inside [Start;End) must be defined
523/// beforehand so mapValue will work.
524void LiveIntervalMap::addRange(SlotIndex Start, SlotIndex End) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000525 assert(LI && "call reset first");
526 LiveInterval::const_iterator B = ParentLI.begin(), E = ParentLI.end();
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000527 LiveInterval::const_iterator I = std::lower_bound(B, E, Start);
528
529 // Check if --I begins before Start and overlaps.
530 if (I != B) {
531 --I;
532 if (I->end > Start)
533 addSimpleRange(Start, std::min(End, I->end), I->valno);
534 ++I;
535 }
536
537 // The remaining ranges begin after Start.
538 for (;I != E && I->start < End; ++I)
539 addSimpleRange(I->start, std::min(End, I->end), I->valno);
540}
541
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000542
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000543//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000544// Split Editor
545//===----------------------------------------------------------------------===//
546
547/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000548SplitEditor::SplitEditor(SplitAnalysis &sa,
549 LiveIntervals &lis,
550 VirtRegMap &vrm,
551 MachineDominatorTree &mdt,
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000552 LiveRangeEdit &edit)
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000553 : SA(sa), LIS(lis), VRM(vrm),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000554 MRI(vrm.getMachineFunction().getRegInfo()),
Eric Christopher0f438112011-02-03 06:18:29 +0000555 MDT(mdt),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000556 TII(*vrm.getMachineFunction().getTarget().getInstrInfo()),
557 TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()),
558 Edit(edit),
Eric Christopher0f438112011-02-03 06:18:29 +0000559 OpenIdx(0),
560 RegAssign(Allocator)
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000561{
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000562 // We don't need an AliasAnalysis since we will only be performing
563 // cheap-as-a-copy remats anyway.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000564 Edit.anyRematerializable(LIS, TII, 0);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000565}
566
Eric Christopher0f438112011-02-03 06:18:29 +0000567void SplitEditor::dump() const {
568 if (RegAssign.empty()) {
569 dbgs() << " empty\n";
570 return;
571 }
572
573 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
574 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
575 dbgs() << '\n';
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000576}
577
Eric Christopher0f438112011-02-03 06:18:29 +0000578VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000579 VNInfo *ParentVNI,
580 SlotIndex UseIdx,
581 MachineBasicBlock &MBB,
582 MachineBasicBlock::iterator I) {
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000583 MachineInstr *CopyMI = 0;
584 SlotIndex Def;
Eric Christopher0f438112011-02-03 06:18:29 +0000585 LiveInterval *LI = Edit.get(RegIdx);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000586
587 // Attempt cheap-as-a-copy rematerialization.
588 LiveRangeEdit::Remat RM(ParentVNI);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000589 if (Edit.canRematerializeAt(RM, UseIdx, true, LIS)) {
Eric Christopher0f438112011-02-03 06:18:29 +0000590 Def = Edit.rematerializeAt(MBB, I, LI->reg, RM, LIS, TII, TRI);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000591 } else {
592 // Can't remat, just insert a copy from parent.
Eric Christopher0f438112011-02-03 06:18:29 +0000593 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
594 .addReg(Edit.getReg());
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000595 Def = LIS.InsertMachineInstrInMaps(CopyMI).getDefIndex();
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000596 }
597
598 // Define the value in Reg.
Eric Christopher0f438112011-02-03 06:18:29 +0000599 VNInfo *VNI = LIMappers[RegIdx].defValue(ParentVNI, Def);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000600 VNI->setCopy(CopyMI);
601
602 // Add minimal liveness for the new value.
Eric Christopher0f438112011-02-03 06:18:29 +0000603 Edit.get(RegIdx)->addRange(LiveRange(Def, Def.getNextSlot(), VNI));
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000604 return VNI;
605}
606
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000607/// Create a new virtual register and live interval.
608void SplitEditor::openIntv() {
Eric Christopher0f438112011-02-03 06:18:29 +0000609 assert(!OpenIdx && "Previous LI not closed before openIntv");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000610
Eric Christopher0f438112011-02-03 06:18:29 +0000611 // Create the complement as index 0.
612 if (Edit.empty()) {
613 Edit.create(MRI, LIS, VRM);
614 LIMappers.push_back(LiveIntervalMap(LIS, MDT, Edit.getParent()));
615 LIMappers.back().reset(Edit.get(0));
616 }
617
618 // Create the open interval.
619 OpenIdx = Edit.size();
620 Edit.create(MRI, LIS, VRM);
621 LIMappers.push_back(LiveIntervalMap(LIS, MDT, Edit.getParent()));
622 LIMappers[OpenIdx].reset(Edit.get(OpenIdx));
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000623}
624
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000625SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000626 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000627 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000628 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000629 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000630 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000631 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000632 return Idx;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000633 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000634 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000635 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000636 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000637
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000638 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
639 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000640}
641
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000642SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000643 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000644 SlotIndex End = LIS.getMBBEndIdx(&MBB);
645 SlotIndex Last = End.getPrevSlot();
646 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
647 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Last);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000648 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000649 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000650 return End;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000651 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000652 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000653 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesencb640472011-02-04 19:33:11 +0000654 LIS.getLastSplitPoint(Edit.getParent(), &MBB));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000655 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopher0f438112011-02-03 06:18:29 +0000656 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000657 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000658}
659
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000660/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000661void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000662 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000663}
664
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000665void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopher0f438112011-02-03 06:18:29 +0000666 assert(OpenIdx && "openIntv not called before useIntv");
667 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
668 RegAssign.insert(Start, End, OpenIdx);
669 DEBUG(dump());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000670}
671
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000672SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000673 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000674 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000675
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000676 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000677 Idx = Idx.getBoundaryIndex();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000678 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000679 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000680 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000681 return Idx.getNextSlot();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000682 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000683 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000684
Jakob Stoklund Olesen01cb34b2011-02-08 18:50:18 +0000685 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
686 assert(MI && "No instruction at index");
687 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(),
688 llvm::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000689 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000690}
691
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000692SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
693 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
694 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
695
696 // The interval must be live into the instruction at Idx.
697 Idx = Idx.getBoundaryIndex();
698 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx);
699 if (!ParentVNI) {
700 DEBUG(dbgs() << ": not live\n");
701 return Idx.getNextSlot();
702 }
703 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
704
705 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
706 assert(MI && "No instruction at index");
707 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
708 return VNI->def;
709}
710
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000711SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000712 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000713 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000714 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000715
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000716 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Start);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000717 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000718 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000719 return Start;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000720 }
721
Eric Christopher0f438112011-02-03 06:18:29 +0000722 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000723 MBB.SkipPHIsAndLabels(MBB.begin()));
Eric Christopher0f438112011-02-03 06:18:29 +0000724 RegAssign.insert(Start, VNI->def, OpenIdx);
725 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000726 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000727}
728
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000729void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
730 assert(OpenIdx && "openIntv not called before overlapIntv");
731 assert(Edit.getParent().getVNInfoAt(Start) ==
732 Edit.getParent().getVNInfoAt(End.getPrevSlot()) &&
733 "Parent changes value in extended range");
734 assert(Edit.get(0)->getVNInfoAt(Start) && "Start must come from leaveIntv*");
735 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
736 "Range cannot span basic blocks");
737
738 // Treat this as useIntv() for now. The complement interval will be extended
739 // as needed by mapValue().
740 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
741 RegAssign.insert(Start, End, OpenIdx);
742 DEBUG(dump());
743}
744
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000745/// closeIntv - Indicate that we are done editing the currently open
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000746/// LiveInterval, and ranges can be trimmed.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000747void SplitEditor::closeIntv() {
Eric Christopher0f438112011-02-03 06:18:29 +0000748 assert(OpenIdx && "openIntv not called before closeIntv");
749 OpenIdx = 0;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000750}
751
Eric Christopher0f438112011-02-03 06:18:29 +0000752/// rewriteAssigned - Rewrite all uses of Edit.getReg().
753void SplitEditor::rewriteAssigned() {
754 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit.getReg()),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000755 RE = MRI.reg_end(); RI != RE;) {
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000756 MachineOperand &MO = RI.getOperand();
757 MachineInstr *MI = MO.getParent();
758 ++RI;
Eric Christopher0f438112011-02-03 06:18:29 +0000759 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000760 if (MI->isDebugValue()) {
761 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000762 MO.setReg(0);
763 continue;
764 }
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +0000765
766 // <undef> operands don't really read the register, so just assign them to
767 // the complement.
768 if (MO.isUse() && MO.isUndef()) {
769 MO.setReg(Edit.get(0)->reg);
770 continue;
771 }
772
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000773 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000774 Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
Eric Christopher0f438112011-02-03 06:18:29 +0000775
776 // Rewrite to the mapped register at Idx.
777 unsigned RegIdx = RegAssign.lookup(Idx);
778 MO.setReg(Edit.get(RegIdx)->reg);
779 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
780 << Idx << ':' << RegIdx << '\t' << *MI);
781
782 // Extend liveness to Idx.
783 const VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx);
784 LIMappers[RegIdx].mapValue(ParentVNI, Idx);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000785 }
786}
787
Eric Christopher0f438112011-02-03 06:18:29 +0000788/// rewriteSplit - Rewrite uses of Intvs[0] according to the ConEQ mapping.
789void SplitEditor::rewriteComponents(const SmallVectorImpl<LiveInterval*> &Intvs,
790 const ConnectedVNInfoEqClasses &ConEq) {
791 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Intvs[0]->reg),
792 RE = MRI.reg_end(); RI != RE;) {
793 MachineOperand &MO = RI.getOperand();
794 MachineInstr *MI = MO.getParent();
795 ++RI;
796 if (MO.isUse() && MO.isUndef())
Jakob Stoklund Olesen9d999772010-10-21 18:47:08 +0000797 continue;
Eric Christopher0f438112011-02-03 06:18:29 +0000798 // DBG_VALUE instructions should have been eliminated earlier.
799 SlotIndex Idx = LIS.getInstructionIndex(MI);
800 Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
801 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
802 << Idx << ':');
803 const VNInfo *VNI = Intvs[0]->getVNInfoAt(Idx);
804 assert(VNI && "Interval not live at use.");
805 MO.setReg(Intvs[ConEq.getEqClass(VNI)]->reg);
806 DEBUG(dbgs() << VNI->id << '\t' << *MI);
Eric Christopher463a2972011-02-03 05:40:54 +0000807 }
Eric Christopher463a2972011-02-03 05:40:54 +0000808}
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000809
Eric Christopher463a2972011-02-03 05:40:54 +0000810void SplitEditor::finish() {
Eric Christopher0f438112011-02-03 06:18:29 +0000811 assert(OpenIdx == 0 && "Previous LI not closed before rewrite");
Eric Christopher463a2972011-02-03 05:40:54 +0000812
Eric Christopher0f438112011-02-03 06:18:29 +0000813 // At this point, the live intervals in Edit contain VNInfos corresponding to
814 // the inserted copies.
815
816 // Add the original defs from the parent interval.
817 for (LiveInterval::const_vni_iterator I = Edit.getParent().vni_begin(),
818 E = Edit.getParent().vni_end(); I != E; ++I) {
819 const VNInfo *ParentVNI = *I;
Jakob Stoklund Olesen9ecd1e72011-02-04 00:59:23 +0000820 if (ParentVNI->isUnused())
821 continue;
Eric Christopher0f438112011-02-03 06:18:29 +0000822 LiveIntervalMap &LIM = LIMappers[RegAssign.lookup(ParentVNI->def)];
823 VNInfo *VNI = LIM.defValue(ParentVNI, ParentVNI->def);
824 LIM.getLI()->addRange(LiveRange(ParentVNI->def,
825 ParentVNI->def.getNextSlot(), VNI));
826 // Mark all values as complex to force liveness computation.
827 // This should really only be necessary for remat victims, but we are lazy.
828 LIM.markComplexMapped(ParentVNI);
829 }
830
831#ifndef NDEBUG
832 // Every new interval must have a def by now, otherwise the split is bogus.
833 for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I)
834 assert((*I)->hasAtLeastOneValue() && "Split interval has no value");
835#endif
836
837 // FIXME: Don't recompute the liveness of all values, infer it from the
838 // overlaps between the parent live interval and RegAssign.
839 // The mapValue algorithm is only necessary when:
840 // - The parent value maps to multiple defs, and new phis are needed, or
841 // - The value has been rematerialized before some uses, and we want to
842 // minimize the live range so it only reaches the remaining uses.
843 // All other values have simple liveness that can be computed from RegAssign
844 // and the parent live interval.
845
846 // Extend live ranges to be live-out for successor PHI values.
847 for (LiveInterval::const_vni_iterator I = Edit.getParent().vni_begin(),
848 E = Edit.getParent().vni_end(); I != E; ++I) {
849 const VNInfo *PHIVNI = *I;
Jakob Stoklund Olesen9ecd1e72011-02-04 00:59:23 +0000850 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
Eric Christopher0f438112011-02-03 06:18:29 +0000851 continue;
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000852 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
853 LiveIntervalMap &LIM = LIMappers[RegIdx];
Eric Christopher0f438112011-02-03 06:18:29 +0000854 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000855 DEBUG(dbgs() << " map phi in BB#" << MBB->getNumber() << '@' << PHIVNI->def
856 << " -> " << RegIdx << '\n');
Eric Christopher0f438112011-02-03 06:18:29 +0000857 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
858 PE = MBB->pred_end(); PI != PE; ++PI) {
859 SlotIndex End = LIS.getMBBEndIdx(*PI).getPrevSlot();
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000860 DEBUG(dbgs() << " pred BB#" << (*PI)->getNumber() << '@' << End);
Eric Christopher0f438112011-02-03 06:18:29 +0000861 // The predecessor may not have a live-out value. That is OK, like an
862 // undef PHI operand.
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000863 if (VNInfo *VNI = Edit.getParent().getVNInfoAt(End)) {
864 DEBUG(dbgs() << " has parent valno #" << VNI->id << " live out\n");
865 assert(RegAssign.lookup(End) == RegIdx &&
866 "Different register assignment in phi predecessor");
Eric Christopher0f438112011-02-03 06:18:29 +0000867 LIM.mapValue(VNI, End);
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000868 }
869 else
870 DEBUG(dbgs() << " is not live-out\n");
Eric Christopher0f438112011-02-03 06:18:29 +0000871 }
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000872 DEBUG(dbgs() << " " << *LIM.getLI() << '\n');
Eric Christopher0f438112011-02-03 06:18:29 +0000873 }
874
875 // Rewrite instructions.
876 rewriteAssigned();
877
878 // FIXME: Delete defs that were rematted everywhere.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000879
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000880 // Get rid of unused values and set phi-kill flags.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000881 for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I)
882 (*I)->RenumberValues(LIS);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000883
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000884 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000885 ConnectedVNInfoEqClasses ConEQ(LIS);
886 for (unsigned i = 0, e = Edit.size(); i != e; ++i) {
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000887 // Don't use iterators, they are invalidated by create() below.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000888 LiveInterval *li = Edit.get(i);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000889 unsigned NumComp = ConEQ.Classify(li);
890 if (NumComp <= 1)
891 continue;
892 DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n');
893 SmallVector<LiveInterval*, 8> dups;
894 dups.push_back(li);
895 for (unsigned i = 1; i != NumComp; ++i)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000896 dups.push_back(&Edit.create(MRI, LIS, VRM));
Eric Christopher0f438112011-02-03 06:18:29 +0000897 rewriteComponents(dups, ConEQ);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000898 ConEQ.Distribute(&dups[0]);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000899 }
900
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000901 // Calculate spill weight and allocation hints for new intervals.
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000902 VirtRegAuxInfo vrai(VRM.getMachineFunction(), LIS, SA.Loops);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000903 for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I){
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000904 LiveInterval &li = **I;
Jakob Stoklund Olesen9db3ea42010-08-10 18:37:40 +0000905 vrai.CalculateRegClass(li.reg);
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000906 vrai.CalculateWeightAndHint(li);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000907 DEBUG(dbgs() << " new interval " << MRI.getRegClass(li.reg)->getName()
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000908 << ":" << li << '\n');
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000909 }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000910}
911
912
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000913//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000914// Single Block Splitting
915//===----------------------------------------------------------------------===//
916
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000917/// getMultiUseBlocks - if CurLI has more than one use in a basic block, it
918/// may be an advantage to split CurLI for the duration of the block.
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000919bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000920 // If CurLI is local to one block, there is no point to splitting it.
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000921 if (LiveBlocks.size() <= 1)
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000922 return false;
923 // Add blocks with multiple uses.
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000924 for (unsigned i = 0, e = LiveBlocks.size(); i != e; ++i) {
925 const BlockInfo &BI = LiveBlocks[i];
926 if (!BI.Uses)
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000927 continue;
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000928 unsigned Instrs = UsingBlocks.lookup(BI.MBB);
929 if (Instrs <= 1)
930 continue;
931 if (Instrs == 2 && BI.LiveIn && BI.LiveOut && !BI.LiveThrough)
932 continue;
933 Blocks.insert(BI.MBB);
934 }
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000935 return !Blocks.empty();
936}
937
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000938/// splitSingleBlocks - Split CurLI into a separate live interval inside each
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000939/// basic block in Blocks.
940void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) {
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000941 DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n");
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000942
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000943 for (unsigned i = 0, e = SA.LiveBlocks.size(); i != e; ++i) {
944 const SplitAnalysis::BlockInfo &BI = SA.LiveBlocks[i];
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000945 if (!BI.Uses || !Blocks.count(BI.MBB))
946 continue;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000947
948 openIntv();
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000949 SlotIndex SegStart = enterIntvBefore(BI.FirstUse);
950 if (BI.LastUse < BI.LastSplitPoint) {
951 useIntv(SegStart, leaveIntvAfter(BI.LastUse));
952 } else {
953 // THe last use os after tha last valid split point.
954 SlotIndex SegStop = leaveIntvBefore(BI.LastSplitPoint);
955 useIntv(SegStart, SegStop);
956 overlapIntv(SegStop, BI.LastUse);
957 }
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000958 closeIntv();
959 }
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000960 finish();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000961}
962
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000963
964//===----------------------------------------------------------------------===//
965// Sub Block Splitting
966//===----------------------------------------------------------------------===//
967
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000968/// getBlockForInsideSplit - If CurLI is contained inside a single basic block,
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000969/// and it wou pay to subdivide the interval inside that block, return it.
970/// Otherwise return NULL. The returned block can be passed to
971/// SplitEditor::splitInsideBlock.
972const MachineBasicBlock *SplitAnalysis::getBlockForInsideSplit() {
973 // The interval must be exclusive to one block.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000974 if (UsingBlocks.size() != 1)
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000975 return 0;
976 // Don't to this for less than 4 instructions. We want to be sure that
977 // splitting actually reduces the instruction count per interval.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000978 if (UsingInstrs.size() < 4)
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000979 return 0;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000980 return UsingBlocks.begin()->first;
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000981}
982
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000983/// splitInsideBlock - Split CurLI into multiple intervals inside MBB.
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000984void SplitEditor::splitInsideBlock(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000985 SmallVector<SlotIndex, 32> Uses;
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000986 Uses.reserve(SA.UsingInstrs.size());
987 for (SplitAnalysis::InstrPtrSet::const_iterator I = SA.UsingInstrs.begin(),
988 E = SA.UsingInstrs.end(); I != E; ++I)
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000989 if ((*I)->getParent() == MBB)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000990 Uses.push_back(LIS.getInstructionIndex(*I));
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000991 DEBUG(dbgs() << " splitInsideBlock BB#" << MBB->getNumber() << " for "
992 << Uses.size() << " instructions.\n");
993 assert(Uses.size() >= 3 && "Need at least 3 instructions");
994 array_pod_sort(Uses.begin(), Uses.end());
995
996 // Simple algorithm: Find the largest gap between uses as determined by slot
997 // indices. Create new intervals for instructions before the gap and after the
998 // gap.
999 unsigned bestPos = 0;
1000 int bestGap = 0;
1001 DEBUG(dbgs() << " dist (" << Uses[0]);
1002 for (unsigned i = 1, e = Uses.size(); i != e; ++i) {
1003 int g = Uses[i-1].distance(Uses[i]);
1004 DEBUG(dbgs() << ") -" << g << "- (" << Uses[i]);
1005 if (g > bestGap)
1006 bestPos = i, bestGap = g;
1007 }
1008 DEBUG(dbgs() << "), best: -" << bestGap << "-\n");
1009
1010 // bestPos points to the first use after the best gap.
1011 assert(bestPos > 0 && "Invalid gap");
1012
1013 // FIXME: Don't create intervals for low densities.
1014
1015 // First interval before the gap. Don't create single-instr intervals.
1016 if (bestPos > 1) {
1017 openIntv();
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +00001018 useIntv(enterIntvBefore(Uses.front()), leaveIntvAfter(Uses[bestPos-1]));
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001019 closeIntv();
1020 }
1021
1022 // Second interval after the gap.
1023 if (bestPos < Uses.size()-1) {
1024 openIntv();
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +00001025 useIntv(enterIntvBefore(Uses[bestPos]), leaveIntvAfter(Uses.back()));
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001026 closeIntv();
1027 }
1028
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001029 finish();
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +00001030}