blob: 436d5bb21d93973291faee1b47cf288d3796a877 [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);
Reid Klecknered170792015-09-17 17:19:40 +000060 // FIXME: Handle multiple EH pad successors.
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000061 const MachineBasicBlock *LPad = MBB->getLandingPadSuccessor();
62 std::pair<SlotIndex, SlotIndex> &LSP = LastSplitPoint[Num];
Jakob Stoklund Olesen8b1d0232012-01-11 02:07:05 +000063 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000064
65 // Compute split points on the first call. The pair is independent of the
66 // current live interval.
67 if (!LSP.first.isValid()) {
68 MachineBasicBlock::const_iterator FirstTerm = MBB->getFirstTerminator();
69 if (FirstTerm == MBB->end())
Jakob Stoklund Olesen8b1d0232012-01-11 02:07:05 +000070 LSP.first = MBBEnd;
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000071 else
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +000072 LSP.first = LIS.getInstructionIndex(*FirstTerm);
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000073
74 // If there is a landing pad successor, also find the call instruction.
75 if (!LPad)
76 return LSP.first;
77 // There may not be a call instruction (?) in which case we ignore LPad.
78 LSP.second = LSP.first;
Jakob Stoklund Olesen040d6592011-06-28 01:18:58 +000079 for (MachineBasicBlock::const_iterator I = MBB->end(), E = MBB->begin();
80 I != E;) {
81 --I;
Evan Cheng7f8e5632011-12-07 07:15:52 +000082 if (I->isCall()) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +000083 LSP.second = LIS.getInstructionIndex(*I);
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000084 break;
85 }
Jakob Stoklund Olesen040d6592011-06-28 01:18:58 +000086 }
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000087 }
88
89 // If CurLI is live into a landing pad successor, move the last split point
90 // back to the call that may throw.
Jakob Stoklund Olesen8b1d0232012-01-11 02:07:05 +000091 if (!LPad || !LSP.second || !LIS.isLiveInToMBB(*CurLI, LPad))
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000092 return LSP.first;
Jakob Stoklund Olesen8b1d0232012-01-11 02:07:05 +000093
94 // Find the value leaving MBB.
95 const VNInfo *VNI = CurLI->getVNInfoBefore(MBBEnd);
96 if (!VNI)
97 return LSP.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, LSP.second) && VNI->def < MBBEnd)
104 return LSP.first;
105
106 // Value is properly live-in to the landing pad.
107 // Only allow splits before the call.
108 return LSP.second;
Jakob Stoklund Olesened4075c2010-07-20 21:46:58 +0000109}
110
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000111MachineBasicBlock::iterator
112SplitAnalysis::getLastSplitPointIter(MachineBasicBlock *MBB) {
113 SlotIndex LSP = getLastSplitPoint(MBB->getNumber());
114 if (LSP == LIS.getMBBEndIdx(MBB))
115 return MBB->end();
116 return LIS.getInstructionFromIndex(LSP);
117}
118
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000119/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000120void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000121 assert(UseSlots.empty() && "Call clear first");
122
123 // First get all the defs from the interval values. This provides the correct
124 // slots for early clobbers.
Matthias Braun96761952014-12-10 23:07:54 +0000125 for (const VNInfo *VNI : CurLI->valnos)
126 if (!VNI->isPHIDef() && !VNI->isUnused())
127 UseSlots.push_back(VNI->def);
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000128
129 // Get use slots form the use-def chain.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000130 const MachineRegisterInfo &MRI = MF.getRegInfo();
Owen Andersonb36376e2014-03-17 19:36:09 +0000131 for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg))
132 if (!MO.isUndef())
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000133 UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot());
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000134
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000135 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000136
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000137 // Remove duplicates, keeping the smaller slot for each instruction.
138 // That is what we want for early clobbers.
139 UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
140 SlotIndex::isSameInstr),
141 UseSlots.end());
142
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000143 // Compute per-live block info.
144 if (!calcLiveBlockInfo()) {
145 // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
Rafael Espindola676c4052011-06-26 22:34:10 +0000146 // I am looking at you, RegisterCoalescer!
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000147 DidRepairRange = true;
Jakob Stoklund Olesen50215af2011-05-10 17:37:41 +0000148 ++NumRepairs;
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000149 DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
150 const_cast<LiveIntervals&>(LIS)
151 .shrinkToUses(const_cast<LiveInterval*>(CurLI));
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000152 UseBlocks.clear();
153 ThroughBlocks.clear();
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000154 bool fixed = calcLiveBlockInfo();
155 (void)fixed;
156 assert(fixed && "Couldn't fix broken live interval");
157 }
158
Jakob Stoklund Olesenbd6b86e2011-03-27 22:49:23 +0000159 DEBUG(dbgs() << "Analyze counted "
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000160 << UseSlots.size() << " instrs in "
161 << UseBlocks.size() << " blocks, through "
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000162 << NumThroughBlocks << " blocks.\n");
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000163}
164
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000165/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
166/// where CurLI is live.
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000167bool SplitAnalysis::calcLiveBlockInfo() {
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000168 ThroughBlocks.resize(MF.getNumBlockIDs());
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000169 NumThroughBlocks = NumGapBlocks = 0;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000170 if (CurLI->empty())
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000171 return true;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000172
173 LiveInterval::const_iterator LVI = CurLI->begin();
174 LiveInterval::const_iterator LVE = CurLI->end();
175
176 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
177 UseI = UseSlots.begin();
178 UseE = UseSlots.end();
179
180 // Loop over basic blocks where CurLI is live.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000181 MachineFunction::iterator MFI =
182 LIS.getMBBFromIndex(LVI->start)->getIterator();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000183 for (;;) {
184 BlockInfo BI;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000185 BI.MBB = &*MFI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000186 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000187 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000188
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000189 // If the block contains no uses, the range must be live through. At one
Rafael Espindola676c4052011-06-26 22:34:10 +0000190 // point, RegisterCoalescer could create dangling ranges that ended
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000191 // mid-block.
192 if (UseI == UseE || *UseI >= Stop) {
193 ++NumThroughBlocks;
194 ThroughBlocks.set(BI.MBB->getNumber());
195 // The range shouldn't end mid-block if there are no uses. This shouldn't
196 // happen.
197 if (LVI->end < Stop)
198 return false;
199 } else {
200 // This block has uses. Find the first and last uses in the block.
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000201 BI.FirstInstr = *UseI;
202 assert(BI.FirstInstr >= Start);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000203 do ++UseI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000204 while (UseI != UseE && *UseI < Stop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000205 BI.LastInstr = UseI[-1];
206 assert(BI.LastInstr < Stop);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000207
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000208 // LVI is the first live segment overlapping MBB.
209 BI.LiveIn = LVI->start <= Start;
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000210
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000211 // When not live in, the first use should be a def.
212 if (!BI.LiveIn) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000213 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000214 assert(LVI->start == BI.FirstInstr && "First instr should be a def");
215 BI.FirstDef = BI.FirstInstr;
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000216 }
217
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000218 // Look for gaps in the live range.
219 BI.LiveOut = true;
220 while (LVI->end < Stop) {
221 SlotIndex LastStop = LVI->end;
222 if (++LVI == LVE || LVI->start >= Stop) {
223 BI.LiveOut = false;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000224 BI.LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000225 break;
226 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000227
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000228 if (LastStop < LVI->start) {
229 // There is a gap in the live range. Create duplicate entries for the
230 // live-in snippet and the live-out snippet.
231 ++NumGapBlocks;
232
233 // Push the Live-in part.
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000234 BI.LiveOut = false;
235 UseBlocks.push_back(BI);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000236 UseBlocks.back().LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000237
238 // Set up BI for the live-out part.
239 BI.LiveIn = false;
240 BI.LiveOut = true;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000241 BI.FirstInstr = BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000242 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000243
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000244 // A Segment that starts in the middle of the block must be a def.
245 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000246 if (!BI.FirstDef)
247 BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000248 }
249
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000250 UseBlocks.push_back(BI);
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000251
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000252 // LVI is now at LVE or LVI->end >= Stop.
253 if (LVI == LVE)
254 break;
255 }
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000256
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000257 // Live segment ends exactly at Stop. Move to the next segment.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000258 if (LVI->end == Stop && ++LVI == LVE)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000259 break;
260
261 // Pick the next basic block.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000262 if (LVI->start < Stop)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000263 ++MFI;
264 else
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000265 MFI = LIS.getMBBFromIndex(LVI->start)->getIterator();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000266 }
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000267
268 assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000269 return true;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000270}
271
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000272unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
273 if (cli->empty())
274 return 0;
275 LiveInterval *li = const_cast<LiveInterval*>(cli);
276 LiveInterval::iterator LVI = li->begin();
277 LiveInterval::iterator LVE = li->end();
278 unsigned Count = 0;
279
280 // Loop over basic blocks where li is live.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000281 MachineFunction::const_iterator MFI =
282 LIS.getMBBFromIndex(LVI->start)->getIterator();
283 SlotIndex Stop = LIS.getMBBEndIdx(&*MFI);
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000284 for (;;) {
285 ++Count;
286 LVI = li->advanceTo(LVI, Stop);
287 if (LVI == LVE)
288 return Count;
289 do {
290 ++MFI;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000291 Stop = LIS.getMBBEndIdx(&*MFI);
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000292 } while (Stop <= LVI->start);
293 }
294}
295
Jakob Stoklund Olesen60a26a62011-02-21 23:09:46 +0000296bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
297 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
298 const LiveInterval &Orig = LIS.getInterval(OrigReg);
299 assert(!Orig.empty() && "Splitting empty interval?");
300 LiveInterval::const_iterator I = Orig.find(Idx);
301
302 // Range containing Idx should begin at Idx.
303 if (I != Orig.end() && I->start <= Idx)
304 return I->start == Idx;
305
306 // Range does not contain Idx, previous must end at Idx.
307 return I != Orig.begin() && (--I)->end == Idx;
308}
309
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000310void SplitAnalysis::analyze(const LiveInterval *li) {
311 clear();
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000312 CurLI = li;
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000313 analyzeUses();
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000314}
315
Jakob Stoklund Olesen28e769c2010-12-15 17:49:52 +0000316
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000317//===----------------------------------------------------------------------===//
318// Split Editor
319//===----------------------------------------------------------------------===//
320
321/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Eric Christopherd9134482014-08-04 21:25:23 +0000322SplitEditor::SplitEditor(SplitAnalysis &sa, LiveIntervals &lis, VirtRegMap &vrm,
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000323 MachineDominatorTree &mdt,
324 MachineBlockFrequencyInfo &mbfi)
Eric Christopherd9134482014-08-04 21:25:23 +0000325 : SA(sa), LIS(lis), VRM(vrm), MRI(vrm.getMachineFunction().getRegInfo()),
Eric Christopher60621802014-10-14 07:22:00 +0000326 MDT(mdt), TII(*vrm.getMachineFunction().getSubtarget().getInstrInfo()),
327 TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()),
Eric Christopherd9134482014-08-04 21:25:23 +0000328 MBFI(mbfi), Edit(nullptr), OpenIdx(0), SpillMode(SM_Partition),
329 RegAssign(Allocator) {}
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000330
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000331void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
332 Edit = &LRE;
333 SpillMode = SM;
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000334 OpenIdx = 0;
335 RegAssign.clear();
336 Values.clear();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000337
338 // Reset the LiveRangeCalc instances needed for this spill mode.
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000339 LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
340 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000341 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000342 LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
343 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000344
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000345 // We don't need an AliasAnalysis since we will only be performing
346 // cheap-as-a-copy remats anyway.
Craig Topperc0196b12014-04-14 00:51:57 +0000347 Edit->anyRematerializable(nullptr);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000348}
349
Manman Ren19f49ac2012-09-11 22:23:19 +0000350#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000351LLVM_DUMP_METHOD void SplitEditor::dump() const {
Eric Christopherede62672011-02-03 06:18:29 +0000352 if (RegAssign.empty()) {
353 dbgs() << " empty\n";
354 return;
355 }
356
357 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
358 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
359 dbgs() << '\n';
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000360}
Manman Ren742534c2012-09-06 19:06:06 +0000361#endif
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000362
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000363VNInfo *SplitEditor::defValue(unsigned RegIdx,
364 const VNInfo *ParentVNI,
365 SlotIndex Idx) {
366 assert(ParentVNI && "Mapping NULL value");
367 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000368 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
Mark Laceyf9ea8852013-08-14 23:50:04 +0000369 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000370
371 // Create a new value.
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000372 VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000373
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000374 // Use insert for lookup, so we can add missing values with a second lookup.
375 std::pair<ValueMap::iterator, bool> InsP =
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000376 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id),
377 ValueForcePair(VNI, false)));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000378
379 // This was the first time (RegIdx, ParentVNI) was mapped.
380 // Keep it as a simple def without any liveness.
381 if (InsP.second)
382 return VNI;
383
384 // If the previous value was a simple mapping, add liveness for it now.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000385 if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000386 SlotIndex Def = OldVNI->def;
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000387 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), OldVNI));
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000388 // No longer a simple mapping. Switch to a complex, non-forced mapping.
389 InsP.first->second = ValueForcePair();
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000390 }
391
392 // This is a complex mapping, add liveness for VNI
393 SlotIndex Def = VNI->def;
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000394 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000395
396 return VNI;
397}
398
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000399void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000400 assert(ParentVNI && "Mapping NULL value");
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000401 ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
402 VNInfo *VNI = VFP.getPointer();
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000403
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000404 // ParentVNI was either unmapped or already complex mapped. Either way, just
405 // set the force bit.
406 if (!VNI) {
407 VFP.setInt(true);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000408 return;
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000409 }
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000410
411 // This was previously a single mapping. Make sure the old def is represented
412 // by a trivial live range.
413 SlotIndex Def = VNI->def;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000414 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000415 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000416 // Mark as complex mapped, forced.
Craig Topperc0196b12014-04-14 00:51:57 +0000417 VFP = ValueForcePair(nullptr, true);
Jakob Stoklund Olesen4484f992011-09-13 18:05:29 +0000418}
419
Eric Christopherede62672011-02-03 06:18:29 +0000420VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000421 VNInfo *ParentVNI,
422 SlotIndex UseIdx,
423 MachineBasicBlock &MBB,
424 MachineBasicBlock::iterator I) {
Craig Topperc0196b12014-04-14 00:51:57 +0000425 MachineInstr *CopyMI = nullptr;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000426 SlotIndex Def;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000427 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000428
Jakob Stoklund Olesen7d406792011-05-02 05:29:58 +0000429 // We may be trying to avoid interference that ends at a deleted instruction,
430 // so always begin RegIdx 0 early and all others late.
431 bool Late = RegIdx != 0;
432
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000433 // Attempt cheap-as-a-copy rematerialization.
Wei Mi9a16d652016-04-13 03:08:27 +0000434 unsigned Original = VRM.getOriginal(Edit->get(RegIdx));
435 LiveInterval &OrigLI = LIS.getInterval(Original);
436 VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000437 LiveRangeEdit::Remat RM(ParentVNI);
Wei Mi9a16d652016-04-13 03:08:27 +0000438 RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
439
440 if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) {
Pete Cooper2bde2f42012-04-02 22:22:53 +0000441 Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late);
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000442 ++NumRemats;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000443 } else {
444 // Can't remat, just insert a copy from parent.
Eric Christopherede62672011-02-03 06:18:29 +0000445 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000446 .addReg(Edit->getReg());
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000447 Def = LIS.getSlotIndexes()
448 ->insertMachineInstrInMaps(*CopyMI, Late)
449 .getRegSlot();
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000450 ++NumCopies;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000451 }
452
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000453 // Define the value in Reg.
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000454 return defValue(RegIdx, ParentVNI, Def);
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000455}
456
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000457/// Create a new virtual register and live interval.
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000458unsigned SplitEditor::openIntv() {
Eric Christopherede62672011-02-03 06:18:29 +0000459 // Create the complement as index 0.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000460 if (Edit->empty())
Mark Lacey9d8103d2013-08-14 23:50:16 +0000461 Edit->createEmptyInterval();
Eric Christopherede62672011-02-03 06:18:29 +0000462
463 // Create the open interval.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000464 OpenIdx = Edit->size();
Mark Lacey9d8103d2013-08-14 23:50:16 +0000465 Edit->createEmptyInterval();
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000466 return OpenIdx;
467}
468
469void SplitEditor::selectIntv(unsigned Idx) {
470 assert(Idx != 0 && "Cannot select the complement interval");
471 assert(Idx < Edit->size() && "Can only select previously opened interval");
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000472 DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000473 OpenIdx = Idx;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000474}
475
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000476SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000477 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000478 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000479 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000480 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000481 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000482 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000483 return Idx;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000484 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000485 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000486 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000487 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000488
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000489 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
490 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000491}
492
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000493SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
494 assert(OpenIdx && "openIntv not called before enterIntvAfter");
495 DEBUG(dbgs() << " enterIntvAfter " << Idx);
496 Idx = Idx.getBoundaryIndex();
497 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
498 if (!ParentVNI) {
499 DEBUG(dbgs() << ": not live\n");
500 return Idx;
501 }
502 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
503 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
504 assert(MI && "enterIntvAfter called with invalid index");
505
506 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000507 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000508 return VNI->def;
509}
510
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000511SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000512 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000513 SlotIndex End = LIS.getMBBEndIdx(&MBB);
514 SlotIndex Last = End.getPrevSlot();
515 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000516 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000517 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000518 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000519 return End;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000520 }
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000521 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000522 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000523 SA.getLastSplitPointIter(&MBB));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000524 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopherede62672011-02-03 06:18:29 +0000525 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000526 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000527}
528
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000529/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000530void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000531 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000532}
533
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000534void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopherede62672011-02-03 06:18:29 +0000535 assert(OpenIdx && "openIntv not called before useIntv");
536 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
537 RegAssign.insert(Start, End, OpenIdx);
538 DEBUG(dump());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000539}
540
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000541SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000542 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000543 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000544
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000545 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000546 SlotIndex Boundary = Idx.getBoundaryIndex();
547 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000548 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000549 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000550 return Boundary.getNextSlot();
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000551 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000552 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000553 MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
Jakob Stoklund Olesen3d11c8e2011-02-08 18:50:18 +0000554 assert(MI && "No instruction at index");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000555
556 // In spill mode, make live ranges as short as possible by inserting the copy
557 // before MI. This is only possible if that instruction doesn't redefine the
558 // value. The inserted COPY is not a kill, and we don't need to recompute
559 // the source live range. The spiller also won't try to hoist this copy.
560 if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
561 MI->readsVirtualRegister(Edit->getReg())) {
562 forceRecompute(0, ParentVNI);
563 defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
564 return Idx;
565 }
566
567 VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000568 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000569 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000570}
571
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000572SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
573 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
574 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
575
576 // The interval must be live into the instruction at Idx.
Jakob Stoklund Olesenc45d38e2011-07-18 18:47:13 +0000577 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000578 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000579 if (!ParentVNI) {
580 DEBUG(dbgs() << ": not live\n");
581 return Idx.getNextSlot();
582 }
583 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
584
585 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
586 assert(MI && "No instruction at index");
587 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
588 return VNI->def;
589}
590
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000591SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000592 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000593 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000594 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000595
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000596 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000597 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000598 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000599 return Start;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000600 }
601
Eric Christopherede62672011-02-03 06:18:29 +0000602 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000603 MBB.SkipPHIsAndLabels(MBB.begin()));
Eric Christopherede62672011-02-03 06:18:29 +0000604 RegAssign.insert(Start, VNI->def, OpenIdx);
605 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000606 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000607}
608
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000609void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
610 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000611 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesend7bcf432011-11-14 01:39:36 +0000612 assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000613 "Parent changes value in extended range");
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000614 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
615 "Range cannot span basic blocks");
616
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000617 // The complement interval will be extended as needed by LRCalc.extend().
Jakob Stoklund Olesen5c482cd2011-04-05 23:43:14 +0000618 if (ParentVNI)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000619 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000620 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
621 RegAssign.insert(Start, End, OpenIdx);
622 DEBUG(dump());
623}
624
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000625//===----------------------------------------------------------------------===//
626// Spill modes
627//===----------------------------------------------------------------------===//
628
629void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
Mark Laceyf9ea8852013-08-14 23:50:04 +0000630 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000631 DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
632 RegAssignMap::iterator AssignI;
633 AssignI.setMap(RegAssign);
634
635 for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
Matthias Braun311730a2015-01-21 19:02:30 +0000636 SlotIndex Def = Copies[i]->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000637 MachineInstr *MI = LIS.getInstructionFromIndex(Def);
638 assert(MI && "No instruction for back-copy");
639
640 MachineBasicBlock *MBB = MI->getParent();
641 MachineBasicBlock::iterator MBBI(MI);
642 bool AtBegin;
643 do AtBegin = MBBI == MBB->begin();
644 while (!AtBegin && (--MBBI)->isDebugValue());
645
646 DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
Matthias Braun311730a2015-01-21 19:02:30 +0000647 LIS.removeVRegDefAt(*LI, Def);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000648 LIS.RemoveMachineInstrFromMaps(*MI);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000649 MI->eraseFromParent();
650
Matthias Braun311730a2015-01-21 19:02:30 +0000651 // Adjust RegAssign if a register assignment is killed at Def. We want to
652 // avoid calculating the live range of the source register if possible.
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000653 AssignI.find(Def.getPrevSlot());
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000654 if (!AssignI.valid() || AssignI.start() >= Def)
655 continue;
656 // If MI doesn't kill the assigned register, just leave it.
657 if (AssignI.stop() != Def)
658 continue;
659 unsigned RegIdx = AssignI.value();
660 if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
661 DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n');
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000662 forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000663 } else {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000664 SlotIndex Kill = LIS.getInstructionIndex(*MBBI).getRegSlot();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000665 DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
666 AssignI.setStop(Kill);
667 }
668 }
669}
670
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000671MachineBasicBlock*
672SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
673 MachineBasicBlock *DefMBB) {
674 if (MBB == DefMBB)
675 return MBB;
676 assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
677
678 const MachineLoopInfo &Loops = SA.Loops;
679 const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
680 MachineDomTreeNode *DefDomNode = MDT[DefMBB];
681
682 // Best candidate so far.
683 MachineBasicBlock *BestMBB = MBB;
684 unsigned BestDepth = UINT_MAX;
685
686 for (;;) {
687 const MachineLoop *Loop = Loops.getLoopFor(MBB);
688
689 // MBB isn't in a loop, it doesn't get any better. All dominators have a
690 // higher frequency by definition.
691 if (!Loop) {
692 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
693 << MBB->getNumber() << " at depth 0\n");
694 return MBB;
695 }
696
697 // We'll never be able to exit the DefLoop.
698 if (Loop == DefLoop) {
699 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
700 << MBB->getNumber() << " in the same loop\n");
701 return MBB;
702 }
703
704 // Least busy dominator seen so far.
705 unsigned Depth = Loop->getLoopDepth();
706 if (Depth < BestDepth) {
707 BestMBB = MBB;
708 BestDepth = Depth;
709 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
710 << MBB->getNumber() << " at depth " << Depth << '\n');
711 }
712
713 // Leave loop by going to the immediate dominator of the loop header.
714 // This is a bigger stride than simply walking up the dominator tree.
715 MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
716
717 // Too far up the dominator tree?
718 if (!IDom || !MDT.dominates(DefDomNode, IDom))
719 return BestMBB;
720
721 MBB = IDom->getBlock();
722 }
723}
724
Wei Mi9a16d652016-04-13 03:08:27 +0000725void SplitEditor::computeRedundantBackCopies(
726 DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) {
727 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
728 LiveInterval *Parent = &Edit->getParent();
729 SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums());
730 SmallPtrSet<VNInfo *, 8> DominatedVNIs;
731
732 // Aggregate VNIs having the same value as ParentVNI.
733 for (VNInfo *VNI : LI->valnos) {
734 if (VNI->isUnused())
735 continue;
736 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
737 EqualVNs[ParentVNI->id].insert(VNI);
738 }
739
740 // For VNI aggregation of each ParentVNI, collect dominated, i.e.,
741 // redundant VNIs to BackCopies.
742 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
743 VNInfo *ParentVNI = Parent->getValNumInfo(i);
744 if (!NotToHoistSet.count(ParentVNI->id))
745 continue;
746 SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin();
747 SmallPtrSetIterator<VNInfo *> It2 = It1;
748 for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) {
749 It2 = It1;
750 for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) {
751 if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2))
752 continue;
753
754 MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def);
755 MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def);
756 if (MBB1 == MBB2) {
757 DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1));
758 } else if (MDT.dominates(MBB1, MBB2)) {
759 DominatedVNIs.insert(*It2);
760 } else if (MDT.dominates(MBB2, MBB1)) {
761 DominatedVNIs.insert(*It1);
762 }
763 }
764 }
765 if (!DominatedVNIs.empty()) {
766 forceRecompute(0, ParentVNI);
767 for (auto VNI : DominatedVNIs) {
768 BackCopies.push_back(VNI);
769 }
770 DominatedVNIs.clear();
771 }
772 }
773}
774
775/// For SM_Size mode, find a common dominator for all the back-copies for
776/// the same ParentVNI and hoist the backcopies to the dominator BB.
777/// For SM_Speed mode, if the common dominator is hot and it is not beneficial
778/// to do the hoisting, simply remove the dominated backcopies for the same
779/// ParentVNI.
780void SplitEditor::hoistCopies() {
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000781 // Get the complement interval, always RegIdx 0.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000782 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000783 LiveInterval *Parent = &Edit->getParent();
784
785 // Track the nearest common dominator for all back-copies for each ParentVNI,
786 // indexed by ParentVNI->id.
787 typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
788 SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
Wei Mi9a16d652016-04-13 03:08:27 +0000789 // The total cost of all the back-copies for each ParentVNI.
790 SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums());
791 // The ParentVNI->id set for which hoisting back-copies are not beneficial
792 // for Speed.
793 DenseSet<unsigned> NotToHoistSet;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000794
795 // Find the nearest common dominator for parent values with multiple
796 // back-copies. If a single back-copy dominates, put it in DomPair.second.
Matthias Braun96761952014-12-10 23:07:54 +0000797 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000798 if (VNI->isUnused())
799 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000800 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
801 assert(ParentVNI && "Parent not live at complement def");
802
803 // Don't hoist remats. The complement is probably going to disappear
804 // completely anyway.
805 if (Edit->didRematerialize(ParentVNI))
806 continue;
807
808 MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
Wei Mi9a16d652016-04-13 03:08:27 +0000809
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000810 DomPair &Dom = NearestDom[ParentVNI->id];
811
812 // Keep directly defined parent values. This is either a PHI or an
813 // instruction in the complement range. All other copies of ParentVNI
814 // should be eliminated.
815 if (VNI->def == ParentVNI->def) {
816 DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
817 Dom = DomPair(ValMBB, VNI->def);
818 continue;
819 }
820 // Skip the singly mapped values. There is nothing to gain from hoisting a
821 // single back-copy.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000822 if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000823 DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
824 continue;
825 }
826
827 if (!Dom.first) {
828 // First time we see ParentVNI. VNI dominates itself.
829 Dom = DomPair(ValMBB, VNI->def);
830 } else if (Dom.first == ValMBB) {
831 // Two defs in the same block. Pick the earlier def.
832 if (!Dom.second.isValid() || VNI->def < Dom.second)
833 Dom.second = VNI->def;
834 } else {
835 // Different basic blocks. Check if one dominates.
836 MachineBasicBlock *Near =
837 MDT.findNearestCommonDominator(Dom.first, ValMBB);
838 if (Near == ValMBB)
839 // Def ValMBB dominates.
840 Dom = DomPair(ValMBB, VNI->def);
841 else if (Near != Dom.first)
842 // None dominate. Hoist to common dominator, need new def.
843 Dom = DomPair(Near, SlotIndex());
Wei Mi9a16d652016-04-13 03:08:27 +0000844 Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000845 }
846
847 DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
848 << " for parent " << ParentVNI->id << '@' << ParentVNI->def
849 << " hoist to BB#" << Dom.first->getNumber() << ' '
850 << Dom.second << '\n');
851 }
852
853 // Insert the hoisted copies.
854 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
855 DomPair &Dom = NearestDom[i];
856 if (!Dom.first || Dom.second.isValid())
857 continue;
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000858 // This value needs a hoisted copy inserted at the end of Dom.first.
859 VNInfo *ParentVNI = Parent->getValNumInfo(i);
860 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
861 // Get a less loopy dominator than Dom.first.
862 Dom.first = findShallowDominator(Dom.first, DefMBB);
Wei Mi9a16d652016-04-13 03:08:27 +0000863 if (SpillMode == SM_Speed &&
864 MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) {
865 NotToHoistSet.insert(ParentVNI->id);
866 continue;
867 }
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000868 SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
869 Dom.second =
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000870 defFromParent(0, ParentVNI, Last, *Dom.first,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000871 SA.getLastSplitPointIter(Dom.first))->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000872 }
873
874 // Remove redundant back-copies that are now known to be dominated by another
875 // def with the same value.
876 SmallVector<VNInfo*, 8> BackCopies;
Matthias Braun96761952014-12-10 23:07:54 +0000877 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000878 if (VNI->isUnused())
879 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000880 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
881 const DomPair &Dom = NearestDom[ParentVNI->id];
Wei Mi9a16d652016-04-13 03:08:27 +0000882 if (!Dom.first || Dom.second == VNI->def ||
883 NotToHoistSet.count(ParentVNI->id))
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000884 continue;
885 BackCopies.push_back(VNI);
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000886 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000887 }
Wei Mi9a16d652016-04-13 03:08:27 +0000888
889 // If it is not beneficial to hoist all the BackCopies, simply remove
890 // redundant BackCopies in speed mode.
891 if (SpillMode == SM_Speed && !NotToHoistSet.empty())
892 computeRedundantBackCopies(NotToHoistSet, BackCopies);
893
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000894 removeBackCopies(BackCopies);
895}
896
897
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000898/// transferValues - Transfer all possible values to the new live ranges.
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000899/// Values that were rematerialized are left alone, they need LRCalc.extend().
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000900bool SplitEditor::transferValues() {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000901 bool Skipped = false;
902 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Matthias Braun96761952014-12-10 23:07:54 +0000903 for (const LiveRange::Segment &S : Edit->getParent()) {
904 DEBUG(dbgs() << " blit " << S << ':');
905 VNInfo *ParentVNI = S.valno;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000906 // RegAssign has holes where RegIdx 0 should be used.
Matthias Braun96761952014-12-10 23:07:54 +0000907 SlotIndex Start = S.start;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000908 AssignI.advanceTo(Start);
909 do {
910 unsigned RegIdx;
Matthias Braun96761952014-12-10 23:07:54 +0000911 SlotIndex End = S.end;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000912 if (!AssignI.valid()) {
913 RegIdx = 0;
914 } else if (AssignI.start() <= Start) {
915 RegIdx = AssignI.value();
916 if (AssignI.stop() < End) {
917 End = AssignI.stop();
918 ++AssignI;
919 }
920 } else {
921 RegIdx = 0;
922 End = std::min(End, AssignI.start());
923 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000924
925 // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000926 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000927 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000928
929 // Check for a simply defined value that can be blitted directly.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000930 ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
931 if (VNInfo *VNI = VFP.getPointer()) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000932 DEBUG(dbgs() << ':' << VNI->id);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000933 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000934 Start = End;
935 continue;
936 }
937
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000938 // Skip values with forced recomputation.
939 if (VFP.getInt()) {
940 DEBUG(dbgs() << "(recalc)");
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000941 Skipped = true;
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000942 Start = End;
943 continue;
944 }
945
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000946 LiveRangeCalc &LRC = getLRCalc(RegIdx);
947
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000948 // This value has multiple defs in RegIdx, but it wasn't rematerialized,
949 // so the live range is accurate. Add live-in blocks in [Start;End) to the
950 // LiveInBlocks.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000951 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000952 SlotIndex BlockStart, BlockEnd;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000953 std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000954
955 // The first block may be live-in, or it may have its own def.
956 if (Start != BlockStart) {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000957 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000958 assert(VNI && "Missing def for complex mapped value");
959 DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
960 // MBB has its own def. Is it also live-out?
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000961 if (BlockEnd <= End)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000962 LRC.setLiveOutValue(&*MBB, VNI);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000963
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000964 // Skip to the next block for live-in.
965 ++MBB;
966 BlockStart = BlockEnd;
967 }
968
969 // Handle the live-in blocks covered by [Start;End).
970 assert(Start <= BlockStart && "Expected live-in block");
971 while (BlockStart < End) {
972 DEBUG(dbgs() << ">BB#" << MBB->getNumber());
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000973 BlockEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000974 if (BlockStart == ParentVNI->def) {
975 // This block has the def of a parent PHI, so it isn't live-in.
976 assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000977 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000978 assert(VNI && "Missing def for complex mapped parent PHI");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000979 if (End >= BlockEnd)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000980 LRC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000981 } else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000982 // This block needs a live-in value. The last block covered may not
983 // be live-out.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000984 if (End < BlockEnd)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000985 LRC.addLiveInBlock(LR, MDT[&*MBB], End);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000986 else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000987 // Live-through, and we don't know the value.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000988 LRC.addLiveInBlock(LR, MDT[&*MBB]);
989 LRC.setLiveOutValue(&*MBB, nullptr);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000990 }
991 }
992 BlockStart = BlockEnd;
993 ++MBB;
994 }
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000995 Start = End;
Matthias Braun96761952014-12-10 23:07:54 +0000996 } while (Start != S.end);
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000997 DEBUG(dbgs() << '\n');
998 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000999
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +00001000 LRCalc[0].calculateValues();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +00001001 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +00001002 LRCalc[1].calculateValues();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001003
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001004 return Skipped;
1005}
1006
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001007void SplitEditor::extendPHIKillRanges() {
Wei Mi9a16d652016-04-13 03:08:27 +00001008 // Extend live ranges to be live-out for successor PHI values.
Matthias Braun96761952014-12-10 23:07:54 +00001009 for (const VNInfo *PHIVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001010 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
1011 continue;
1012 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
Matthias Braun2d5c32b2013-10-10 21:28:57 +00001013 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Wei Mi9a16d652016-04-13 03:08:27 +00001014
1015 // Check whether PHI is dead.
1016 const LiveRange::Segment *Segment = LR.getSegmentContaining(PHIVNI->def);
1017 assert(Segment != nullptr && "Missing segment for VNI");
1018 if (Segment->end == PHIVNI->def.getDeadSlot()) {
1019 // This is a dead PHI. Remove it.
1020 LR.removeSegment(*Segment, true);
1021 continue;
1022 }
1023
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001024 LiveRangeCalc &LRC = getLRCalc(RegIdx);
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001025 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
1026 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
1027 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001028 SlotIndex End = LIS.getMBBEndIdx(*PI);
1029 SlotIndex LastUse = End.getPrevSlot();
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001030 // The predecessor may not have a live-out value. That is OK, like an
1031 // undef PHI operand.
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001032 if (Edit->getParent().liveAt(LastUse)) {
1033 assert(RegAssign.lookup(LastUse) == RegIdx &&
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001034 "Different register assignment in phi predecessor");
Matthias Braun2d5c32b2013-10-10 21:28:57 +00001035 LRC.extend(LR, End);
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001036 }
1037 }
1038 }
1039}
1040
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001041/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001042void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001043 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +00001044 RE = MRI.reg_end(); RI != RE;) {
Owen Anderson16c6bf42014-03-13 23:12:04 +00001045 MachineOperand &MO = *RI;
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001046 MachineInstr *MI = MO.getParent();
1047 ++RI;
Eric Christopherede62672011-02-03 06:18:29 +00001048 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001049 if (MI->isDebugValue()) {
1050 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001051 MO.setReg(0);
1052 continue;
1053 }
Jakob Stoklund Olesenf6e03942011-02-09 21:52:09 +00001054
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +00001055 // <undef> operands don't really read the register, so it doesn't matter
1056 // which register we choose. When the use operand is tied to a def, we must
1057 // use the same register as the def, so just do that always.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001058 SlotIndex Idx = LIS.getInstructionIndex(*MI);
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +00001059 if (MO.isDef() || MO.isUndef())
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +00001060 Idx = Idx.getRegSlot(MO.isEarlyClobber());
Eric Christopherede62672011-02-03 06:18:29 +00001061
1062 // Rewrite to the mapped register at Idx.
1063 unsigned RegIdx = RegAssign.lookup(Idx);
Mark Laceyf9ea8852013-08-14 23:50:04 +00001064 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001065 MO.setReg(LI->reg);
Eric Christopherede62672011-02-03 06:18:29 +00001066 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
1067 << Idx << ':' << RegIdx << '\t' << *MI);
1068
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001069 // Extend liveness to Idx if the instruction reads reg.
Jakob Stoklund Olesen73a9eb92011-07-24 20:33:23 +00001070 if (!ExtendRanges || MO.isUndef())
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001071 continue;
1072
1073 // Skip instructions that don't read Reg.
1074 if (MO.isDef()) {
1075 if (!MO.getSubReg() && !MO.isEarlyClobber())
1076 continue;
1077 // We may wan't to extend a live range for a partial redef, or for a use
1078 // tied to an early clobber.
1079 Idx = Idx.getPrevSlot();
1080 if (!Edit->getParent().liveAt(Idx))
1081 continue;
1082 } else
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +00001083 Idx = Idx.getRegSlot(true);
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001084
Matthias Braun2d5c32b2013-10-10 21:28:57 +00001085 getLRCalc(RegIdx).extend(*LI, Idx.getNextSlot());
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001086 }
1087}
1088
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001089void SplitEditor::deleteRematVictims() {
1090 SmallVector<MachineInstr*, 8> Dead;
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001091 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
Mark Laceyf9ea8852013-08-14 23:50:04 +00001092 LiveInterval *LI = &LIS.getInterval(*I);
Matthias Braun96761952014-12-10 23:07:54 +00001093 for (const LiveRange::Segment &S : LI->segments) {
Jakob Stoklund Olesend8f24052011-11-13 22:42:13 +00001094 // Dead defs end at the dead slot.
Matthias Braun96761952014-12-10 23:07:54 +00001095 if (S.end != S.valno->def.getDeadSlot())
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001096 continue;
Wei Mi9a16d652016-04-13 03:08:27 +00001097 if (S.valno->isPHIDef())
1098 continue;
Matthias Braun96761952014-12-10 23:07:54 +00001099 MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001100 assert(MI && "Missing instruction for dead def");
1101 MI->addRegisterDead(LI->reg, &TRI);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001102
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001103 if (!MI->allDefsAreDead())
1104 continue;
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001105
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001106 DEBUG(dbgs() << "All defs dead: " << *MI);
1107 Dead.push_back(MI);
1108 }
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001109 }
1110
1111 if (Dead.empty())
1112 return;
1113
Pete Cooper2bde2f42012-04-02 22:22:53 +00001114 Edit->eliminateDeadDefs(Dead);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001115}
1116
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001117void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001118 ++NumFinished;
Eric Christopher21933532011-02-03 05:40:54 +00001119
Eric Christopherede62672011-02-03 06:18:29 +00001120 // At this point, the live intervals in Edit contain VNInfos corresponding to
1121 // the inserted copies.
1122
1123 // Add the original defs from the parent interval.
Matthias Braun96761952014-12-10 23:07:54 +00001124 for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen3295a992011-02-04 00:59:23 +00001125 if (ParentVNI->isUnused())
1126 continue;
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +00001127 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Jakob Stoklund Olesen97e14e02012-07-27 21:11:14 +00001128 defValue(RegIdx, ParentVNI, ParentVNI->def);
Jakob Stoklund Olesen32210de2011-03-15 21:13:22 +00001129
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001130 // Force rematted values to be recomputed everywhere.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001131 // The new live ranges may be truncated.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001132 if (Edit->didRematerialize(ParentVNI))
1133 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001134 forceRecompute(i, ParentVNI);
Eric Christopherede62672011-02-03 06:18:29 +00001135 }
1136
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001137 // Hoist back-copies to the complement interval when in spill mode.
1138 switch (SpillMode) {
1139 case SM_Partition:
1140 // Leave all back-copies as is.
1141 break;
1142 case SM_Size:
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001143 case SM_Speed:
Wei Mi9a16d652016-04-13 03:08:27 +00001144 // hoistCopies will behave differently between size and speed.
1145 hoistCopies();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001146 }
1147
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001148 // Transfer the simply mapped values, check if any are skipped.
1149 bool Skipped = transferValues();
Wei Mi9a16d652016-04-13 03:08:27 +00001150
1151 // Rewrite virtual registers, possibly extending ranges.
1152 rewriteAssigned(Skipped);
1153
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001154 if (Skipped)
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001155 extendPHIKillRanges();
1156 else
1157 ++NumSimple;
Eric Christopherede62672011-02-03 06:18:29 +00001158
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001159 // Delete defs that were rematted everywhere.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001160 if (Skipped)
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001161 deleteRematVictims();
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001162
Jakob Stoklund Olesen0f1677e2010-10-07 23:34:34 +00001163 // Get rid of unused values and set phi-kill flags.
Mark Laceyf9ea8852013-08-14 23:50:04 +00001164 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) {
1165 LiveInterval &LI = LIS.getInterval(*I);
1166 LI.RenumberValues();
1167 }
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001168
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001169 // Provide a reverse mapping from original indices to Edit ranges.
1170 if (LRMap) {
1171 LRMap->clear();
1172 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1173 LRMap->push_back(i);
1174 }
1175
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001176 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +00001177 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001178 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001179 // Don't use iterators, they are invalidated by create() below.
Matthias Braund3dd1352015-09-22 03:44:41 +00001180 unsigned VReg = Edit->get(i);
1181 LiveInterval &LI = LIS.getInterval(VReg);
1182 SmallVector<LiveInterval*, 8> SplitLIs;
1183 LIS.splitSeparateComponents(LI, SplitLIs);
1184 unsigned Original = VRM.getOriginal(VReg);
1185 for (LiveInterval *SplitLI : SplitLIs)
1186 VRM.setIsSplitFromReg(SplitLI->reg, Original);
1187
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001188 // The new intervals all map back to i.
1189 if (LRMap)
1190 LRMap->resize(Edit->size(), i);
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001191 }
1192
Jakob Stoklund Olesen284c2db2010-08-10 17:07:22 +00001193 // Calculate spill weight and allocation hints for new intervals.
Benjamin Kramere2a1d892013-06-17 19:00:36 +00001194 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001195
1196 assert(!LRMap || LRMap->size() == Edit->size());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +00001197}
1198
1199
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +00001200//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +00001201// Single Block Splitting
1202//===----------------------------------------------------------------------===//
1203
Jakob Stoklund Olesen8627ea92011-08-05 22:20:45 +00001204bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
1205 bool SingleInstrs) const {
1206 // Always split for multiple instructions.
1207 if (!BI.isOneInstr())
1208 return true;
1209 // Don't split for single instructions unless explicitly requested.
1210 if (!SingleInstrs)
1211 return false;
1212 // Splitting a live-through range always makes progress.
1213 if (BI.LiveIn && BI.LiveOut)
1214 return true;
1215 // No point in isolating a copy. It has no register class constraints.
1216 if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
1217 return false;
1218 // Finally, don't isolate an end point that was created by earlier splits.
1219 return isOriginalEndpoint(BI.FirstInstr);
1220}
1221
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001222void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1223 openIntv();
1224 SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001225 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001226 LastSplitPoint));
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001227 if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
1228 useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001229 } else {
1230 // The last use is after the last valid split point.
1231 SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1232 useIntv(SegStart, SegStop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001233 overlapIntv(SegStop, BI.LastInstr);
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001234 }
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001235}
1236
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001237
1238//===----------------------------------------------------------------------===//
1239// Global Live Range Splitting Support
1240//===----------------------------------------------------------------------===//
1241
1242// These methods support a method of global live range splitting that uses a
1243// global algorithm to decide intervals for CFG edges. They will insert split
1244// points and color intervals in basic blocks while avoiding interference.
1245//
1246// Note that splitSingleBlock is also useful for blocks where both CFG edges
1247// are on the stack.
1248
1249void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1250 unsigned IntvIn, SlotIndex LeaveBefore,
1251 unsigned IntvOut, SlotIndex EnterAfter){
1252 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001253 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001254
1255 DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
1256 << ") intf " << LeaveBefore << '-' << EnterAfter
1257 << ", live-through " << IntvIn << " -> " << IntvOut);
1258
1259 assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1260
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001261 assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
1262 assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
1263 assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
1264
1265 MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
1266
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001267 if (!IntvOut) {
1268 DEBUG(dbgs() << ", spill on entry.\n");
1269 //
1270 // <<<<<<<<< Possible LeaveBefore interference.
1271 // |-----------| Live through.
1272 // -____________ Spill on entry.
1273 //
1274 selectIntv(IntvIn);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001275 SlotIndex Idx = leaveIntvAtTop(*MBB);
1276 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1277 (void)Idx;
1278 return;
1279 }
1280
1281 if (!IntvIn) {
1282 DEBUG(dbgs() << ", reload on exit.\n");
1283 //
1284 // >>>>>>> Possible EnterAfter interference.
1285 // |-----------| Live through.
1286 // ___________-- Reload on exit.
1287 //
1288 selectIntv(IntvOut);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001289 SlotIndex Idx = enterIntvAtEnd(*MBB);
1290 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1291 (void)Idx;
1292 return;
1293 }
1294
1295 if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1296 DEBUG(dbgs() << ", straight through.\n");
1297 //
1298 // |-----------| Live through.
1299 // ------------- Straight through, same intv, no interference.
1300 //
1301 selectIntv(IntvOut);
1302 useIntv(Start, Stop);
1303 return;
1304 }
1305
1306 // We cannot legally insert splits after LSP.
1307 SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001308 assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001309
1310 if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1311 LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1312 DEBUG(dbgs() << ", switch avoiding interference.\n");
1313 //
1314 // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
1315 // |-----------| Live through.
1316 // ------======= Switch intervals between interference.
1317 //
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001318 selectIntv(IntvOut);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001319 SlotIndex Idx;
1320 if (LeaveBefore && LeaveBefore < LSP) {
1321 Idx = enterIntvBefore(LeaveBefore);
1322 useIntv(Idx, Stop);
1323 } else {
1324 Idx = enterIntvAtEnd(*MBB);
1325 }
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001326 selectIntv(IntvIn);
1327 useIntv(Start, Idx);
1328 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1329 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1330 return;
1331 }
1332
1333 DEBUG(dbgs() << ", create local intv for interference.\n");
1334 //
1335 // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
1336 // |-----------| Live through.
1337 // ==---------== Switch intervals before/after interference.
1338 //
1339 assert(LeaveBefore <= EnterAfter && "Missed case");
1340
1341 selectIntv(IntvOut);
1342 SlotIndex Idx = enterIntvAfter(EnterAfter);
1343 useIntv(Idx, Stop);
1344 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1345
1346 selectIntv(IntvIn);
1347 Idx = leaveIntvBefore(LeaveBefore);
1348 useIntv(Start, Idx);
1349 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1350}
1351
1352
1353void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1354 unsigned IntvIn, SlotIndex LeaveBefore) {
1355 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001356 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001357
1358 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001359 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001360 << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
1361 << (BI.LiveOut ? ", stack-out" : ", killed in block"));
1362
1363 assert(IntvIn && "Must have register in");
1364 assert(BI.LiveIn && "Must be live-in");
1365 assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1366
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001367 if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001368 DEBUG(dbgs() << " before interference.\n");
1369 //
1370 // <<< Interference after kill.
1371 // |---o---x | Killed in block.
1372 // ========= Use IntvIn everywhere.
1373 //
1374 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001375 useIntv(Start, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001376 return;
1377 }
1378
1379 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1380
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001381 if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001382 //
1383 // <<< Possible interference after last use.
1384 // |---o---o---| Live-out on stack.
1385 // =========____ Leave IntvIn after last use.
1386 //
1387 // < Interference after last use.
1388 // |---o---o--o| Live-out on stack, late last use.
1389 // ============ Copy to stack after LSP, overlap IntvIn.
1390 // \_____ Stack interval is live-out.
1391 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001392 if (BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001393 DEBUG(dbgs() << ", spill after last use before interference.\n");
1394 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001395 SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001396 useIntv(Start, Idx);
1397 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1398 } else {
1399 DEBUG(dbgs() << ", spill before last split point.\n");
1400 selectIntv(IntvIn);
Jakob Stoklund Olesen37e3a132011-07-16 00:13:30 +00001401 SlotIndex Idx = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001402 overlapIntv(Idx, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001403 useIntv(Start, Idx);
1404 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1405 }
1406 return;
1407 }
1408
1409 // The interference is overlapping somewhere we wanted to use IntvIn. That
1410 // means we need to create a local interval that can be allocated a
1411 // different register.
1412 unsigned LocalIntv = openIntv();
Matt Beaumont-Gay26909d82011-07-16 04:18:47 +00001413 (void)LocalIntv;
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001414 DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
1415
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001416 if (!BI.LiveOut || BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001417 //
1418 // <<<<<<< Interference overlapping uses.
1419 // |---o---o---| Live-out on stack.
1420 // =====----____ Leave IntvIn before interference, then spill.
1421 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001422 SlotIndex To = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001423 SlotIndex From = enterIntvBefore(LeaveBefore);
1424 useIntv(From, To);
1425 selectIntv(IntvIn);
1426 useIntv(Start, From);
1427 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1428 return;
1429 }
1430
1431 // <<<<<<< Interference overlapping uses.
1432 // |---o---o--o| Live-out on stack, late last use.
1433 // =====------- Copy to stack before LSP, overlap LocalIntv.
1434 // \_____ Stack interval is live-out.
1435 //
1436 SlotIndex To = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001437 overlapIntv(To, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001438 SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1439 useIntv(From, To);
1440 selectIntv(IntvIn);
1441 useIntv(Start, From);
1442 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1443}
1444
1445void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1446 unsigned IntvOut, SlotIndex EnterAfter) {
1447 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001448 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001449
1450 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001451 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001452 << ", reg-out " << IntvOut << ", enter after " << EnterAfter
1453 << (BI.LiveIn ? ", stack-in" : ", defined in block"));
1454
1455 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1456
1457 assert(IntvOut && "Must have register out");
1458 assert(BI.LiveOut && "Must be live-out");
1459 assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1460
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001461 if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001462 DEBUG(dbgs() << " after interference.\n");
1463 //
1464 // >>>> Interference before def.
1465 // | o---o---| Defined in block.
1466 // ========= Use IntvOut everywhere.
1467 //
1468 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001469 useIntv(BI.FirstInstr, Stop);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001470 return;
1471 }
1472
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001473 if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001474 DEBUG(dbgs() << ", reload after interference.\n");
1475 //
1476 // >>>> Interference before def.
1477 // |---o---o---| Live-through, stack-in.
1478 // ____========= Enter IntvOut before first use.
1479 //
1480 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001481 SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001482 useIntv(Idx, Stop);
1483 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1484 return;
1485 }
1486
1487 // The interference is overlapping somewhere we wanted to use IntvOut. That
1488 // means we need to create a local interval that can be allocated a
1489 // different register.
1490 DEBUG(dbgs() << ", interference overlaps uses.\n");
1491 //
1492 // >>>>>>> Interference overlapping uses.
1493 // |---o---o---| Live-through, stack-in.
1494 // ____---====== Create local interval for interference range.
1495 //
1496 selectIntv(IntvOut);
1497 SlotIndex Idx = enterIntvAfter(EnterAfter);
1498 useIntv(Idx, Stop);
1499 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1500
1501 openIntv();
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001502 SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001503 useIntv(From, Idx);
1504}