blob: 07be24b18dd557cb8b99652da8b94f95b4d3d537 [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)
Wei Mif3c8f532016-05-23 19:39:19 +000046 : LIS(lis), LastInsertPoint(BBNum) {}
Wei Mi35ee9332016-05-11 22:28:29 +000047
48SlotIndex
Wei Mif3c8f532016-05-23 19:39:19 +000049InsertPointAnalysis::computeLastInsertPoint(const LiveInterval &CurLI,
50 const MachineBasicBlock &MBB) {
Wei Mi35ee9332016-05-11 22:28:29 +000051 unsigned Num = MBB.getNumber();
52 std::pair<SlotIndex, SlotIndex> &LIP = LastInsertPoint[Num];
53 SlotIndex MBBEnd = LIS.getMBBEndIdx(&MBB);
54
55 SmallVector<const MachineBasicBlock *, 1> EHPadSucessors;
56 for (const MachineBasicBlock *SMBB : MBB.successors())
57 if (SMBB->isEHPad())
58 EHPadSucessors.push_back(SMBB);
59
60 // Compute insert points on the first call. The pair is independent of the
61 // current live interval.
62 if (!LIP.first.isValid()) {
63 MachineBasicBlock::const_iterator FirstTerm = MBB.getFirstTerminator();
64 if (FirstTerm == MBB.end())
65 LIP.first = MBBEnd;
66 else
67 LIP.first = LIS.getInstructionIndex(*FirstTerm);
68
69 // If there is a landing pad successor, also find the call instruction.
70 if (EHPadSucessors.empty())
71 return LIP.first;
72 // There may not be a call instruction (?) in which case we ignore LPad.
73 LIP.second = LIP.first;
74 for (MachineBasicBlock::const_iterator I = MBB.end(), E = MBB.begin();
75 I != E;) {
76 --I;
77 if (I->isCall()) {
78 LIP.second = LIS.getInstructionIndex(*I);
79 break;
80 }
81 }
82 }
83
84 // If CurLI is live into a landing pad successor, move the last insert point
85 // back to the call that may throw.
86 if (!LIP.second)
87 return LIP.first;
88
Wei Mi35ee9332016-05-11 22:28:29 +000089 if (none_of(EHPadSucessors, [&](const MachineBasicBlock *EHPad) {
Wei Mif3c8f532016-05-23 19:39:19 +000090 return LIS.isLiveInToMBB(CurLI, EHPad);
Wei Mi35ee9332016-05-11 22:28:29 +000091 }))
92 return LIP.first;
93
94 // Find the value leaving MBB.
Wei Mif3c8f532016-05-23 19:39:19 +000095 const VNInfo *VNI = CurLI.getVNInfoBefore(MBBEnd);
Wei Mi35ee9332016-05-11 22:28:29 +000096 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
Wei Mif3c8f532016-05-23 19:39:19 +0000112InsertPointAnalysis::getLastInsertPointIter(const LiveInterval &CurLI,
113 MachineBasicBlock &MBB) {
114 SlotIndex LIP = getLastInsertPoint(CurLI, MBB);
Wei Mi35ee9332016-05-11 22:28:29 +0000115 if (LIP == LIS.getMBBEndIdx(&MBB))
116 return MBB.end();
117 return LIS.getInstructionFromIndex(LIP);
118}
119
120//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000121// Split Analysis
122//===----------------------------------------------------------------------===//
123
Eric Christopherd9134482014-08-04 21:25:23 +0000124SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
Jakob Stoklund Olesen0fef9dd2010-07-20 23:50:15 +0000125 const MachineLoopInfo &mli)
Eric Christopherd9134482014-08-04 21:25:23 +0000126 : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli),
Eric Christopherfc6de422014-08-05 02:39:49 +0000127 TII(*MF.getSubtarget().getInstrInfo()), CurLI(nullptr),
Wei Mi35ee9332016-05-11 22:28:29 +0000128 IPA(lis, MF.getNumBlockIDs()) {}
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000129
130void SplitAnalysis::clear() {
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000131 UseSlots.clear();
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000132 UseBlocks.clear();
133 ThroughBlocks.clear();
Craig Topperc0196b12014-04-14 00:51:57 +0000134 CurLI = nullptr;
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000135 DidRepairRange = false;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000136}
137
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000138/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000139void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000140 assert(UseSlots.empty() && "Call clear first");
141
142 // First get all the defs from the interval values. This provides the correct
143 // slots for early clobbers.
Matthias Braun96761952014-12-10 23:07:54 +0000144 for (const VNInfo *VNI : CurLI->valnos)
145 if (!VNI->isPHIDef() && !VNI->isUnused())
146 UseSlots.push_back(VNI->def);
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000147
148 // Get use slots form the use-def chain.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000149 const MachineRegisterInfo &MRI = MF.getRegInfo();
Owen Andersonb36376e2014-03-17 19:36:09 +0000150 for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg))
151 if (!MO.isUndef())
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000152 UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot());
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000153
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000154 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000155
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000156 // Remove duplicates, keeping the smaller slot for each instruction.
157 // That is what we want for early clobbers.
158 UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
159 SlotIndex::isSameInstr),
160 UseSlots.end());
161
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000162 // Compute per-live block info.
163 if (!calcLiveBlockInfo()) {
164 // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
Rafael Espindola676c4052011-06-26 22:34:10 +0000165 // I am looking at you, RegisterCoalescer!
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000166 DidRepairRange = true;
Jakob Stoklund Olesen50215af2011-05-10 17:37:41 +0000167 ++NumRepairs;
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000168 DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
169 const_cast<LiveIntervals&>(LIS)
170 .shrinkToUses(const_cast<LiveInterval*>(CurLI));
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000171 UseBlocks.clear();
172 ThroughBlocks.clear();
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000173 bool fixed = calcLiveBlockInfo();
174 (void)fixed;
175 assert(fixed && "Couldn't fix broken live interval");
176 }
177
Jakob Stoklund Olesenbd6b86e2011-03-27 22:49:23 +0000178 DEBUG(dbgs() << "Analyze counted "
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000179 << UseSlots.size() << " instrs in "
180 << UseBlocks.size() << " blocks, through "
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000181 << NumThroughBlocks << " blocks.\n");
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000182}
183
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000184/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
185/// where CurLI is live.
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000186bool SplitAnalysis::calcLiveBlockInfo() {
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000187 ThroughBlocks.resize(MF.getNumBlockIDs());
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000188 NumThroughBlocks = NumGapBlocks = 0;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000189 if (CurLI->empty())
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000190 return true;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000191
192 LiveInterval::const_iterator LVI = CurLI->begin();
193 LiveInterval::const_iterator LVE = CurLI->end();
194
195 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
196 UseI = UseSlots.begin();
197 UseE = UseSlots.end();
198
199 // Loop over basic blocks where CurLI is live.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000200 MachineFunction::iterator MFI =
201 LIS.getMBBFromIndex(LVI->start)->getIterator();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000202 for (;;) {
203 BlockInfo BI;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000204 BI.MBB = &*MFI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000205 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000206 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000207
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000208 // If the block contains no uses, the range must be live through. At one
Rafael Espindola676c4052011-06-26 22:34:10 +0000209 // point, RegisterCoalescer could create dangling ranges that ended
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000210 // mid-block.
211 if (UseI == UseE || *UseI >= Stop) {
212 ++NumThroughBlocks;
213 ThroughBlocks.set(BI.MBB->getNumber());
214 // The range shouldn't end mid-block if there are no uses. This shouldn't
215 // happen.
216 if (LVI->end < Stop)
217 return false;
218 } else {
219 // This block has uses. Find the first and last uses in the block.
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000220 BI.FirstInstr = *UseI;
221 assert(BI.FirstInstr >= Start);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000222 do ++UseI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000223 while (UseI != UseE && *UseI < Stop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000224 BI.LastInstr = UseI[-1];
225 assert(BI.LastInstr < Stop);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000226
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000227 // LVI is the first live segment overlapping MBB.
228 BI.LiveIn = LVI->start <= Start;
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000229
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000230 // When not live in, the first use should be a def.
231 if (!BI.LiveIn) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000232 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000233 assert(LVI->start == BI.FirstInstr && "First instr should be a def");
234 BI.FirstDef = BI.FirstInstr;
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000235 }
236
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000237 // Look for gaps in the live range.
238 BI.LiveOut = true;
239 while (LVI->end < Stop) {
240 SlotIndex LastStop = LVI->end;
241 if (++LVI == LVE || LVI->start >= Stop) {
242 BI.LiveOut = false;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000243 BI.LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000244 break;
245 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000246
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000247 if (LastStop < LVI->start) {
248 // There is a gap in the live range. Create duplicate entries for the
249 // live-in snippet and the live-out snippet.
250 ++NumGapBlocks;
251
252 // Push the Live-in part.
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000253 BI.LiveOut = false;
254 UseBlocks.push_back(BI);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000255 UseBlocks.back().LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000256
257 // Set up BI for the live-out part.
258 BI.LiveIn = false;
259 BI.LiveOut = true;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000260 BI.FirstInstr = BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000261 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000262
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000263 // A Segment that starts in the middle of the block must be a def.
264 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000265 if (!BI.FirstDef)
266 BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000267 }
268
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000269 UseBlocks.push_back(BI);
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000270
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000271 // LVI is now at LVE or LVI->end >= Stop.
272 if (LVI == LVE)
273 break;
274 }
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000275
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000276 // Live segment ends exactly at Stop. Move to the next segment.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000277 if (LVI->end == Stop && ++LVI == LVE)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000278 break;
279
280 // Pick the next basic block.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000281 if (LVI->start < Stop)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000282 ++MFI;
283 else
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000284 MFI = LIS.getMBBFromIndex(LVI->start)->getIterator();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000285 }
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000286
287 assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000288 return true;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000289}
290
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000291unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
292 if (cli->empty())
293 return 0;
294 LiveInterval *li = const_cast<LiveInterval*>(cli);
295 LiveInterval::iterator LVI = li->begin();
296 LiveInterval::iterator LVE = li->end();
297 unsigned Count = 0;
298
299 // Loop over basic blocks where li is live.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000300 MachineFunction::const_iterator MFI =
301 LIS.getMBBFromIndex(LVI->start)->getIterator();
302 SlotIndex Stop = LIS.getMBBEndIdx(&*MFI);
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000303 for (;;) {
304 ++Count;
305 LVI = li->advanceTo(LVI, Stop);
306 if (LVI == LVE)
307 return Count;
308 do {
309 ++MFI;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000310 Stop = LIS.getMBBEndIdx(&*MFI);
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000311 } while (Stop <= LVI->start);
312 }
313}
314
Jakob Stoklund Olesen60a26a62011-02-21 23:09:46 +0000315bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
316 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
317 const LiveInterval &Orig = LIS.getInterval(OrigReg);
318 assert(!Orig.empty() && "Splitting empty interval?");
319 LiveInterval::const_iterator I = Orig.find(Idx);
320
321 // Range containing Idx should begin at Idx.
322 if (I != Orig.end() && I->start <= Idx)
323 return I->start == Idx;
324
325 // Range does not contain Idx, previous must end at Idx.
326 return I != Orig.begin() && (--I)->end == Idx;
327}
328
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000329void SplitAnalysis::analyze(const LiveInterval *li) {
330 clear();
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000331 CurLI = 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.
Wei Mic0223702016-07-08 21:08:09 +0000341SplitEditor::SplitEditor(SplitAnalysis &sa, AliasAnalysis &aa,
342 LiveIntervals &lis, VirtRegMap &vrm,
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000343 MachineDominatorTree &mdt,
344 MachineBlockFrequencyInfo &mbfi)
Wei Mic0223702016-07-08 21:08:09 +0000345 : SA(sa), AA(aa), LIS(lis), VRM(vrm),
346 MRI(vrm.getMachineFunction().getRegInfo()), MDT(mdt),
347 TII(*vrm.getMachineFunction().getSubtarget().getInstrInfo()),
Eric Christopher60621802014-10-14 07:22:00 +0000348 TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()),
Eric Christopherd9134482014-08-04 21:25:23 +0000349 MBFI(mbfi), Edit(nullptr), OpenIdx(0), SpillMode(SM_Partition),
350 RegAssign(Allocator) {}
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000351
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000352void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
353 Edit = &LRE;
354 SpillMode = SM;
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000355 OpenIdx = 0;
356 RegAssign.clear();
357 Values.clear();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000358
359 // Reset the LiveRangeCalc instances needed for this spill mode.
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000360 LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
361 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000362 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000363 LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
364 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000365
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000366 // We don't need an AliasAnalysis since we will only be performing
367 // cheap-as-a-copy remats anyway.
Craig Topperc0196b12014-04-14 00:51:57 +0000368 Edit->anyRematerializable(nullptr);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000369}
370
Manman Ren19f49ac2012-09-11 22:23:19 +0000371#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000372LLVM_DUMP_METHOD void SplitEditor::dump() const {
Eric Christopherede62672011-02-03 06:18:29 +0000373 if (RegAssign.empty()) {
374 dbgs() << " empty\n";
375 return;
376 }
377
378 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
379 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
380 dbgs() << '\n';
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000381}
Manman Ren742534c2012-09-06 19:06:06 +0000382#endif
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000383
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000384VNInfo *SplitEditor::defValue(unsigned RegIdx,
385 const VNInfo *ParentVNI,
386 SlotIndex Idx) {
387 assert(ParentVNI && "Mapping NULL value");
388 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000389 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
Mark Laceyf9ea8852013-08-14 23:50:04 +0000390 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000391
392 // Create a new value.
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000393 VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000394
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000395 // Use insert for lookup, so we can add missing values with a second lookup.
396 std::pair<ValueMap::iterator, bool> InsP =
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000397 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id),
398 ValueForcePair(VNI, false)));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000399
400 // This was the first time (RegIdx, ParentVNI) was mapped.
401 // Keep it as a simple def without any liveness.
402 if (InsP.second)
403 return VNI;
404
405 // If the previous value was a simple mapping, add liveness for it now.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000406 if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000407 SlotIndex Def = OldVNI->def;
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000408 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), OldVNI));
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000409 // No longer a simple mapping. Switch to a complex, non-forced mapping.
410 InsP.first->second = ValueForcePair();
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000411 }
412
413 // This is a complex mapping, add liveness for VNI
414 SlotIndex Def = VNI->def;
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000415 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000416
417 return VNI;
418}
419
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000420void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000421 assert(ParentVNI && "Mapping NULL value");
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000422 ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
423 VNInfo *VNI = VFP.getPointer();
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000424
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000425 // ParentVNI was either unmapped or already complex mapped. Either way, just
426 // set the force bit.
427 if (!VNI) {
428 VFP.setInt(true);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000429 return;
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000430 }
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000431
432 // This was previously a single mapping. Make sure the old def is represented
433 // by a trivial live range.
434 SlotIndex Def = VNI->def;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000435 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000436 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000437 // Mark as complex mapped, forced.
Craig Topperc0196b12014-04-14 00:51:57 +0000438 VFP = ValueForcePair(nullptr, true);
Jakob Stoklund Olesen4484f992011-09-13 18:05:29 +0000439}
440
Eric Christopherede62672011-02-03 06:18:29 +0000441VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000442 VNInfo *ParentVNI,
443 SlotIndex UseIdx,
444 MachineBasicBlock &MBB,
445 MachineBasicBlock::iterator I) {
Craig Topperc0196b12014-04-14 00:51:57 +0000446 MachineInstr *CopyMI = nullptr;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000447 SlotIndex Def;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000448 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000449
Jakob Stoklund Olesen7d406792011-05-02 05:29:58 +0000450 // We may be trying to avoid interference that ends at a deleted instruction,
451 // so always begin RegIdx 0 early and all others late.
452 bool Late = RegIdx != 0;
453
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000454 // Attempt cheap-as-a-copy rematerialization.
Wei Mi9a16d652016-04-13 03:08:27 +0000455 unsigned Original = VRM.getOriginal(Edit->get(RegIdx));
456 LiveInterval &OrigLI = LIS.getInterval(Original);
457 VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000458 LiveRangeEdit::Remat RM(ParentVNI);
Wei Mi9a16d652016-04-13 03:08:27 +0000459 RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
460
461 if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) {
Pete Cooper2bde2f42012-04-02 22:22:53 +0000462 Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late);
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000463 ++NumRemats;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000464 } else {
465 // Can't remat, just insert a copy from parent.
Eric Christopherede62672011-02-03 06:18:29 +0000466 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000467 .addReg(Edit->getReg());
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000468 Def = LIS.getSlotIndexes()
469 ->insertMachineInstrInMaps(*CopyMI, Late)
470 .getRegSlot();
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000471 ++NumCopies;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000472 }
473
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000474 // Define the value in Reg.
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000475 return defValue(RegIdx, ParentVNI, Def);
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000476}
477
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000478/// Create a new virtual register and live interval.
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000479unsigned SplitEditor::openIntv() {
Eric Christopherede62672011-02-03 06:18:29 +0000480 // Create the complement as index 0.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000481 if (Edit->empty())
Mark Lacey9d8103d2013-08-14 23:50:16 +0000482 Edit->createEmptyInterval();
Eric Christopherede62672011-02-03 06:18:29 +0000483
484 // Create the open interval.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000485 OpenIdx = Edit->size();
Mark Lacey9d8103d2013-08-14 23:50:16 +0000486 Edit->createEmptyInterval();
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000487 return OpenIdx;
488}
489
490void SplitEditor::selectIntv(unsigned Idx) {
491 assert(Idx != 0 && "Cannot select the complement interval");
492 assert(Idx < Edit->size() && "Can only select previously opened interval");
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000493 DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000494 OpenIdx = Idx;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000495}
496
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000497SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000498 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000499 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000500 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000501 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000502 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000503 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000504 return Idx;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000505 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000506 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000507 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000508 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000509
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000510 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
511 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000512}
513
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000514SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
515 assert(OpenIdx && "openIntv not called before enterIntvAfter");
516 DEBUG(dbgs() << " enterIntvAfter " << Idx);
517 Idx = Idx.getBoundaryIndex();
518 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
519 if (!ParentVNI) {
520 DEBUG(dbgs() << ": not live\n");
521 return Idx;
522 }
523 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
524 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
525 assert(MI && "enterIntvAfter called with invalid index");
526
527 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000528 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000529 return VNI->def;
530}
531
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000532SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000533 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000534 SlotIndex End = LIS.getMBBEndIdx(&MBB);
535 SlotIndex Last = End.getPrevSlot();
536 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000537 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000538 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000539 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000540 return End;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000541 }
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000542 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000543 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000544 SA.getLastSplitPointIter(&MBB));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000545 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopherede62672011-02-03 06:18:29 +0000546 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000547 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000548}
549
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000550/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000551void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000552 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000553}
554
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000555void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopherede62672011-02-03 06:18:29 +0000556 assert(OpenIdx && "openIntv not called before useIntv");
557 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
558 RegAssign.insert(Start, End, OpenIdx);
559 DEBUG(dump());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000560}
561
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000562SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000563 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000564 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000565
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000566 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000567 SlotIndex Boundary = Idx.getBoundaryIndex();
568 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000569 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000570 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000571 return Boundary.getNextSlot();
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000572 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000573 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000574 MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
Jakob Stoklund Olesen3d11c8e2011-02-08 18:50:18 +0000575 assert(MI && "No instruction at index");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000576
577 // In spill mode, make live ranges as short as possible by inserting the copy
578 // before MI. This is only possible if that instruction doesn't redefine the
579 // value. The inserted COPY is not a kill, and we don't need to recompute
580 // the source live range. The spiller also won't try to hoist this copy.
581 if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
582 MI->readsVirtualRegister(Edit->getReg())) {
583 forceRecompute(0, ParentVNI);
584 defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
585 return Idx;
586 }
587
588 VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000589 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000590 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000591}
592
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000593SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
594 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
595 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
596
597 // The interval must be live into the instruction at Idx.
Jakob Stoklund Olesenc45d38e2011-07-18 18:47:13 +0000598 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000599 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000600 if (!ParentVNI) {
601 DEBUG(dbgs() << ": not live\n");
602 return Idx.getNextSlot();
603 }
604 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
605
606 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
607 assert(MI && "No instruction at index");
608 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
609 return VNI->def;
610}
611
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000612SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000613 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000614 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000615 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000616
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000617 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000618 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000619 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000620 return Start;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000621 }
622
Eric Christopherede62672011-02-03 06:18:29 +0000623 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000624 MBB.SkipPHIsAndLabels(MBB.begin()));
Eric Christopherede62672011-02-03 06:18:29 +0000625 RegAssign.insert(Start, VNI->def, OpenIdx);
626 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000627 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000628}
629
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000630void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
631 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000632 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesend7bcf432011-11-14 01:39:36 +0000633 assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000634 "Parent changes value in extended range");
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000635 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
636 "Range cannot span basic blocks");
637
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000638 // The complement interval will be extended as needed by LRCalc.extend().
Jakob Stoklund Olesen5c482cd2011-04-05 23:43:14 +0000639 if (ParentVNI)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000640 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000641 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
642 RegAssign.insert(Start, End, OpenIdx);
643 DEBUG(dump());
644}
645
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000646//===----------------------------------------------------------------------===//
647// Spill modes
648//===----------------------------------------------------------------------===//
649
650void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
Mark Laceyf9ea8852013-08-14 23:50:04 +0000651 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000652 DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
653 RegAssignMap::iterator AssignI;
654 AssignI.setMap(RegAssign);
655
656 for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
Matthias Braun311730a2015-01-21 19:02:30 +0000657 SlotIndex Def = Copies[i]->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000658 MachineInstr *MI = LIS.getInstructionFromIndex(Def);
659 assert(MI && "No instruction for back-copy");
660
661 MachineBasicBlock *MBB = MI->getParent();
662 MachineBasicBlock::iterator MBBI(MI);
663 bool AtBegin;
664 do AtBegin = MBBI == MBB->begin();
665 while (!AtBegin && (--MBBI)->isDebugValue());
666
667 DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
Matthias Braun311730a2015-01-21 19:02:30 +0000668 LIS.removeVRegDefAt(*LI, Def);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000669 LIS.RemoveMachineInstrFromMaps(*MI);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000670 MI->eraseFromParent();
671
Matthias Braun311730a2015-01-21 19:02:30 +0000672 // Adjust RegAssign if a register assignment is killed at Def. We want to
673 // avoid calculating the live range of the source register if possible.
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000674 AssignI.find(Def.getPrevSlot());
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000675 if (!AssignI.valid() || AssignI.start() >= Def)
676 continue;
677 // If MI doesn't kill the assigned register, just leave it.
678 if (AssignI.stop() != Def)
679 continue;
680 unsigned RegIdx = AssignI.value();
681 if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
682 DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n');
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000683 forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000684 } else {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000685 SlotIndex Kill = LIS.getInstructionIndex(*MBBI).getRegSlot();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000686 DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
687 AssignI.setStop(Kill);
688 }
689 }
690}
691
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000692MachineBasicBlock*
693SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
694 MachineBasicBlock *DefMBB) {
695 if (MBB == DefMBB)
696 return MBB;
697 assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
698
699 const MachineLoopInfo &Loops = SA.Loops;
700 const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
701 MachineDomTreeNode *DefDomNode = MDT[DefMBB];
702
703 // Best candidate so far.
704 MachineBasicBlock *BestMBB = MBB;
705 unsigned BestDepth = UINT_MAX;
706
707 for (;;) {
708 const MachineLoop *Loop = Loops.getLoopFor(MBB);
709
710 // MBB isn't in a loop, it doesn't get any better. All dominators have a
711 // higher frequency by definition.
712 if (!Loop) {
713 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
714 << MBB->getNumber() << " at depth 0\n");
715 return MBB;
716 }
717
718 // We'll never be able to exit the DefLoop.
719 if (Loop == DefLoop) {
720 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
721 << MBB->getNumber() << " in the same loop\n");
722 return MBB;
723 }
724
725 // Least busy dominator seen so far.
726 unsigned Depth = Loop->getLoopDepth();
727 if (Depth < BestDepth) {
728 BestMBB = MBB;
729 BestDepth = Depth;
730 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
731 << MBB->getNumber() << " at depth " << Depth << '\n');
732 }
733
734 // Leave loop by going to the immediate dominator of the loop header.
735 // This is a bigger stride than simply walking up the dominator tree.
736 MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
737
738 // Too far up the dominator tree?
739 if (!IDom || !MDT.dominates(DefDomNode, IDom))
740 return BestMBB;
741
742 MBB = IDom->getBlock();
743 }
744}
745
Wei Mi9a16d652016-04-13 03:08:27 +0000746void SplitEditor::computeRedundantBackCopies(
747 DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) {
748 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
749 LiveInterval *Parent = &Edit->getParent();
750 SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums());
751 SmallPtrSet<VNInfo *, 8> DominatedVNIs;
752
753 // Aggregate VNIs having the same value as ParentVNI.
754 for (VNInfo *VNI : LI->valnos) {
755 if (VNI->isUnused())
756 continue;
757 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
758 EqualVNs[ParentVNI->id].insert(VNI);
759 }
760
761 // For VNI aggregation of each ParentVNI, collect dominated, i.e.,
762 // redundant VNIs to BackCopies.
763 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
764 VNInfo *ParentVNI = Parent->getValNumInfo(i);
765 if (!NotToHoistSet.count(ParentVNI->id))
766 continue;
767 SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin();
768 SmallPtrSetIterator<VNInfo *> It2 = It1;
769 for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) {
770 It2 = It1;
771 for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) {
772 if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2))
773 continue;
774
775 MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def);
776 MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def);
777 if (MBB1 == MBB2) {
778 DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1));
779 } else if (MDT.dominates(MBB1, MBB2)) {
780 DominatedVNIs.insert(*It2);
781 } else if (MDT.dominates(MBB2, MBB1)) {
782 DominatedVNIs.insert(*It1);
783 }
784 }
785 }
786 if (!DominatedVNIs.empty()) {
787 forceRecompute(0, ParentVNI);
788 for (auto VNI : DominatedVNIs) {
789 BackCopies.push_back(VNI);
790 }
791 DominatedVNIs.clear();
792 }
793 }
794}
795
796/// For SM_Size mode, find a common dominator for all the back-copies for
797/// the same ParentVNI and hoist the backcopies to the dominator BB.
798/// For SM_Speed mode, if the common dominator is hot and it is not beneficial
799/// to do the hoisting, simply remove the dominated backcopies for the same
800/// ParentVNI.
801void SplitEditor::hoistCopies() {
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000802 // Get the complement interval, always RegIdx 0.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000803 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000804 LiveInterval *Parent = &Edit->getParent();
805
806 // Track the nearest common dominator for all back-copies for each ParentVNI,
807 // indexed by ParentVNI->id.
808 typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
809 SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
Wei Mi9a16d652016-04-13 03:08:27 +0000810 // The total cost of all the back-copies for each ParentVNI.
811 SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums());
812 // The ParentVNI->id set for which hoisting back-copies are not beneficial
813 // for Speed.
814 DenseSet<unsigned> NotToHoistSet;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000815
816 // Find the nearest common dominator for parent values with multiple
817 // back-copies. If a single back-copy dominates, put it in DomPair.second.
Matthias Braun96761952014-12-10 23:07:54 +0000818 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000819 if (VNI->isUnused())
820 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000821 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
822 assert(ParentVNI && "Parent not live at complement def");
823
824 // Don't hoist remats. The complement is probably going to disappear
825 // completely anyway.
826 if (Edit->didRematerialize(ParentVNI))
827 continue;
828
829 MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
Wei Mi9a16d652016-04-13 03:08:27 +0000830
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000831 DomPair &Dom = NearestDom[ParentVNI->id];
832
833 // Keep directly defined parent values. This is either a PHI or an
834 // instruction in the complement range. All other copies of ParentVNI
835 // should be eliminated.
836 if (VNI->def == ParentVNI->def) {
837 DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
838 Dom = DomPair(ValMBB, VNI->def);
839 continue;
840 }
841 // Skip the singly mapped values. There is nothing to gain from hoisting a
842 // single back-copy.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000843 if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000844 DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
845 continue;
846 }
847
848 if (!Dom.first) {
849 // First time we see ParentVNI. VNI dominates itself.
850 Dom = DomPair(ValMBB, VNI->def);
851 } else if (Dom.first == ValMBB) {
852 // Two defs in the same block. Pick the earlier def.
853 if (!Dom.second.isValid() || VNI->def < Dom.second)
854 Dom.second = VNI->def;
855 } else {
856 // Different basic blocks. Check if one dominates.
857 MachineBasicBlock *Near =
858 MDT.findNearestCommonDominator(Dom.first, ValMBB);
859 if (Near == ValMBB)
860 // Def ValMBB dominates.
861 Dom = DomPair(ValMBB, VNI->def);
862 else if (Near != Dom.first)
863 // None dominate. Hoist to common dominator, need new def.
864 Dom = DomPair(Near, SlotIndex());
Wei Mi9a16d652016-04-13 03:08:27 +0000865 Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000866 }
867
868 DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
869 << " for parent " << ParentVNI->id << '@' << ParentVNI->def
870 << " hoist to BB#" << Dom.first->getNumber() << ' '
871 << Dom.second << '\n');
872 }
873
874 // Insert the hoisted copies.
875 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
876 DomPair &Dom = NearestDom[i];
877 if (!Dom.first || Dom.second.isValid())
878 continue;
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000879 // This value needs a hoisted copy inserted at the end of Dom.first.
880 VNInfo *ParentVNI = Parent->getValNumInfo(i);
881 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
882 // Get a less loopy dominator than Dom.first.
883 Dom.first = findShallowDominator(Dom.first, DefMBB);
Wei Mi9a16d652016-04-13 03:08:27 +0000884 if (SpillMode == SM_Speed &&
885 MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) {
886 NotToHoistSet.insert(ParentVNI->id);
887 continue;
888 }
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000889 SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
890 Dom.second =
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000891 defFromParent(0, ParentVNI, Last, *Dom.first,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000892 SA.getLastSplitPointIter(Dom.first))->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000893 }
894
895 // Remove redundant back-copies that are now known to be dominated by another
896 // def with the same value.
897 SmallVector<VNInfo*, 8> BackCopies;
Matthias Braun96761952014-12-10 23:07:54 +0000898 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000899 if (VNI->isUnused())
900 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000901 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
902 const DomPair &Dom = NearestDom[ParentVNI->id];
Wei Mi9a16d652016-04-13 03:08:27 +0000903 if (!Dom.first || Dom.second == VNI->def ||
904 NotToHoistSet.count(ParentVNI->id))
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000905 continue;
906 BackCopies.push_back(VNI);
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000907 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000908 }
Wei Mi9a16d652016-04-13 03:08:27 +0000909
910 // If it is not beneficial to hoist all the BackCopies, simply remove
911 // redundant BackCopies in speed mode.
912 if (SpillMode == SM_Speed && !NotToHoistSet.empty())
913 computeRedundantBackCopies(NotToHoistSet, BackCopies);
914
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000915 removeBackCopies(BackCopies);
916}
917
918
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000919/// transferValues - Transfer all possible values to the new live ranges.
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000920/// Values that were rematerialized are left alone, they need LRCalc.extend().
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000921bool SplitEditor::transferValues() {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000922 bool Skipped = false;
923 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Matthias Braun96761952014-12-10 23:07:54 +0000924 for (const LiveRange::Segment &S : Edit->getParent()) {
925 DEBUG(dbgs() << " blit " << S << ':');
926 VNInfo *ParentVNI = S.valno;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000927 // RegAssign has holes where RegIdx 0 should be used.
Matthias Braun96761952014-12-10 23:07:54 +0000928 SlotIndex Start = S.start;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000929 AssignI.advanceTo(Start);
930 do {
931 unsigned RegIdx;
Matthias Braun96761952014-12-10 23:07:54 +0000932 SlotIndex End = S.end;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000933 if (!AssignI.valid()) {
934 RegIdx = 0;
935 } else if (AssignI.start() <= Start) {
936 RegIdx = AssignI.value();
937 if (AssignI.stop() < End) {
938 End = AssignI.stop();
939 ++AssignI;
940 }
941 } else {
942 RegIdx = 0;
943 End = std::min(End, AssignI.start());
944 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000945
946 // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000947 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000948 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000949
950 // Check for a simply defined value that can be blitted directly.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000951 ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
952 if (VNInfo *VNI = VFP.getPointer()) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000953 DEBUG(dbgs() << ':' << VNI->id);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000954 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000955 Start = End;
956 continue;
957 }
958
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000959 // Skip values with forced recomputation.
960 if (VFP.getInt()) {
961 DEBUG(dbgs() << "(recalc)");
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000962 Skipped = true;
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000963 Start = End;
964 continue;
965 }
966
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000967 LiveRangeCalc &LRC = getLRCalc(RegIdx);
968
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000969 // This value has multiple defs in RegIdx, but it wasn't rematerialized,
970 // so the live range is accurate. Add live-in blocks in [Start;End) to the
971 // LiveInBlocks.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000972 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000973 SlotIndex BlockStart, BlockEnd;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000974 std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000975
976 // The first block may be live-in, or it may have its own def.
977 if (Start != BlockStart) {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000978 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000979 assert(VNI && "Missing def for complex mapped value");
980 DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
981 // MBB has its own def. Is it also live-out?
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000982 if (BlockEnd <= End)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000983 LRC.setLiveOutValue(&*MBB, VNI);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000984
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000985 // Skip to the next block for live-in.
986 ++MBB;
987 BlockStart = BlockEnd;
988 }
989
990 // Handle the live-in blocks covered by [Start;End).
991 assert(Start <= BlockStart && "Expected live-in block");
992 while (BlockStart < End) {
993 DEBUG(dbgs() << ">BB#" << MBB->getNumber());
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000994 BlockEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000995 if (BlockStart == ParentVNI->def) {
996 // This block has the def of a parent PHI, so it isn't live-in.
997 assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000998 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000999 assert(VNI && "Missing def for complex mapped parent PHI");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001000 if (End >= BlockEnd)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001001 LRC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001002 } else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001003 // This block needs a live-in value. The last block covered may not
1004 // be live-out.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001005 if (End < BlockEnd)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001006 LRC.addLiveInBlock(LR, MDT[&*MBB], End);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001007 else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001008 // Live-through, and we don't know the value.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001009 LRC.addLiveInBlock(LR, MDT[&*MBB]);
1010 LRC.setLiveOutValue(&*MBB, nullptr);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001011 }
1012 }
1013 BlockStart = BlockEnd;
1014 ++MBB;
1015 }
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001016 Start = End;
Matthias Braun96761952014-12-10 23:07:54 +00001017 } while (Start != S.end);
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001018 DEBUG(dbgs() << '\n');
1019 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001020
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +00001021 LRCalc[0].calculateValues();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +00001022 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +00001023 LRCalc[1].calculateValues();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001024
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001025 return Skipped;
1026}
1027
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001028void SplitEditor::extendPHIKillRanges() {
Wei Mi9a16d652016-04-13 03:08:27 +00001029 // Extend live ranges to be live-out for successor PHI values.
Matthias Braun96761952014-12-10 23:07:54 +00001030 for (const VNInfo *PHIVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001031 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
1032 continue;
1033 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
Matthias Braun2d5c32b2013-10-10 21:28:57 +00001034 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Wei Mi9a16d652016-04-13 03:08:27 +00001035
1036 // Check whether PHI is dead.
1037 const LiveRange::Segment *Segment = LR.getSegmentContaining(PHIVNI->def);
1038 assert(Segment != nullptr && "Missing segment for VNI");
1039 if (Segment->end == PHIVNI->def.getDeadSlot()) {
1040 // This is a dead PHI. Remove it.
1041 LR.removeSegment(*Segment, true);
1042 continue;
1043 }
1044
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001045 LiveRangeCalc &LRC = getLRCalc(RegIdx);
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001046 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
1047 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
1048 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001049 SlotIndex End = LIS.getMBBEndIdx(*PI);
1050 SlotIndex LastUse = End.getPrevSlot();
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001051 // The predecessor may not have a live-out value. That is OK, like an
1052 // undef PHI operand.
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001053 if (Edit->getParent().liveAt(LastUse)) {
1054 assert(RegAssign.lookup(LastUse) == RegIdx &&
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001055 "Different register assignment in phi predecessor");
Matthias Braun2d5c32b2013-10-10 21:28:57 +00001056 LRC.extend(LR, End);
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001057 }
1058 }
1059 }
1060}
1061
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001062/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001063void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001064 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +00001065 RE = MRI.reg_end(); RI != RE;) {
Owen Anderson16c6bf42014-03-13 23:12:04 +00001066 MachineOperand &MO = *RI;
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001067 MachineInstr *MI = MO.getParent();
1068 ++RI;
Eric Christopherede62672011-02-03 06:18:29 +00001069 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001070 if (MI->isDebugValue()) {
1071 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001072 MO.setReg(0);
1073 continue;
1074 }
Jakob Stoklund Olesenf6e03942011-02-09 21:52:09 +00001075
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +00001076 // <undef> operands don't really read the register, so it doesn't matter
1077 // which register we choose. When the use operand is tied to a def, we must
1078 // use the same register as the def, so just do that always.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001079 SlotIndex Idx = LIS.getInstructionIndex(*MI);
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +00001080 if (MO.isDef() || MO.isUndef())
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +00001081 Idx = Idx.getRegSlot(MO.isEarlyClobber());
Eric Christopherede62672011-02-03 06:18:29 +00001082
1083 // Rewrite to the mapped register at Idx.
1084 unsigned RegIdx = RegAssign.lookup(Idx);
Mark Laceyf9ea8852013-08-14 23:50:04 +00001085 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001086 MO.setReg(LI->reg);
Eric Christopherede62672011-02-03 06:18:29 +00001087 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
1088 << Idx << ':' << RegIdx << '\t' << *MI);
1089
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001090 // Extend liveness to Idx if the instruction reads reg.
Jakob Stoklund Olesen73a9eb92011-07-24 20:33:23 +00001091 if (!ExtendRanges || MO.isUndef())
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001092 continue;
1093
1094 // Skip instructions that don't read Reg.
1095 if (MO.isDef()) {
1096 if (!MO.getSubReg() && !MO.isEarlyClobber())
1097 continue;
1098 // We may wan't to extend a live range for a partial redef, or for a use
1099 // tied to an early clobber.
1100 Idx = Idx.getPrevSlot();
1101 if (!Edit->getParent().liveAt(Idx))
1102 continue;
1103 } else
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +00001104 Idx = Idx.getRegSlot(true);
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001105
Matthias Braun2d5c32b2013-10-10 21:28:57 +00001106 getLRCalc(RegIdx).extend(*LI, Idx.getNextSlot());
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001107 }
1108}
1109
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001110void SplitEditor::deleteRematVictims() {
1111 SmallVector<MachineInstr*, 8> Dead;
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001112 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
Mark Laceyf9ea8852013-08-14 23:50:04 +00001113 LiveInterval *LI = &LIS.getInterval(*I);
Matthias Braun96761952014-12-10 23:07:54 +00001114 for (const LiveRange::Segment &S : LI->segments) {
Jakob Stoklund Olesend8f24052011-11-13 22:42:13 +00001115 // Dead defs end at the dead slot.
Matthias Braun96761952014-12-10 23:07:54 +00001116 if (S.end != S.valno->def.getDeadSlot())
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001117 continue;
Wei Mi9a16d652016-04-13 03:08:27 +00001118 if (S.valno->isPHIDef())
1119 continue;
Matthias Braun96761952014-12-10 23:07:54 +00001120 MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001121 assert(MI && "Missing instruction for dead def");
1122 MI->addRegisterDead(LI->reg, &TRI);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001123
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001124 if (!MI->allDefsAreDead())
1125 continue;
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001126
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001127 DEBUG(dbgs() << "All defs dead: " << *MI);
1128 Dead.push_back(MI);
1129 }
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001130 }
1131
1132 if (Dead.empty())
1133 return;
1134
Wei Mic0223702016-07-08 21:08:09 +00001135 Edit->eliminateDeadDefs(Dead, None, &AA);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001136}
1137
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001138void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001139 ++NumFinished;
Eric Christopher21933532011-02-03 05:40:54 +00001140
Eric Christopherede62672011-02-03 06:18:29 +00001141 // At this point, the live intervals in Edit contain VNInfos corresponding to
1142 // the inserted copies.
1143
1144 // Add the original defs from the parent interval.
Matthias Braun96761952014-12-10 23:07:54 +00001145 for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen3295a992011-02-04 00:59:23 +00001146 if (ParentVNI->isUnused())
1147 continue;
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +00001148 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Jakob Stoklund Olesen97e14e02012-07-27 21:11:14 +00001149 defValue(RegIdx, ParentVNI, ParentVNI->def);
Jakob Stoklund Olesen32210de2011-03-15 21:13:22 +00001150
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001151 // Force rematted values to be recomputed everywhere.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001152 // The new live ranges may be truncated.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001153 if (Edit->didRematerialize(ParentVNI))
1154 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001155 forceRecompute(i, ParentVNI);
Eric Christopherede62672011-02-03 06:18:29 +00001156 }
1157
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001158 // Hoist back-copies to the complement interval when in spill mode.
1159 switch (SpillMode) {
1160 case SM_Partition:
1161 // Leave all back-copies as is.
1162 break;
1163 case SM_Size:
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001164 case SM_Speed:
Wei Mi9a16d652016-04-13 03:08:27 +00001165 // hoistCopies will behave differently between size and speed.
1166 hoistCopies();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001167 }
1168
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001169 // Transfer the simply mapped values, check if any are skipped.
1170 bool Skipped = transferValues();
Wei Mi9a16d652016-04-13 03:08:27 +00001171
1172 // Rewrite virtual registers, possibly extending ranges.
1173 rewriteAssigned(Skipped);
1174
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001175 if (Skipped)
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001176 extendPHIKillRanges();
1177 else
1178 ++NumSimple;
Eric Christopherede62672011-02-03 06:18:29 +00001179
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001180 // Delete defs that were rematted everywhere.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001181 if (Skipped)
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001182 deleteRematVictims();
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001183
Jakob Stoklund Olesen0f1677e2010-10-07 23:34:34 +00001184 // Get rid of unused values and set phi-kill flags.
Mark Laceyf9ea8852013-08-14 23:50:04 +00001185 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) {
1186 LiveInterval &LI = LIS.getInterval(*I);
1187 LI.RenumberValues();
1188 }
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001189
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001190 // Provide a reverse mapping from original indices to Edit ranges.
1191 if (LRMap) {
1192 LRMap->clear();
1193 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1194 LRMap->push_back(i);
1195 }
1196
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001197 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +00001198 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001199 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001200 // Don't use iterators, they are invalidated by create() below.
Matthias Braund3dd1352015-09-22 03:44:41 +00001201 unsigned VReg = Edit->get(i);
1202 LiveInterval &LI = LIS.getInterval(VReg);
1203 SmallVector<LiveInterval*, 8> SplitLIs;
1204 LIS.splitSeparateComponents(LI, SplitLIs);
1205 unsigned Original = VRM.getOriginal(VReg);
1206 for (LiveInterval *SplitLI : SplitLIs)
1207 VRM.setIsSplitFromReg(SplitLI->reg, Original);
1208
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001209 // The new intervals all map back to i.
1210 if (LRMap)
1211 LRMap->resize(Edit->size(), i);
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001212 }
1213
Jakob Stoklund Olesen284c2db2010-08-10 17:07:22 +00001214 // Calculate spill weight and allocation hints for new intervals.
Benjamin Kramere2a1d892013-06-17 19:00:36 +00001215 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001216
1217 assert(!LRMap || LRMap->size() == Edit->size());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +00001218}
1219
1220
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +00001221//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +00001222// Single Block Splitting
1223//===----------------------------------------------------------------------===//
1224
Jakob Stoklund Olesen8627ea92011-08-05 22:20:45 +00001225bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
1226 bool SingleInstrs) const {
1227 // Always split for multiple instructions.
1228 if (!BI.isOneInstr())
1229 return true;
1230 // Don't split for single instructions unless explicitly requested.
1231 if (!SingleInstrs)
1232 return false;
1233 // Splitting a live-through range always makes progress.
1234 if (BI.LiveIn && BI.LiveOut)
1235 return true;
1236 // No point in isolating a copy. It has no register class constraints.
1237 if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
1238 return false;
1239 // Finally, don't isolate an end point that was created by earlier splits.
1240 return isOriginalEndpoint(BI.FirstInstr);
1241}
1242
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001243void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1244 openIntv();
1245 SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001246 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001247 LastSplitPoint));
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001248 if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
1249 useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001250 } else {
1251 // The last use is after the last valid split point.
1252 SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1253 useIntv(SegStart, SegStop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001254 overlapIntv(SegStop, BI.LastInstr);
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001255 }
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001256}
1257
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001258
1259//===----------------------------------------------------------------------===//
1260// Global Live Range Splitting Support
1261//===----------------------------------------------------------------------===//
1262
1263// These methods support a method of global live range splitting that uses a
1264// global algorithm to decide intervals for CFG edges. They will insert split
1265// points and color intervals in basic blocks while avoiding interference.
1266//
1267// Note that splitSingleBlock is also useful for blocks where both CFG edges
1268// are on the stack.
1269
1270void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1271 unsigned IntvIn, SlotIndex LeaveBefore,
1272 unsigned IntvOut, SlotIndex EnterAfter){
1273 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001274 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001275
1276 DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
1277 << ") intf " << LeaveBefore << '-' << EnterAfter
1278 << ", live-through " << IntvIn << " -> " << IntvOut);
1279
1280 assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1281
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001282 assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
1283 assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
1284 assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
1285
1286 MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
1287
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001288 if (!IntvOut) {
1289 DEBUG(dbgs() << ", spill on entry.\n");
1290 //
1291 // <<<<<<<<< Possible LeaveBefore interference.
1292 // |-----------| Live through.
1293 // -____________ Spill on entry.
1294 //
1295 selectIntv(IntvIn);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001296 SlotIndex Idx = leaveIntvAtTop(*MBB);
1297 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1298 (void)Idx;
1299 return;
1300 }
1301
1302 if (!IntvIn) {
1303 DEBUG(dbgs() << ", reload on exit.\n");
1304 //
1305 // >>>>>>> Possible EnterAfter interference.
1306 // |-----------| Live through.
1307 // ___________-- Reload on exit.
1308 //
1309 selectIntv(IntvOut);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001310 SlotIndex Idx = enterIntvAtEnd(*MBB);
1311 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1312 (void)Idx;
1313 return;
1314 }
1315
1316 if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1317 DEBUG(dbgs() << ", straight through.\n");
1318 //
1319 // |-----------| Live through.
1320 // ------------- Straight through, same intv, no interference.
1321 //
1322 selectIntv(IntvOut);
1323 useIntv(Start, Stop);
1324 return;
1325 }
1326
1327 // We cannot legally insert splits after LSP.
1328 SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001329 assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001330
1331 if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1332 LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1333 DEBUG(dbgs() << ", switch avoiding interference.\n");
1334 //
1335 // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
1336 // |-----------| Live through.
1337 // ------======= Switch intervals between interference.
1338 //
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001339 selectIntv(IntvOut);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001340 SlotIndex Idx;
1341 if (LeaveBefore && LeaveBefore < LSP) {
1342 Idx = enterIntvBefore(LeaveBefore);
1343 useIntv(Idx, Stop);
1344 } else {
1345 Idx = enterIntvAtEnd(*MBB);
1346 }
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001347 selectIntv(IntvIn);
1348 useIntv(Start, Idx);
1349 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1350 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1351 return;
1352 }
1353
1354 DEBUG(dbgs() << ", create local intv for interference.\n");
1355 //
1356 // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
1357 // |-----------| Live through.
1358 // ==---------== Switch intervals before/after interference.
1359 //
1360 assert(LeaveBefore <= EnterAfter && "Missed case");
1361
1362 selectIntv(IntvOut);
1363 SlotIndex Idx = enterIntvAfter(EnterAfter);
1364 useIntv(Idx, Stop);
1365 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1366
1367 selectIntv(IntvIn);
1368 Idx = leaveIntvBefore(LeaveBefore);
1369 useIntv(Start, Idx);
1370 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1371}
1372
1373
1374void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1375 unsigned IntvIn, SlotIndex LeaveBefore) {
1376 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001377 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001378
1379 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001380 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001381 << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
1382 << (BI.LiveOut ? ", stack-out" : ", killed in block"));
1383
1384 assert(IntvIn && "Must have register in");
1385 assert(BI.LiveIn && "Must be live-in");
1386 assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1387
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001388 if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001389 DEBUG(dbgs() << " before interference.\n");
1390 //
1391 // <<< Interference after kill.
1392 // |---o---x | Killed in block.
1393 // ========= Use IntvIn everywhere.
1394 //
1395 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001396 useIntv(Start, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001397 return;
1398 }
1399
1400 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1401
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001402 if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001403 //
1404 // <<< Possible interference after last use.
1405 // |---o---o---| Live-out on stack.
1406 // =========____ Leave IntvIn after last use.
1407 //
1408 // < Interference after last use.
1409 // |---o---o--o| Live-out on stack, late last use.
1410 // ============ Copy to stack after LSP, overlap IntvIn.
1411 // \_____ Stack interval is live-out.
1412 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001413 if (BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001414 DEBUG(dbgs() << ", spill after last use before interference.\n");
1415 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001416 SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001417 useIntv(Start, Idx);
1418 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1419 } else {
1420 DEBUG(dbgs() << ", spill before last split point.\n");
1421 selectIntv(IntvIn);
Jakob Stoklund Olesen37e3a132011-07-16 00:13:30 +00001422 SlotIndex Idx = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001423 overlapIntv(Idx, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001424 useIntv(Start, Idx);
1425 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1426 }
1427 return;
1428 }
1429
1430 // The interference is overlapping somewhere we wanted to use IntvIn. That
1431 // means we need to create a local interval that can be allocated a
1432 // different register.
1433 unsigned LocalIntv = openIntv();
Matt Beaumont-Gay26909d82011-07-16 04:18:47 +00001434 (void)LocalIntv;
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001435 DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
1436
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001437 if (!BI.LiveOut || BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001438 //
1439 // <<<<<<< Interference overlapping uses.
1440 // |---o---o---| Live-out on stack.
1441 // =====----____ Leave IntvIn before interference, then spill.
1442 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001443 SlotIndex To = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001444 SlotIndex From = enterIntvBefore(LeaveBefore);
1445 useIntv(From, To);
1446 selectIntv(IntvIn);
1447 useIntv(Start, From);
1448 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1449 return;
1450 }
1451
1452 // <<<<<<< Interference overlapping uses.
1453 // |---o---o--o| Live-out on stack, late last use.
1454 // =====------- Copy to stack before LSP, overlap LocalIntv.
1455 // \_____ Stack interval is live-out.
1456 //
1457 SlotIndex To = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001458 overlapIntv(To, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001459 SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1460 useIntv(From, To);
1461 selectIntv(IntvIn);
1462 useIntv(Start, From);
1463 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1464}
1465
1466void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1467 unsigned IntvOut, SlotIndex EnterAfter) {
1468 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001469 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001470
1471 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001472 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001473 << ", reg-out " << IntvOut << ", enter after " << EnterAfter
1474 << (BI.LiveIn ? ", stack-in" : ", defined in block"));
1475
1476 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1477
1478 assert(IntvOut && "Must have register out");
1479 assert(BI.LiveOut && "Must be live-out");
1480 assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1481
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001482 if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001483 DEBUG(dbgs() << " after interference.\n");
1484 //
1485 // >>>> Interference before def.
1486 // | o---o---| Defined in block.
1487 // ========= Use IntvOut everywhere.
1488 //
1489 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001490 useIntv(BI.FirstInstr, Stop);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001491 return;
1492 }
1493
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001494 if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001495 DEBUG(dbgs() << ", reload after interference.\n");
1496 //
1497 // >>>> Interference before def.
1498 // |---o---o---| Live-through, stack-in.
1499 // ____========= Enter IntvOut before first use.
1500 //
1501 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001502 SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001503 useIntv(Idx, Stop);
1504 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1505 return;
1506 }
1507
1508 // The interference is overlapping somewhere we wanted to use IntvOut. That
1509 // means we need to create a local interval that can be allocated a
1510 // different register.
1511 DEBUG(dbgs() << ", interference overlaps uses.\n");
1512 //
1513 // >>>>>>> Interference overlapping uses.
1514 // |---o---o---| Live-through, stack-in.
1515 // ____---====== Create local interval for interference range.
1516 //
1517 selectIntv(IntvOut);
1518 SlotIndex Idx = enterIntvAfter(EnterAfter);
1519 useIntv(Idx, Stop);
1520 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1521
1522 openIntv();
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001523 SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001524 useIntv(From, Idx);
1525}