blob: 8bf139156eeed991f348a4f4c66b365613dd4a8b [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//===----------------------------------------------------------------------===//
41// Split Analysis
42//===----------------------------------------------------------------------===//
43
Eric Christopherd9134482014-08-04 21:25:23 +000044SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
Jakob Stoklund Olesen0fef9dd2010-07-20 23:50:15 +000045 const MachineLoopInfo &mli)
Eric Christopherd9134482014-08-04 21:25:23 +000046 : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli),
Eric Christopherfc6de422014-08-05 02:39:49 +000047 TII(*MF.getSubtarget().getInstrInfo()), CurLI(nullptr),
Eric Christopherd9134482014-08-04 21:25:23 +000048 LastSplitPoint(MF.getNumBlockIDs()) {}
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000049
50void SplitAnalysis::clear() {
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +000051 UseSlots.clear();
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +000052 UseBlocks.clear();
53 ThroughBlocks.clear();
Craig Topperc0196b12014-04-14 00:51:57 +000054 CurLI = nullptr;
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +000055 DidRepairRange = false;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000056}
57
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000058SlotIndex SplitAnalysis::computeLastSplitPoint(unsigned Num) {
59 const MachineBasicBlock *MBB = MF.getBlockNumbered(Num);
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000060 std::pair<SlotIndex, SlotIndex> &LSP = LastSplitPoint[Num];
Jakob Stoklund Olesen8b1d0232012-01-11 02:07:05 +000061 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000062
David Majnemerdd215232016-04-25 14:31:32 +000063 SmallVector<const MachineBasicBlock *, 1> EHPadSucessors;
64 for (const MachineBasicBlock *SMBB : MBB->successors())
65 if (SMBB->isEHPad())
66 EHPadSucessors.push_back(SMBB);
67
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000068 // Compute split points on the first call. The pair is independent of the
69 // current live interval.
70 if (!LSP.first.isValid()) {
71 MachineBasicBlock::const_iterator FirstTerm = MBB->getFirstTerminator();
72 if (FirstTerm == MBB->end())
Jakob Stoklund Olesen8b1d0232012-01-11 02:07:05 +000073 LSP.first = MBBEnd;
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000074 else
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +000075 LSP.first = LIS.getInstructionIndex(*FirstTerm);
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000076
77 // If there is a landing pad successor, also find the call instruction.
David Majnemerdd215232016-04-25 14:31:32 +000078 if (EHPadSucessors.empty())
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000079 return LSP.first;
80 // There may not be a call instruction (?) in which case we ignore LPad.
81 LSP.second = LSP.first;
Jakob Stoklund Olesen040d6592011-06-28 01:18:58 +000082 for (MachineBasicBlock::const_iterator I = MBB->end(), E = MBB->begin();
83 I != E;) {
84 --I;
Evan Cheng7f8e5632011-12-07 07:15:52 +000085 if (I->isCall()) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +000086 LSP.second = LIS.getInstructionIndex(*I);
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000087 break;
88 }
Jakob Stoklund Olesen040d6592011-06-28 01:18:58 +000089 }
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000090 }
91
92 // If CurLI is live into a landing pad successor, move the last split point
93 // back to the call that may throw.
David Majnemerdd215232016-04-25 14:31:32 +000094 if (!LSP.second)
95 return LSP.first;
96
97 if (none_of(EHPadSucessors, [&](const MachineBasicBlock *EHPad) {
98 return LIS.isLiveInToMBB(*CurLI, EHPad);
99 }))
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +0000100 return LSP.first;
Jakob Stoklund Olesen8b1d0232012-01-11 02:07:05 +0000101
102 // Find the value leaving MBB.
103 const VNInfo *VNI = CurLI->getVNInfoBefore(MBBEnd);
104 if (!VNI)
105 return LSP.first;
106
107 // If the value leaving MBB was defined after the call in MBB, it can't
108 // really be live-in to the landing pad. This can happen if the landing pad
109 // has a PHI, and this register is undef on the exceptional edge.
110 // <rdar://problem/10664933>
111 if (!SlotIndex::isEarlierInstr(VNI->def, LSP.second) && VNI->def < MBBEnd)
112 return LSP.first;
113
114 // Value is properly live-in to the landing pad.
115 // Only allow splits before the call.
116 return LSP.second;
Jakob Stoklund Olesened4075c2010-07-20 21:46:58 +0000117}
118
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000119MachineBasicBlock::iterator
120SplitAnalysis::getLastSplitPointIter(MachineBasicBlock *MBB) {
121 SlotIndex LSP = getLastSplitPoint(MBB->getNumber());
122 if (LSP == LIS.getMBBEndIdx(MBB))
123 return MBB->end();
124 return LIS.getInstructionFromIndex(LSP);
125}
126
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000127/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000128void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000129 assert(UseSlots.empty() && "Call clear first");
130
131 // First get all the defs from the interval values. This provides the correct
132 // slots for early clobbers.
Matthias Braun96761952014-12-10 23:07:54 +0000133 for (const VNInfo *VNI : CurLI->valnos)
134 if (!VNI->isPHIDef() && !VNI->isUnused())
135 UseSlots.push_back(VNI->def);
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000136
137 // Get use slots form the use-def chain.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000138 const MachineRegisterInfo &MRI = MF.getRegInfo();
Owen Andersonb36376e2014-03-17 19:36:09 +0000139 for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg))
140 if (!MO.isUndef())
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000141 UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot());
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000142
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000143 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000144
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000145 // Remove duplicates, keeping the smaller slot for each instruction.
146 // That is what we want for early clobbers.
147 UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
148 SlotIndex::isSameInstr),
149 UseSlots.end());
150
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000151 // Compute per-live block info.
152 if (!calcLiveBlockInfo()) {
153 // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
Rafael Espindola676c4052011-06-26 22:34:10 +0000154 // I am looking at you, RegisterCoalescer!
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000155 DidRepairRange = true;
Jakob Stoklund Olesen50215af2011-05-10 17:37:41 +0000156 ++NumRepairs;
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000157 DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
158 const_cast<LiveIntervals&>(LIS)
159 .shrinkToUses(const_cast<LiveInterval*>(CurLI));
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000160 UseBlocks.clear();
161 ThroughBlocks.clear();
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000162 bool fixed = calcLiveBlockInfo();
163 (void)fixed;
164 assert(fixed && "Couldn't fix broken live interval");
165 }
166
Jakob Stoklund Olesenbd6b86e2011-03-27 22:49:23 +0000167 DEBUG(dbgs() << "Analyze counted "
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000168 << UseSlots.size() << " instrs in "
169 << UseBlocks.size() << " blocks, through "
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000170 << NumThroughBlocks << " blocks.\n");
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000171}
172
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000173/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
174/// where CurLI is live.
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000175bool SplitAnalysis::calcLiveBlockInfo() {
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000176 ThroughBlocks.resize(MF.getNumBlockIDs());
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000177 NumThroughBlocks = NumGapBlocks = 0;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000178 if (CurLI->empty())
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000179 return true;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000180
181 LiveInterval::const_iterator LVI = CurLI->begin();
182 LiveInterval::const_iterator LVE = CurLI->end();
183
184 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
185 UseI = UseSlots.begin();
186 UseE = UseSlots.end();
187
188 // Loop over basic blocks where CurLI is live.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000189 MachineFunction::iterator MFI =
190 LIS.getMBBFromIndex(LVI->start)->getIterator();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000191 for (;;) {
192 BlockInfo BI;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000193 BI.MBB = &*MFI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000194 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000195 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000196
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000197 // If the block contains no uses, the range must be live through. At one
Rafael Espindola676c4052011-06-26 22:34:10 +0000198 // point, RegisterCoalescer could create dangling ranges that ended
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000199 // mid-block.
200 if (UseI == UseE || *UseI >= Stop) {
201 ++NumThroughBlocks;
202 ThroughBlocks.set(BI.MBB->getNumber());
203 // The range shouldn't end mid-block if there are no uses. This shouldn't
204 // happen.
205 if (LVI->end < Stop)
206 return false;
207 } else {
208 // This block has uses. Find the first and last uses in the block.
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000209 BI.FirstInstr = *UseI;
210 assert(BI.FirstInstr >= Start);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000211 do ++UseI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000212 while (UseI != UseE && *UseI < Stop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000213 BI.LastInstr = UseI[-1];
214 assert(BI.LastInstr < Stop);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000215
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000216 // LVI is the first live segment overlapping MBB.
217 BI.LiveIn = LVI->start <= Start;
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000218
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000219 // When not live in, the first use should be a def.
220 if (!BI.LiveIn) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000221 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000222 assert(LVI->start == BI.FirstInstr && "First instr should be a def");
223 BI.FirstDef = BI.FirstInstr;
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000224 }
225
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000226 // Look for gaps in the live range.
227 BI.LiveOut = true;
228 while (LVI->end < Stop) {
229 SlotIndex LastStop = LVI->end;
230 if (++LVI == LVE || LVI->start >= Stop) {
231 BI.LiveOut = false;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000232 BI.LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000233 break;
234 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000235
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000236 if (LastStop < LVI->start) {
237 // There is a gap in the live range. Create duplicate entries for the
238 // live-in snippet and the live-out snippet.
239 ++NumGapBlocks;
240
241 // Push the Live-in part.
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000242 BI.LiveOut = false;
243 UseBlocks.push_back(BI);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000244 UseBlocks.back().LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000245
246 // Set up BI for the live-out part.
247 BI.LiveIn = false;
248 BI.LiveOut = true;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000249 BI.FirstInstr = BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000250 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000251
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000252 // A Segment that starts in the middle of the block must be a def.
253 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000254 if (!BI.FirstDef)
255 BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000256 }
257
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000258 UseBlocks.push_back(BI);
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000259
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000260 // LVI is now at LVE or LVI->end >= Stop.
261 if (LVI == LVE)
262 break;
263 }
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000264
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000265 // Live segment ends exactly at Stop. Move to the next segment.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000266 if (LVI->end == Stop && ++LVI == LVE)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000267 break;
268
269 // Pick the next basic block.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000270 if (LVI->start < Stop)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000271 ++MFI;
272 else
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000273 MFI = LIS.getMBBFromIndex(LVI->start)->getIterator();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000274 }
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000275
276 assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000277 return true;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000278}
279
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000280unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
281 if (cli->empty())
282 return 0;
283 LiveInterval *li = const_cast<LiveInterval*>(cli);
284 LiveInterval::iterator LVI = li->begin();
285 LiveInterval::iterator LVE = li->end();
286 unsigned Count = 0;
287
288 // Loop over basic blocks where li is live.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000289 MachineFunction::const_iterator MFI =
290 LIS.getMBBFromIndex(LVI->start)->getIterator();
291 SlotIndex Stop = LIS.getMBBEndIdx(&*MFI);
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000292 for (;;) {
293 ++Count;
294 LVI = li->advanceTo(LVI, Stop);
295 if (LVI == LVE)
296 return Count;
297 do {
298 ++MFI;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000299 Stop = LIS.getMBBEndIdx(&*MFI);
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000300 } while (Stop <= LVI->start);
301 }
302}
303
Jakob Stoklund Olesen60a26a62011-02-21 23:09:46 +0000304bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
305 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
306 const LiveInterval &Orig = LIS.getInterval(OrigReg);
307 assert(!Orig.empty() && "Splitting empty interval?");
308 LiveInterval::const_iterator I = Orig.find(Idx);
309
310 // Range containing Idx should begin at Idx.
311 if (I != Orig.end() && I->start <= Idx)
312 return I->start == Idx;
313
314 // Range does not contain Idx, previous must end at Idx.
315 return I != Orig.begin() && (--I)->end == Idx;
316}
317
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000318void SplitAnalysis::analyze(const LiveInterval *li) {
319 clear();
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000320 CurLI = li;
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000321 analyzeUses();
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000322}
323
Jakob Stoklund Olesen28e769c2010-12-15 17:49:52 +0000324
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000325//===----------------------------------------------------------------------===//
326// Split Editor
327//===----------------------------------------------------------------------===//
328
329/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Eric Christopherd9134482014-08-04 21:25:23 +0000330SplitEditor::SplitEditor(SplitAnalysis &sa, LiveIntervals &lis, VirtRegMap &vrm,
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000331 MachineDominatorTree &mdt,
332 MachineBlockFrequencyInfo &mbfi)
Eric Christopherd9134482014-08-04 21:25:23 +0000333 : SA(sa), LIS(lis), VRM(vrm), MRI(vrm.getMachineFunction().getRegInfo()),
Eric Christopher60621802014-10-14 07:22:00 +0000334 MDT(mdt), TII(*vrm.getMachineFunction().getSubtarget().getInstrInfo()),
335 TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()),
Eric Christopherd9134482014-08-04 21:25:23 +0000336 MBFI(mbfi), Edit(nullptr), OpenIdx(0), SpillMode(SM_Partition),
337 RegAssign(Allocator) {}
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000338
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000339void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
340 Edit = &LRE;
341 SpillMode = SM;
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000342 OpenIdx = 0;
343 RegAssign.clear();
344 Values.clear();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000345
346 // Reset the LiveRangeCalc instances needed for this spill mode.
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000347 LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
348 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000349 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000350 LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
351 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000352
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000353 // We don't need an AliasAnalysis since we will only be performing
354 // cheap-as-a-copy remats anyway.
Craig Topperc0196b12014-04-14 00:51:57 +0000355 Edit->anyRematerializable(nullptr);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000356}
357
Manman Ren19f49ac2012-09-11 22:23:19 +0000358#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000359LLVM_DUMP_METHOD void SplitEditor::dump() const {
Eric Christopherede62672011-02-03 06:18:29 +0000360 if (RegAssign.empty()) {
361 dbgs() << " empty\n";
362 return;
363 }
364
365 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
366 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
367 dbgs() << '\n';
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000368}
Manman Ren742534c2012-09-06 19:06:06 +0000369#endif
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000370
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000371VNInfo *SplitEditor::defValue(unsigned RegIdx,
372 const VNInfo *ParentVNI,
373 SlotIndex Idx) {
374 assert(ParentVNI && "Mapping NULL value");
375 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000376 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
Mark Laceyf9ea8852013-08-14 23:50:04 +0000377 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000378
379 // Create a new value.
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000380 VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000381
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000382 // Use insert for lookup, so we can add missing values with a second lookup.
383 std::pair<ValueMap::iterator, bool> InsP =
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000384 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id),
385 ValueForcePair(VNI, false)));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000386
387 // This was the first time (RegIdx, ParentVNI) was mapped.
388 // Keep it as a simple def without any liveness.
389 if (InsP.second)
390 return VNI;
391
392 // If the previous value was a simple mapping, add liveness for it now.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000393 if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000394 SlotIndex Def = OldVNI->def;
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000395 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), OldVNI));
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000396 // No longer a simple mapping. Switch to a complex, non-forced mapping.
397 InsP.first->second = ValueForcePair();
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000398 }
399
400 // This is a complex mapping, add liveness for VNI
401 SlotIndex Def = VNI->def;
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000402 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000403
404 return VNI;
405}
406
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000407void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000408 assert(ParentVNI && "Mapping NULL value");
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000409 ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
410 VNInfo *VNI = VFP.getPointer();
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000411
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000412 // ParentVNI was either unmapped or already complex mapped. Either way, just
413 // set the force bit.
414 if (!VNI) {
415 VFP.setInt(true);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000416 return;
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000417 }
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000418
419 // This was previously a single mapping. Make sure the old def is represented
420 // by a trivial live range.
421 SlotIndex Def = VNI->def;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000422 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000423 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000424 // Mark as complex mapped, forced.
Craig Topperc0196b12014-04-14 00:51:57 +0000425 VFP = ValueForcePair(nullptr, true);
Jakob Stoklund Olesen4484f992011-09-13 18:05:29 +0000426}
427
Eric Christopherede62672011-02-03 06:18:29 +0000428VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000429 VNInfo *ParentVNI,
430 SlotIndex UseIdx,
431 MachineBasicBlock &MBB,
432 MachineBasicBlock::iterator I) {
Craig Topperc0196b12014-04-14 00:51:57 +0000433 MachineInstr *CopyMI = nullptr;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000434 SlotIndex Def;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000435 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000436
Jakob Stoklund Olesen7d406792011-05-02 05:29:58 +0000437 // We may be trying to avoid interference that ends at a deleted instruction,
438 // so always begin RegIdx 0 early and all others late.
439 bool Late = RegIdx != 0;
440
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000441 // Attempt cheap-as-a-copy rematerialization.
Wei Mi9a16d652016-04-13 03:08:27 +0000442 unsigned Original = VRM.getOriginal(Edit->get(RegIdx));
443 LiveInterval &OrigLI = LIS.getInterval(Original);
444 VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000445 LiveRangeEdit::Remat RM(ParentVNI);
Wei Mi9a16d652016-04-13 03:08:27 +0000446 RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
447
448 if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) {
Pete Cooper2bde2f42012-04-02 22:22:53 +0000449 Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late);
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000450 ++NumRemats;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000451 } else {
452 // Can't remat, just insert a copy from parent.
Eric Christopherede62672011-02-03 06:18:29 +0000453 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000454 .addReg(Edit->getReg());
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000455 Def = LIS.getSlotIndexes()
456 ->insertMachineInstrInMaps(*CopyMI, Late)
457 .getRegSlot();
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000458 ++NumCopies;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000459 }
460
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000461 // Define the value in Reg.
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000462 return defValue(RegIdx, ParentVNI, Def);
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000463}
464
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000465/// Create a new virtual register and live interval.
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000466unsigned SplitEditor::openIntv() {
Eric Christopherede62672011-02-03 06:18:29 +0000467 // Create the complement as index 0.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000468 if (Edit->empty())
Mark Lacey9d8103d2013-08-14 23:50:16 +0000469 Edit->createEmptyInterval();
Eric Christopherede62672011-02-03 06:18:29 +0000470
471 // Create the open interval.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000472 OpenIdx = Edit->size();
Mark Lacey9d8103d2013-08-14 23:50:16 +0000473 Edit->createEmptyInterval();
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000474 return OpenIdx;
475}
476
477void SplitEditor::selectIntv(unsigned Idx) {
478 assert(Idx != 0 && "Cannot select the complement interval");
479 assert(Idx < Edit->size() && "Can only select previously opened interval");
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000480 DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000481 OpenIdx = Idx;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000482}
483
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000484SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000485 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000486 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000487 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000488 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000489 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000490 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000491 return Idx;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000492 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000493 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000494 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000495 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000496
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000497 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
498 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000499}
500
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000501SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
502 assert(OpenIdx && "openIntv not called before enterIntvAfter");
503 DEBUG(dbgs() << " enterIntvAfter " << Idx);
504 Idx = Idx.getBoundaryIndex();
505 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
506 if (!ParentVNI) {
507 DEBUG(dbgs() << ": not live\n");
508 return Idx;
509 }
510 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
511 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
512 assert(MI && "enterIntvAfter called with invalid index");
513
514 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000515 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000516 return VNI->def;
517}
518
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000519SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000520 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000521 SlotIndex End = LIS.getMBBEndIdx(&MBB);
522 SlotIndex Last = End.getPrevSlot();
523 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000524 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000525 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000526 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000527 return End;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000528 }
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000529 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000530 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000531 SA.getLastSplitPointIter(&MBB));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000532 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopherede62672011-02-03 06:18:29 +0000533 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000534 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000535}
536
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000537/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000538void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000539 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000540}
541
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000542void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopherede62672011-02-03 06:18:29 +0000543 assert(OpenIdx && "openIntv not called before useIntv");
544 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
545 RegAssign.insert(Start, End, OpenIdx);
546 DEBUG(dump());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000547}
548
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000549SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000550 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000551 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000552
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000553 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000554 SlotIndex Boundary = Idx.getBoundaryIndex();
555 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000556 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000557 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000558 return Boundary.getNextSlot();
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000559 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000560 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000561 MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
Jakob Stoklund Olesen3d11c8e2011-02-08 18:50:18 +0000562 assert(MI && "No instruction at index");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000563
564 // In spill mode, make live ranges as short as possible by inserting the copy
565 // before MI. This is only possible if that instruction doesn't redefine the
566 // value. The inserted COPY is not a kill, and we don't need to recompute
567 // the source live range. The spiller also won't try to hoist this copy.
568 if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
569 MI->readsVirtualRegister(Edit->getReg())) {
570 forceRecompute(0, ParentVNI);
571 defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
572 return Idx;
573 }
574
575 VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000576 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000577 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000578}
579
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000580SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
581 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
582 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
583
584 // The interval must be live into the instruction at Idx.
Jakob Stoklund Olesenc45d38e2011-07-18 18:47:13 +0000585 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000586 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000587 if (!ParentVNI) {
588 DEBUG(dbgs() << ": not live\n");
589 return Idx.getNextSlot();
590 }
591 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
592
593 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
594 assert(MI && "No instruction at index");
595 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
596 return VNI->def;
597}
598
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000599SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000600 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000601 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000602 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000603
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000604 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000605 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000606 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000607 return Start;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000608 }
609
Eric Christopherede62672011-02-03 06:18:29 +0000610 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000611 MBB.SkipPHIsAndLabels(MBB.begin()));
Eric Christopherede62672011-02-03 06:18:29 +0000612 RegAssign.insert(Start, VNI->def, OpenIdx);
613 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000614 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000615}
616
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000617void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
618 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000619 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesend7bcf432011-11-14 01:39:36 +0000620 assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000621 "Parent changes value in extended range");
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000622 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
623 "Range cannot span basic blocks");
624
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000625 // The complement interval will be extended as needed by LRCalc.extend().
Jakob Stoklund Olesen5c482cd2011-04-05 23:43:14 +0000626 if (ParentVNI)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000627 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000628 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
629 RegAssign.insert(Start, End, OpenIdx);
630 DEBUG(dump());
631}
632
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000633//===----------------------------------------------------------------------===//
634// Spill modes
635//===----------------------------------------------------------------------===//
636
637void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
Mark Laceyf9ea8852013-08-14 23:50:04 +0000638 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000639 DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
640 RegAssignMap::iterator AssignI;
641 AssignI.setMap(RegAssign);
642
643 for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
Matthias Braun311730a2015-01-21 19:02:30 +0000644 SlotIndex Def = Copies[i]->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000645 MachineInstr *MI = LIS.getInstructionFromIndex(Def);
646 assert(MI && "No instruction for back-copy");
647
648 MachineBasicBlock *MBB = MI->getParent();
649 MachineBasicBlock::iterator MBBI(MI);
650 bool AtBegin;
651 do AtBegin = MBBI == MBB->begin();
652 while (!AtBegin && (--MBBI)->isDebugValue());
653
654 DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
Matthias Braun311730a2015-01-21 19:02:30 +0000655 LIS.removeVRegDefAt(*LI, Def);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000656 LIS.RemoveMachineInstrFromMaps(*MI);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000657 MI->eraseFromParent();
658
Matthias Braun311730a2015-01-21 19:02:30 +0000659 // Adjust RegAssign if a register assignment is killed at Def. We want to
660 // avoid calculating the live range of the source register if possible.
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000661 AssignI.find(Def.getPrevSlot());
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000662 if (!AssignI.valid() || AssignI.start() >= Def)
663 continue;
664 // If MI doesn't kill the assigned register, just leave it.
665 if (AssignI.stop() != Def)
666 continue;
667 unsigned RegIdx = AssignI.value();
668 if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
669 DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n');
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000670 forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000671 } else {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000672 SlotIndex Kill = LIS.getInstructionIndex(*MBBI).getRegSlot();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000673 DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
674 AssignI.setStop(Kill);
675 }
676 }
677}
678
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000679MachineBasicBlock*
680SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
681 MachineBasicBlock *DefMBB) {
682 if (MBB == DefMBB)
683 return MBB;
684 assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
685
686 const MachineLoopInfo &Loops = SA.Loops;
687 const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
688 MachineDomTreeNode *DefDomNode = MDT[DefMBB];
689
690 // Best candidate so far.
691 MachineBasicBlock *BestMBB = MBB;
692 unsigned BestDepth = UINT_MAX;
693
694 for (;;) {
695 const MachineLoop *Loop = Loops.getLoopFor(MBB);
696
697 // MBB isn't in a loop, it doesn't get any better. All dominators have a
698 // higher frequency by definition.
699 if (!Loop) {
700 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
701 << MBB->getNumber() << " at depth 0\n");
702 return MBB;
703 }
704
705 // We'll never be able to exit the DefLoop.
706 if (Loop == DefLoop) {
707 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
708 << MBB->getNumber() << " in the same loop\n");
709 return MBB;
710 }
711
712 // Least busy dominator seen so far.
713 unsigned Depth = Loop->getLoopDepth();
714 if (Depth < BestDepth) {
715 BestMBB = MBB;
716 BestDepth = Depth;
717 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
718 << MBB->getNumber() << " at depth " << Depth << '\n');
719 }
720
721 // Leave loop by going to the immediate dominator of the loop header.
722 // This is a bigger stride than simply walking up the dominator tree.
723 MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
724
725 // Too far up the dominator tree?
726 if (!IDom || !MDT.dominates(DefDomNode, IDom))
727 return BestMBB;
728
729 MBB = IDom->getBlock();
730 }
731}
732
Wei Mi9a16d652016-04-13 03:08:27 +0000733void SplitEditor::computeRedundantBackCopies(
734 DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) {
735 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
736 LiveInterval *Parent = &Edit->getParent();
737 SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums());
738 SmallPtrSet<VNInfo *, 8> DominatedVNIs;
739
740 // Aggregate VNIs having the same value as ParentVNI.
741 for (VNInfo *VNI : LI->valnos) {
742 if (VNI->isUnused())
743 continue;
744 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
745 EqualVNs[ParentVNI->id].insert(VNI);
746 }
747
748 // For VNI aggregation of each ParentVNI, collect dominated, i.e.,
749 // redundant VNIs to BackCopies.
750 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
751 VNInfo *ParentVNI = Parent->getValNumInfo(i);
752 if (!NotToHoistSet.count(ParentVNI->id))
753 continue;
754 SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin();
755 SmallPtrSetIterator<VNInfo *> It2 = It1;
756 for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) {
757 It2 = It1;
758 for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) {
759 if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2))
760 continue;
761
762 MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def);
763 MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def);
764 if (MBB1 == MBB2) {
765 DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1));
766 } else if (MDT.dominates(MBB1, MBB2)) {
767 DominatedVNIs.insert(*It2);
768 } else if (MDT.dominates(MBB2, MBB1)) {
769 DominatedVNIs.insert(*It1);
770 }
771 }
772 }
773 if (!DominatedVNIs.empty()) {
774 forceRecompute(0, ParentVNI);
775 for (auto VNI : DominatedVNIs) {
776 BackCopies.push_back(VNI);
777 }
778 DominatedVNIs.clear();
779 }
780 }
781}
782
783/// For SM_Size mode, find a common dominator for all the back-copies for
784/// the same ParentVNI and hoist the backcopies to the dominator BB.
785/// For SM_Speed mode, if the common dominator is hot and it is not beneficial
786/// to do the hoisting, simply remove the dominated backcopies for the same
787/// ParentVNI.
788void SplitEditor::hoistCopies() {
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000789 // Get the complement interval, always RegIdx 0.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000790 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000791 LiveInterval *Parent = &Edit->getParent();
792
793 // Track the nearest common dominator for all back-copies for each ParentVNI,
794 // indexed by ParentVNI->id.
795 typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
796 SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
Wei Mi9a16d652016-04-13 03:08:27 +0000797 // The total cost of all the back-copies for each ParentVNI.
798 SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums());
799 // The ParentVNI->id set for which hoisting back-copies are not beneficial
800 // for Speed.
801 DenseSet<unsigned> NotToHoistSet;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000802
803 // Find the nearest common dominator for parent values with multiple
804 // back-copies. If a single back-copy dominates, put it in DomPair.second.
Matthias Braun96761952014-12-10 23:07:54 +0000805 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000806 if (VNI->isUnused())
807 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000808 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
809 assert(ParentVNI && "Parent not live at complement def");
810
811 // Don't hoist remats. The complement is probably going to disappear
812 // completely anyway.
813 if (Edit->didRematerialize(ParentVNI))
814 continue;
815
816 MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
Wei Mi9a16d652016-04-13 03:08:27 +0000817
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000818 DomPair &Dom = NearestDom[ParentVNI->id];
819
820 // Keep directly defined parent values. This is either a PHI or an
821 // instruction in the complement range. All other copies of ParentVNI
822 // should be eliminated.
823 if (VNI->def == ParentVNI->def) {
824 DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
825 Dom = DomPair(ValMBB, VNI->def);
826 continue;
827 }
828 // Skip the singly mapped values. There is nothing to gain from hoisting a
829 // single back-copy.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000830 if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000831 DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
832 continue;
833 }
834
835 if (!Dom.first) {
836 // First time we see ParentVNI. VNI dominates itself.
837 Dom = DomPair(ValMBB, VNI->def);
838 } else if (Dom.first == ValMBB) {
839 // Two defs in the same block. Pick the earlier def.
840 if (!Dom.second.isValid() || VNI->def < Dom.second)
841 Dom.second = VNI->def;
842 } else {
843 // Different basic blocks. Check if one dominates.
844 MachineBasicBlock *Near =
845 MDT.findNearestCommonDominator(Dom.first, ValMBB);
846 if (Near == ValMBB)
847 // Def ValMBB dominates.
848 Dom = DomPair(ValMBB, VNI->def);
849 else if (Near != Dom.first)
850 // None dominate. Hoist to common dominator, need new def.
851 Dom = DomPair(Near, SlotIndex());
Wei Mi9a16d652016-04-13 03:08:27 +0000852 Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000853 }
854
855 DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
856 << " for parent " << ParentVNI->id << '@' << ParentVNI->def
857 << " hoist to BB#" << Dom.first->getNumber() << ' '
858 << Dom.second << '\n');
859 }
860
861 // Insert the hoisted copies.
862 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
863 DomPair &Dom = NearestDom[i];
864 if (!Dom.first || Dom.second.isValid())
865 continue;
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000866 // This value needs a hoisted copy inserted at the end of Dom.first.
867 VNInfo *ParentVNI = Parent->getValNumInfo(i);
868 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
869 // Get a less loopy dominator than Dom.first.
870 Dom.first = findShallowDominator(Dom.first, DefMBB);
Wei Mi9a16d652016-04-13 03:08:27 +0000871 if (SpillMode == SM_Speed &&
872 MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) {
873 NotToHoistSet.insert(ParentVNI->id);
874 continue;
875 }
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000876 SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
877 Dom.second =
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000878 defFromParent(0, ParentVNI, Last, *Dom.first,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000879 SA.getLastSplitPointIter(Dom.first))->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000880 }
881
882 // Remove redundant back-copies that are now known to be dominated by another
883 // def with the same value.
884 SmallVector<VNInfo*, 8> BackCopies;
Matthias Braun96761952014-12-10 23:07:54 +0000885 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000886 if (VNI->isUnused())
887 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000888 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
889 const DomPair &Dom = NearestDom[ParentVNI->id];
Wei Mi9a16d652016-04-13 03:08:27 +0000890 if (!Dom.first || Dom.second == VNI->def ||
891 NotToHoistSet.count(ParentVNI->id))
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000892 continue;
893 BackCopies.push_back(VNI);
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000894 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000895 }
Wei Mi9a16d652016-04-13 03:08:27 +0000896
897 // If it is not beneficial to hoist all the BackCopies, simply remove
898 // redundant BackCopies in speed mode.
899 if (SpillMode == SM_Speed && !NotToHoistSet.empty())
900 computeRedundantBackCopies(NotToHoistSet, BackCopies);
901
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000902 removeBackCopies(BackCopies);
903}
904
905
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000906/// transferValues - Transfer all possible values to the new live ranges.
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000907/// Values that were rematerialized are left alone, they need LRCalc.extend().
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000908bool SplitEditor::transferValues() {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000909 bool Skipped = false;
910 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Matthias Braun96761952014-12-10 23:07:54 +0000911 for (const LiveRange::Segment &S : Edit->getParent()) {
912 DEBUG(dbgs() << " blit " << S << ':');
913 VNInfo *ParentVNI = S.valno;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000914 // RegAssign has holes where RegIdx 0 should be used.
Matthias Braun96761952014-12-10 23:07:54 +0000915 SlotIndex Start = S.start;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000916 AssignI.advanceTo(Start);
917 do {
918 unsigned RegIdx;
Matthias Braun96761952014-12-10 23:07:54 +0000919 SlotIndex End = S.end;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000920 if (!AssignI.valid()) {
921 RegIdx = 0;
922 } else if (AssignI.start() <= Start) {
923 RegIdx = AssignI.value();
924 if (AssignI.stop() < End) {
925 End = AssignI.stop();
926 ++AssignI;
927 }
928 } else {
929 RegIdx = 0;
930 End = std::min(End, AssignI.start());
931 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000932
933 // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000934 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000935 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000936
937 // Check for a simply defined value that can be blitted directly.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000938 ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
939 if (VNInfo *VNI = VFP.getPointer()) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000940 DEBUG(dbgs() << ':' << VNI->id);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000941 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000942 Start = End;
943 continue;
944 }
945
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000946 // Skip values with forced recomputation.
947 if (VFP.getInt()) {
948 DEBUG(dbgs() << "(recalc)");
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000949 Skipped = true;
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000950 Start = End;
951 continue;
952 }
953
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000954 LiveRangeCalc &LRC = getLRCalc(RegIdx);
955
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000956 // This value has multiple defs in RegIdx, but it wasn't rematerialized,
957 // so the live range is accurate. Add live-in blocks in [Start;End) to the
958 // LiveInBlocks.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000959 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000960 SlotIndex BlockStart, BlockEnd;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000961 std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000962
963 // The first block may be live-in, or it may have its own def.
964 if (Start != BlockStart) {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000965 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000966 assert(VNI && "Missing def for complex mapped value");
967 DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
968 // MBB has its own def. Is it also live-out?
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000969 if (BlockEnd <= End)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000970 LRC.setLiveOutValue(&*MBB, VNI);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000971
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000972 // Skip to the next block for live-in.
973 ++MBB;
974 BlockStart = BlockEnd;
975 }
976
977 // Handle the live-in blocks covered by [Start;End).
978 assert(Start <= BlockStart && "Expected live-in block");
979 while (BlockStart < End) {
980 DEBUG(dbgs() << ">BB#" << MBB->getNumber());
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000981 BlockEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000982 if (BlockStart == ParentVNI->def) {
983 // This block has the def of a parent PHI, so it isn't live-in.
984 assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000985 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000986 assert(VNI && "Missing def for complex mapped parent PHI");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000987 if (End >= BlockEnd)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000988 LRC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000989 } else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000990 // This block needs a live-in value. The last block covered may not
991 // be live-out.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000992 if (End < BlockEnd)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000993 LRC.addLiveInBlock(LR, MDT[&*MBB], End);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000994 else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000995 // Live-through, and we don't know the value.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000996 LRC.addLiveInBlock(LR, MDT[&*MBB]);
997 LRC.setLiveOutValue(&*MBB, nullptr);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000998 }
999 }
1000 BlockStart = BlockEnd;
1001 ++MBB;
1002 }
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001003 Start = End;
Matthias Braun96761952014-12-10 23:07:54 +00001004 } while (Start != S.end);
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001005 DEBUG(dbgs() << '\n');
1006 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001007
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +00001008 LRCalc[0].calculateValues();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +00001009 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +00001010 LRCalc[1].calculateValues();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001011
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001012 return Skipped;
1013}
1014
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001015void SplitEditor::extendPHIKillRanges() {
Wei Mi9a16d652016-04-13 03:08:27 +00001016 // Extend live ranges to be live-out for successor PHI values.
Matthias Braun96761952014-12-10 23:07:54 +00001017 for (const VNInfo *PHIVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001018 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
1019 continue;
1020 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
Matthias Braun2d5c32b2013-10-10 21:28:57 +00001021 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Wei Mi9a16d652016-04-13 03:08:27 +00001022
1023 // Check whether PHI is dead.
1024 const LiveRange::Segment *Segment = LR.getSegmentContaining(PHIVNI->def);
1025 assert(Segment != nullptr && "Missing segment for VNI");
1026 if (Segment->end == PHIVNI->def.getDeadSlot()) {
1027 // This is a dead PHI. Remove it.
1028 LR.removeSegment(*Segment, true);
1029 continue;
1030 }
1031
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001032 LiveRangeCalc &LRC = getLRCalc(RegIdx);
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001033 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
1034 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
1035 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001036 SlotIndex End = LIS.getMBBEndIdx(*PI);
1037 SlotIndex LastUse = End.getPrevSlot();
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001038 // The predecessor may not have a live-out value. That is OK, like an
1039 // undef PHI operand.
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001040 if (Edit->getParent().liveAt(LastUse)) {
1041 assert(RegAssign.lookup(LastUse) == RegIdx &&
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001042 "Different register assignment in phi predecessor");
Matthias Braun2d5c32b2013-10-10 21:28:57 +00001043 LRC.extend(LR, End);
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001044 }
1045 }
1046 }
1047}
1048
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001049/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001050void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001051 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +00001052 RE = MRI.reg_end(); RI != RE;) {
Owen Anderson16c6bf42014-03-13 23:12:04 +00001053 MachineOperand &MO = *RI;
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001054 MachineInstr *MI = MO.getParent();
1055 ++RI;
Eric Christopherede62672011-02-03 06:18:29 +00001056 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001057 if (MI->isDebugValue()) {
1058 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001059 MO.setReg(0);
1060 continue;
1061 }
Jakob Stoklund Olesenf6e03942011-02-09 21:52:09 +00001062
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +00001063 // <undef> operands don't really read the register, so it doesn't matter
1064 // which register we choose. When the use operand is tied to a def, we must
1065 // use the same register as the def, so just do that always.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001066 SlotIndex Idx = LIS.getInstructionIndex(*MI);
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +00001067 if (MO.isDef() || MO.isUndef())
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +00001068 Idx = Idx.getRegSlot(MO.isEarlyClobber());
Eric Christopherede62672011-02-03 06:18:29 +00001069
1070 // Rewrite to the mapped register at Idx.
1071 unsigned RegIdx = RegAssign.lookup(Idx);
Mark Laceyf9ea8852013-08-14 23:50:04 +00001072 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001073 MO.setReg(LI->reg);
Eric Christopherede62672011-02-03 06:18:29 +00001074 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
1075 << Idx << ':' << RegIdx << '\t' << *MI);
1076
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001077 // Extend liveness to Idx if the instruction reads reg.
Jakob Stoklund Olesen73a9eb92011-07-24 20:33:23 +00001078 if (!ExtendRanges || MO.isUndef())
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001079 continue;
1080
1081 // Skip instructions that don't read Reg.
1082 if (MO.isDef()) {
1083 if (!MO.getSubReg() && !MO.isEarlyClobber())
1084 continue;
1085 // We may wan't to extend a live range for a partial redef, or for a use
1086 // tied to an early clobber.
1087 Idx = Idx.getPrevSlot();
1088 if (!Edit->getParent().liveAt(Idx))
1089 continue;
1090 } else
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +00001091 Idx = Idx.getRegSlot(true);
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001092
Matthias Braun2d5c32b2013-10-10 21:28:57 +00001093 getLRCalc(RegIdx).extend(*LI, Idx.getNextSlot());
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001094 }
1095}
1096
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001097void SplitEditor::deleteRematVictims() {
1098 SmallVector<MachineInstr*, 8> Dead;
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001099 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
Mark Laceyf9ea8852013-08-14 23:50:04 +00001100 LiveInterval *LI = &LIS.getInterval(*I);
Matthias Braun96761952014-12-10 23:07:54 +00001101 for (const LiveRange::Segment &S : LI->segments) {
Jakob Stoklund Olesend8f24052011-11-13 22:42:13 +00001102 // Dead defs end at the dead slot.
Matthias Braun96761952014-12-10 23:07:54 +00001103 if (S.end != S.valno->def.getDeadSlot())
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001104 continue;
Wei Mi9a16d652016-04-13 03:08:27 +00001105 if (S.valno->isPHIDef())
1106 continue;
Matthias Braun96761952014-12-10 23:07:54 +00001107 MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001108 assert(MI && "Missing instruction for dead def");
1109 MI->addRegisterDead(LI->reg, &TRI);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001110
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001111 if (!MI->allDefsAreDead())
1112 continue;
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001113
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001114 DEBUG(dbgs() << "All defs dead: " << *MI);
1115 Dead.push_back(MI);
1116 }
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001117 }
1118
1119 if (Dead.empty())
1120 return;
1121
Pete Cooper2bde2f42012-04-02 22:22:53 +00001122 Edit->eliminateDeadDefs(Dead);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001123}
1124
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001125void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001126 ++NumFinished;
Eric Christopher21933532011-02-03 05:40:54 +00001127
Eric Christopherede62672011-02-03 06:18:29 +00001128 // At this point, the live intervals in Edit contain VNInfos corresponding to
1129 // the inserted copies.
1130
1131 // Add the original defs from the parent interval.
Matthias Braun96761952014-12-10 23:07:54 +00001132 for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen3295a992011-02-04 00:59:23 +00001133 if (ParentVNI->isUnused())
1134 continue;
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +00001135 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Jakob Stoklund Olesen97e14e02012-07-27 21:11:14 +00001136 defValue(RegIdx, ParentVNI, ParentVNI->def);
Jakob Stoklund Olesen32210de2011-03-15 21:13:22 +00001137
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001138 // Force rematted values to be recomputed everywhere.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001139 // The new live ranges may be truncated.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001140 if (Edit->didRematerialize(ParentVNI))
1141 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001142 forceRecompute(i, ParentVNI);
Eric Christopherede62672011-02-03 06:18:29 +00001143 }
1144
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001145 // Hoist back-copies to the complement interval when in spill mode.
1146 switch (SpillMode) {
1147 case SM_Partition:
1148 // Leave all back-copies as is.
1149 break;
1150 case SM_Size:
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001151 case SM_Speed:
Wei Mi9a16d652016-04-13 03:08:27 +00001152 // hoistCopies will behave differently between size and speed.
1153 hoistCopies();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001154 }
1155
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001156 // Transfer the simply mapped values, check if any are skipped.
1157 bool Skipped = transferValues();
Wei Mi9a16d652016-04-13 03:08:27 +00001158
1159 // Rewrite virtual registers, possibly extending ranges.
1160 rewriteAssigned(Skipped);
1161
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001162 if (Skipped)
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001163 extendPHIKillRanges();
1164 else
1165 ++NumSimple;
Eric Christopherede62672011-02-03 06:18:29 +00001166
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001167 // Delete defs that were rematted everywhere.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001168 if (Skipped)
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001169 deleteRematVictims();
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001170
Jakob Stoklund Olesen0f1677e2010-10-07 23:34:34 +00001171 // Get rid of unused values and set phi-kill flags.
Mark Laceyf9ea8852013-08-14 23:50:04 +00001172 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) {
1173 LiveInterval &LI = LIS.getInterval(*I);
1174 LI.RenumberValues();
1175 }
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001176
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001177 // Provide a reverse mapping from original indices to Edit ranges.
1178 if (LRMap) {
1179 LRMap->clear();
1180 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1181 LRMap->push_back(i);
1182 }
1183
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001184 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +00001185 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001186 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001187 // Don't use iterators, they are invalidated by create() below.
Matthias Braund3dd1352015-09-22 03:44:41 +00001188 unsigned VReg = Edit->get(i);
1189 LiveInterval &LI = LIS.getInterval(VReg);
1190 SmallVector<LiveInterval*, 8> SplitLIs;
1191 LIS.splitSeparateComponents(LI, SplitLIs);
1192 unsigned Original = VRM.getOriginal(VReg);
1193 for (LiveInterval *SplitLI : SplitLIs)
1194 VRM.setIsSplitFromReg(SplitLI->reg, Original);
1195
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001196 // The new intervals all map back to i.
1197 if (LRMap)
1198 LRMap->resize(Edit->size(), i);
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001199 }
1200
Jakob Stoklund Olesen284c2db2010-08-10 17:07:22 +00001201 // Calculate spill weight and allocation hints for new intervals.
Benjamin Kramere2a1d892013-06-17 19:00:36 +00001202 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001203
1204 assert(!LRMap || LRMap->size() == Edit->size());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +00001205}
1206
1207
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +00001208//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +00001209// Single Block Splitting
1210//===----------------------------------------------------------------------===//
1211
Jakob Stoklund Olesen8627ea92011-08-05 22:20:45 +00001212bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
1213 bool SingleInstrs) const {
1214 // Always split for multiple instructions.
1215 if (!BI.isOneInstr())
1216 return true;
1217 // Don't split for single instructions unless explicitly requested.
1218 if (!SingleInstrs)
1219 return false;
1220 // Splitting a live-through range always makes progress.
1221 if (BI.LiveIn && BI.LiveOut)
1222 return true;
1223 // No point in isolating a copy. It has no register class constraints.
1224 if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
1225 return false;
1226 // Finally, don't isolate an end point that was created by earlier splits.
1227 return isOriginalEndpoint(BI.FirstInstr);
1228}
1229
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001230void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1231 openIntv();
1232 SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001233 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001234 LastSplitPoint));
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001235 if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
1236 useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001237 } else {
1238 // The last use is after the last valid split point.
1239 SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1240 useIntv(SegStart, SegStop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001241 overlapIntv(SegStop, BI.LastInstr);
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001242 }
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001243}
1244
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001245
1246//===----------------------------------------------------------------------===//
1247// Global Live Range Splitting Support
1248//===----------------------------------------------------------------------===//
1249
1250// These methods support a method of global live range splitting that uses a
1251// global algorithm to decide intervals for CFG edges. They will insert split
1252// points and color intervals in basic blocks while avoiding interference.
1253//
1254// Note that splitSingleBlock is also useful for blocks where both CFG edges
1255// are on the stack.
1256
1257void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1258 unsigned IntvIn, SlotIndex LeaveBefore,
1259 unsigned IntvOut, SlotIndex EnterAfter){
1260 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001261 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001262
1263 DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
1264 << ") intf " << LeaveBefore << '-' << EnterAfter
1265 << ", live-through " << IntvIn << " -> " << IntvOut);
1266
1267 assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1268
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001269 assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
1270 assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
1271 assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
1272
1273 MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
1274
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001275 if (!IntvOut) {
1276 DEBUG(dbgs() << ", spill on entry.\n");
1277 //
1278 // <<<<<<<<< Possible LeaveBefore interference.
1279 // |-----------| Live through.
1280 // -____________ Spill on entry.
1281 //
1282 selectIntv(IntvIn);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001283 SlotIndex Idx = leaveIntvAtTop(*MBB);
1284 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1285 (void)Idx;
1286 return;
1287 }
1288
1289 if (!IntvIn) {
1290 DEBUG(dbgs() << ", reload on exit.\n");
1291 //
1292 // >>>>>>> Possible EnterAfter interference.
1293 // |-----------| Live through.
1294 // ___________-- Reload on exit.
1295 //
1296 selectIntv(IntvOut);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001297 SlotIndex Idx = enterIntvAtEnd(*MBB);
1298 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1299 (void)Idx;
1300 return;
1301 }
1302
1303 if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1304 DEBUG(dbgs() << ", straight through.\n");
1305 //
1306 // |-----------| Live through.
1307 // ------------- Straight through, same intv, no interference.
1308 //
1309 selectIntv(IntvOut);
1310 useIntv(Start, Stop);
1311 return;
1312 }
1313
1314 // We cannot legally insert splits after LSP.
1315 SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001316 assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001317
1318 if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1319 LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1320 DEBUG(dbgs() << ", switch avoiding interference.\n");
1321 //
1322 // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
1323 // |-----------| Live through.
1324 // ------======= Switch intervals between interference.
1325 //
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001326 selectIntv(IntvOut);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001327 SlotIndex Idx;
1328 if (LeaveBefore && LeaveBefore < LSP) {
1329 Idx = enterIntvBefore(LeaveBefore);
1330 useIntv(Idx, Stop);
1331 } else {
1332 Idx = enterIntvAtEnd(*MBB);
1333 }
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001334 selectIntv(IntvIn);
1335 useIntv(Start, Idx);
1336 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1337 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1338 return;
1339 }
1340
1341 DEBUG(dbgs() << ", create local intv for interference.\n");
1342 //
1343 // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
1344 // |-----------| Live through.
1345 // ==---------== Switch intervals before/after interference.
1346 //
1347 assert(LeaveBefore <= EnterAfter && "Missed case");
1348
1349 selectIntv(IntvOut);
1350 SlotIndex Idx = enterIntvAfter(EnterAfter);
1351 useIntv(Idx, Stop);
1352 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1353
1354 selectIntv(IntvIn);
1355 Idx = leaveIntvBefore(LeaveBefore);
1356 useIntv(Start, Idx);
1357 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1358}
1359
1360
1361void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1362 unsigned IntvIn, SlotIndex LeaveBefore) {
1363 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001364 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001365
1366 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001367 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001368 << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
1369 << (BI.LiveOut ? ", stack-out" : ", killed in block"));
1370
1371 assert(IntvIn && "Must have register in");
1372 assert(BI.LiveIn && "Must be live-in");
1373 assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1374
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001375 if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001376 DEBUG(dbgs() << " before interference.\n");
1377 //
1378 // <<< Interference after kill.
1379 // |---o---x | Killed in block.
1380 // ========= Use IntvIn everywhere.
1381 //
1382 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001383 useIntv(Start, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001384 return;
1385 }
1386
1387 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1388
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001389 if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001390 //
1391 // <<< Possible interference after last use.
1392 // |---o---o---| Live-out on stack.
1393 // =========____ Leave IntvIn after last use.
1394 //
1395 // < Interference after last use.
1396 // |---o---o--o| Live-out on stack, late last use.
1397 // ============ Copy to stack after LSP, overlap IntvIn.
1398 // \_____ Stack interval is live-out.
1399 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001400 if (BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001401 DEBUG(dbgs() << ", spill after last use before interference.\n");
1402 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001403 SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001404 useIntv(Start, Idx);
1405 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1406 } else {
1407 DEBUG(dbgs() << ", spill before last split point.\n");
1408 selectIntv(IntvIn);
Jakob Stoklund Olesen37e3a132011-07-16 00:13:30 +00001409 SlotIndex Idx = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001410 overlapIntv(Idx, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001411 useIntv(Start, Idx);
1412 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1413 }
1414 return;
1415 }
1416
1417 // The interference is overlapping somewhere we wanted to use IntvIn. That
1418 // means we need to create a local interval that can be allocated a
1419 // different register.
1420 unsigned LocalIntv = openIntv();
Matt Beaumont-Gay26909d82011-07-16 04:18:47 +00001421 (void)LocalIntv;
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001422 DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
1423
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001424 if (!BI.LiveOut || BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001425 //
1426 // <<<<<<< Interference overlapping uses.
1427 // |---o---o---| Live-out on stack.
1428 // =====----____ Leave IntvIn before interference, then spill.
1429 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001430 SlotIndex To = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001431 SlotIndex From = enterIntvBefore(LeaveBefore);
1432 useIntv(From, To);
1433 selectIntv(IntvIn);
1434 useIntv(Start, From);
1435 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1436 return;
1437 }
1438
1439 // <<<<<<< Interference overlapping uses.
1440 // |---o---o--o| Live-out on stack, late last use.
1441 // =====------- Copy to stack before LSP, overlap LocalIntv.
1442 // \_____ Stack interval is live-out.
1443 //
1444 SlotIndex To = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001445 overlapIntv(To, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001446 SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1447 useIntv(From, To);
1448 selectIntv(IntvIn);
1449 useIntv(Start, From);
1450 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1451}
1452
1453void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1454 unsigned IntvOut, SlotIndex EnterAfter) {
1455 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001456 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001457
1458 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001459 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001460 << ", reg-out " << IntvOut << ", enter after " << EnterAfter
1461 << (BI.LiveIn ? ", stack-in" : ", defined in block"));
1462
1463 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1464
1465 assert(IntvOut && "Must have register out");
1466 assert(BI.LiveOut && "Must be live-out");
1467 assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1468
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001469 if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001470 DEBUG(dbgs() << " after interference.\n");
1471 //
1472 // >>>> Interference before def.
1473 // | o---o---| Defined in block.
1474 // ========= Use IntvOut everywhere.
1475 //
1476 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001477 useIntv(BI.FirstInstr, Stop);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001478 return;
1479 }
1480
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001481 if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001482 DEBUG(dbgs() << ", reload after interference.\n");
1483 //
1484 // >>>> Interference before def.
1485 // |---o---o---| Live-through, stack-in.
1486 // ____========= Enter IntvOut before first use.
1487 //
1488 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001489 SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001490 useIntv(Idx, Stop);
1491 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1492 return;
1493 }
1494
1495 // The interference is overlapping somewhere we wanted to use IntvOut. That
1496 // means we need to create a local interval that can be allocated a
1497 // different register.
1498 DEBUG(dbgs() << ", interference overlaps uses.\n");
1499 //
1500 // >>>>>>> Interference overlapping uses.
1501 // |---o---o---| Live-through, stack-in.
1502 // ____---====== Create local interval for interference range.
1503 //
1504 selectIntv(IntvOut);
1505 SlotIndex Idx = enterIntvAfter(EnterAfter);
1506 useIntv(Idx, Stop);
1507 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1508
1509 openIntv();
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001510 SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001511 useIntv(From, Idx);
1512}