blob: 3a5f50214ca3d9e7fd0d48ad2a3c1ec9f5b7ae7d [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 Olesen06c0f252011-02-21 23:09:46 +0000170bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
171 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
172 const LiveInterval &Orig = LIS.getInterval(OrigReg);
173 assert(!Orig.empty() && "Splitting empty interval?");
174 LiveInterval::const_iterator I = Orig.find(Idx);
175
176 // Range containing Idx should begin at Idx.
177 if (I != Orig.end() && I->start <= Idx)
178 return I->start == Idx;
179
180 // Range does not contain Idx, previous must end at Idx.
181 return I != Orig.begin() && (--I)->end == Idx;
182}
183
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000184void SplitAnalysis::print(const BlockPtrSet &B, raw_ostream &OS) const {
185 for (BlockPtrSet::const_iterator I = B.begin(), E = B.end(); I != E; ++I) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000186 unsigned count = UsingBlocks.lookup(*I);
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000187 OS << " BB#" << (*I)->getNumber();
188 if (count)
189 OS << '(' << count << ')';
190 }
191}
192
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000193void SplitAnalysis::analyze(const LiveInterval *li) {
194 clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000195 CurLI = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000196 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000197}
198
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000199
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000200//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000201// LiveIntervalMap
202//===----------------------------------------------------------------------===//
203
Jakob Stoklund Olesenb3e96812010-09-13 21:29:45 +0000204// Work around the fact that the std::pair constructors are broken for pointer
205// pairs in some implementations. makeVV(x, 0) works.
206static inline std::pair<const VNInfo*, VNInfo*>
207makeVV(const VNInfo *a, VNInfo *b) {
208 return std::make_pair(a, b);
209}
210
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000211void LiveIntervalMap::reset(LiveInterval *li) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000212 LI = li;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000213 LiveOutCache.clear();
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000214}
215
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000216
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000217// mapValue - Find the mapped value for ParentVNI at Idx.
218// Potentially create phi-def values.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000219VNInfo *LiveIntervalMap::mapValue(const VNInfo *ParentVNI, SlotIndex Idx,
220 bool *simple) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000221 assert(LI && "call reset first");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000222 assert(ParentVNI && "Mapping NULL value");
223 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000224 assert(ParentLI.getVNInfoAt(Idx) == ParentVNI && "Bad ParentVNI");
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000225
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000226 // This is a complex mapped value. There may be multiple defs, and we may need
227 // to create phi-defs.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000228 if (simple) *simple = false;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000229 MachineBasicBlock *IdxMBB = LIS.getMBBFromIndex(Idx);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000230 assert(IdxMBB && "No MBB at Idx");
231
232 // Is there a def in the same MBB we can extend?
Jakob Stoklund Olesen9763e2b2011-03-02 00:06:15 +0000233 if (VNInfo *VNI = LI->extendInBlock(LIS.getMBBStartIdx(IdxMBB), Idx))
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000234 return VNI;
235
236 // Now for the fun part. We know that ParentVNI potentially has multiple defs,
237 // and we may need to create even more phi-defs to preserve VNInfo SSA form.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000238 // Perform a search for all predecessor blocks where we know the dominating
239 // VNInfo. Insert phi-def VNInfos along the path back to IdxMBB.
240 DEBUG(dbgs() << "\n Reaching defs for BB#" << IdxMBB->getNumber()
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000241 << " at " << Idx << " in " << *LI << '\n');
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000242
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000243 // Blocks where LI should be live-in.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000244 SmallVector<MachineDomTreeNode*, 16> LiveIn;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000245 LiveIn.push_back(MDT[IdxMBB]);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000246
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000247 // Using LiveOutCache as a visited set, perform a BFS for all reaching defs.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000248 for (unsigned i = 0; i != LiveIn.size(); ++i) {
249 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
250 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
251 PE = MBB->pred_end(); PI != PE; ++PI) {
252 MachineBasicBlock *Pred = *PI;
253 // Is this a known live-out block?
254 std::pair<LiveOutMap::iterator,bool> LOIP =
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000255 LiveOutCache.insert(std::make_pair(Pred, LiveOutPair()));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000256 // Yes, we have been here before.
257 if (!LOIP.second) {
Benjamin Kramer8a8e26f2010-10-29 17:40:05 +0000258 DEBUG(if (VNInfo *VNI = LOIP.first->second.first)
259 dbgs() << " known valno #" << VNI->id
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000260 << " at BB#" << Pred->getNumber() << '\n');
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000261 continue;
262 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000263
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000264 // Does Pred provide a live-out value?
Jakob Stoklund Olesen9763e2b2011-03-02 00:06:15 +0000265 SlotIndex Start, Last;
266 tie(Start, Last) = LIS.getSlotIndexes()->getMBBRange(Pred);
267 Last = Last.getPrevSlot();
268 if (VNInfo *VNI = LI->extendInBlock(Start, Last)) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000269 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(VNI->def);
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000270 DEBUG(dbgs() << " found valno #" << VNI->id
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000271 << " from BB#" << DefMBB->getNumber()
272 << " at BB#" << Pred->getNumber() << '\n');
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000273 LiveOutPair &LOP = LOIP.first->second;
274 LOP.first = VNI;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000275 LOP.second = MDT[DefMBB];
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000276 continue;
277 }
278 // No, we need a live-in value for Pred as well
279 if (Pred != IdxMBB)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000280 LiveIn.push_back(MDT[Pred]);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000281 }
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000282 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000283
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000284 // We may need to add phi-def values to preserve the SSA form.
285 // This is essentially the same iterative algorithm that SSAUpdater uses,
286 // except we already have a dominator tree, so we don't have to recompute it.
287 VNInfo *IdxVNI = 0;
288 unsigned Changes;
289 do {
290 Changes = 0;
291 DEBUG(dbgs() << " Iterating over " << LiveIn.size() << " blocks.\n");
292 // Propagate live-out values down the dominator tree, inserting phi-defs when
293 // necessary. Since LiveIn was created by a BFS, going backwards makes it more
294 // likely for us to visit immediate dominators before their children.
295 for (unsigned i = LiveIn.size(); i; --i) {
296 MachineDomTreeNode *Node = LiveIn[i-1];
297 MachineBasicBlock *MBB = Node->getBlock();
298 MachineDomTreeNode *IDom = Node->getIDom();
299 LiveOutPair IDomValue;
300 // We need a live-in value to a block with no immediate dominator?
301 // This is probably an unreachable block that has survived somehow.
302 bool needPHI = !IDom;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000303
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000304 // Get the IDom live-out value.
305 if (!needPHI) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000306 LiveOutMap::iterator I = LiveOutCache.find(IDom->getBlock());
307 if (I != LiveOutCache.end())
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000308 IDomValue = I->second;
309 else
310 // If IDom is outside our set of live-out blocks, there must be new
311 // defs, and we need a phi-def here.
312 needPHI = true;
313 }
Jakob Stoklund Olesen984a7fc2010-10-05 20:36:28 +0000314
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000315 // IDom dominates all of our predecessors, but it may not be the immediate
316 // dominator. Check if any of them have live-out values that are properly
317 // dominated by IDom. If so, we need a phi-def here.
318 if (!needPHI) {
319 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
320 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000321 LiveOutPair Value = LiveOutCache[*PI];
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000322 if (!Value.first || Value.first == IDomValue.first)
323 continue;
324 // This predecessor is carrying something other than IDomValue.
325 // It could be because IDomValue hasn't propagated yet, or it could be
326 // because MBB is in the dominance frontier of that value.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000327 if (MDT.dominates(IDom, Value.second)) {
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000328 needPHI = true;
329 break;
330 }
331 }
332 }
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000333
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000334 // Create a phi-def if required.
335 if (needPHI) {
336 ++Changes;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000337 SlotIndex Start = LIS.getMBBStartIdx(MBB);
338 VNInfo *VNI = LI->getNextValue(Start, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000339 VNI->setIsPHIDef(true);
340 DEBUG(dbgs() << " - BB#" << MBB->getNumber()
341 << " phi-def #" << VNI->id << " at " << Start << '\n');
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000342 // We no longer need LI to be live-in.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000343 LiveIn.erase(LiveIn.begin()+(i-1));
344 // Blocks in LiveIn are either IdxMBB, or have a value live-through.
345 if (MBB == IdxMBB)
346 IdxVNI = VNI;
347 // Check if we need to update live-out info.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000348 LiveOutMap::iterator I = LiveOutCache.find(MBB);
349 if (I == LiveOutCache.end() || I->second.second == Node) {
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000350 // We already have a live-out defined in MBB, so this must be IdxMBB.
351 assert(MBB == IdxMBB && "Adding phi-def to known live-out");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000352 LI->addRange(LiveRange(Start, Idx.getNextSlot(), VNI));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000353 } else {
354 // This phi-def is also live-out, so color the whole block.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000355 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000356 I->second = LiveOutPair(VNI, Node);
357 }
358 } else if (IDomValue.first) {
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000359 // No phi-def here. Remember incoming value for IdxMBB.
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000360 if (MBB == IdxMBB)
361 IdxVNI = IDomValue.first;
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000362 // Propagate IDomValue if needed:
363 // MBB is live-out and doesn't define its own value.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000364 LiveOutMap::iterator I = LiveOutCache.find(MBB);
365 if (I != LiveOutCache.end() && I->second.second != Node &&
Jakob Stoklund Olesenc94fcb12010-10-29 17:37:25 +0000366 I->second.first != IDomValue.first) {
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000367 ++Changes;
368 I->second = IDomValue;
369 DEBUG(dbgs() << " - BB#" << MBB->getNumber()
370 << " idom valno #" << IDomValue.first->id
371 << " from BB#" << IDom->getBlock()->getNumber() << '\n');
372 }
Jakob Stoklund Olesenff3ae862010-08-18 20:29:53 +0000373 }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000374 }
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000375 DEBUG(dbgs() << " - made " << Changes << " changes.\n");
376 } while (Changes);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000377
378 assert(IdxVNI && "Didn't find value for Idx");
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000379
380#ifndef NDEBUG
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000381 // Check the LiveOutCache invariants.
382 for (LiveOutMap::iterator I = LiveOutCache.begin(), E = LiveOutCache.end();
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000383 I != E; ++I) {
384 assert(I->first && "Null MBB entry in cache");
385 assert(I->second.first && "Null VNInfo in cache");
386 assert(I->second.second && "Null DomTreeNode in cache");
387 if (I->second.second->getBlock() == I->first)
388 continue;
389 for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(),
390 PE = I->first->pred_end(); PI != PE; ++PI)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000391 assert(LiveOutCache.lookup(*PI) == I->second && "Bad invariant");
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000392 }
393#endif
394
395 // Since we went through the trouble of a full BFS visiting all reaching defs,
396 // the values in LiveIn are now accurate. No more phi-defs are needed
397 // for these blocks, so we can color the live ranges.
398 // This makes the next mapValue call much faster.
399 for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) {
400 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000401 SlotIndex Start = LIS.getMBBStartIdx(MBB);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000402 VNInfo *VNI = LiveOutCache.lookup(MBB).first;
Jakob Stoklund Olesen08eb8dd2011-02-03 20:29:36 +0000403
404 // Anything in LiveIn other than IdxMBB is live-through.
405 // In IdxMBB, we should stop at Idx unless the same value is live-out.
406 if (MBB == IdxMBB && IdxVNI != VNI)
407 LI->addRange(LiveRange(Start, Idx.getNextSlot(), IdxVNI));
408 else
409 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesene1dde7b2010-10-28 20:34:52 +0000410 }
411
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000412 return IdxVNI;
413}
414
Jakob Stoklund Olesend7ca5772011-01-20 17:45:20 +0000415#ifndef NDEBUG
416void LiveIntervalMap::dumpCache() {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000417 for (LiveOutMap::iterator I = LiveOutCache.begin(), E = LiveOutCache.end();
Jakob Stoklund Olesend7ca5772011-01-20 17:45:20 +0000418 I != E; ++I) {
419 assert(I->first && "Null MBB entry in cache");
420 assert(I->second.first && "Null VNInfo in cache");
421 assert(I->second.second && "Null DomTreeNode in cache");
422 dbgs() << " cache: BB#" << I->first->getNumber()
423 << " has valno #" << I->second.first->id << " from BB#"
424 << I->second.second->getBlock()->getNumber() << ", preds";
425 for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(),
426 PE = I->first->pred_end(); PI != PE; ++PI)
427 dbgs() << " BB#" << (*PI)->getNumber();
428 dbgs() << '\n';
429 }
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000430 dbgs() << " cache: " << LiveOutCache.size() << " entries.\n";
Jakob Stoklund Olesend7ca5772011-01-20 17:45:20 +0000431}
432#endif
433
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000434
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000435//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000436// Split Editor
437//===----------------------------------------------------------------------===//
438
439/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000440SplitEditor::SplitEditor(SplitAnalysis &sa,
441 LiveIntervals &lis,
442 VirtRegMap &vrm,
443 MachineDominatorTree &mdt,
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000444 LiveRangeEdit &edit)
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000445 : SA(sa), LIS(lis), VRM(vrm),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000446 MRI(vrm.getMachineFunction().getRegInfo()),
Eric Christopher0f438112011-02-03 06:18:29 +0000447 MDT(mdt),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000448 TII(*vrm.getMachineFunction().getTarget().getInstrInfo()),
449 TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()),
450 Edit(edit),
Eric Christopher0f438112011-02-03 06:18:29 +0000451 OpenIdx(0),
452 RegAssign(Allocator)
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000453{
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000454 // We don't need an AliasAnalysis since we will only be performing
455 // cheap-as-a-copy remats anyway.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000456 Edit.anyRematerializable(LIS, TII, 0);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000457}
458
Eric Christopher0f438112011-02-03 06:18:29 +0000459void SplitEditor::dump() const {
460 if (RegAssign.empty()) {
461 dbgs() << " empty\n";
462 return;
463 }
464
465 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
466 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
467 dbgs() << '\n';
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000468}
469
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000470VNInfo *SplitEditor::defValue(unsigned RegIdx,
471 const VNInfo *ParentVNI,
472 SlotIndex Idx) {
473 assert(ParentVNI && "Mapping NULL value");
474 assert(Idx.isValid() && "Invalid SlotIndex");
475 assert(Edit.getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
476 LiveInterval *LI = Edit.get(RegIdx);
477
478 // Create a new value.
479 VNInfo *VNI = LI->getNextValue(Idx, 0, LIS.getVNInfoAllocator());
480
481 // Preserve the PHIDef bit.
482 if (ParentVNI->isPHIDef() && Idx == ParentVNI->def)
483 VNI->setIsPHIDef(true);
484
485 // Use insert for lookup, so we can add missing values with a second lookup.
486 std::pair<ValueMap::iterator, bool> InsP =
487 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), VNI));
488
489 // This was the first time (RegIdx, ParentVNI) was mapped.
490 // Keep it as a simple def without any liveness.
491 if (InsP.second)
492 return VNI;
493
494 // If the previous value was a simple mapping, add liveness for it now.
495 if (VNInfo *OldVNI = InsP.first->second) {
496 SlotIndex Def = OldVNI->def;
497 LI->addRange(LiveRange(Def, Def.getNextSlot(), OldVNI));
498 // No longer a simple mapping.
499 InsP.first->second = 0;
500 }
501
502 // This is a complex mapping, add liveness for VNI
503 SlotIndex Def = VNI->def;
504 LI->addRange(LiveRange(Def, Def.getNextSlot(), VNI));
505
506 return VNI;
507}
508
509void SplitEditor::markComplexMapped(unsigned RegIdx, const VNInfo *ParentVNI) {
510 assert(ParentVNI && "Mapping NULL value");
511 VNInfo *&VNI = Values[std::make_pair(RegIdx, ParentVNI->id)];
512
513 // ParentVNI was either unmapped or already complex mapped. Either way.
514 if (!VNI)
515 return;
516
517 // This was previously a single mapping. Make sure the old def is represented
518 // by a trivial live range.
519 SlotIndex Def = VNI->def;
520 Edit.get(RegIdx)->addRange(LiveRange(Def, Def.getNextSlot(), VNI));
521 VNI = 0;
522}
523
Eric Christopher0f438112011-02-03 06:18:29 +0000524VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000525 VNInfo *ParentVNI,
526 SlotIndex UseIdx,
527 MachineBasicBlock &MBB,
528 MachineBasicBlock::iterator I) {
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000529 MachineInstr *CopyMI = 0;
530 SlotIndex Def;
Eric Christopher0f438112011-02-03 06:18:29 +0000531 LiveInterval *LI = Edit.get(RegIdx);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000532
533 // Attempt cheap-as-a-copy rematerialization.
534 LiveRangeEdit::Remat RM(ParentVNI);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000535 if (Edit.canRematerializeAt(RM, UseIdx, true, LIS)) {
Eric Christopher0f438112011-02-03 06:18:29 +0000536 Def = Edit.rematerializeAt(MBB, I, LI->reg, RM, LIS, TII, TRI);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000537 } else {
538 // Can't remat, just insert a copy from parent.
Eric Christopher0f438112011-02-03 06:18:29 +0000539 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
540 .addReg(Edit.getReg());
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000541 Def = LIS.InsertMachineInstrInMaps(CopyMI).getDefIndex();
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000542 }
543
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000544 // Temporarily mark all values as complex mapped.
545 markComplexMapped(RegIdx, ParentVNI);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000546
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000547 // Define the value in Reg.
548 VNInfo *VNI = defValue(RegIdx, ParentVNI, Def);
549 VNI->setCopy(CopyMI);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000550 return VNI;
551}
552
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000553/// Create a new virtual register and live interval.
554void SplitEditor::openIntv() {
Eric Christopher0f438112011-02-03 06:18:29 +0000555 assert(!OpenIdx && "Previous LI not closed before openIntv");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000556
Eric Christopher0f438112011-02-03 06:18:29 +0000557 // Create the complement as index 0.
558 if (Edit.empty()) {
559 Edit.create(MRI, LIS, VRM);
560 LIMappers.push_back(LiveIntervalMap(LIS, MDT, Edit.getParent()));
561 LIMappers.back().reset(Edit.get(0));
562 }
563
564 // Create the open interval.
565 OpenIdx = Edit.size();
566 Edit.create(MRI, LIS, VRM);
567 LIMappers.push_back(LiveIntervalMap(LIS, MDT, Edit.getParent()));
568 LIMappers[OpenIdx].reset(Edit.get(OpenIdx));
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000569}
570
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000571SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000572 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000573 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000574 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000575 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000576 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000577 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000578 return Idx;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000579 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000580 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000581 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000582 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000583
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000584 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
585 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000586}
587
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000588SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000589 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000590 SlotIndex End = LIS.getMBBEndIdx(&MBB);
591 SlotIndex Last = End.getPrevSlot();
592 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
593 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Last);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000594 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000595 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000596 return End;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000597 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000598 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000599 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesencb640472011-02-04 19:33:11 +0000600 LIS.getLastSplitPoint(Edit.getParent(), &MBB));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000601 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopher0f438112011-02-03 06:18:29 +0000602 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000603 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000604}
605
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000606/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000607void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000608 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000609}
610
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000611void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopher0f438112011-02-03 06:18:29 +0000612 assert(OpenIdx && "openIntv not called before useIntv");
613 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
614 RegAssign.insert(Start, End, OpenIdx);
615 DEBUG(dump());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000616}
617
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000618SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000619 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000620 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000621
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000622 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000623 Idx = Idx.getBoundaryIndex();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000624 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000625 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000626 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000627 return Idx.getNextSlot();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000628 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000629 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000630
Jakob Stoklund Olesen01cb34b2011-02-08 18:50:18 +0000631 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
632 assert(MI && "No instruction at index");
633 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(),
634 llvm::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000635 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000636}
637
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000638SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
639 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
640 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
641
642 // The interval must be live into the instruction at Idx.
643 Idx = Idx.getBoundaryIndex();
644 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx);
645 if (!ParentVNI) {
646 DEBUG(dbgs() << ": not live\n");
647 return Idx.getNextSlot();
648 }
649 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
650
651 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
652 assert(MI && "No instruction at index");
653 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
654 return VNI->def;
655}
656
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000657SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000658 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000659 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000660 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000661
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000662 VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Start);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000663 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000664 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000665 return Start;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000666 }
667
Eric Christopher0f438112011-02-03 06:18:29 +0000668 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000669 MBB.SkipPHIsAndLabels(MBB.begin()));
Eric Christopher0f438112011-02-03 06:18:29 +0000670 RegAssign.insert(Start, VNI->def, OpenIdx);
671 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000672 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000673}
674
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000675void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
676 assert(OpenIdx && "openIntv not called before overlapIntv");
677 assert(Edit.getParent().getVNInfoAt(Start) ==
678 Edit.getParent().getVNInfoAt(End.getPrevSlot()) &&
679 "Parent changes value in extended range");
680 assert(Edit.get(0)->getVNInfoAt(Start) && "Start must come from leaveIntv*");
681 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
682 "Range cannot span basic blocks");
683
684 // Treat this as useIntv() for now. The complement interval will be extended
685 // as needed by mapValue().
686 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
687 RegAssign.insert(Start, End, OpenIdx);
688 DEBUG(dump());
689}
690
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000691/// closeIntv - Indicate that we are done editing the currently open
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000692/// LiveInterval, and ranges can be trimmed.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000693void SplitEditor::closeIntv() {
Eric Christopher0f438112011-02-03 06:18:29 +0000694 assert(OpenIdx && "openIntv not called before closeIntv");
695 OpenIdx = 0;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000696}
697
Eric Christopher0f438112011-02-03 06:18:29 +0000698/// rewriteAssigned - Rewrite all uses of Edit.getReg().
699void SplitEditor::rewriteAssigned() {
700 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit.getReg()),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000701 RE = MRI.reg_end(); RI != RE;) {
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000702 MachineOperand &MO = RI.getOperand();
703 MachineInstr *MI = MO.getParent();
704 ++RI;
Eric Christopher0f438112011-02-03 06:18:29 +0000705 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000706 if (MI->isDebugValue()) {
707 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000708 MO.setReg(0);
709 continue;
710 }
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +0000711
712 // <undef> operands don't really read the register, so just assign them to
713 // the complement.
714 if (MO.isUse() && MO.isUndef()) {
715 MO.setReg(Edit.get(0)->reg);
716 continue;
717 }
718
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000719 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000720 Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
Eric Christopher0f438112011-02-03 06:18:29 +0000721
722 // Rewrite to the mapped register at Idx.
723 unsigned RegIdx = RegAssign.lookup(Idx);
724 MO.setReg(Edit.get(RegIdx)->reg);
725 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
726 << Idx << ':' << RegIdx << '\t' << *MI);
727
728 // Extend liveness to Idx.
729 const VNInfo *ParentVNI = Edit.getParent().getVNInfoAt(Idx);
730 LIMappers[RegIdx].mapValue(ParentVNI, Idx);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000731 }
732}
733
Eric Christopher0f438112011-02-03 06:18:29 +0000734/// rewriteSplit - Rewrite uses of Intvs[0] according to the ConEQ mapping.
735void SplitEditor::rewriteComponents(const SmallVectorImpl<LiveInterval*> &Intvs,
736 const ConnectedVNInfoEqClasses &ConEq) {
737 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Intvs[0]->reg),
738 RE = MRI.reg_end(); RI != RE;) {
739 MachineOperand &MO = RI.getOperand();
740 MachineInstr *MI = MO.getParent();
741 ++RI;
742 if (MO.isUse() && MO.isUndef())
Jakob Stoklund Olesen9d999772010-10-21 18:47:08 +0000743 continue;
Eric Christopher0f438112011-02-03 06:18:29 +0000744 // DBG_VALUE instructions should have been eliminated earlier.
745 SlotIndex Idx = LIS.getInstructionIndex(MI);
746 Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
747 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
748 << Idx << ':');
749 const VNInfo *VNI = Intvs[0]->getVNInfoAt(Idx);
750 assert(VNI && "Interval not live at use.");
751 MO.setReg(Intvs[ConEq.getEqClass(VNI)]->reg);
752 DEBUG(dbgs() << VNI->id << '\t' << *MI);
Eric Christopher463a2972011-02-03 05:40:54 +0000753 }
Eric Christopher463a2972011-02-03 05:40:54 +0000754}
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000755
Eric Christopher463a2972011-02-03 05:40:54 +0000756void SplitEditor::finish() {
Eric Christopher0f438112011-02-03 06:18:29 +0000757 assert(OpenIdx == 0 && "Previous LI not closed before rewrite");
Eric Christopher463a2972011-02-03 05:40:54 +0000758
Eric Christopher0f438112011-02-03 06:18:29 +0000759 // At this point, the live intervals in Edit contain VNInfos corresponding to
760 // the inserted copies.
761
762 // Add the original defs from the parent interval.
763 for (LiveInterval::const_vni_iterator I = Edit.getParent().vni_begin(),
764 E = Edit.getParent().vni_end(); I != E; ++I) {
765 const VNInfo *ParentVNI = *I;
Jakob Stoklund Olesen9ecd1e72011-02-04 00:59:23 +0000766 if (ParentVNI->isUnused())
767 continue;
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000768 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Eric Christopher0f438112011-02-03 06:18:29 +0000769 // Mark all values as complex to force liveness computation.
770 // This should really only be necessary for remat victims, but we are lazy.
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000771 markComplexMapped(RegIdx, ParentVNI);
772 defValue(RegIdx, ParentVNI, ParentVNI->def);
Eric Christopher0f438112011-02-03 06:18:29 +0000773 }
774
775#ifndef NDEBUG
776 // Every new interval must have a def by now, otherwise the split is bogus.
777 for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I)
778 assert((*I)->hasAtLeastOneValue() && "Split interval has no value");
779#endif
780
781 // FIXME: Don't recompute the liveness of all values, infer it from the
782 // overlaps between the parent live interval and RegAssign.
783 // The mapValue algorithm is only necessary when:
784 // - The parent value maps to multiple defs, and new phis are needed, or
785 // - The value has been rematerialized before some uses, and we want to
786 // minimize the live range so it only reaches the remaining uses.
787 // All other values have simple liveness that can be computed from RegAssign
788 // and the parent live interval.
789
790 // Extend live ranges to be live-out for successor PHI values.
791 for (LiveInterval::const_vni_iterator I = Edit.getParent().vni_begin(),
792 E = Edit.getParent().vni_end(); I != E; ++I) {
793 const VNInfo *PHIVNI = *I;
Jakob Stoklund Olesen9ecd1e72011-02-04 00:59:23 +0000794 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
Eric Christopher0f438112011-02-03 06:18:29 +0000795 continue;
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000796 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
797 LiveIntervalMap &LIM = LIMappers[RegIdx];
Eric Christopher0f438112011-02-03 06:18:29 +0000798 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000799 DEBUG(dbgs() << " map phi in BB#" << MBB->getNumber() << '@' << PHIVNI->def
800 << " -> " << RegIdx << '\n');
Eric Christopher0f438112011-02-03 06:18:29 +0000801 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
802 PE = MBB->pred_end(); PI != PE; ++PI) {
803 SlotIndex End = LIS.getMBBEndIdx(*PI).getPrevSlot();
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000804 DEBUG(dbgs() << " pred BB#" << (*PI)->getNumber() << '@' << End);
Eric Christopher0f438112011-02-03 06:18:29 +0000805 // The predecessor may not have a live-out value. That is OK, like an
806 // undef PHI operand.
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000807 if (VNInfo *VNI = Edit.getParent().getVNInfoAt(End)) {
808 DEBUG(dbgs() << " has parent valno #" << VNI->id << " live out\n");
809 assert(RegAssign.lookup(End) == RegIdx &&
810 "Different register assignment in phi predecessor");
Eric Christopher0f438112011-02-03 06:18:29 +0000811 LIM.mapValue(VNI, End);
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000812 }
813 else
814 DEBUG(dbgs() << " is not live-out\n");
Eric Christopher0f438112011-02-03 06:18:29 +0000815 }
Jakob Stoklund Olesenc50f0772011-02-03 20:29:39 +0000816 DEBUG(dbgs() << " " << *LIM.getLI() << '\n');
Eric Christopher0f438112011-02-03 06:18:29 +0000817 }
818
819 // Rewrite instructions.
820 rewriteAssigned();
821
822 // FIXME: Delete defs that were rematted everywhere.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000823
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000824 // Get rid of unused values and set phi-kill flags.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000825 for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I)
826 (*I)->RenumberValues(LIS);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000827
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000828 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000829 ConnectedVNInfoEqClasses ConEQ(LIS);
830 for (unsigned i = 0, e = Edit.size(); i != e; ++i) {
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000831 // Don't use iterators, they are invalidated by create() below.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000832 LiveInterval *li = Edit.get(i);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000833 unsigned NumComp = ConEQ.Classify(li);
834 if (NumComp <= 1)
835 continue;
836 DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n');
837 SmallVector<LiveInterval*, 8> dups;
838 dups.push_back(li);
839 for (unsigned i = 1; i != NumComp; ++i)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000840 dups.push_back(&Edit.create(MRI, LIS, VRM));
Eric Christopher0f438112011-02-03 06:18:29 +0000841 rewriteComponents(dups, ConEQ);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000842 ConEQ.Distribute(&dups[0]);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000843 }
844
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000845 // Calculate spill weight and allocation hints for new intervals.
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000846 VirtRegAuxInfo vrai(VRM.getMachineFunction(), LIS, SA.Loops);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000847 for (LiveRangeEdit::iterator I = Edit.begin(), E = Edit.end(); I != E; ++I){
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000848 LiveInterval &li = **I;
Jakob Stoklund Olesen9db3ea42010-08-10 18:37:40 +0000849 vrai.CalculateRegClass(li.reg);
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000850 vrai.CalculateWeightAndHint(li);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000851 DEBUG(dbgs() << " new interval " << MRI.getRegClass(li.reg)->getName()
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000852 << ":" << li << '\n');
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000853 }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000854}
855
856
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000857//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000858// Single Block Splitting
859//===----------------------------------------------------------------------===//
860
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000861/// getMultiUseBlocks - if CurLI has more than one use in a basic block, it
862/// may be an advantage to split CurLI for the duration of the block.
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000863bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000864 // If CurLI is local to one block, there is no point to splitting it.
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000865 if (LiveBlocks.size() <= 1)
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000866 return false;
867 // Add blocks with multiple uses.
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000868 for (unsigned i = 0, e = LiveBlocks.size(); i != e; ++i) {
869 const BlockInfo &BI = LiveBlocks[i];
870 if (!BI.Uses)
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000871 continue;
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000872 unsigned Instrs = UsingBlocks.lookup(BI.MBB);
873 if (Instrs <= 1)
874 continue;
875 if (Instrs == 2 && BI.LiveIn && BI.LiveOut && !BI.LiveThrough)
876 continue;
877 Blocks.insert(BI.MBB);
878 }
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000879 return !Blocks.empty();
880}
881
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000882/// splitSingleBlocks - Split CurLI into a separate live interval inside each
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000883/// basic block in Blocks.
884void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) {
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000885 DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n");
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000886
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000887 for (unsigned i = 0, e = SA.LiveBlocks.size(); i != e; ++i) {
888 const SplitAnalysis::BlockInfo &BI = SA.LiveBlocks[i];
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000889 if (!BI.Uses || !Blocks.count(BI.MBB))
890 continue;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000891
892 openIntv();
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000893 SlotIndex SegStart = enterIntvBefore(BI.FirstUse);
Jakob Stoklund Olesenc70f6872011-02-23 18:26:31 +0000894 if (!BI.LiveOut || BI.LastUse < BI.LastSplitPoint) {
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000895 useIntv(SegStart, leaveIntvAfter(BI.LastUse));
896 } else {
Jakob Stoklund Olesenc70f6872011-02-23 18:26:31 +0000897 // The last use is after the last valid split point.
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000898 SlotIndex SegStop = leaveIntvBefore(BI.LastSplitPoint);
899 useIntv(SegStart, SegStop);
900 overlapIntv(SegStop, BI.LastUse);
901 }
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000902 closeIntv();
903 }
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000904 finish();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000905}