blob: 3974cb3ef67db51ad940114b8a5012fbd2edebc8 [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 Olesen46703532011-03-02 23:05:19 +000019#include "llvm/ADT/Statistic.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
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000036STATISTIC(NumFinished, "Number of splits finished");
37STATISTIC(NumSimple, "Number of splits that were simple");
38
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000039//===----------------------------------------------------------------------===//
40// Split Analysis
41//===----------------------------------------------------------------------===//
42
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000043SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm,
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000044 const LiveIntervals &lis,
45 const MachineLoopInfo &mli)
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000046 : MF(vrm.getMachineFunction()),
47 VRM(vrm),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000048 LIS(lis),
49 Loops(mli),
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000050 TII(*MF.getTarget().getInstrInfo()),
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000051 CurLI(0),
52 LastSplitPoint(MF.getNumBlockIDs()) {}
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000053
54void SplitAnalysis::clear() {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000055 UseSlots.clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000056 UsingInstrs.clear();
57 UsingBlocks.clear();
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +000058 LiveBlocks.clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000059 CurLI = 0;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000060}
61
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000062SlotIndex SplitAnalysis::computeLastSplitPoint(unsigned Num) {
63 const MachineBasicBlock *MBB = MF.getBlockNumbered(Num);
64 const MachineBasicBlock *LPad = MBB->getLandingPadSuccessor();
65 std::pair<SlotIndex, SlotIndex> &LSP = LastSplitPoint[Num];
66
67 // Compute split points on the first call. The pair is independent of the
68 // current live interval.
69 if (!LSP.first.isValid()) {
70 MachineBasicBlock::const_iterator FirstTerm = MBB->getFirstTerminator();
71 if (FirstTerm == MBB->end())
72 LSP.first = LIS.getMBBEndIdx(MBB);
73 else
74 LSP.first = LIS.getInstructionIndex(FirstTerm);
75
76 // If there is a landing pad successor, also find the call instruction.
77 if (!LPad)
78 return LSP.first;
79 // There may not be a call instruction (?) in which case we ignore LPad.
80 LSP.second = LSP.first;
81 for (MachineBasicBlock::const_iterator I = FirstTerm, E = MBB->begin();
82 I != E; --I)
83 if (I->getDesc().isCall()) {
84 LSP.second = LIS.getInstructionIndex(I);
85 break;
86 }
87 }
88
89 // If CurLI is live into a landing pad successor, move the last split point
90 // back to the call that may throw.
91 if (LPad && LSP.second.isValid() && !LIS.isLiveInToMBB(*CurLI, LPad))
92 return LSP.second;
93 else
94 return LSP.first;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000095}
96
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000097/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +000098void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000099 const MachineRegisterInfo &MRI = MF.getRegInfo();
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +0000100 for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(CurLI->reg),
101 E = MRI.reg_end(); I != E; ++I) {
102 MachineOperand &MO = I.getOperand();
103 if (MO.isUse() && MO.isUndef())
104 continue;
105 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000106 if (MI->isDebugValue() || !UsingInstrs.insert(MI))
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000107 continue;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000108 UseSlots.push_back(LIS.getInstructionIndex(MI).getDefIndex());
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000109 MachineBasicBlock *MBB = MI->getParent();
Jakob Stoklund Olesen4f5c9d22011-02-09 23:56:18 +0000110 UsingBlocks[MBB]++;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000111 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000112 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000113
114 // Compute per-live block info.
115 if (!calcLiveBlockInfo()) {
116 // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
117 // I am looking at you, SimpleRegisterCoalescing!
118 DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
119 const_cast<LiveIntervals&>(LIS)
120 .shrinkToUses(const_cast<LiveInterval*>(CurLI));
121 LiveBlocks.clear();
122 bool fixed = calcLiveBlockInfo();
123 (void)fixed;
124 assert(fixed && "Couldn't fix broken live interval");
125 }
126
Jakob Stoklund Olesenef1f5cc2011-03-27 22:49:23 +0000127 DEBUG(dbgs() << "Analyze counted "
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000128 << UsingInstrs.size() << " instrs, "
Jakob Stoklund Olesenef1f5cc2011-03-27 22:49:23 +0000129 << UsingBlocks.size() << " blocks, "
130 << LiveBlocks.size() << " spanned.\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000131}
132
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000133/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
134/// where CurLI is live.
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000135bool SplitAnalysis::calcLiveBlockInfo() {
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000136 if (CurLI->empty())
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000137 return true;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000138
139 LiveInterval::const_iterator LVI = CurLI->begin();
140 LiveInterval::const_iterator LVE = CurLI->end();
141
142 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
143 UseI = UseSlots.begin();
144 UseE = UseSlots.end();
145
146 // Loop over basic blocks where CurLI is live.
147 MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start);
148 for (;;) {
149 BlockInfo BI;
150 BI.MBB = MFI;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000151 SlotIndex Start, Stop;
152 tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000153
154 // The last split point is the latest possible insertion point that dominates
155 // all successor blocks. If interference reaches LastSplitPoint, it is not
156 // possible to insert a split or reload that makes CurLI live in the
157 // outgoing bundle.
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +0000158 BI.LastSplitPoint = getLastSplitPoint(BI.MBB->getNumber());
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000159
160 // LVI is the first live segment overlapping MBB.
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000161 BI.LiveIn = LVI->start <= Start;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000162 if (!BI.LiveIn)
163 BI.Def = LVI->start;
164
165 // Find the first and last uses in the block.
166 BI.Uses = hasUses(MFI);
167 if (BI.Uses && UseI != UseE) {
168 BI.FirstUse = *UseI;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000169 assert(BI.FirstUse >= Start);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000170 do ++UseI;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000171 while (UseI != UseE && *UseI < Stop);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000172 BI.LastUse = UseI[-1];
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000173 assert(BI.LastUse < Stop);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000174 }
175
176 // Look for gaps in the live range.
177 bool hasGap = false;
178 BI.LiveOut = true;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000179 while (LVI->end < Stop) {
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000180 SlotIndex LastStop = LVI->end;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000181 if (++LVI == LVE || LVI->start >= Stop) {
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000182 BI.Kill = LastStop;
183 BI.LiveOut = false;
184 break;
185 }
186 if (LastStop < LVI->start) {
187 hasGap = true;
188 BI.Kill = LastStop;
189 BI.Def = LVI->start;
190 }
191 }
192
193 // Don't set LiveThrough when the block has a gap.
194 BI.LiveThrough = !hasGap && BI.LiveIn && BI.LiveOut;
195 LiveBlocks.push_back(BI);
196
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000197 // FIXME: This should never happen. The live range stops or starts without a
198 // corresponding use. An earlier pass did something wrong.
199 if (!BI.LiveThrough && !BI.Uses)
200 return false;
201
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000202 // LVI is now at LVE or LVI->end >= Stop.
203 if (LVI == LVE)
204 break;
205
206 // Live segment ends exactly at Stop. Move to the next segment.
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000207 if (LVI->end == Stop && ++LVI == LVE)
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000208 break;
209
210 // Pick the next basic block.
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000211 if (LVI->start < Stop)
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000212 ++MFI;
213 else
214 MFI = LIS.getMBBFromIndex(LVI->start);
215 }
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000216 return true;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000217}
218
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000219bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
220 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
221 const LiveInterval &Orig = LIS.getInterval(OrigReg);
222 assert(!Orig.empty() && "Splitting empty interval?");
223 LiveInterval::const_iterator I = Orig.find(Idx);
224
225 // Range containing Idx should begin at Idx.
226 if (I != Orig.end() && I->start <= Idx)
227 return I->start == Idx;
228
229 // Range does not contain Idx, previous must end at Idx.
230 return I != Orig.begin() && (--I)->end == Idx;
231}
232
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000233void SplitAnalysis::print(const BlockPtrSet &B, raw_ostream &OS) const {
234 for (BlockPtrSet::const_iterator I = B.begin(), E = B.end(); I != E; ++I) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000235 unsigned count = UsingBlocks.lookup(*I);
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000236 OS << " BB#" << (*I)->getNumber();
237 if (count)
238 OS << '(' << count << ')';
239 }
240}
241
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000242void SplitAnalysis::analyze(const LiveInterval *li) {
243 clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000244 CurLI = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000245 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000246}
247
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000248
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000249//===----------------------------------------------------------------------===//
250// Split Editor
251//===----------------------------------------------------------------------===//
252
253/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000254SplitEditor::SplitEditor(SplitAnalysis &sa,
255 LiveIntervals &lis,
256 VirtRegMap &vrm,
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000257 MachineDominatorTree &mdt)
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000258 : SA(sa), LIS(lis), VRM(vrm),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000259 MRI(vrm.getMachineFunction().getRegInfo()),
Eric Christopher0f438112011-02-03 06:18:29 +0000260 MDT(mdt),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000261 TII(*vrm.getMachineFunction().getTarget().getInstrInfo()),
262 TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()),
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000263 Edit(0),
Eric Christopher0f438112011-02-03 06:18:29 +0000264 OpenIdx(0),
265 RegAssign(Allocator)
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000266{}
267
268void SplitEditor::reset(LiveRangeEdit &lre) {
269 Edit = &lre;
270 OpenIdx = 0;
271 RegAssign.clear();
272 Values.clear();
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000273
274 // We don't need to clear LiveOutCache, only LiveOutSeen entries are read.
275 LiveOutSeen.clear();
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000276
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000277 // We don't need an AliasAnalysis since we will only be performing
278 // cheap-as-a-copy remats anyway.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000279 Edit->anyRematerializable(LIS, TII, 0);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000280}
281
Eric Christopher0f438112011-02-03 06:18:29 +0000282void SplitEditor::dump() const {
283 if (RegAssign.empty()) {
284 dbgs() << " empty\n";
285 return;
286 }
287
288 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
289 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
290 dbgs() << '\n';
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000291}
292
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000293VNInfo *SplitEditor::defValue(unsigned RegIdx,
294 const VNInfo *ParentVNI,
295 SlotIndex Idx) {
296 assert(ParentVNI && "Mapping NULL value");
297 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000298 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
299 LiveInterval *LI = Edit->get(RegIdx);
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000300
301 // Create a new value.
302 VNInfo *VNI = LI->getNextValue(Idx, 0, LIS.getVNInfoAllocator());
303
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000304 // Use insert for lookup, so we can add missing values with a second lookup.
305 std::pair<ValueMap::iterator, bool> InsP =
306 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), VNI));
307
308 // This was the first time (RegIdx, ParentVNI) was mapped.
309 // Keep it as a simple def without any liveness.
310 if (InsP.second)
311 return VNI;
312
313 // If the previous value was a simple mapping, add liveness for it now.
314 if (VNInfo *OldVNI = InsP.first->second) {
315 SlotIndex Def = OldVNI->def;
316 LI->addRange(LiveRange(Def, Def.getNextSlot(), OldVNI));
317 // No longer a simple mapping.
318 InsP.first->second = 0;
319 }
320
321 // This is a complex mapping, add liveness for VNI
322 SlotIndex Def = VNI->def;
323 LI->addRange(LiveRange(Def, Def.getNextSlot(), VNI));
324
325 return VNI;
326}
327
328void SplitEditor::markComplexMapped(unsigned RegIdx, const VNInfo *ParentVNI) {
329 assert(ParentVNI && "Mapping NULL value");
330 VNInfo *&VNI = Values[std::make_pair(RegIdx, ParentVNI->id)];
331
332 // ParentVNI was either unmapped or already complex mapped. Either way.
333 if (!VNI)
334 return;
335
336 // This was previously a single mapping. Make sure the old def is represented
337 // by a trivial live range.
338 SlotIndex Def = VNI->def;
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000339 Edit->get(RegIdx)->addRange(LiveRange(Def, Def.getNextSlot(), VNI));
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000340 VNI = 0;
341}
342
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000343// extendRange - Extend the live range to reach Idx.
344// Potentially create phi-def values.
345void SplitEditor::extendRange(unsigned RegIdx, SlotIndex Idx) {
346 assert(Idx.isValid() && "Invalid SlotIndex");
347 MachineBasicBlock *IdxMBB = LIS.getMBBFromIndex(Idx);
348 assert(IdxMBB && "No MBB at Idx");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000349 LiveInterval *LI = Edit->get(RegIdx);
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000350
351 // Is there a def in the same MBB we can extend?
352 if (LI->extendInBlock(LIS.getMBBStartIdx(IdxMBB), Idx))
353 return;
354
355 // Now for the fun part. We know that ParentVNI potentially has multiple defs,
356 // and we may need to create even more phi-defs to preserve VNInfo SSA form.
357 // Perform a search for all predecessor blocks where we know the dominating
358 // VNInfo. Insert phi-def VNInfos along the path back to IdxMBB.
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000359
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000360 // Initialize the live-out cache the first time it is needed.
361 if (LiveOutSeen.empty()) {
362 unsigned N = VRM.getMachineFunction().getNumBlockIDs();
363 LiveOutSeen.resize(N);
364 LiveOutCache.resize(N);
365 }
366
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000367 // Blocks where LI should be live-in.
368 SmallVector<MachineDomTreeNode*, 16> LiveIn;
369 LiveIn.push_back(MDT[IdxMBB]);
370
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000371 // Remember if we have seen more than one value.
372 bool UniqueVNI = true;
373 VNInfo *IdxVNI = 0;
374
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000375 // Using LiveOutCache as a visited set, perform a BFS for all reaching defs.
376 for (unsigned i = 0; i != LiveIn.size(); ++i) {
377 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +0000378 assert(!MBB->pred_empty() && "Value live-in to entry block?");
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000379 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
380 PE = MBB->pred_end(); PI != PE; ++PI) {
381 MachineBasicBlock *Pred = *PI;
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000382 LiveOutPair &LOP = LiveOutCache[Pred];
383
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000384 // Is this a known live-out block?
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000385 if (LiveOutSeen.test(Pred->getNumber())) {
386 if (VNInfo *VNI = LOP.first) {
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000387 if (IdxVNI && IdxVNI != VNI)
388 UniqueVNI = false;
389 IdxVNI = VNI;
390 }
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000391 continue;
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000392 }
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000393
394 // First time. LOP is garbage and must be cleared below.
395 LiveOutSeen.set(Pred->getNumber());
396
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000397 // Does Pred provide a live-out value?
398 SlotIndex Start, Last;
399 tie(Start, Last) = LIS.getSlotIndexes()->getMBBRange(Pred);
400 Last = Last.getPrevSlot();
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000401 VNInfo *VNI = LI->extendInBlock(Start, Last);
402 LOP.first = VNI;
403 if (VNI) {
404 LOP.second = MDT[LIS.getMBBFromIndex(VNI->def)];
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000405 if (IdxVNI && IdxVNI != VNI)
406 UniqueVNI = false;
407 IdxVNI = VNI;
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000408 continue;
409 }
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000410 LOP.second = 0;
411
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000412 // No, we need a live-in value for Pred as well
413 if (Pred != IdxMBB)
414 LiveIn.push_back(MDT[Pred]);
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000415 else
416 UniqueVNI = false; // Loopback to IdxMBB, ask updateSSA() for help.
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000417 }
418 }
419
420 // We may need to add phi-def values to preserve the SSA form.
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000421 if (UniqueVNI) {
422 LiveOutPair LOP(IdxVNI, MDT[LIS.getMBBFromIndex(IdxVNI->def)]);
423 // Update LiveOutCache, but skip IdxMBB at LiveIn[0].
424 for (unsigned i = 1, e = LiveIn.size(); i != e; ++i)
425 LiveOutCache[LiveIn[i]->getBlock()] = LOP;
426 } else
427 IdxVNI = updateSSA(RegIdx, LiveIn, Idx, IdxMBB);
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000428
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000429 // Since we went through the trouble of a full BFS visiting all reaching defs,
430 // the values in LiveIn are now accurate. No more phi-defs are needed
431 // for these blocks, so we can color the live ranges.
432 for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) {
433 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
434 SlotIndex Start = LIS.getMBBStartIdx(MBB);
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000435 VNInfo *VNI = LiveOutCache[MBB].first;
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +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));
443 }
444}
445
446VNInfo *SplitEditor::updateSSA(unsigned RegIdx,
447 SmallVectorImpl<MachineDomTreeNode*> &LiveIn,
448 SlotIndex Idx,
449 const MachineBasicBlock *IdxMBB) {
450 // This is essentially the same iterative algorithm that SSAUpdater uses,
451 // except we already have a dominator tree, so we don't have to recompute it.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000452 LiveInterval *LI = Edit->get(RegIdx);
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000453 VNInfo *IdxVNI = 0;
454 unsigned Changes;
455 do {
456 Changes = 0;
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000457 // Propagate live-out values down the dominator tree, inserting phi-defs
458 // when necessary. Since LiveIn was created by a BFS, going backwards makes
459 // it more likely for us to visit immediate dominators before their
460 // children.
461 for (unsigned i = LiveIn.size(); i; --i) {
462 MachineDomTreeNode *Node = LiveIn[i-1];
463 MachineBasicBlock *MBB = Node->getBlock();
464 MachineDomTreeNode *IDom = Node->getIDom();
465 LiveOutPair IDomValue;
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000466
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000467 // We need a live-in value to a block with no immediate dominator?
468 // This is probably an unreachable block that has survived somehow.
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000469 bool needPHI = !IDom || !LiveOutSeen.test(IDom->getBlock()->getNumber());
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000470
471 // IDom dominates all of our predecessors, but it may not be the immediate
472 // dominator. Check if any of them have live-out values that are properly
473 // dominated by IDom. If so, we need a phi-def here.
474 if (!needPHI) {
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000475 IDomValue = LiveOutCache[IDom->getBlock()];
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000476 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
477 PE = MBB->pred_end(); PI != PE; ++PI) {
478 LiveOutPair Value = LiveOutCache[*PI];
479 if (!Value.first || Value.first == IDomValue.first)
480 continue;
481 // This predecessor is carrying something other than IDomValue.
482 // It could be because IDomValue hasn't propagated yet, or it could be
483 // because MBB is in the dominance frontier of that value.
484 if (MDT.dominates(IDom, Value.second)) {
485 needPHI = true;
486 break;
487 }
488 }
489 }
490
491 // Create a phi-def if required.
492 if (needPHI) {
493 ++Changes;
494 SlotIndex Start = LIS.getMBBStartIdx(MBB);
495 VNInfo *VNI = LI->getNextValue(Start, 0, LIS.getVNInfoAllocator());
496 VNI->setIsPHIDef(true);
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000497 // We no longer need LI to be live-in.
498 LiveIn.erase(LiveIn.begin()+(i-1));
499 // Blocks in LiveIn are either IdxMBB, or have a value live-through.
500 if (MBB == IdxMBB)
501 IdxVNI = VNI;
502 // Check if we need to update live-out info.
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000503 LiveOutPair &LOP = LiveOutCache[MBB];
504 if (LOP.second == Node || !LiveOutSeen.test(MBB->getNumber())) {
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000505 // We already have a live-out defined in MBB, so this must be IdxMBB.
506 assert(MBB == IdxMBB && "Adding phi-def to known live-out");
507 LI->addRange(LiveRange(Start, Idx.getNextSlot(), VNI));
508 } else {
509 // This phi-def is also live-out, so color the whole block.
510 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000511 LOP = LiveOutPair(VNI, Node);
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000512 }
513 } else if (IDomValue.first) {
514 // No phi-def here. Remember incoming value for IdxMBB.
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000515 if (MBB == IdxMBB) {
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000516 IdxVNI = IDomValue.first;
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000517 // IdxMBB need not be live-out.
518 if (!LiveOutSeen.test(MBB->getNumber()))
519 continue;
520 }
521 assert(LiveOutSeen.test(MBB->getNumber()) && "Expected live-out block");
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000522 // Propagate IDomValue if needed:
523 // MBB is live-out and doesn't define its own value.
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000524 LiveOutPair &LOP = LiveOutCache[MBB];
525 if (LOP.second != Node && LOP.first != IDomValue.first) {
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000526 ++Changes;
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000527 LOP = IDomValue;
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000528 }
529 }
530 }
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000531 } while (Changes);
532
533 assert(IdxVNI && "Didn't find value for Idx");
534 return IdxVNI;
535}
536
Eric Christopher0f438112011-02-03 06:18:29 +0000537VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000538 VNInfo *ParentVNI,
539 SlotIndex UseIdx,
540 MachineBasicBlock &MBB,
541 MachineBasicBlock::iterator I) {
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000542 MachineInstr *CopyMI = 0;
543 SlotIndex Def;
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000544 LiveInterval *LI = Edit->get(RegIdx);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000545
546 // Attempt cheap-as-a-copy rematerialization.
547 LiveRangeEdit::Remat RM(ParentVNI);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000548 if (Edit->canRematerializeAt(RM, UseIdx, true, LIS)) {
549 Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, LIS, TII, TRI);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000550 } else {
551 // Can't remat, just insert a copy from parent.
Eric Christopher0f438112011-02-03 06:18:29 +0000552 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000553 .addReg(Edit->getReg());
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000554 Def = LIS.InsertMachineInstrInMaps(CopyMI).getDefIndex();
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000555 }
556
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000557 // Define the value in Reg.
558 VNInfo *VNI = defValue(RegIdx, ParentVNI, Def);
559 VNI->setCopy(CopyMI);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000560 return VNI;
561}
562
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000563/// Create a new virtual register and live interval.
564void SplitEditor::openIntv() {
Eric Christopher0f438112011-02-03 06:18:29 +0000565 assert(!OpenIdx && "Previous LI not closed before openIntv");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000566
Eric Christopher0f438112011-02-03 06:18:29 +0000567 // Create the complement as index 0.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000568 if (Edit->empty())
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000569 Edit->create(LIS, VRM);
Eric Christopher0f438112011-02-03 06:18:29 +0000570
571 // Create the open interval.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000572 OpenIdx = Edit->size();
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000573 Edit->create(LIS, VRM);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000574}
575
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000576SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000577 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000578 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000579 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000580 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000581 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000582 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000583 return Idx;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000584 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000585 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000586 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000587 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000588
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000589 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
590 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000591}
592
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000593SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000594 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000595 SlotIndex End = LIS.getMBBEndIdx(&MBB);
596 SlotIndex Last = End.getPrevSlot();
597 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000598 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000599 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000600 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000601 return End;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000602 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000603 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000604 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000605 LIS.getLastSplitPoint(Edit->getParent(), &MBB));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000606 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopher0f438112011-02-03 06:18:29 +0000607 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000608 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000609}
610
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000611/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000612void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000613 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000614}
615
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000616void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopher0f438112011-02-03 06:18:29 +0000617 assert(OpenIdx && "openIntv not called before useIntv");
618 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
619 RegAssign.insert(Start, End, OpenIdx);
620 DEBUG(dump());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000621}
622
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000623SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000624 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000625 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000626
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000627 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000628 Idx = Idx.getBoundaryIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +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.getNextSlot();
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 Olesenf1b05f22010-08-12 17:07:14 +0000635
Jakob Stoklund Olesen01cb34b2011-02-08 18:50:18 +0000636 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
637 assert(MI && "No instruction at index");
638 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(),
639 llvm::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000640 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000641}
642
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000643SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
644 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
645 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
646
647 // The interval must be live into the instruction at Idx.
648 Idx = Idx.getBoundaryIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000649 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000650 if (!ParentVNI) {
651 DEBUG(dbgs() << ": not live\n");
652 return Idx.getNextSlot();
653 }
654 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
655
656 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
657 assert(MI && "No instruction at index");
658 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
659 return VNI->def;
660}
661
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000662SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000663 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000664 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000665 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000666
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000667 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000668 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000669 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000670 return Start;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000671 }
672
Eric Christopher0f438112011-02-03 06:18:29 +0000673 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000674 MBB.SkipPHIsAndLabels(MBB.begin()));
Eric Christopher0f438112011-02-03 06:18:29 +0000675 RegAssign.insert(Start, VNI->def, OpenIdx);
676 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000677 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000678}
679
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000680void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
681 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000682 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
683 assert(ParentVNI == Edit->getParent().getVNInfoAt(End.getPrevSlot()) &&
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000684 "Parent changes value in extended range");
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000685 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
686 "Range cannot span basic blocks");
687
Jakob Stoklund Olesend3fdaeb2011-03-02 00:49:28 +0000688 // The complement interval will be extended as needed by extendRange().
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000689 markComplexMapped(0, ParentVNI);
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000690 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
691 RegAssign.insert(Start, End, OpenIdx);
692 DEBUG(dump());
693}
694
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000695/// closeIntv - Indicate that we are done editing the currently open
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000696/// LiveInterval, and ranges can be trimmed.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000697void SplitEditor::closeIntv() {
Eric Christopher0f438112011-02-03 06:18:29 +0000698 assert(OpenIdx && "openIntv not called before closeIntv");
699 OpenIdx = 0;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000700}
701
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000702/// transferSimpleValues - Transfer all simply defined values to the new live
703/// ranges.
704/// Values that were rematerialized or that have multiple defs are left alone.
705bool SplitEditor::transferSimpleValues() {
706 bool Skipped = false;
707 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000708 for (LiveInterval::const_iterator ParentI = Edit->getParent().begin(),
709 ParentE = Edit->getParent().end(); ParentI != ParentE; ++ParentI) {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000710 DEBUG(dbgs() << " blit " << *ParentI << ':');
711 VNInfo *ParentVNI = ParentI->valno;
712 // RegAssign has holes where RegIdx 0 should be used.
713 SlotIndex Start = ParentI->start;
714 AssignI.advanceTo(Start);
715 do {
716 unsigned RegIdx;
717 SlotIndex End = ParentI->end;
718 if (!AssignI.valid()) {
719 RegIdx = 0;
720 } else if (AssignI.start() <= Start) {
721 RegIdx = AssignI.value();
722 if (AssignI.stop() < End) {
723 End = AssignI.stop();
724 ++AssignI;
725 }
726 } else {
727 RegIdx = 0;
728 End = std::min(End, AssignI.start());
729 }
730 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
731 if (VNInfo *VNI = Values.lookup(std::make_pair(RegIdx, ParentVNI->id))) {
732 DEBUG(dbgs() << ':' << VNI->id);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000733 Edit->get(RegIdx)->addRange(LiveRange(Start, End, VNI));
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000734 } else
735 Skipped = true;
736 Start = End;
737 } while (Start != ParentI->end);
738 DEBUG(dbgs() << '\n');
739 }
740 return Skipped;
741}
742
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000743void SplitEditor::extendPHIKillRanges() {
744 // Extend live ranges to be live-out for successor PHI values.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000745 for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
746 E = Edit->getParent().vni_end(); I != E; ++I) {
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000747 const VNInfo *PHIVNI = *I;
748 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
749 continue;
750 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
751 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
752 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
753 PE = MBB->pred_end(); PI != PE; ++PI) {
754 SlotIndex End = LIS.getMBBEndIdx(*PI).getPrevSlot();
755 // The predecessor may not have a live-out value. That is OK, like an
756 // undef PHI operand.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000757 if (Edit->getParent().liveAt(End)) {
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000758 assert(RegAssign.lookup(End) == RegIdx &&
759 "Different register assignment in phi predecessor");
760 extendRange(RegIdx, End);
761 }
762 }
763 }
764}
765
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000766/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000767void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000768 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000769 RE = MRI.reg_end(); RI != RE;) {
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000770 MachineOperand &MO = RI.getOperand();
771 MachineInstr *MI = MO.getParent();
772 ++RI;
Eric Christopher0f438112011-02-03 06:18:29 +0000773 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000774 if (MI->isDebugValue()) {
775 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000776 MO.setReg(0);
777 continue;
778 }
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +0000779
780 // <undef> operands don't really read the register, so just assign them to
781 // the complement.
782 if (MO.isUse() && MO.isUndef()) {
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000783 MO.setReg(Edit->get(0)->reg);
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +0000784 continue;
785 }
786
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000787 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +0000788 if (MO.isDef())
789 Idx = MO.isEarlyClobber() ? Idx.getUseIndex() : Idx.getDefIndex();
Eric Christopher0f438112011-02-03 06:18:29 +0000790
791 // Rewrite to the mapped register at Idx.
792 unsigned RegIdx = RegAssign.lookup(Idx);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000793 MO.setReg(Edit->get(RegIdx)->reg);
Eric Christopher0f438112011-02-03 06:18:29 +0000794 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
795 << Idx << ':' << RegIdx << '\t' << *MI);
796
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +0000797 // Extend liveness to Idx if the instruction reads reg.
798 if (!ExtendRanges)
799 continue;
800
801 // Skip instructions that don't read Reg.
802 if (MO.isDef()) {
803 if (!MO.getSubReg() && !MO.isEarlyClobber())
804 continue;
805 // We may wan't to extend a live range for a partial redef, or for a use
806 // tied to an early clobber.
807 Idx = Idx.getPrevSlot();
808 if (!Edit->getParent().liveAt(Idx))
809 continue;
810 } else
811 Idx = Idx.getUseIndex();
812
813 extendRange(RegIdx, Idx);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000814 }
815}
816
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000817void SplitEditor::deleteRematVictims() {
818 SmallVector<MachineInstr*, 8> Dead;
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +0000819 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
820 LiveInterval *LI = *I;
821 for (LiveInterval::const_iterator LII = LI->begin(), LIE = LI->end();
822 LII != LIE; ++LII) {
823 // Dead defs end at the store slot.
824 if (LII->end != LII->valno->def.getNextSlot())
825 continue;
826 MachineInstr *MI = LIS.getInstructionFromIndex(LII->valno->def);
827 assert(MI && "Missing instruction for dead def");
828 MI->addRegisterDead(LI->reg, &TRI);
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000829
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +0000830 if (!MI->allDefsAreDead())
831 continue;
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000832
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +0000833 DEBUG(dbgs() << "All defs dead: " << *MI);
834 Dead.push_back(MI);
835 }
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000836 }
837
838 if (Dead.empty())
839 return;
840
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000841 Edit->eliminateDeadDefs(Dead, LIS, VRM, TII);
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000842}
843
Eric Christopher463a2972011-02-03 05:40:54 +0000844void SplitEditor::finish() {
Eric Christopher0f438112011-02-03 06:18:29 +0000845 assert(OpenIdx == 0 && "Previous LI not closed before rewrite");
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000846 ++NumFinished;
Eric Christopher463a2972011-02-03 05:40:54 +0000847
Eric Christopher0f438112011-02-03 06:18:29 +0000848 // At this point, the live intervals in Edit contain VNInfos corresponding to
849 // the inserted copies.
850
851 // Add the original defs from the parent interval.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000852 for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
853 E = Edit->getParent().vni_end(); I != E; ++I) {
Eric Christopher0f438112011-02-03 06:18:29 +0000854 const VNInfo *ParentVNI = *I;
Jakob Stoklund Olesen9ecd1e72011-02-04 00:59:23 +0000855 if (ParentVNI->isUnused())
856 continue;
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000857 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Jakob Stoklund Olesen29ef8752011-03-15 21:13:22 +0000858 VNInfo *VNI = defValue(RegIdx, ParentVNI, ParentVNI->def);
859 VNI->setIsPHIDef(ParentVNI->isPHIDef());
860 VNI->setCopy(ParentVNI->getCopy());
861
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000862 // Mark rematted values as complex everywhere to force liveness computation.
863 // The new live ranges may be truncated.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000864 if (Edit->didRematerialize(ParentVNI))
865 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000866 markComplexMapped(i, ParentVNI);
Eric Christopher0f438112011-02-03 06:18:29 +0000867 }
868
869#ifndef NDEBUG
870 // Every new interval must have a def by now, otherwise the split is bogus.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000871 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I)
Eric Christopher0f438112011-02-03 06:18:29 +0000872 assert((*I)->hasAtLeastOneValue() && "Split interval has no value");
873#endif
874
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000875 // Transfer the simply mapped values, check if any are complex.
876 bool Complex = transferSimpleValues();
877 if (Complex)
878 extendPHIKillRanges();
879 else
880 ++NumSimple;
Eric Christopher0f438112011-02-03 06:18:29 +0000881
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000882 // Rewrite virtual registers, possibly extending ranges.
883 rewriteAssigned(Complex);
Eric Christopher0f438112011-02-03 06:18:29 +0000884
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000885 // Delete defs that were rematted everywhere.
886 if (Complex)
887 deleteRematVictims();
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000888
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000889 // Get rid of unused values and set phi-kill flags.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000890 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000891 (*I)->RenumberValues(LIS);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000892
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000893 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000894 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000895 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000896 // Don't use iterators, they are invalidated by create() below.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000897 LiveInterval *li = Edit->get(i);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000898 unsigned NumComp = ConEQ.Classify(li);
899 if (NumComp <= 1)
900 continue;
901 DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n');
902 SmallVector<LiveInterval*, 8> dups;
903 dups.push_back(li);
904 for (unsigned i = 1; i != NumComp; ++i)
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000905 dups.push_back(&Edit->create(LIS, VRM));
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +0000906 ConEQ.Distribute(&dups[0], MRI);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000907 }
908
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000909 // Calculate spill weight and allocation hints for new intervals.
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000910 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), LIS, SA.Loops);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000911}
912
913
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000914//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000915// Single Block Splitting
916//===----------------------------------------------------------------------===//
917
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000918/// getMultiUseBlocks - if CurLI has more than one use in a basic block, it
919/// may be an advantage to split CurLI for the duration of the block.
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000920bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000921 // If CurLI is local to one block, there is no point to splitting it.
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000922 if (LiveBlocks.size() <= 1)
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000923 return false;
924 // Add blocks with multiple uses.
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000925 for (unsigned i = 0, e = LiveBlocks.size(); i != e; ++i) {
926 const BlockInfo &BI = LiveBlocks[i];
927 if (!BI.Uses)
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000928 continue;
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000929 unsigned Instrs = UsingBlocks.lookup(BI.MBB);
930 if (Instrs <= 1)
931 continue;
932 if (Instrs == 2 && BI.LiveIn && BI.LiveOut && !BI.LiveThrough)
933 continue;
934 Blocks.insert(BI.MBB);
935 }
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000936 return !Blocks.empty();
937}
938
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000939/// splitSingleBlocks - Split CurLI into a separate live interval inside each
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000940/// basic block in Blocks.
941void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) {
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000942 DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n");
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000943
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000944 for (unsigned i = 0, e = SA.LiveBlocks.size(); i != e; ++i) {
945 const SplitAnalysis::BlockInfo &BI = SA.LiveBlocks[i];
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000946 if (!BI.Uses || !Blocks.count(BI.MBB))
947 continue;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000948
949 openIntv();
Jakob Stoklund Olesenc8ec7652011-03-29 03:12:04 +0000950 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstUse,
951 BI.LastSplitPoint));
Jakob Stoklund Olesenc70f6872011-02-23 18:26:31 +0000952 if (!BI.LiveOut || BI.LastUse < BI.LastSplitPoint) {
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000953 useIntv(SegStart, leaveIntvAfter(BI.LastUse));
954 } else {
Jakob Stoklund Olesenc70f6872011-02-23 18:26:31 +0000955 // The last use is after the last valid split point.
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000956 SlotIndex SegStop = leaveIntvBefore(BI.LastSplitPoint);
957 useIntv(SegStart, SegStop);
958 overlapIntv(SegStop, BI.LastUse);
959 }
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000960 closeIntv();
961 }
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000962 finish();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000963}