blob: b63fef8c87c9f54c18fc40e9180fedf1ec4c124a [file] [log] [blame]
Jakob Stoklund Olesen36d12c62010-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 Olesen36d12c62010-07-20 15:41:07 +000015#include "SplitKit.h"
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +000016#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000017#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper3ca96f92012-04-02 22:44:18 +000018#include "llvm/CodeGen/LiveRangeEdit.h"
Wei Mi9a16d652016-04-13 03:08:27 +000019#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Jakob Stoklund Olesene172a8b2010-10-28 20:34:50 +000020#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +000022#include "llvm/CodeGen/MachineLoopInfo.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000024#include "llvm/CodeGen/VirtRegMap.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000025#include "llvm/Support/Debug.h"
26#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesened4075c2010-07-20 21:46:58 +000027#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000029
30using namespace llvm;
31
Chandler Carruth1b9dde02014-04-22 02:02:50 +000032#define DEBUG_TYPE "regalloc"
33
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +000034STATISTIC(NumFinished, "Number of splits finished");
35STATISTIC(NumSimple, "Number of splits that were simple");
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +000036STATISTIC(NumCopies, "Number of copies inserted for splitting");
37STATISTIC(NumRemats, "Number of rematerialized defs for splitting");
Jakob Stoklund Olesen50215af2011-05-10 17:37:41 +000038STATISTIC(NumRepairs, "Number of invalid live ranges repaired");
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +000039
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000040//===----------------------------------------------------------------------===//
Wei Mi35ee9332016-05-11 22:28:29 +000041// Last Insert Point Analysis
42//===----------------------------------------------------------------------===//
43
44InsertPointAnalysis::InsertPointAnalysis(const LiveIntervals &lis,
45 unsigned BBNum)
46 : LIS(lis), CurLI(nullptr), LastInsertPoint(BBNum) {}
47
48SlotIndex
49InsertPointAnalysis::computeLastInsertPoint(const MachineBasicBlock &MBB) {
50 unsigned Num = MBB.getNumber();
51 std::pair<SlotIndex, SlotIndex> &LIP = LastInsertPoint[Num];
52 SlotIndex MBBEnd = LIS.getMBBEndIdx(&MBB);
53
54 SmallVector<const MachineBasicBlock *, 1> EHPadSucessors;
55 for (const MachineBasicBlock *SMBB : MBB.successors())
56 if (SMBB->isEHPad())
57 EHPadSucessors.push_back(SMBB);
58
59 // Compute insert points on the first call. The pair is independent of the
60 // current live interval.
61 if (!LIP.first.isValid()) {
62 MachineBasicBlock::const_iterator FirstTerm = MBB.getFirstTerminator();
63 if (FirstTerm == MBB.end())
64 LIP.first = MBBEnd;
65 else
66 LIP.first = LIS.getInstructionIndex(*FirstTerm);
67
68 // If there is a landing pad successor, also find the call instruction.
69 if (EHPadSucessors.empty())
70 return LIP.first;
71 // There may not be a call instruction (?) in which case we ignore LPad.
72 LIP.second = LIP.first;
73 for (MachineBasicBlock::const_iterator I = MBB.end(), E = MBB.begin();
74 I != E;) {
75 --I;
76 if (I->isCall()) {
77 LIP.second = LIS.getInstructionIndex(*I);
78 break;
79 }
80 }
81 }
82
83 // If CurLI is live into a landing pad successor, move the last insert point
84 // back to the call that may throw.
85 if (!LIP.second)
86 return LIP.first;
87
88 assert(CurLI && "CurLI not being set");
89 if (none_of(EHPadSucessors, [&](const MachineBasicBlock *EHPad) {
90 return LIS.isLiveInToMBB(*CurLI, EHPad);
91 }))
92 return LIP.first;
93
94 // Find the value leaving MBB.
95 const VNInfo *VNI = CurLI->getVNInfoBefore(MBBEnd);
96 if (!VNI)
97 return LIP.first;
98
99 // If the value leaving MBB was defined after the call in MBB, it can't
100 // really be live-in to the landing pad. This can happen if the landing pad
101 // has a PHI, and this register is undef on the exceptional edge.
102 // <rdar://problem/10664933>
103 if (!SlotIndex::isEarlierInstr(VNI->def, LIP.second) && VNI->def < MBBEnd)
104 return LIP.first;
105
106 // Value is properly live-in to the landing pad.
107 // Only allow inserts before the call.
108 return LIP.second;
109}
110
111MachineBasicBlock::iterator
112InsertPointAnalysis::getLastInsertPointIter(MachineBasicBlock &MBB) {
113 SlotIndex LIP = getLastInsertPoint(MBB);
114 if (LIP == LIS.getMBBEndIdx(&MBB))
115 return MBB.end();
116 return LIS.getInstructionFromIndex(LIP);
117}
118
119//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000120// Split Analysis
121//===----------------------------------------------------------------------===//
122
Eric Christopherd9134482014-08-04 21:25:23 +0000123SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
Jakob Stoklund Olesen0fef9dd2010-07-20 23:50:15 +0000124 const MachineLoopInfo &mli)
Eric Christopherd9134482014-08-04 21:25:23 +0000125 : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli),
Eric Christopherfc6de422014-08-05 02:39:49 +0000126 TII(*MF.getSubtarget().getInstrInfo()), CurLI(nullptr),
Wei Mi35ee9332016-05-11 22:28:29 +0000127 IPA(lis, MF.getNumBlockIDs()) {}
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000128
129void SplitAnalysis::clear() {
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000130 UseSlots.clear();
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000131 UseBlocks.clear();
132 ThroughBlocks.clear();
Craig Topperc0196b12014-04-14 00:51:57 +0000133 CurLI = nullptr;
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000134 DidRepairRange = false;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000135}
136
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000137/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000138void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000139 assert(UseSlots.empty() && "Call clear first");
140
141 // First get all the defs from the interval values. This provides the correct
142 // slots for early clobbers.
Matthias Braun96761952014-12-10 23:07:54 +0000143 for (const VNInfo *VNI : CurLI->valnos)
144 if (!VNI->isPHIDef() && !VNI->isUnused())
145 UseSlots.push_back(VNI->def);
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000146
147 // Get use slots form the use-def chain.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000148 const MachineRegisterInfo &MRI = MF.getRegInfo();
Owen Andersonb36376e2014-03-17 19:36:09 +0000149 for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg))
150 if (!MO.isUndef())
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000151 UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot());
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000152
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000153 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000154
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000155 // Remove duplicates, keeping the smaller slot for each instruction.
156 // That is what we want for early clobbers.
157 UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
158 SlotIndex::isSameInstr),
159 UseSlots.end());
160
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000161 // Compute per-live block info.
162 if (!calcLiveBlockInfo()) {
163 // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
Rafael Espindola676c4052011-06-26 22:34:10 +0000164 // I am looking at you, RegisterCoalescer!
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000165 DidRepairRange = true;
Jakob Stoklund Olesen50215af2011-05-10 17:37:41 +0000166 ++NumRepairs;
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000167 DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
168 const_cast<LiveIntervals&>(LIS)
169 .shrinkToUses(const_cast<LiveInterval*>(CurLI));
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000170 UseBlocks.clear();
171 ThroughBlocks.clear();
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000172 bool fixed = calcLiveBlockInfo();
173 (void)fixed;
174 assert(fixed && "Couldn't fix broken live interval");
175 }
176
Jakob Stoklund Olesenbd6b86e2011-03-27 22:49:23 +0000177 DEBUG(dbgs() << "Analyze counted "
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000178 << UseSlots.size() << " instrs in "
179 << UseBlocks.size() << " blocks, through "
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000180 << NumThroughBlocks << " blocks.\n");
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000181}
182
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000183/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
184/// where CurLI is live.
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000185bool SplitAnalysis::calcLiveBlockInfo() {
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000186 ThroughBlocks.resize(MF.getNumBlockIDs());
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000187 NumThroughBlocks = NumGapBlocks = 0;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000188 if (CurLI->empty())
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000189 return true;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000190
191 LiveInterval::const_iterator LVI = CurLI->begin();
192 LiveInterval::const_iterator LVE = CurLI->end();
193
194 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
195 UseI = UseSlots.begin();
196 UseE = UseSlots.end();
197
198 // Loop over basic blocks where CurLI is live.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000199 MachineFunction::iterator MFI =
200 LIS.getMBBFromIndex(LVI->start)->getIterator();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000201 for (;;) {
202 BlockInfo BI;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000203 BI.MBB = &*MFI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000204 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000205 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000206
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000207 // If the block contains no uses, the range must be live through. At one
Rafael Espindola676c4052011-06-26 22:34:10 +0000208 // point, RegisterCoalescer could create dangling ranges that ended
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000209 // mid-block.
210 if (UseI == UseE || *UseI >= Stop) {
211 ++NumThroughBlocks;
212 ThroughBlocks.set(BI.MBB->getNumber());
213 // The range shouldn't end mid-block if there are no uses. This shouldn't
214 // happen.
215 if (LVI->end < Stop)
216 return false;
217 } else {
218 // This block has uses. Find the first and last uses in the block.
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000219 BI.FirstInstr = *UseI;
220 assert(BI.FirstInstr >= Start);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000221 do ++UseI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000222 while (UseI != UseE && *UseI < Stop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000223 BI.LastInstr = UseI[-1];
224 assert(BI.LastInstr < Stop);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000225
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000226 // LVI is the first live segment overlapping MBB.
227 BI.LiveIn = LVI->start <= Start;
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000228
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000229 // When not live in, the first use should be a def.
230 if (!BI.LiveIn) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000231 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000232 assert(LVI->start == BI.FirstInstr && "First instr should be a def");
233 BI.FirstDef = BI.FirstInstr;
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000234 }
235
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000236 // Look for gaps in the live range.
237 BI.LiveOut = true;
238 while (LVI->end < Stop) {
239 SlotIndex LastStop = LVI->end;
240 if (++LVI == LVE || LVI->start >= Stop) {
241 BI.LiveOut = false;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000242 BI.LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000243 break;
244 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000245
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000246 if (LastStop < LVI->start) {
247 // There is a gap in the live range. Create duplicate entries for the
248 // live-in snippet and the live-out snippet.
249 ++NumGapBlocks;
250
251 // Push the Live-in part.
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000252 BI.LiveOut = false;
253 UseBlocks.push_back(BI);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000254 UseBlocks.back().LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000255
256 // Set up BI for the live-out part.
257 BI.LiveIn = false;
258 BI.LiveOut = true;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000259 BI.FirstInstr = BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000260 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000261
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000262 // A Segment that starts in the middle of the block must be a def.
263 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000264 if (!BI.FirstDef)
265 BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000266 }
267
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000268 UseBlocks.push_back(BI);
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000269
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000270 // LVI is now at LVE or LVI->end >= Stop.
271 if (LVI == LVE)
272 break;
273 }
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000274
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000275 // Live segment ends exactly at Stop. Move to the next segment.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000276 if (LVI->end == Stop && ++LVI == LVE)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000277 break;
278
279 // Pick the next basic block.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000280 if (LVI->start < Stop)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000281 ++MFI;
282 else
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000283 MFI = LIS.getMBBFromIndex(LVI->start)->getIterator();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000284 }
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000285
286 assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000287 return true;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000288}
289
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000290unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
291 if (cli->empty())
292 return 0;
293 LiveInterval *li = const_cast<LiveInterval*>(cli);
294 LiveInterval::iterator LVI = li->begin();
295 LiveInterval::iterator LVE = li->end();
296 unsigned Count = 0;
297
298 // Loop over basic blocks where li is live.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000299 MachineFunction::const_iterator MFI =
300 LIS.getMBBFromIndex(LVI->start)->getIterator();
301 SlotIndex Stop = LIS.getMBBEndIdx(&*MFI);
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000302 for (;;) {
303 ++Count;
304 LVI = li->advanceTo(LVI, Stop);
305 if (LVI == LVE)
306 return Count;
307 do {
308 ++MFI;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000309 Stop = LIS.getMBBEndIdx(&*MFI);
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000310 } while (Stop <= LVI->start);
311 }
312}
313
Jakob Stoklund Olesen60a26a62011-02-21 23:09:46 +0000314bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
315 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
316 const LiveInterval &Orig = LIS.getInterval(OrigReg);
317 assert(!Orig.empty() && "Splitting empty interval?");
318 LiveInterval::const_iterator I = Orig.find(Idx);
319
320 // Range containing Idx should begin at Idx.
321 if (I != Orig.end() && I->start <= Idx)
322 return I->start == Idx;
323
324 // Range does not contain Idx, previous must end at Idx.
325 return I != Orig.begin() && (--I)->end == Idx;
326}
327
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000328void SplitAnalysis::analyze(const LiveInterval *li) {
329 clear();
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000330 CurLI = li;
Wei Mi35ee9332016-05-11 22:28:29 +0000331 IPA.setInterval(li);
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000332 analyzeUses();
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000333}
334
Jakob Stoklund Olesen28e769c2010-12-15 17:49:52 +0000335
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000336//===----------------------------------------------------------------------===//
337// Split Editor
338//===----------------------------------------------------------------------===//
339
340/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Eric Christopherd9134482014-08-04 21:25:23 +0000341SplitEditor::SplitEditor(SplitAnalysis &sa, LiveIntervals &lis, VirtRegMap &vrm,
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000342 MachineDominatorTree &mdt,
343 MachineBlockFrequencyInfo &mbfi)
Eric Christopherd9134482014-08-04 21:25:23 +0000344 : SA(sa), LIS(lis), VRM(vrm), MRI(vrm.getMachineFunction().getRegInfo()),
Eric Christopher60621802014-10-14 07:22:00 +0000345 MDT(mdt), TII(*vrm.getMachineFunction().getSubtarget().getInstrInfo()),
346 TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()),
Eric Christopherd9134482014-08-04 21:25:23 +0000347 MBFI(mbfi), Edit(nullptr), OpenIdx(0), SpillMode(SM_Partition),
348 RegAssign(Allocator) {}
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000349
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000350void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
351 Edit = &LRE;
352 SpillMode = SM;
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000353 OpenIdx = 0;
354 RegAssign.clear();
355 Values.clear();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000356
357 // Reset the LiveRangeCalc instances needed for this spill mode.
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000358 LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
359 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000360 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000361 LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
362 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000363
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000364 // We don't need an AliasAnalysis since we will only be performing
365 // cheap-as-a-copy remats anyway.
Craig Topperc0196b12014-04-14 00:51:57 +0000366 Edit->anyRematerializable(nullptr);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000367}
368
Manman Ren19f49ac2012-09-11 22:23:19 +0000369#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000370LLVM_DUMP_METHOD void SplitEditor::dump() const {
Eric Christopherede62672011-02-03 06:18:29 +0000371 if (RegAssign.empty()) {
372 dbgs() << " empty\n";
373 return;
374 }
375
376 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
377 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
378 dbgs() << '\n';
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000379}
Manman Ren742534c2012-09-06 19:06:06 +0000380#endif
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000381
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000382VNInfo *SplitEditor::defValue(unsigned RegIdx,
383 const VNInfo *ParentVNI,
384 SlotIndex Idx) {
385 assert(ParentVNI && "Mapping NULL value");
386 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000387 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
Mark Laceyf9ea8852013-08-14 23:50:04 +0000388 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000389
390 // Create a new value.
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000391 VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000392
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000393 // Use insert for lookup, so we can add missing values with a second lookup.
394 std::pair<ValueMap::iterator, bool> InsP =
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000395 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id),
396 ValueForcePair(VNI, false)));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000397
398 // This was the first time (RegIdx, ParentVNI) was mapped.
399 // Keep it as a simple def without any liveness.
400 if (InsP.second)
401 return VNI;
402
403 // If the previous value was a simple mapping, add liveness for it now.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000404 if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000405 SlotIndex Def = OldVNI->def;
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000406 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), OldVNI));
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000407 // No longer a simple mapping. Switch to a complex, non-forced mapping.
408 InsP.first->second = ValueForcePair();
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000409 }
410
411 // This is a complex mapping, add liveness for VNI
412 SlotIndex Def = VNI->def;
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000413 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000414
415 return VNI;
416}
417
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000418void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000419 assert(ParentVNI && "Mapping NULL value");
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000420 ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
421 VNInfo *VNI = VFP.getPointer();
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000422
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000423 // ParentVNI was either unmapped or already complex mapped. Either way, just
424 // set the force bit.
425 if (!VNI) {
426 VFP.setInt(true);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000427 return;
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000428 }
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000429
430 // This was previously a single mapping. Make sure the old def is represented
431 // by a trivial live range.
432 SlotIndex Def = VNI->def;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000433 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000434 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000435 // Mark as complex mapped, forced.
Craig Topperc0196b12014-04-14 00:51:57 +0000436 VFP = ValueForcePair(nullptr, true);
Jakob Stoklund Olesen4484f992011-09-13 18:05:29 +0000437}
438
Eric Christopherede62672011-02-03 06:18:29 +0000439VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000440 VNInfo *ParentVNI,
441 SlotIndex UseIdx,
442 MachineBasicBlock &MBB,
443 MachineBasicBlock::iterator I) {
Craig Topperc0196b12014-04-14 00:51:57 +0000444 MachineInstr *CopyMI = nullptr;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000445 SlotIndex Def;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000446 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000447
Jakob Stoklund Olesen7d406792011-05-02 05:29:58 +0000448 // We may be trying to avoid interference that ends at a deleted instruction,
449 // so always begin RegIdx 0 early and all others late.
450 bool Late = RegIdx != 0;
451
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000452 // Attempt cheap-as-a-copy rematerialization.
Wei Mi9a16d652016-04-13 03:08:27 +0000453 unsigned Original = VRM.getOriginal(Edit->get(RegIdx));
454 LiveInterval &OrigLI = LIS.getInterval(Original);
455 VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000456 LiveRangeEdit::Remat RM(ParentVNI);
Wei Mi9a16d652016-04-13 03:08:27 +0000457 RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
458
459 if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) {
Pete Cooper2bde2f42012-04-02 22:22:53 +0000460 Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late);
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000461 ++NumRemats;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000462 } else {
463 // Can't remat, just insert a copy from parent.
Eric Christopherede62672011-02-03 06:18:29 +0000464 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000465 .addReg(Edit->getReg());
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000466 Def = LIS.getSlotIndexes()
467 ->insertMachineInstrInMaps(*CopyMI, Late)
468 .getRegSlot();
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000469 ++NumCopies;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000470 }
471
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000472 // Define the value in Reg.
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000473 return defValue(RegIdx, ParentVNI, Def);
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000474}
475
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000476/// Create a new virtual register and live interval.
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000477unsigned SplitEditor::openIntv() {
Eric Christopherede62672011-02-03 06:18:29 +0000478 // Create the complement as index 0.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000479 if (Edit->empty())
Mark Lacey9d8103d2013-08-14 23:50:16 +0000480 Edit->createEmptyInterval();
Eric Christopherede62672011-02-03 06:18:29 +0000481
482 // Create the open interval.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000483 OpenIdx = Edit->size();
Mark Lacey9d8103d2013-08-14 23:50:16 +0000484 Edit->createEmptyInterval();
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000485 return OpenIdx;
486}
487
488void SplitEditor::selectIntv(unsigned Idx) {
489 assert(Idx != 0 && "Cannot select the complement interval");
490 assert(Idx < Edit->size() && "Can only select previously opened interval");
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000491 DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000492 OpenIdx = Idx;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000493}
494
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000495SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000496 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000497 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000498 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000499 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000500 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000501 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000502 return Idx;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000503 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000504 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000505 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000506 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000507
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000508 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
509 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000510}
511
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000512SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
513 assert(OpenIdx && "openIntv not called before enterIntvAfter");
514 DEBUG(dbgs() << " enterIntvAfter " << Idx);
515 Idx = Idx.getBoundaryIndex();
516 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
517 if (!ParentVNI) {
518 DEBUG(dbgs() << ": not live\n");
519 return Idx;
520 }
521 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
522 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
523 assert(MI && "enterIntvAfter called with invalid index");
524
525 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000526 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000527 return VNI->def;
528}
529
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000530SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000531 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000532 SlotIndex End = LIS.getMBBEndIdx(&MBB);
533 SlotIndex Last = End.getPrevSlot();
534 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000535 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000536 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000537 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000538 return End;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000539 }
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000540 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000541 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000542 SA.getLastSplitPointIter(&MBB));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000543 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopherede62672011-02-03 06:18:29 +0000544 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000545 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000546}
547
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000548/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000549void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000550 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000551}
552
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000553void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopherede62672011-02-03 06:18:29 +0000554 assert(OpenIdx && "openIntv not called before useIntv");
555 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
556 RegAssign.insert(Start, End, OpenIdx);
557 DEBUG(dump());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000558}
559
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000560SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000561 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000562 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000563
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000564 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000565 SlotIndex Boundary = Idx.getBoundaryIndex();
566 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000567 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000568 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000569 return Boundary.getNextSlot();
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000570 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000571 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000572 MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
Jakob Stoklund Olesen3d11c8e2011-02-08 18:50:18 +0000573 assert(MI && "No instruction at index");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000574
575 // In spill mode, make live ranges as short as possible by inserting the copy
576 // before MI. This is only possible if that instruction doesn't redefine the
577 // value. The inserted COPY is not a kill, and we don't need to recompute
578 // the source live range. The spiller also won't try to hoist this copy.
579 if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
580 MI->readsVirtualRegister(Edit->getReg())) {
581 forceRecompute(0, ParentVNI);
582 defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
583 return Idx;
584 }
585
586 VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000587 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000588 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000589}
590
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000591SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
592 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
593 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
594
595 // The interval must be live into the instruction at Idx.
Jakob Stoklund Olesenc45d38e2011-07-18 18:47:13 +0000596 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000597 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000598 if (!ParentVNI) {
599 DEBUG(dbgs() << ": not live\n");
600 return Idx.getNextSlot();
601 }
602 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
603
604 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
605 assert(MI && "No instruction at index");
606 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
607 return VNI->def;
608}
609
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000610SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000611 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000612 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000613 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000614
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000615 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000616 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000617 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000618 return Start;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000619 }
620
Eric Christopherede62672011-02-03 06:18:29 +0000621 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000622 MBB.SkipPHIsAndLabels(MBB.begin()));
Eric Christopherede62672011-02-03 06:18:29 +0000623 RegAssign.insert(Start, VNI->def, OpenIdx);
624 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000625 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000626}
627
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000628void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
629 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000630 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesend7bcf432011-11-14 01:39:36 +0000631 assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000632 "Parent changes value in extended range");
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000633 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
634 "Range cannot span basic blocks");
635
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000636 // The complement interval will be extended as needed by LRCalc.extend().
Jakob Stoklund Olesen5c482cd2011-04-05 23:43:14 +0000637 if (ParentVNI)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000638 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000639 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
640 RegAssign.insert(Start, End, OpenIdx);
641 DEBUG(dump());
642}
643
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000644//===----------------------------------------------------------------------===//
645// Spill modes
646//===----------------------------------------------------------------------===//
647
648void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
Mark Laceyf9ea8852013-08-14 23:50:04 +0000649 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000650 DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
651 RegAssignMap::iterator AssignI;
652 AssignI.setMap(RegAssign);
653
654 for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
Matthias Braun311730a2015-01-21 19:02:30 +0000655 SlotIndex Def = Copies[i]->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000656 MachineInstr *MI = LIS.getInstructionFromIndex(Def);
657 assert(MI && "No instruction for back-copy");
658
659 MachineBasicBlock *MBB = MI->getParent();
660 MachineBasicBlock::iterator MBBI(MI);
661 bool AtBegin;
662 do AtBegin = MBBI == MBB->begin();
663 while (!AtBegin && (--MBBI)->isDebugValue());
664
665 DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
Matthias Braun311730a2015-01-21 19:02:30 +0000666 LIS.removeVRegDefAt(*LI, Def);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000667 LIS.RemoveMachineInstrFromMaps(*MI);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000668 MI->eraseFromParent();
669
Matthias Braun311730a2015-01-21 19:02:30 +0000670 // Adjust RegAssign if a register assignment is killed at Def. We want to
671 // avoid calculating the live range of the source register if possible.
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000672 AssignI.find(Def.getPrevSlot());
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000673 if (!AssignI.valid() || AssignI.start() >= Def)
674 continue;
675 // If MI doesn't kill the assigned register, just leave it.
676 if (AssignI.stop() != Def)
677 continue;
678 unsigned RegIdx = AssignI.value();
679 if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
680 DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n');
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000681 forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000682 } else {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000683 SlotIndex Kill = LIS.getInstructionIndex(*MBBI).getRegSlot();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000684 DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
685 AssignI.setStop(Kill);
686 }
687 }
688}
689
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000690MachineBasicBlock*
691SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
692 MachineBasicBlock *DefMBB) {
693 if (MBB == DefMBB)
694 return MBB;
695 assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
696
697 const MachineLoopInfo &Loops = SA.Loops;
698 const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
699 MachineDomTreeNode *DefDomNode = MDT[DefMBB];
700
701 // Best candidate so far.
702 MachineBasicBlock *BestMBB = MBB;
703 unsigned BestDepth = UINT_MAX;
704
705 for (;;) {
706 const MachineLoop *Loop = Loops.getLoopFor(MBB);
707
708 // MBB isn't in a loop, it doesn't get any better. All dominators have a
709 // higher frequency by definition.
710 if (!Loop) {
711 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
712 << MBB->getNumber() << " at depth 0\n");
713 return MBB;
714 }
715
716 // We'll never be able to exit the DefLoop.
717 if (Loop == DefLoop) {
718 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
719 << MBB->getNumber() << " in the same loop\n");
720 return MBB;
721 }
722
723 // Least busy dominator seen so far.
724 unsigned Depth = Loop->getLoopDepth();
725 if (Depth < BestDepth) {
726 BestMBB = MBB;
727 BestDepth = Depth;
728 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
729 << MBB->getNumber() << " at depth " << Depth << '\n');
730 }
731
732 // Leave loop by going to the immediate dominator of the loop header.
733 // This is a bigger stride than simply walking up the dominator tree.
734 MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
735
736 // Too far up the dominator tree?
737 if (!IDom || !MDT.dominates(DefDomNode, IDom))
738 return BestMBB;
739
740 MBB = IDom->getBlock();
741 }
742}
743
Wei Mi9a16d652016-04-13 03:08:27 +0000744void SplitEditor::computeRedundantBackCopies(
745 DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) {
746 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
747 LiveInterval *Parent = &Edit->getParent();
748 SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums());
749 SmallPtrSet<VNInfo *, 8> DominatedVNIs;
750
751 // Aggregate VNIs having the same value as ParentVNI.
752 for (VNInfo *VNI : LI->valnos) {
753 if (VNI->isUnused())
754 continue;
755 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
756 EqualVNs[ParentVNI->id].insert(VNI);
757 }
758
759 // For VNI aggregation of each ParentVNI, collect dominated, i.e.,
760 // redundant VNIs to BackCopies.
761 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
762 VNInfo *ParentVNI = Parent->getValNumInfo(i);
763 if (!NotToHoistSet.count(ParentVNI->id))
764 continue;
765 SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin();
766 SmallPtrSetIterator<VNInfo *> It2 = It1;
767 for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) {
768 It2 = It1;
769 for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) {
770 if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2))
771 continue;
772
773 MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def);
774 MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def);
775 if (MBB1 == MBB2) {
776 DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1));
777 } else if (MDT.dominates(MBB1, MBB2)) {
778 DominatedVNIs.insert(*It2);
779 } else if (MDT.dominates(MBB2, MBB1)) {
780 DominatedVNIs.insert(*It1);
781 }
782 }
783 }
784 if (!DominatedVNIs.empty()) {
785 forceRecompute(0, ParentVNI);
786 for (auto VNI : DominatedVNIs) {
787 BackCopies.push_back(VNI);
788 }
789 DominatedVNIs.clear();
790 }
791 }
792}
793
794/// For SM_Size mode, find a common dominator for all the back-copies for
795/// the same ParentVNI and hoist the backcopies to the dominator BB.
796/// For SM_Speed mode, if the common dominator is hot and it is not beneficial
797/// to do the hoisting, simply remove the dominated backcopies for the same
798/// ParentVNI.
799void SplitEditor::hoistCopies() {
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000800 // Get the complement interval, always RegIdx 0.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000801 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000802 LiveInterval *Parent = &Edit->getParent();
803
804 // Track the nearest common dominator for all back-copies for each ParentVNI,
805 // indexed by ParentVNI->id.
806 typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
807 SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
Wei Mi9a16d652016-04-13 03:08:27 +0000808 // The total cost of all the back-copies for each ParentVNI.
809 SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums());
810 // The ParentVNI->id set for which hoisting back-copies are not beneficial
811 // for Speed.
812 DenseSet<unsigned> NotToHoistSet;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000813
814 // Find the nearest common dominator for parent values with multiple
815 // back-copies. If a single back-copy dominates, put it in DomPair.second.
Matthias Braun96761952014-12-10 23:07:54 +0000816 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000817 if (VNI->isUnused())
818 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000819 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
820 assert(ParentVNI && "Parent not live at complement def");
821
822 // Don't hoist remats. The complement is probably going to disappear
823 // completely anyway.
824 if (Edit->didRematerialize(ParentVNI))
825 continue;
826
827 MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
Wei Mi9a16d652016-04-13 03:08:27 +0000828
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000829 DomPair &Dom = NearestDom[ParentVNI->id];
830
831 // Keep directly defined parent values. This is either a PHI or an
832 // instruction in the complement range. All other copies of ParentVNI
833 // should be eliminated.
834 if (VNI->def == ParentVNI->def) {
835 DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
836 Dom = DomPair(ValMBB, VNI->def);
837 continue;
838 }
839 // Skip the singly mapped values. There is nothing to gain from hoisting a
840 // single back-copy.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000841 if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000842 DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
843 continue;
844 }
845
846 if (!Dom.first) {
847 // First time we see ParentVNI. VNI dominates itself.
848 Dom = DomPair(ValMBB, VNI->def);
849 } else if (Dom.first == ValMBB) {
850 // Two defs in the same block. Pick the earlier def.
851 if (!Dom.second.isValid() || VNI->def < Dom.second)
852 Dom.second = VNI->def;
853 } else {
854 // Different basic blocks. Check if one dominates.
855 MachineBasicBlock *Near =
856 MDT.findNearestCommonDominator(Dom.first, ValMBB);
857 if (Near == ValMBB)
858 // Def ValMBB dominates.
859 Dom = DomPair(ValMBB, VNI->def);
860 else if (Near != Dom.first)
861 // None dominate. Hoist to common dominator, need new def.
862 Dom = DomPair(Near, SlotIndex());
Wei Mi9a16d652016-04-13 03:08:27 +0000863 Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000864 }
865
866 DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
867 << " for parent " << ParentVNI->id << '@' << ParentVNI->def
868 << " hoist to BB#" << Dom.first->getNumber() << ' '
869 << Dom.second << '\n');
870 }
871
872 // Insert the hoisted copies.
873 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
874 DomPair &Dom = NearestDom[i];
875 if (!Dom.first || Dom.second.isValid())
876 continue;
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000877 // This value needs a hoisted copy inserted at the end of Dom.first.
878 VNInfo *ParentVNI = Parent->getValNumInfo(i);
879 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
880 // Get a less loopy dominator than Dom.first.
881 Dom.first = findShallowDominator(Dom.first, DefMBB);
Wei Mi9a16d652016-04-13 03:08:27 +0000882 if (SpillMode == SM_Speed &&
883 MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) {
884 NotToHoistSet.insert(ParentVNI->id);
885 continue;
886 }
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000887 SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
888 Dom.second =
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000889 defFromParent(0, ParentVNI, Last, *Dom.first,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000890 SA.getLastSplitPointIter(Dom.first))->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000891 }
892
893 // Remove redundant back-copies that are now known to be dominated by another
894 // def with the same value.
895 SmallVector<VNInfo*, 8> BackCopies;
Matthias Braun96761952014-12-10 23:07:54 +0000896 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000897 if (VNI->isUnused())
898 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000899 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
900 const DomPair &Dom = NearestDom[ParentVNI->id];
Wei Mi9a16d652016-04-13 03:08:27 +0000901 if (!Dom.first || Dom.second == VNI->def ||
902 NotToHoistSet.count(ParentVNI->id))
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000903 continue;
904 BackCopies.push_back(VNI);
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000905 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000906 }
Wei Mi9a16d652016-04-13 03:08:27 +0000907
908 // If it is not beneficial to hoist all the BackCopies, simply remove
909 // redundant BackCopies in speed mode.
910 if (SpillMode == SM_Speed && !NotToHoistSet.empty())
911 computeRedundantBackCopies(NotToHoistSet, BackCopies);
912
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000913 removeBackCopies(BackCopies);
914}
915
916
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000917/// transferValues - Transfer all possible values to the new live ranges.
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000918/// Values that were rematerialized are left alone, they need LRCalc.extend().
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000919bool SplitEditor::transferValues() {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000920 bool Skipped = false;
921 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Matthias Braun96761952014-12-10 23:07:54 +0000922 for (const LiveRange::Segment &S : Edit->getParent()) {
923 DEBUG(dbgs() << " blit " << S << ':');
924 VNInfo *ParentVNI = S.valno;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000925 // RegAssign has holes where RegIdx 0 should be used.
Matthias Braun96761952014-12-10 23:07:54 +0000926 SlotIndex Start = S.start;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000927 AssignI.advanceTo(Start);
928 do {
929 unsigned RegIdx;
Matthias Braun96761952014-12-10 23:07:54 +0000930 SlotIndex End = S.end;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000931 if (!AssignI.valid()) {
932 RegIdx = 0;
933 } else if (AssignI.start() <= Start) {
934 RegIdx = AssignI.value();
935 if (AssignI.stop() < End) {
936 End = AssignI.stop();
937 ++AssignI;
938 }
939 } else {
940 RegIdx = 0;
941 End = std::min(End, AssignI.start());
942 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000943
944 // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000945 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000946 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000947
948 // Check for a simply defined value that can be blitted directly.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000949 ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
950 if (VNInfo *VNI = VFP.getPointer()) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000951 DEBUG(dbgs() << ':' << VNI->id);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000952 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000953 Start = End;
954 continue;
955 }
956
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000957 // Skip values with forced recomputation.
958 if (VFP.getInt()) {
959 DEBUG(dbgs() << "(recalc)");
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000960 Skipped = true;
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000961 Start = End;
962 continue;
963 }
964
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000965 LiveRangeCalc &LRC = getLRCalc(RegIdx);
966
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000967 // This value has multiple defs in RegIdx, but it wasn't rematerialized,
968 // so the live range is accurate. Add live-in blocks in [Start;End) to the
969 // LiveInBlocks.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000970 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000971 SlotIndex BlockStart, BlockEnd;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000972 std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000973
974 // The first block may be live-in, or it may have its own def.
975 if (Start != BlockStart) {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000976 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000977 assert(VNI && "Missing def for complex mapped value");
978 DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
979 // MBB has its own def. Is it also live-out?
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000980 if (BlockEnd <= End)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000981 LRC.setLiveOutValue(&*MBB, VNI);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000982
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000983 // Skip to the next block for live-in.
984 ++MBB;
985 BlockStart = BlockEnd;
986 }
987
988 // Handle the live-in blocks covered by [Start;End).
989 assert(Start <= BlockStart && "Expected live-in block");
990 while (BlockStart < End) {
991 DEBUG(dbgs() << ">BB#" << MBB->getNumber());
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000992 BlockEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000993 if (BlockStart == ParentVNI->def) {
994 // This block has the def of a parent PHI, so it isn't live-in.
995 assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000996 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000997 assert(VNI && "Missing def for complex mapped parent PHI");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000998 if (End >= BlockEnd)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000999 LRC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001000 } else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001001 // This block needs a live-in value. The last block covered may not
1002 // be live-out.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001003 if (End < BlockEnd)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001004 LRC.addLiveInBlock(LR, MDT[&*MBB], End);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001005 else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001006 // Live-through, and we don't know the value.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001007 LRC.addLiveInBlock(LR, MDT[&*MBB]);
1008 LRC.setLiveOutValue(&*MBB, nullptr);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001009 }
1010 }
1011 BlockStart = BlockEnd;
1012 ++MBB;
1013 }
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001014 Start = End;
Matthias Braun96761952014-12-10 23:07:54 +00001015 } while (Start != S.end);
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001016 DEBUG(dbgs() << '\n');
1017 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001018
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +00001019 LRCalc[0].calculateValues();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +00001020 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +00001021 LRCalc[1].calculateValues();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001022
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001023 return Skipped;
1024}
1025
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001026void SplitEditor::extendPHIKillRanges() {
Wei Mi9a16d652016-04-13 03:08:27 +00001027 // Extend live ranges to be live-out for successor PHI values.
Matthias Braun96761952014-12-10 23:07:54 +00001028 for (const VNInfo *PHIVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001029 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
1030 continue;
1031 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
Matthias Braun2d5c32b2013-10-10 21:28:57 +00001032 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Wei Mi9a16d652016-04-13 03:08:27 +00001033
1034 // Check whether PHI is dead.
1035 const LiveRange::Segment *Segment = LR.getSegmentContaining(PHIVNI->def);
1036 assert(Segment != nullptr && "Missing segment for VNI");
1037 if (Segment->end == PHIVNI->def.getDeadSlot()) {
1038 // This is a dead PHI. Remove it.
1039 LR.removeSegment(*Segment, true);
1040 continue;
1041 }
1042
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001043 LiveRangeCalc &LRC = getLRCalc(RegIdx);
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001044 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
1045 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
1046 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001047 SlotIndex End = LIS.getMBBEndIdx(*PI);
1048 SlotIndex LastUse = End.getPrevSlot();
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001049 // The predecessor may not have a live-out value. That is OK, like an
1050 // undef PHI operand.
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001051 if (Edit->getParent().liveAt(LastUse)) {
1052 assert(RegAssign.lookup(LastUse) == RegIdx &&
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001053 "Different register assignment in phi predecessor");
Matthias Braun2d5c32b2013-10-10 21:28:57 +00001054 LRC.extend(LR, End);
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001055 }
1056 }
1057 }
1058}
1059
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001060/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001061void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001062 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +00001063 RE = MRI.reg_end(); RI != RE;) {
Owen Anderson16c6bf42014-03-13 23:12:04 +00001064 MachineOperand &MO = *RI;
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001065 MachineInstr *MI = MO.getParent();
1066 ++RI;
Eric Christopherede62672011-02-03 06:18:29 +00001067 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001068 if (MI->isDebugValue()) {
1069 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001070 MO.setReg(0);
1071 continue;
1072 }
Jakob Stoklund Olesenf6e03942011-02-09 21:52:09 +00001073
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +00001074 // <undef> operands don't really read the register, so it doesn't matter
1075 // which register we choose. When the use operand is tied to a def, we must
1076 // use the same register as the def, so just do that always.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001077 SlotIndex Idx = LIS.getInstructionIndex(*MI);
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +00001078 if (MO.isDef() || MO.isUndef())
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +00001079 Idx = Idx.getRegSlot(MO.isEarlyClobber());
Eric Christopherede62672011-02-03 06:18:29 +00001080
1081 // Rewrite to the mapped register at Idx.
1082 unsigned RegIdx = RegAssign.lookup(Idx);
Mark Laceyf9ea8852013-08-14 23:50:04 +00001083 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001084 MO.setReg(LI->reg);
Eric Christopherede62672011-02-03 06:18:29 +00001085 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
1086 << Idx << ':' << RegIdx << '\t' << *MI);
1087
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001088 // Extend liveness to Idx if the instruction reads reg.
Jakob Stoklund Olesen73a9eb92011-07-24 20:33:23 +00001089 if (!ExtendRanges || MO.isUndef())
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001090 continue;
1091
1092 // Skip instructions that don't read Reg.
1093 if (MO.isDef()) {
1094 if (!MO.getSubReg() && !MO.isEarlyClobber())
1095 continue;
1096 // We may wan't to extend a live range for a partial redef, or for a use
1097 // tied to an early clobber.
1098 Idx = Idx.getPrevSlot();
1099 if (!Edit->getParent().liveAt(Idx))
1100 continue;
1101 } else
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +00001102 Idx = Idx.getRegSlot(true);
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001103
Matthias Braun2d5c32b2013-10-10 21:28:57 +00001104 getLRCalc(RegIdx).extend(*LI, Idx.getNextSlot());
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001105 }
1106}
1107
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001108void SplitEditor::deleteRematVictims() {
1109 SmallVector<MachineInstr*, 8> Dead;
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001110 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
Mark Laceyf9ea8852013-08-14 23:50:04 +00001111 LiveInterval *LI = &LIS.getInterval(*I);
Matthias Braun96761952014-12-10 23:07:54 +00001112 for (const LiveRange::Segment &S : LI->segments) {
Jakob Stoklund Olesend8f24052011-11-13 22:42:13 +00001113 // Dead defs end at the dead slot.
Matthias Braun96761952014-12-10 23:07:54 +00001114 if (S.end != S.valno->def.getDeadSlot())
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001115 continue;
Wei Mi9a16d652016-04-13 03:08:27 +00001116 if (S.valno->isPHIDef())
1117 continue;
Matthias Braun96761952014-12-10 23:07:54 +00001118 MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001119 assert(MI && "Missing instruction for dead def");
1120 MI->addRegisterDead(LI->reg, &TRI);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001121
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001122 if (!MI->allDefsAreDead())
1123 continue;
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001124
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001125 DEBUG(dbgs() << "All defs dead: " << *MI);
1126 Dead.push_back(MI);
1127 }
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001128 }
1129
1130 if (Dead.empty())
1131 return;
1132
Pete Cooper2bde2f42012-04-02 22:22:53 +00001133 Edit->eliminateDeadDefs(Dead);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001134}
1135
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001136void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001137 ++NumFinished;
Eric Christopher21933532011-02-03 05:40:54 +00001138
Eric Christopherede62672011-02-03 06:18:29 +00001139 // At this point, the live intervals in Edit contain VNInfos corresponding to
1140 // the inserted copies.
1141
1142 // Add the original defs from the parent interval.
Matthias Braun96761952014-12-10 23:07:54 +00001143 for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen3295a992011-02-04 00:59:23 +00001144 if (ParentVNI->isUnused())
1145 continue;
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +00001146 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Jakob Stoklund Olesen97e14e02012-07-27 21:11:14 +00001147 defValue(RegIdx, ParentVNI, ParentVNI->def);
Jakob Stoklund Olesen32210de2011-03-15 21:13:22 +00001148
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001149 // Force rematted values to be recomputed everywhere.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001150 // The new live ranges may be truncated.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001151 if (Edit->didRematerialize(ParentVNI))
1152 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001153 forceRecompute(i, ParentVNI);
Eric Christopherede62672011-02-03 06:18:29 +00001154 }
1155
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001156 // Hoist back-copies to the complement interval when in spill mode.
1157 switch (SpillMode) {
1158 case SM_Partition:
1159 // Leave all back-copies as is.
1160 break;
1161 case SM_Size:
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001162 case SM_Speed:
Wei Mi9a16d652016-04-13 03:08:27 +00001163 // hoistCopies will behave differently between size and speed.
1164 hoistCopies();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001165 }
1166
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001167 // Transfer the simply mapped values, check if any are skipped.
1168 bool Skipped = transferValues();
Wei Mi9a16d652016-04-13 03:08:27 +00001169
1170 // Rewrite virtual registers, possibly extending ranges.
1171 rewriteAssigned(Skipped);
1172
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001173 if (Skipped)
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001174 extendPHIKillRanges();
1175 else
1176 ++NumSimple;
Eric Christopherede62672011-02-03 06:18:29 +00001177
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001178 // Delete defs that were rematted everywhere.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001179 if (Skipped)
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001180 deleteRematVictims();
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001181
Jakob Stoklund Olesen0f1677e2010-10-07 23:34:34 +00001182 // Get rid of unused values and set phi-kill flags.
Mark Laceyf9ea8852013-08-14 23:50:04 +00001183 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) {
1184 LiveInterval &LI = LIS.getInterval(*I);
1185 LI.RenumberValues();
1186 }
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001187
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001188 // Provide a reverse mapping from original indices to Edit ranges.
1189 if (LRMap) {
1190 LRMap->clear();
1191 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1192 LRMap->push_back(i);
1193 }
1194
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001195 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +00001196 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001197 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001198 // Don't use iterators, they are invalidated by create() below.
Matthias Braund3dd1352015-09-22 03:44:41 +00001199 unsigned VReg = Edit->get(i);
1200 LiveInterval &LI = LIS.getInterval(VReg);
1201 SmallVector<LiveInterval*, 8> SplitLIs;
1202 LIS.splitSeparateComponents(LI, SplitLIs);
1203 unsigned Original = VRM.getOriginal(VReg);
1204 for (LiveInterval *SplitLI : SplitLIs)
1205 VRM.setIsSplitFromReg(SplitLI->reg, Original);
1206
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001207 // The new intervals all map back to i.
1208 if (LRMap)
1209 LRMap->resize(Edit->size(), i);
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001210 }
1211
Jakob Stoklund Olesen284c2db2010-08-10 17:07:22 +00001212 // Calculate spill weight and allocation hints for new intervals.
Benjamin Kramere2a1d892013-06-17 19:00:36 +00001213 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001214
1215 assert(!LRMap || LRMap->size() == Edit->size());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +00001216}
1217
1218
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +00001219//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +00001220// Single Block Splitting
1221//===----------------------------------------------------------------------===//
1222
Jakob Stoklund Olesen8627ea92011-08-05 22:20:45 +00001223bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
1224 bool SingleInstrs) const {
1225 // Always split for multiple instructions.
1226 if (!BI.isOneInstr())
1227 return true;
1228 // Don't split for single instructions unless explicitly requested.
1229 if (!SingleInstrs)
1230 return false;
1231 // Splitting a live-through range always makes progress.
1232 if (BI.LiveIn && BI.LiveOut)
1233 return true;
1234 // No point in isolating a copy. It has no register class constraints.
1235 if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
1236 return false;
1237 // Finally, don't isolate an end point that was created by earlier splits.
1238 return isOriginalEndpoint(BI.FirstInstr);
1239}
1240
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001241void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1242 openIntv();
1243 SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001244 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001245 LastSplitPoint));
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001246 if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
1247 useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001248 } else {
1249 // The last use is after the last valid split point.
1250 SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1251 useIntv(SegStart, SegStop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001252 overlapIntv(SegStop, BI.LastInstr);
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001253 }
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001254}
1255
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001256
1257//===----------------------------------------------------------------------===//
1258// Global Live Range Splitting Support
1259//===----------------------------------------------------------------------===//
1260
1261// These methods support a method of global live range splitting that uses a
1262// global algorithm to decide intervals for CFG edges. They will insert split
1263// points and color intervals in basic blocks while avoiding interference.
1264//
1265// Note that splitSingleBlock is also useful for blocks where both CFG edges
1266// are on the stack.
1267
1268void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1269 unsigned IntvIn, SlotIndex LeaveBefore,
1270 unsigned IntvOut, SlotIndex EnterAfter){
1271 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001272 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001273
1274 DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
1275 << ") intf " << LeaveBefore << '-' << EnterAfter
1276 << ", live-through " << IntvIn << " -> " << IntvOut);
1277
1278 assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1279
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001280 assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
1281 assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
1282 assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
1283
1284 MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
1285
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001286 if (!IntvOut) {
1287 DEBUG(dbgs() << ", spill on entry.\n");
1288 //
1289 // <<<<<<<<< Possible LeaveBefore interference.
1290 // |-----------| Live through.
1291 // -____________ Spill on entry.
1292 //
1293 selectIntv(IntvIn);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001294 SlotIndex Idx = leaveIntvAtTop(*MBB);
1295 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1296 (void)Idx;
1297 return;
1298 }
1299
1300 if (!IntvIn) {
1301 DEBUG(dbgs() << ", reload on exit.\n");
1302 //
1303 // >>>>>>> Possible EnterAfter interference.
1304 // |-----------| Live through.
1305 // ___________-- Reload on exit.
1306 //
1307 selectIntv(IntvOut);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001308 SlotIndex Idx = enterIntvAtEnd(*MBB);
1309 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1310 (void)Idx;
1311 return;
1312 }
1313
1314 if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1315 DEBUG(dbgs() << ", straight through.\n");
1316 //
1317 // |-----------| Live through.
1318 // ------------- Straight through, same intv, no interference.
1319 //
1320 selectIntv(IntvOut);
1321 useIntv(Start, Stop);
1322 return;
1323 }
1324
1325 // We cannot legally insert splits after LSP.
1326 SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001327 assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001328
1329 if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1330 LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1331 DEBUG(dbgs() << ", switch avoiding interference.\n");
1332 //
1333 // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
1334 // |-----------| Live through.
1335 // ------======= Switch intervals between interference.
1336 //
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001337 selectIntv(IntvOut);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001338 SlotIndex Idx;
1339 if (LeaveBefore && LeaveBefore < LSP) {
1340 Idx = enterIntvBefore(LeaveBefore);
1341 useIntv(Idx, Stop);
1342 } else {
1343 Idx = enterIntvAtEnd(*MBB);
1344 }
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001345 selectIntv(IntvIn);
1346 useIntv(Start, Idx);
1347 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1348 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1349 return;
1350 }
1351
1352 DEBUG(dbgs() << ", create local intv for interference.\n");
1353 //
1354 // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
1355 // |-----------| Live through.
1356 // ==---------== Switch intervals before/after interference.
1357 //
1358 assert(LeaveBefore <= EnterAfter && "Missed case");
1359
1360 selectIntv(IntvOut);
1361 SlotIndex Idx = enterIntvAfter(EnterAfter);
1362 useIntv(Idx, Stop);
1363 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1364
1365 selectIntv(IntvIn);
1366 Idx = leaveIntvBefore(LeaveBefore);
1367 useIntv(Start, Idx);
1368 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1369}
1370
1371
1372void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1373 unsigned IntvIn, SlotIndex LeaveBefore) {
1374 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001375 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001376
1377 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001378 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001379 << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
1380 << (BI.LiveOut ? ", stack-out" : ", killed in block"));
1381
1382 assert(IntvIn && "Must have register in");
1383 assert(BI.LiveIn && "Must be live-in");
1384 assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1385
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001386 if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001387 DEBUG(dbgs() << " before interference.\n");
1388 //
1389 // <<< Interference after kill.
1390 // |---o---x | Killed in block.
1391 // ========= Use IntvIn everywhere.
1392 //
1393 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001394 useIntv(Start, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001395 return;
1396 }
1397
1398 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1399
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001400 if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001401 //
1402 // <<< Possible interference after last use.
1403 // |---o---o---| Live-out on stack.
1404 // =========____ Leave IntvIn after last use.
1405 //
1406 // < Interference after last use.
1407 // |---o---o--o| Live-out on stack, late last use.
1408 // ============ Copy to stack after LSP, overlap IntvIn.
1409 // \_____ Stack interval is live-out.
1410 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001411 if (BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001412 DEBUG(dbgs() << ", spill after last use before interference.\n");
1413 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001414 SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001415 useIntv(Start, Idx);
1416 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1417 } else {
1418 DEBUG(dbgs() << ", spill before last split point.\n");
1419 selectIntv(IntvIn);
Jakob Stoklund Olesen37e3a132011-07-16 00:13:30 +00001420 SlotIndex Idx = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001421 overlapIntv(Idx, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001422 useIntv(Start, Idx);
1423 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1424 }
1425 return;
1426 }
1427
1428 // The interference is overlapping somewhere we wanted to use IntvIn. That
1429 // means we need to create a local interval that can be allocated a
1430 // different register.
1431 unsigned LocalIntv = openIntv();
Matt Beaumont-Gay26909d82011-07-16 04:18:47 +00001432 (void)LocalIntv;
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001433 DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
1434
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001435 if (!BI.LiveOut || BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001436 //
1437 // <<<<<<< Interference overlapping uses.
1438 // |---o---o---| Live-out on stack.
1439 // =====----____ Leave IntvIn before interference, then spill.
1440 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001441 SlotIndex To = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001442 SlotIndex From = enterIntvBefore(LeaveBefore);
1443 useIntv(From, To);
1444 selectIntv(IntvIn);
1445 useIntv(Start, From);
1446 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1447 return;
1448 }
1449
1450 // <<<<<<< Interference overlapping uses.
1451 // |---o---o--o| Live-out on stack, late last use.
1452 // =====------- Copy to stack before LSP, overlap LocalIntv.
1453 // \_____ Stack interval is live-out.
1454 //
1455 SlotIndex To = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001456 overlapIntv(To, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001457 SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1458 useIntv(From, To);
1459 selectIntv(IntvIn);
1460 useIntv(Start, From);
1461 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1462}
1463
1464void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1465 unsigned IntvOut, SlotIndex EnterAfter) {
1466 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001467 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001468
1469 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001470 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001471 << ", reg-out " << IntvOut << ", enter after " << EnterAfter
1472 << (BI.LiveIn ? ", stack-in" : ", defined in block"));
1473
1474 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1475
1476 assert(IntvOut && "Must have register out");
1477 assert(BI.LiveOut && "Must be live-out");
1478 assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1479
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001480 if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001481 DEBUG(dbgs() << " after interference.\n");
1482 //
1483 // >>>> Interference before def.
1484 // | o---o---| Defined in block.
1485 // ========= Use IntvOut everywhere.
1486 //
1487 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001488 useIntv(BI.FirstInstr, Stop);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001489 return;
1490 }
1491
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001492 if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001493 DEBUG(dbgs() << ", reload after interference.\n");
1494 //
1495 // >>>> Interference before def.
1496 // |---o---o---| Live-through, stack-in.
1497 // ____========= Enter IntvOut before first use.
1498 //
1499 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001500 SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001501 useIntv(Idx, Stop);
1502 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1503 return;
1504 }
1505
1506 // The interference is overlapping somewhere we wanted to use IntvOut. That
1507 // means we need to create a local interval that can be allocated a
1508 // different register.
1509 DEBUG(dbgs() << ", interference overlaps uses.\n");
1510 //
1511 // >>>>>>> Interference overlapping uses.
1512 // |---o---o---| Live-through, stack-in.
1513 // ____---====== Create local interval for interference range.
1514 //
1515 selectIntv(IntvOut);
1516 SlotIndex Idx = enterIntvAfter(EnterAfter);
1517 useIntv(Idx, Stop);
1518 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1519
1520 openIntv();
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001521 SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001522 useIntv(From, Idx);
1523}