blob: 6899924035502a5ad42cabd492b433626db7a816 [file] [log] [blame]
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +00001//===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the SplitAnalysis class as well as mutator functions for
11// live range splitting.
12//
13//===----------------------------------------------------------------------===//
14
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000015#include "SplitKit.h"
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +000016#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000017#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper3ca96f92012-04-02 22:44:18 +000018#include "llvm/CodeGen/LiveRangeEdit.h"
Wei Mi9a16d652016-04-13 03:08:27 +000019#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Jakob Stoklund Olesene172a8b2010-10-28 20:34:50 +000020#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +000022#include "llvm/CodeGen/MachineLoopInfo.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000024#include "llvm/CodeGen/VirtRegMap.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000025#include "llvm/Support/Debug.h"
26#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesened4075c2010-07-20 21:46:58 +000027#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000029
30using namespace llvm;
31
Chandler Carruth1b9dde02014-04-22 02:02:50 +000032#define DEBUG_TYPE "regalloc"
33
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +000034STATISTIC(NumFinished, "Number of splits finished");
35STATISTIC(NumSimple, "Number of splits that were simple");
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +000036STATISTIC(NumCopies, "Number of copies inserted for splitting");
37STATISTIC(NumRemats, "Number of rematerialized defs for splitting");
Jakob Stoklund Olesen50215af2011-05-10 17:37:41 +000038STATISTIC(NumRepairs, "Number of invalid live ranges repaired");
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +000039
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000040//===----------------------------------------------------------------------===//
Wei Mi35ee9332016-05-11 22:28:29 +000041// Last Insert Point Analysis
42//===----------------------------------------------------------------------===//
43
44InsertPointAnalysis::InsertPointAnalysis(const LiveIntervals &lis,
45 unsigned BBNum)
Wei Mif3c8f532016-05-23 19:39:19 +000046 : LIS(lis), LastInsertPoint(BBNum) {}
Wei Mi35ee9332016-05-11 22:28:29 +000047
48SlotIndex
Wei Mif3c8f532016-05-23 19:39:19 +000049InsertPointAnalysis::computeLastInsertPoint(const LiveInterval &CurLI,
50 const MachineBasicBlock &MBB) {
Wei Mi35ee9332016-05-11 22:28:29 +000051 unsigned Num = MBB.getNumber();
52 std::pair<SlotIndex, SlotIndex> &LIP = LastInsertPoint[Num];
53 SlotIndex MBBEnd = LIS.getMBBEndIdx(&MBB);
54
55 SmallVector<const MachineBasicBlock *, 1> EHPadSucessors;
56 for (const MachineBasicBlock *SMBB : MBB.successors())
57 if (SMBB->isEHPad())
58 EHPadSucessors.push_back(SMBB);
59
60 // Compute insert points on the first call. The pair is independent of the
61 // current live interval.
62 if (!LIP.first.isValid()) {
63 MachineBasicBlock::const_iterator FirstTerm = MBB.getFirstTerminator();
64 if (FirstTerm == MBB.end())
65 LIP.first = MBBEnd;
66 else
67 LIP.first = LIS.getInstructionIndex(*FirstTerm);
68
69 // If there is a landing pad successor, also find the call instruction.
70 if (EHPadSucessors.empty())
71 return LIP.first;
72 // There may not be a call instruction (?) in which case we ignore LPad.
73 LIP.second = LIP.first;
74 for (MachineBasicBlock::const_iterator I = MBB.end(), E = MBB.begin();
75 I != E;) {
76 --I;
77 if (I->isCall()) {
78 LIP.second = LIS.getInstructionIndex(*I);
79 break;
80 }
81 }
82 }
83
84 // If CurLI is live into a landing pad successor, move the last insert point
85 // back to the call that may throw.
86 if (!LIP.second)
87 return LIP.first;
88
Wei Mi35ee9332016-05-11 22:28:29 +000089 if (none_of(EHPadSucessors, [&](const MachineBasicBlock *EHPad) {
Wei Mif3c8f532016-05-23 19:39:19 +000090 return LIS.isLiveInToMBB(CurLI, EHPad);
Wei Mi35ee9332016-05-11 22:28:29 +000091 }))
92 return LIP.first;
93
94 // Find the value leaving MBB.
Wei Mif3c8f532016-05-23 19:39:19 +000095 const VNInfo *VNI = CurLI.getVNInfoBefore(MBBEnd);
Wei Mi35ee9332016-05-11 22:28:29 +000096 if (!VNI)
97 return LIP.first;
98
99 // If the value leaving MBB was defined after the call in MBB, it can't
100 // really be live-in to the landing pad. This can happen if the landing pad
101 // has a PHI, and this register is undef on the exceptional edge.
102 // <rdar://problem/10664933>
103 if (!SlotIndex::isEarlierInstr(VNI->def, LIP.second) && VNI->def < MBBEnd)
104 return LIP.first;
105
106 // Value is properly live-in to the landing pad.
107 // Only allow inserts before the call.
108 return LIP.second;
109}
110
111MachineBasicBlock::iterator
Wei Mif3c8f532016-05-23 19:39:19 +0000112InsertPointAnalysis::getLastInsertPointIter(const LiveInterval &CurLI,
113 MachineBasicBlock &MBB) {
114 SlotIndex LIP = getLastInsertPoint(CurLI, MBB);
Wei Mi35ee9332016-05-11 22:28:29 +0000115 if (LIP == LIS.getMBBEndIdx(&MBB))
116 return MBB.end();
117 return LIS.getInstructionFromIndex(LIP);
118}
119
120//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000121// Split Analysis
122//===----------------------------------------------------------------------===//
123
Eric Christopherd9134482014-08-04 21:25:23 +0000124SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
Jakob Stoklund Olesen0fef9dd2010-07-20 23:50:15 +0000125 const MachineLoopInfo &mli)
Eric Christopherd9134482014-08-04 21:25:23 +0000126 : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli),
Eric Christopherfc6de422014-08-05 02:39:49 +0000127 TII(*MF.getSubtarget().getInstrInfo()), CurLI(nullptr),
Wei Mi35ee9332016-05-11 22:28:29 +0000128 IPA(lis, MF.getNumBlockIDs()) {}
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000129
130void SplitAnalysis::clear() {
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000131 UseSlots.clear();
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000132 UseBlocks.clear();
133 ThroughBlocks.clear();
Craig Topperc0196b12014-04-14 00:51:57 +0000134 CurLI = nullptr;
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000135 DidRepairRange = false;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000136}
137
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000138/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000139void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000140 assert(UseSlots.empty() && "Call clear first");
141
142 // First get all the defs from the interval values. This provides the correct
143 // slots for early clobbers.
Matthias Braun96761952014-12-10 23:07:54 +0000144 for (const VNInfo *VNI : CurLI->valnos)
145 if (!VNI->isPHIDef() && !VNI->isUnused())
146 UseSlots.push_back(VNI->def);
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000147
148 // Get use slots form the use-def chain.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000149 const MachineRegisterInfo &MRI = MF.getRegInfo();
Owen Andersonb36376e2014-03-17 19:36:09 +0000150 for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg))
151 if (!MO.isUndef())
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000152 UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot());
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000153
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000154 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000155
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000156 // Remove duplicates, keeping the smaller slot for each instruction.
157 // That is what we want for early clobbers.
158 UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
159 SlotIndex::isSameInstr),
160 UseSlots.end());
161
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000162 // Compute per-live block info.
163 if (!calcLiveBlockInfo()) {
164 // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
Rafael Espindola676c4052011-06-26 22:34:10 +0000165 // I am looking at you, RegisterCoalescer!
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000166 DidRepairRange = true;
Jakob Stoklund Olesen50215af2011-05-10 17:37:41 +0000167 ++NumRepairs;
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000168 DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
169 const_cast<LiveIntervals&>(LIS)
170 .shrinkToUses(const_cast<LiveInterval*>(CurLI));
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000171 UseBlocks.clear();
172 ThroughBlocks.clear();
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000173 bool fixed = calcLiveBlockInfo();
174 (void)fixed;
175 assert(fixed && "Couldn't fix broken live interval");
176 }
177
Jakob Stoklund Olesenbd6b86e2011-03-27 22:49:23 +0000178 DEBUG(dbgs() << "Analyze counted "
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000179 << UseSlots.size() << " instrs in "
180 << UseBlocks.size() << " blocks, through "
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000181 << NumThroughBlocks << " blocks.\n");
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000182}
183
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000184/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
185/// where CurLI is live.
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000186bool SplitAnalysis::calcLiveBlockInfo() {
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000187 ThroughBlocks.resize(MF.getNumBlockIDs());
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000188 NumThroughBlocks = NumGapBlocks = 0;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000189 if (CurLI->empty())
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000190 return true;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000191
192 LiveInterval::const_iterator LVI = CurLI->begin();
193 LiveInterval::const_iterator LVE = CurLI->end();
194
195 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
196 UseI = UseSlots.begin();
197 UseE = UseSlots.end();
198
199 // Loop over basic blocks where CurLI is live.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000200 MachineFunction::iterator MFI =
201 LIS.getMBBFromIndex(LVI->start)->getIterator();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000202 for (;;) {
203 BlockInfo BI;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000204 BI.MBB = &*MFI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000205 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000206 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000207
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000208 // If the block contains no uses, the range must be live through. At one
Rafael Espindola676c4052011-06-26 22:34:10 +0000209 // point, RegisterCoalescer could create dangling ranges that ended
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000210 // mid-block.
211 if (UseI == UseE || *UseI >= Stop) {
212 ++NumThroughBlocks;
213 ThroughBlocks.set(BI.MBB->getNumber());
214 // The range shouldn't end mid-block if there are no uses. This shouldn't
215 // happen.
216 if (LVI->end < Stop)
217 return false;
218 } else {
219 // This block has uses. Find the first and last uses in the block.
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000220 BI.FirstInstr = *UseI;
221 assert(BI.FirstInstr >= Start);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000222 do ++UseI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000223 while (UseI != UseE && *UseI < Stop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000224 BI.LastInstr = UseI[-1];
225 assert(BI.LastInstr < Stop);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000226
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000227 // LVI is the first live segment overlapping MBB.
228 BI.LiveIn = LVI->start <= Start;
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000229
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000230 // When not live in, the first use should be a def.
231 if (!BI.LiveIn) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000232 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000233 assert(LVI->start == BI.FirstInstr && "First instr should be a def");
234 BI.FirstDef = BI.FirstInstr;
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000235 }
236
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000237 // Look for gaps in the live range.
238 BI.LiveOut = true;
239 while (LVI->end < Stop) {
240 SlotIndex LastStop = LVI->end;
241 if (++LVI == LVE || LVI->start >= Stop) {
242 BI.LiveOut = false;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000243 BI.LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000244 break;
245 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000246
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000247 if (LastStop < LVI->start) {
248 // There is a gap in the live range. Create duplicate entries for the
249 // live-in snippet and the live-out snippet.
250 ++NumGapBlocks;
251
252 // Push the Live-in part.
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000253 BI.LiveOut = false;
254 UseBlocks.push_back(BI);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000255 UseBlocks.back().LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000256
257 // Set up BI for the live-out part.
258 BI.LiveIn = false;
259 BI.LiveOut = true;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000260 BI.FirstInstr = BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000261 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000262
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000263 // A Segment that starts in the middle of the block must be a def.
264 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000265 if (!BI.FirstDef)
266 BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000267 }
268
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000269 UseBlocks.push_back(BI);
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000270
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000271 // LVI is now at LVE or LVI->end >= Stop.
272 if (LVI == LVE)
273 break;
274 }
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000275
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000276 // Live segment ends exactly at Stop. Move to the next segment.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000277 if (LVI->end == Stop && ++LVI == LVE)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000278 break;
279
280 // Pick the next basic block.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000281 if (LVI->start < Stop)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000282 ++MFI;
283 else
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000284 MFI = LIS.getMBBFromIndex(LVI->start)->getIterator();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000285 }
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000286
287 assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000288 return true;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000289}
290
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000291unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
292 if (cli->empty())
293 return 0;
294 LiveInterval *li = const_cast<LiveInterval*>(cli);
295 LiveInterval::iterator LVI = li->begin();
296 LiveInterval::iterator LVE = li->end();
297 unsigned Count = 0;
298
299 // Loop over basic blocks where li is live.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000300 MachineFunction::const_iterator MFI =
301 LIS.getMBBFromIndex(LVI->start)->getIterator();
302 SlotIndex Stop = LIS.getMBBEndIdx(&*MFI);
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000303 for (;;) {
304 ++Count;
305 LVI = li->advanceTo(LVI, Stop);
306 if (LVI == LVE)
307 return Count;
308 do {
309 ++MFI;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000310 Stop = LIS.getMBBEndIdx(&*MFI);
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000311 } while (Stop <= LVI->start);
312 }
313}
314
Jakob Stoklund Olesen60a26a62011-02-21 23:09:46 +0000315bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
316 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
317 const LiveInterval &Orig = LIS.getInterval(OrigReg);
318 assert(!Orig.empty() && "Splitting empty interval?");
319 LiveInterval::const_iterator I = Orig.find(Idx);
320
321 // Range containing Idx should begin at Idx.
322 if (I != Orig.end() && I->start <= Idx)
323 return I->start == Idx;
324
325 // Range does not contain Idx, previous must end at Idx.
326 return I != Orig.begin() && (--I)->end == Idx;
327}
328
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000329void SplitAnalysis::analyze(const LiveInterval *li) {
330 clear();
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000331 CurLI = li;
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000332 analyzeUses();
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000333}
334
Jakob Stoklund Olesen28e769c2010-12-15 17:49:52 +0000335
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000336//===----------------------------------------------------------------------===//
337// Split Editor
338//===----------------------------------------------------------------------===//
339
340/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Wei Mic0223702016-07-08 21:08:09 +0000341SplitEditor::SplitEditor(SplitAnalysis &sa, AliasAnalysis &aa,
342 LiveIntervals &lis, VirtRegMap &vrm,
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000343 MachineDominatorTree &mdt,
344 MachineBlockFrequencyInfo &mbfi)
Wei Mic0223702016-07-08 21:08:09 +0000345 : SA(sa), AA(aa), LIS(lis), VRM(vrm),
346 MRI(vrm.getMachineFunction().getRegInfo()), MDT(mdt),
347 TII(*vrm.getMachineFunction().getSubtarget().getInstrInfo()),
Eric Christopher60621802014-10-14 07:22:00 +0000348 TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()),
Eric Christopherd9134482014-08-04 21:25:23 +0000349 MBFI(mbfi), Edit(nullptr), OpenIdx(0), SpillMode(SM_Partition),
350 RegAssign(Allocator) {}
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000351
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000352void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
353 Edit = &LRE;
354 SpillMode = SM;
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000355 OpenIdx = 0;
356 RegAssign.clear();
357 Values.clear();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000358
359 // Reset the LiveRangeCalc instances needed for this spill mode.
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000360 LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
361 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000362 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000363 LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
364 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000365
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000366 // We don't need an AliasAnalysis since we will only be performing
367 // cheap-as-a-copy remats anyway.
Craig Topperc0196b12014-04-14 00:51:57 +0000368 Edit->anyRematerializable(nullptr);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000369}
370
Manman Ren19f49ac2012-09-11 22:23:19 +0000371#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000372LLVM_DUMP_METHOD void SplitEditor::dump() const {
Eric Christopherede62672011-02-03 06:18:29 +0000373 if (RegAssign.empty()) {
374 dbgs() << " empty\n";
375 return;
376 }
377
378 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
379 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
380 dbgs() << '\n';
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000381}
Manman Ren742534c2012-09-06 19:06:06 +0000382#endif
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000383
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000384LiveInterval::SubRange &SplitEditor::getSubRangeForMask(LaneBitmask LM,
385 LiveInterval &LI) {
386 for (LiveInterval::SubRange &S : LI.subranges())
387 if (S.LaneMask == LM)
388 return S;
389 llvm_unreachable("SubRange for this mask not found");
George Burgess IVb42e0e72016-08-25 02:15:54 +0000390}
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000391
392void SplitEditor::addDeadDef(LiveInterval &LI, VNInfo *VNI, bool Original) {
393 if (!LI.hasSubRanges()) {
394 LI.createDeadDef(VNI);
395 return;
396 }
397
398 SlotIndex Def = VNI->def;
399 if (Original) {
400 // If we are transferring a def from the original interval, make sure
401 // to only update the subranges for which the original subranges had
402 // a def at this location.
403 for (LiveInterval::SubRange &S : LI.subranges()) {
404 auto &PS = getSubRangeForMask(S.LaneMask, Edit->getParent());
405 VNInfo *PV = PS.getVNInfoAt(Def);
406 if (PV != nullptr && PV->def == Def)
407 S.createDeadDef(Def, LIS.getVNInfoAllocator());
408 }
409 } else {
410 // This is a new def: either from rematerialization, or from an inserted
411 // copy. Since rematerialization can regenerate a definition of a sub-
412 // register, we need to check which subranges need to be updated.
413 const MachineInstr *DefMI = LIS.getInstructionFromIndex(Def);
414 assert(DefMI != nullptr);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000415 LaneBitmask LM;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000416 for (const MachineOperand &DefOp : DefMI->defs()) {
417 unsigned R = DefOp.getReg();
418 if (R != LI.reg)
419 continue;
420 if (unsigned SR = DefOp.getSubReg())
421 LM |= TRI.getSubRegIndexLaneMask(SR);
422 else {
423 LM = MRI.getMaxLaneMaskForVReg(R);
424 break;
425 }
426 }
427 for (LiveInterval::SubRange &S : LI.subranges())
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000428 if ((S.LaneMask & LM).any())
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000429 S.createDeadDef(Def, LIS.getVNInfoAllocator());
430 }
431}
432
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000433VNInfo *SplitEditor::defValue(unsigned RegIdx,
434 const VNInfo *ParentVNI,
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000435 SlotIndex Idx,
436 bool Original) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000437 assert(ParentVNI && "Mapping NULL value");
438 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000439 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
Mark Laceyf9ea8852013-08-14 23:50:04 +0000440 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000441
442 // Create a new value.
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000443 VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000444
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000445 bool Force = LI->hasSubRanges();
446 ValueForcePair FP(Force ? nullptr : VNI, Force);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000447 // Use insert for lookup, so we can add missing values with a second lookup.
448 std::pair<ValueMap::iterator, bool> InsP =
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000449 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), FP));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000450
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000451 // This was the first time (RegIdx, ParentVNI) was mapped, and it is not
452 // forced. Keep it as a simple def without any liveness.
453 if (!Force && InsP.second)
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000454 return VNI;
455
456 // If the previous value was a simple mapping, add liveness for it now.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000457 if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000458 addDeadDef(*LI, OldVNI, Original);
459
460 // No longer a simple mapping. Switch to a complex mapping. If the
461 // interval has subranges, make it a forced mapping.
462 InsP.first->second = ValueForcePair(nullptr, Force);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000463 }
464
465 // This is a complex mapping, add liveness for VNI
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000466 addDeadDef(*LI, VNI, Original);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000467 return VNI;
468}
469
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000470void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000471 assert(ParentVNI && "Mapping NULL value");
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000472 ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
473 VNInfo *VNI = VFP.getPointer();
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000474
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000475 // ParentVNI was either unmapped or already complex mapped. Either way, just
476 // set the force bit.
477 if (!VNI) {
478 VFP.setInt(true);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000479 return;
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000480 }
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000481
482 // This was previously a single mapping. Make sure the old def is represented
483 // by a trivial live range.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000484 addDeadDef(LIS.getInterval(Edit->get(RegIdx)), VNI, false);
485
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000486 // Mark as complex mapped, forced.
Craig Topperc0196b12014-04-14 00:51:57 +0000487 VFP = ValueForcePair(nullptr, true);
Jakob Stoklund Olesen4484f992011-09-13 18:05:29 +0000488}
489
Eric Christopherede62672011-02-03 06:18:29 +0000490VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000491 VNInfo *ParentVNI,
492 SlotIndex UseIdx,
493 MachineBasicBlock &MBB,
494 MachineBasicBlock::iterator I) {
Craig Topperc0196b12014-04-14 00:51:57 +0000495 MachineInstr *CopyMI = nullptr;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000496 SlotIndex Def;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000497 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000498
Jakob Stoklund Olesen7d406792011-05-02 05:29:58 +0000499 // We may be trying to avoid interference that ends at a deleted instruction,
500 // so always begin RegIdx 0 early and all others late.
501 bool Late = RegIdx != 0;
502
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000503 // Attempt cheap-as-a-copy rematerialization.
Wei Mi9a16d652016-04-13 03:08:27 +0000504 unsigned Original = VRM.getOriginal(Edit->get(RegIdx));
505 LiveInterval &OrigLI = LIS.getInterval(Original);
506 VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
Wei Mi9a16d652016-04-13 03:08:27 +0000507
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000508 bool DidRemat = false;
509 if (OrigVNI) {
510 LiveRangeEdit::Remat RM(ParentVNI);
511 RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
512 if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) {
513 Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late);
514 ++NumRemats;
515 DidRemat = true;
516 }
517 }
518 if (!DidRemat) {
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000519 // Can't remat, just insert a copy from parent.
Eric Christopherede62672011-02-03 06:18:29 +0000520 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000521 .addReg(Edit->getReg());
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000522 Def = LIS.getSlotIndexes()
523 ->insertMachineInstrInMaps(*CopyMI, Late)
524 .getRegSlot();
Stanislav Mekhanoshin70c245e2017-02-01 01:18:36 +0000525 if (LI->hasSubRanges()) {
526 LaneBitmask LM = LaneBitmask::getNone();
527 for (LiveInterval::SubRange &S : LI->subranges())
528 LM |= S.LaneMask;
529
530 if (MRI.getMaxLaneMaskForVReg(LI->reg) != LM) {
531 // Find subreg for the lane mask.
532 unsigned SubIdx = 0;
533 for (unsigned I = 1, E = TRI.getNumSubRegIndices(); I < E; ++I) {
534 if (TRI.getSubRegIndexLaneMask(I) == LM) {
535 SubIdx = I;
536 break;
537 }
538 }
539 if (SubIdx == 0)
540 report_fatal_error("Cannot find subreg index to cover all alive lanes");
541 CopyMI->getOperand(0).setSubReg(SubIdx);
542 CopyMI->getOperand(1).setSubReg(SubIdx);
543 CopyMI->getOperand(0).setIsUndef(true);
544 }
545 }
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000546 ++NumCopies;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000547 }
548
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000549 // Define the value in Reg.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000550 return defValue(RegIdx, ParentVNI, Def, false);
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000551}
552
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000553/// Create a new virtual register and live interval.
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000554unsigned SplitEditor::openIntv() {
Eric Christopherede62672011-02-03 06:18:29 +0000555 // Create the complement as index 0.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000556 if (Edit->empty())
Mark Lacey9d8103d2013-08-14 23:50:16 +0000557 Edit->createEmptyInterval();
Eric Christopherede62672011-02-03 06:18:29 +0000558
559 // Create the open interval.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000560 OpenIdx = Edit->size();
Mark Lacey9d8103d2013-08-14 23:50:16 +0000561 Edit->createEmptyInterval();
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000562 return OpenIdx;
563}
564
565void SplitEditor::selectIntv(unsigned Idx) {
566 assert(Idx != 0 && "Cannot select the complement interval");
567 assert(Idx < Edit->size() && "Can only select previously opened interval");
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000568 DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000569 OpenIdx = Idx;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000570}
571
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000572SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000573 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000574 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000575 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000576 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000577 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000578 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000579 return Idx;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000580 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000581 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000582 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000583 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000584
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000585 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
586 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000587}
588
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000589SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
590 assert(OpenIdx && "openIntv not called before enterIntvAfter");
591 DEBUG(dbgs() << " enterIntvAfter " << Idx);
592 Idx = Idx.getBoundaryIndex();
593 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
594 if (!ParentVNI) {
595 DEBUG(dbgs() << ": not live\n");
596 return Idx;
597 }
598 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
599 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
600 assert(MI && "enterIntvAfter called with invalid index");
601
602 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000603 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000604 return VNI->def;
605}
606
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000607SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000608 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000609 SlotIndex End = LIS.getMBBEndIdx(&MBB);
610 SlotIndex Last = End.getPrevSlot();
611 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000612 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000613 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000614 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000615 return End;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000616 }
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000617 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000618 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000619 SA.getLastSplitPointIter(&MBB));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000620 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopherede62672011-02-03 06:18:29 +0000621 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000622 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000623}
624
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000625/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000626void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000627 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000628}
629
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000630void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopherede62672011-02-03 06:18:29 +0000631 assert(OpenIdx && "openIntv not called before useIntv");
632 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
633 RegAssign.insert(Start, End, OpenIdx);
634 DEBUG(dump());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000635}
636
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000637SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000638 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000639 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000640
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000641 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000642 SlotIndex Boundary = Idx.getBoundaryIndex();
643 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000644 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000645 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000646 return Boundary.getNextSlot();
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000647 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000648 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000649 MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
Jakob Stoklund Olesen3d11c8e2011-02-08 18:50:18 +0000650 assert(MI && "No instruction at index");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000651
652 // In spill mode, make live ranges as short as possible by inserting the copy
653 // before MI. This is only possible if that instruction doesn't redefine the
654 // value. The inserted COPY is not a kill, and we don't need to recompute
655 // the source live range. The spiller also won't try to hoist this copy.
656 if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
657 MI->readsVirtualRegister(Edit->getReg())) {
658 forceRecompute(0, ParentVNI);
659 defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
660 return Idx;
661 }
662
663 VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000664 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000665 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000666}
667
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000668SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
669 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
670 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
671
672 // The interval must be live into the instruction at Idx.
Jakob Stoklund Olesenc45d38e2011-07-18 18:47:13 +0000673 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000674 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000675 if (!ParentVNI) {
676 DEBUG(dbgs() << ": not live\n");
677 return Idx.getNextSlot();
678 }
679 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
680
681 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
682 assert(MI && "No instruction at index");
683 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
684 return VNI->def;
685}
686
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000687SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000688 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000689 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000690 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000691
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000692 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000693 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000694 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000695 return Start;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000696 }
697
Eric Christopherede62672011-02-03 06:18:29 +0000698 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Keith Walker830a8c12016-09-16 14:07:29 +0000699 MBB.SkipPHIsLabelsAndDebug(MBB.begin()));
Eric Christopherede62672011-02-03 06:18:29 +0000700 RegAssign.insert(Start, VNI->def, OpenIdx);
701 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000702 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000703}
704
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000705void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
706 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000707 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesend7bcf432011-11-14 01:39:36 +0000708 assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000709 "Parent changes value in extended range");
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000710 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
711 "Range cannot span basic blocks");
712
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000713 // The complement interval will be extended as needed by LRCalc.extend().
Jakob Stoklund Olesen5c482cd2011-04-05 23:43:14 +0000714 if (ParentVNI)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000715 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000716 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
717 RegAssign.insert(Start, End, OpenIdx);
718 DEBUG(dump());
719}
720
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000721//===----------------------------------------------------------------------===//
722// Spill modes
723//===----------------------------------------------------------------------===//
724
725void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
Mark Laceyf9ea8852013-08-14 23:50:04 +0000726 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000727 DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
728 RegAssignMap::iterator AssignI;
729 AssignI.setMap(RegAssign);
730
731 for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
Matthias Braun311730a2015-01-21 19:02:30 +0000732 SlotIndex Def = Copies[i]->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000733 MachineInstr *MI = LIS.getInstructionFromIndex(Def);
734 assert(MI && "No instruction for back-copy");
735
736 MachineBasicBlock *MBB = MI->getParent();
737 MachineBasicBlock::iterator MBBI(MI);
738 bool AtBegin;
739 do AtBegin = MBBI == MBB->begin();
740 while (!AtBegin && (--MBBI)->isDebugValue());
741
742 DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
Matthias Braun311730a2015-01-21 19:02:30 +0000743 LIS.removeVRegDefAt(*LI, Def);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000744 LIS.RemoveMachineInstrFromMaps(*MI);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000745 MI->eraseFromParent();
746
Matthias Braun311730a2015-01-21 19:02:30 +0000747 // Adjust RegAssign if a register assignment is killed at Def. We want to
748 // avoid calculating the live range of the source register if possible.
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000749 AssignI.find(Def.getPrevSlot());
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000750 if (!AssignI.valid() || AssignI.start() >= Def)
751 continue;
752 // If MI doesn't kill the assigned register, just leave it.
753 if (AssignI.stop() != Def)
754 continue;
755 unsigned RegIdx = AssignI.value();
756 if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
757 DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n');
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000758 forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000759 } else {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000760 SlotIndex Kill = LIS.getInstructionIndex(*MBBI).getRegSlot();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000761 DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
762 AssignI.setStop(Kill);
763 }
764 }
765}
766
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000767MachineBasicBlock*
768SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
769 MachineBasicBlock *DefMBB) {
770 if (MBB == DefMBB)
771 return MBB;
772 assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
773
774 const MachineLoopInfo &Loops = SA.Loops;
775 const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
776 MachineDomTreeNode *DefDomNode = MDT[DefMBB];
777
778 // Best candidate so far.
779 MachineBasicBlock *BestMBB = MBB;
780 unsigned BestDepth = UINT_MAX;
781
782 for (;;) {
783 const MachineLoop *Loop = Loops.getLoopFor(MBB);
784
785 // MBB isn't in a loop, it doesn't get any better. All dominators have a
786 // higher frequency by definition.
787 if (!Loop) {
788 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
789 << MBB->getNumber() << " at depth 0\n");
790 return MBB;
791 }
792
793 // We'll never be able to exit the DefLoop.
794 if (Loop == DefLoop) {
795 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
796 << MBB->getNumber() << " in the same loop\n");
797 return MBB;
798 }
799
800 // Least busy dominator seen so far.
801 unsigned Depth = Loop->getLoopDepth();
802 if (Depth < BestDepth) {
803 BestMBB = MBB;
804 BestDepth = Depth;
805 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
806 << MBB->getNumber() << " at depth " << Depth << '\n');
807 }
808
809 // Leave loop by going to the immediate dominator of the loop header.
810 // This is a bigger stride than simply walking up the dominator tree.
811 MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
812
813 // Too far up the dominator tree?
814 if (!IDom || !MDT.dominates(DefDomNode, IDom))
815 return BestMBB;
816
817 MBB = IDom->getBlock();
818 }
819}
820
Wei Mi9a16d652016-04-13 03:08:27 +0000821void SplitEditor::computeRedundantBackCopies(
822 DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) {
823 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
824 LiveInterval *Parent = &Edit->getParent();
825 SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums());
826 SmallPtrSet<VNInfo *, 8> DominatedVNIs;
827
828 // Aggregate VNIs having the same value as ParentVNI.
829 for (VNInfo *VNI : LI->valnos) {
830 if (VNI->isUnused())
831 continue;
832 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
833 EqualVNs[ParentVNI->id].insert(VNI);
834 }
835
836 // For VNI aggregation of each ParentVNI, collect dominated, i.e.,
837 // redundant VNIs to BackCopies.
838 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
839 VNInfo *ParentVNI = Parent->getValNumInfo(i);
840 if (!NotToHoistSet.count(ParentVNI->id))
841 continue;
842 SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin();
843 SmallPtrSetIterator<VNInfo *> It2 = It1;
844 for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) {
845 It2 = It1;
846 for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) {
847 if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2))
848 continue;
849
850 MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def);
851 MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def);
852 if (MBB1 == MBB2) {
853 DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1));
854 } else if (MDT.dominates(MBB1, MBB2)) {
855 DominatedVNIs.insert(*It2);
856 } else if (MDT.dominates(MBB2, MBB1)) {
857 DominatedVNIs.insert(*It1);
858 }
859 }
860 }
861 if (!DominatedVNIs.empty()) {
862 forceRecompute(0, ParentVNI);
863 for (auto VNI : DominatedVNIs) {
864 BackCopies.push_back(VNI);
865 }
866 DominatedVNIs.clear();
867 }
868 }
869}
870
871/// For SM_Size mode, find a common dominator for all the back-copies for
872/// the same ParentVNI and hoist the backcopies to the dominator BB.
873/// For SM_Speed mode, if the common dominator is hot and it is not beneficial
874/// to do the hoisting, simply remove the dominated backcopies for the same
875/// ParentVNI.
876void SplitEditor::hoistCopies() {
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000877 // Get the complement interval, always RegIdx 0.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000878 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000879 LiveInterval *Parent = &Edit->getParent();
880
881 // Track the nearest common dominator for all back-copies for each ParentVNI,
882 // indexed by ParentVNI->id.
883 typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
884 SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
Wei Mi9a16d652016-04-13 03:08:27 +0000885 // The total cost of all the back-copies for each ParentVNI.
886 SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums());
887 // The ParentVNI->id set for which hoisting back-copies are not beneficial
888 // for Speed.
889 DenseSet<unsigned> NotToHoistSet;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000890
891 // Find the nearest common dominator for parent values with multiple
892 // back-copies. If a single back-copy dominates, put it in DomPair.second.
Matthias Braun96761952014-12-10 23:07:54 +0000893 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000894 if (VNI->isUnused())
895 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000896 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
897 assert(ParentVNI && "Parent not live at complement def");
898
899 // Don't hoist remats. The complement is probably going to disappear
900 // completely anyway.
901 if (Edit->didRematerialize(ParentVNI))
902 continue;
903
904 MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
Wei Mi9a16d652016-04-13 03:08:27 +0000905
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000906 DomPair &Dom = NearestDom[ParentVNI->id];
907
908 // Keep directly defined parent values. This is either a PHI or an
909 // instruction in the complement range. All other copies of ParentVNI
910 // should be eliminated.
911 if (VNI->def == ParentVNI->def) {
912 DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
913 Dom = DomPair(ValMBB, VNI->def);
914 continue;
915 }
916 // Skip the singly mapped values. There is nothing to gain from hoisting a
917 // single back-copy.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000918 if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000919 DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
920 continue;
921 }
922
923 if (!Dom.first) {
924 // First time we see ParentVNI. VNI dominates itself.
925 Dom = DomPair(ValMBB, VNI->def);
926 } else if (Dom.first == ValMBB) {
927 // Two defs in the same block. Pick the earlier def.
928 if (!Dom.second.isValid() || VNI->def < Dom.second)
929 Dom.second = VNI->def;
930 } else {
931 // Different basic blocks. Check if one dominates.
932 MachineBasicBlock *Near =
933 MDT.findNearestCommonDominator(Dom.first, ValMBB);
934 if (Near == ValMBB)
935 // Def ValMBB dominates.
936 Dom = DomPair(ValMBB, VNI->def);
937 else if (Near != Dom.first)
938 // None dominate. Hoist to common dominator, need new def.
939 Dom = DomPair(Near, SlotIndex());
Wei Mi9a16d652016-04-13 03:08:27 +0000940 Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000941 }
942
943 DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
944 << " for parent " << ParentVNI->id << '@' << ParentVNI->def
945 << " hoist to BB#" << Dom.first->getNumber() << ' '
946 << Dom.second << '\n');
947 }
948
949 // Insert the hoisted copies.
950 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
951 DomPair &Dom = NearestDom[i];
952 if (!Dom.first || Dom.second.isValid())
953 continue;
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000954 // This value needs a hoisted copy inserted at the end of Dom.first.
955 VNInfo *ParentVNI = Parent->getValNumInfo(i);
956 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
957 // Get a less loopy dominator than Dom.first.
958 Dom.first = findShallowDominator(Dom.first, DefMBB);
Wei Mi9a16d652016-04-13 03:08:27 +0000959 if (SpillMode == SM_Speed &&
960 MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) {
961 NotToHoistSet.insert(ParentVNI->id);
962 continue;
963 }
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000964 SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
965 Dom.second =
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000966 defFromParent(0, ParentVNI, Last, *Dom.first,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000967 SA.getLastSplitPointIter(Dom.first))->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000968 }
969
970 // Remove redundant back-copies that are now known to be dominated by another
971 // def with the same value.
972 SmallVector<VNInfo*, 8> BackCopies;
Matthias Braun96761952014-12-10 23:07:54 +0000973 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000974 if (VNI->isUnused())
975 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000976 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
977 const DomPair &Dom = NearestDom[ParentVNI->id];
Wei Mi9a16d652016-04-13 03:08:27 +0000978 if (!Dom.first || Dom.second == VNI->def ||
979 NotToHoistSet.count(ParentVNI->id))
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000980 continue;
981 BackCopies.push_back(VNI);
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000982 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000983 }
Wei Mi9a16d652016-04-13 03:08:27 +0000984
985 // If it is not beneficial to hoist all the BackCopies, simply remove
986 // redundant BackCopies in speed mode.
987 if (SpillMode == SM_Speed && !NotToHoistSet.empty())
988 computeRedundantBackCopies(NotToHoistSet, BackCopies);
989
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000990 removeBackCopies(BackCopies);
991}
992
993
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000994/// transferValues - Transfer all possible values to the new live ranges.
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000995/// Values that were rematerialized are left alone, they need LRCalc.extend().
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000996bool SplitEditor::transferValues() {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000997 bool Skipped = false;
998 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Matthias Braun96761952014-12-10 23:07:54 +0000999 for (const LiveRange::Segment &S : Edit->getParent()) {
1000 DEBUG(dbgs() << " blit " << S << ':');
1001 VNInfo *ParentVNI = S.valno;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001002 // RegAssign has holes where RegIdx 0 should be used.
Matthias Braun96761952014-12-10 23:07:54 +00001003 SlotIndex Start = S.start;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001004 AssignI.advanceTo(Start);
1005 do {
1006 unsigned RegIdx;
Matthias Braun96761952014-12-10 23:07:54 +00001007 SlotIndex End = S.end;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001008 if (!AssignI.valid()) {
1009 RegIdx = 0;
1010 } else if (AssignI.start() <= Start) {
1011 RegIdx = AssignI.value();
1012 if (AssignI.stop() < End) {
1013 End = AssignI.stop();
1014 ++AssignI;
1015 }
1016 } else {
1017 RegIdx = 0;
1018 End = std::min(End, AssignI.start());
1019 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001020
1021 // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001022 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx
1023 << '(' << PrintReg(Edit->get(RegIdx)) << ')');
1024 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001025
1026 // Check for a simply defined value that can be blitted directly.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001027 ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
1028 if (VNInfo *VNI = VFP.getPointer()) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001029 DEBUG(dbgs() << ':' << VNI->id);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001030 LI.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001031 Start = End;
1032 continue;
1033 }
1034
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001035 // Skip values with forced recomputation.
1036 if (VFP.getInt()) {
1037 DEBUG(dbgs() << "(recalc)");
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001038 Skipped = true;
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001039 Start = End;
1040 continue;
1041 }
1042
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +00001043 LiveRangeCalc &LRC = getLRCalc(RegIdx);
1044
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001045 // This value has multiple defs in RegIdx, but it wasn't rematerialized,
1046 // so the live range is accurate. Add live-in blocks in [Start;End) to the
1047 // LiveInBlocks.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001048 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001049 SlotIndex BlockStart, BlockEnd;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001050 std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001051
1052 // The first block may be live-in, or it may have its own def.
1053 if (Start != BlockStart) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001054 VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001055 assert(VNI && "Missing def for complex mapped value");
1056 DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
1057 // MBB has its own def. Is it also live-out?
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001058 if (BlockEnd <= End)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001059 LRC.setLiveOutValue(&*MBB, VNI);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001060
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001061 // Skip to the next block for live-in.
1062 ++MBB;
1063 BlockStart = BlockEnd;
1064 }
1065
1066 // Handle the live-in blocks covered by [Start;End).
1067 assert(Start <= BlockStart && "Expected live-in block");
1068 while (BlockStart < End) {
1069 DEBUG(dbgs() << ">BB#" << MBB->getNumber());
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001070 BlockEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001071 if (BlockStart == ParentVNI->def) {
1072 // This block has the def of a parent PHI, so it isn't live-in.
1073 assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001074 VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001075 assert(VNI && "Missing def for complex mapped parent PHI");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001076 if (End >= BlockEnd)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001077 LRC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001078 } else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001079 // This block needs a live-in value. The last block covered may not
1080 // be live-out.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001081 if (End < BlockEnd)
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001082 LRC.addLiveInBlock(LI, MDT[&*MBB], End);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001083 else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001084 // Live-through, and we don't know the value.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001085 LRC.addLiveInBlock(LI, MDT[&*MBB]);
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001086 LRC.setLiveOutValue(&*MBB, nullptr);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001087 }
1088 }
1089 BlockStart = BlockEnd;
1090 ++MBB;
1091 }
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001092 Start = End;
Matthias Braun96761952014-12-10 23:07:54 +00001093 } while (Start != S.end);
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001094 DEBUG(dbgs() << '\n');
1095 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001096
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +00001097 LRCalc[0].calculateValues();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +00001098 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +00001099 LRCalc[1].calculateValues();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001100
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001101 return Skipped;
1102}
1103
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001104static bool removeDeadSegment(SlotIndex Def, LiveRange &LR) {
1105 const LiveRange::Segment *Seg = LR.getSegmentContaining(Def);
1106 if (Seg == nullptr)
1107 return true;
1108 if (Seg->end != Def.getDeadSlot())
1109 return false;
1110 // This is a dead PHI. Remove it.
1111 LR.removeSegment(*Seg, true);
1112 return true;
1113}
1114
1115void SplitEditor::extendPHIRange(MachineBasicBlock &B, LiveRangeCalc &LRC,
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +00001116 LiveRange &LR, LaneBitmask LM,
1117 ArrayRef<SlotIndex> Undefs) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001118 for (MachineBasicBlock *P : B.predecessors()) {
1119 SlotIndex End = LIS.getMBBEndIdx(P);
1120 SlotIndex LastUse = End.getPrevSlot();
1121 // The predecessor may not have a live-out value. That is OK, like an
1122 // undef PHI operand.
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +00001123 LiveInterval &PLI = Edit->getParent();
1124 // Need the cast because the inputs to ?: would otherwise be deemed
1125 // "incompatible": SubRange vs LiveInterval.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001126 LiveRange &PSR = !LM.all() ? getSubRangeForMask(LM, PLI)
1127 : static_cast<LiveRange&>(PLI);
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +00001128 if (PSR.liveAt(LastUse))
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001129 LRC.extend(LR, End, /*PhysReg=*/0, Undefs);
1130 }
1131}
1132
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001133void SplitEditor::extendPHIKillRanges() {
Wei Mi9a16d652016-04-13 03:08:27 +00001134 // Extend live ranges to be live-out for successor PHI values.
Wei Mi9a16d652016-04-13 03:08:27 +00001135
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001136 // Visit each PHI def slot in the parent live interval. If the def is dead,
1137 // remove it. Otherwise, extend the live interval to reach the end indexes
1138 // of all predecessor blocks.
Wei Mi9a16d652016-04-13 03:08:27 +00001139
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001140 LiveInterval &ParentLI = Edit->getParent();
1141 for (const VNInfo *V : ParentLI.valnos) {
1142 if (V->isUnused() || !V->isPHIDef())
1143 continue;
1144
1145 unsigned RegIdx = RegAssign.lookup(V->def);
1146 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001147 LiveRangeCalc &LRC = getLRCalc(RegIdx);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001148 MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
1149 if (!removeDeadSegment(V->def, LI))
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001150 extendPHIRange(B, LRC, LI, LaneBitmask::getAll(), /*Undefs=*/{});
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001151 }
1152
1153 SmallVector<SlotIndex, 4> Undefs;
1154 LiveRangeCalc SubLRC;
1155
1156 for (LiveInterval::SubRange &PS : ParentLI.subranges()) {
1157 for (const VNInfo *V : PS.valnos) {
1158 if (V->isUnused() || !V->isPHIDef())
1159 continue;
1160 unsigned RegIdx = RegAssign.lookup(V->def);
1161 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1162 LiveInterval::SubRange &S = getSubRangeForMask(PS.LaneMask, LI);
1163 if (removeDeadSegment(V->def, S))
1164 continue;
1165
1166 MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
1167 SubLRC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
1168 &LIS.getVNInfoAllocator());
1169 Undefs.clear();
1170 LI.computeSubRangeUndefs(Undefs, PS.LaneMask, MRI, *LIS.getSlotIndexes());
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +00001171 extendPHIRange(B, SubLRC, S, PS.LaneMask, Undefs);
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001172 }
1173 }
1174}
1175
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001176/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001177void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001178 struct ExtPoint {
1179 ExtPoint(const MachineOperand &O, unsigned R, SlotIndex N)
1180 : MO(O), RegIdx(R), Next(N) {}
1181 MachineOperand MO;
1182 unsigned RegIdx;
1183 SlotIndex Next;
1184 };
1185
1186 SmallVector<ExtPoint,4> ExtPoints;
1187
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001188 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +00001189 RE = MRI.reg_end(); RI != RE;) {
Owen Anderson16c6bf42014-03-13 23:12:04 +00001190 MachineOperand &MO = *RI;
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001191 MachineInstr *MI = MO.getParent();
1192 ++RI;
Eric Christopherede62672011-02-03 06:18:29 +00001193 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001194 if (MI->isDebugValue()) {
1195 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001196 MO.setReg(0);
1197 continue;
1198 }
Jakob Stoklund Olesenf6e03942011-02-09 21:52:09 +00001199
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +00001200 // <undef> operands don't really read the register, so it doesn't matter
1201 // which register we choose. When the use operand is tied to a def, we must
1202 // use the same register as the def, so just do that always.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001203 SlotIndex Idx = LIS.getInstructionIndex(*MI);
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +00001204 if (MO.isDef() || MO.isUndef())
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +00001205 Idx = Idx.getRegSlot(MO.isEarlyClobber());
Eric Christopherede62672011-02-03 06:18:29 +00001206
1207 // Rewrite to the mapped register at Idx.
1208 unsigned RegIdx = RegAssign.lookup(Idx);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001209 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1210 MO.setReg(LI.reg);
Eric Christopherede62672011-02-03 06:18:29 +00001211 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
1212 << Idx << ':' << RegIdx << '\t' << *MI);
1213
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001214 // Extend liveness to Idx if the instruction reads reg.
Jakob Stoklund Olesen73a9eb92011-07-24 20:33:23 +00001215 if (!ExtendRanges || MO.isUndef())
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001216 continue;
1217
1218 // Skip instructions that don't read Reg.
1219 if (MO.isDef()) {
1220 if (!MO.getSubReg() && !MO.isEarlyClobber())
1221 continue;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001222 // We may want to extend a live range for a partial redef, or for a use
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001223 // tied to an early clobber.
1224 Idx = Idx.getPrevSlot();
1225 if (!Edit->getParent().liveAt(Idx))
1226 continue;
1227 } else
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +00001228 Idx = Idx.getRegSlot(true);
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001229
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001230 SlotIndex Next = Idx.getNextSlot();
1231 if (LI.hasSubRanges()) {
1232 // We have to delay extending subranges until we have seen all operands
1233 // defining the register. This is because a <def,read-undef> operand
1234 // will create an "undef" point, and we cannot extend any subranges
1235 // until all of them have been accounted for.
Krzysztof Parzyszek3bf4aec2016-09-02 19:48:55 +00001236 if (MO.isUse())
1237 ExtPoints.push_back(ExtPoint(MO, RegIdx, Next));
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001238 } else {
1239 LiveRangeCalc &LRC = getLRCalc(RegIdx);
1240 LRC.extend(LI, Next, 0, ArrayRef<SlotIndex>());
1241 }
1242 }
1243
1244 for (ExtPoint &EP : ExtPoints) {
1245 LiveInterval &LI = LIS.getInterval(Edit->get(EP.RegIdx));
1246 assert(LI.hasSubRanges());
1247
1248 LiveRangeCalc SubLRC;
1249 unsigned Reg = EP.MO.getReg(), Sub = EP.MO.getSubReg();
1250 LaneBitmask LM = Sub != 0 ? TRI.getSubRegIndexLaneMask(Sub)
1251 : MRI.getMaxLaneMaskForVReg(Reg);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001252 for (LiveInterval::SubRange &S : LI.subranges()) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001253 if ((S.LaneMask & LM).none())
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001254 continue;
1255 // The problem here can be that the new register may have been created
1256 // for a partially defined original register. For example:
1257 // %vreg827:subreg_hireg<def,read-undef> = ...
1258 // ...
1259 // %vreg828<def> = COPY %vreg827
1260 if (S.empty())
1261 continue;
1262 SubLRC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
1263 &LIS.getVNInfoAllocator());
1264 SmallVector<SlotIndex, 4> Undefs;
1265 LI.computeSubRangeUndefs(Undefs, S.LaneMask, MRI, *LIS.getSlotIndexes());
1266 SubLRC.extend(S, EP.Next, 0, Undefs);
1267 }
1268 }
1269
1270 for (unsigned R : *Edit) {
1271 LiveInterval &LI = LIS.getInterval(R);
1272 if (!LI.hasSubRanges())
1273 continue;
1274 LI.clear();
1275 LI.removeEmptySubRanges();
1276 LIS.constructMainRangeFromSubranges(LI);
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001277 }
1278}
1279
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001280void SplitEditor::deleteRematVictims() {
1281 SmallVector<MachineInstr*, 8> Dead;
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001282 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
Mark Laceyf9ea8852013-08-14 23:50:04 +00001283 LiveInterval *LI = &LIS.getInterval(*I);
Matthias Braun96761952014-12-10 23:07:54 +00001284 for (const LiveRange::Segment &S : LI->segments) {
Jakob Stoklund Olesend8f24052011-11-13 22:42:13 +00001285 // Dead defs end at the dead slot.
Matthias Braun96761952014-12-10 23:07:54 +00001286 if (S.end != S.valno->def.getDeadSlot())
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001287 continue;
Wei Mi9a16d652016-04-13 03:08:27 +00001288 if (S.valno->isPHIDef())
1289 continue;
Matthias Braun96761952014-12-10 23:07:54 +00001290 MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001291 assert(MI && "Missing instruction for dead def");
1292 MI->addRegisterDead(LI->reg, &TRI);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001293
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001294 if (!MI->allDefsAreDead())
1295 continue;
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001296
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001297 DEBUG(dbgs() << "All defs dead: " << *MI);
1298 Dead.push_back(MI);
1299 }
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001300 }
1301
1302 if (Dead.empty())
1303 return;
1304
Wei Mic0223702016-07-08 21:08:09 +00001305 Edit->eliminateDeadDefs(Dead, None, &AA);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001306}
1307
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001308void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001309 ++NumFinished;
Eric Christopher21933532011-02-03 05:40:54 +00001310
Eric Christopherede62672011-02-03 06:18:29 +00001311 // At this point, the live intervals in Edit contain VNInfos corresponding to
1312 // the inserted copies.
1313
1314 // Add the original defs from the parent interval.
Matthias Braun96761952014-12-10 23:07:54 +00001315 for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen3295a992011-02-04 00:59:23 +00001316 if (ParentVNI->isUnused())
1317 continue;
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +00001318 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001319 defValue(RegIdx, ParentVNI, ParentVNI->def, true);
Jakob Stoklund Olesen32210de2011-03-15 21:13:22 +00001320
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001321 // Force rematted values to be recomputed everywhere.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001322 // The new live ranges may be truncated.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001323 if (Edit->didRematerialize(ParentVNI))
1324 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001325 forceRecompute(i, ParentVNI);
Eric Christopherede62672011-02-03 06:18:29 +00001326 }
1327
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001328 // Hoist back-copies to the complement interval when in spill mode.
1329 switch (SpillMode) {
1330 case SM_Partition:
1331 // Leave all back-copies as is.
1332 break;
1333 case SM_Size:
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001334 case SM_Speed:
Wei Mi9a16d652016-04-13 03:08:27 +00001335 // hoistCopies will behave differently between size and speed.
1336 hoistCopies();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001337 }
1338
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001339 // Transfer the simply mapped values, check if any are skipped.
1340 bool Skipped = transferValues();
Wei Mi9a16d652016-04-13 03:08:27 +00001341
1342 // Rewrite virtual registers, possibly extending ranges.
1343 rewriteAssigned(Skipped);
1344
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001345 if (Skipped)
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001346 extendPHIKillRanges();
1347 else
1348 ++NumSimple;
Eric Christopherede62672011-02-03 06:18:29 +00001349
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001350 // Delete defs that were rematted everywhere.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001351 if (Skipped)
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001352 deleteRematVictims();
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001353
Jakob Stoklund Olesen0f1677e2010-10-07 23:34:34 +00001354 // Get rid of unused values and set phi-kill flags.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001355 for (unsigned Reg : *Edit) {
1356 LiveInterval &LI = LIS.getInterval(Reg);
1357 LI.removeEmptySubRanges();
Mark Laceyf9ea8852013-08-14 23:50:04 +00001358 LI.RenumberValues();
1359 }
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001360
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001361 // Provide a reverse mapping from original indices to Edit ranges.
1362 if (LRMap) {
1363 LRMap->clear();
1364 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1365 LRMap->push_back(i);
1366 }
1367
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001368 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +00001369 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001370 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001371 // Don't use iterators, they are invalidated by create() below.
Matthias Braund3dd1352015-09-22 03:44:41 +00001372 unsigned VReg = Edit->get(i);
1373 LiveInterval &LI = LIS.getInterval(VReg);
1374 SmallVector<LiveInterval*, 8> SplitLIs;
1375 LIS.splitSeparateComponents(LI, SplitLIs);
1376 unsigned Original = VRM.getOriginal(VReg);
1377 for (LiveInterval *SplitLI : SplitLIs)
1378 VRM.setIsSplitFromReg(SplitLI->reg, Original);
1379
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001380 // The new intervals all map back to i.
1381 if (LRMap)
1382 LRMap->resize(Edit->size(), i);
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001383 }
1384
Jakob Stoklund Olesen284c2db2010-08-10 17:07:22 +00001385 // Calculate spill weight and allocation hints for new intervals.
Benjamin Kramere2a1d892013-06-17 19:00:36 +00001386 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001387
1388 assert(!LRMap || LRMap->size() == Edit->size());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +00001389}
1390
1391
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +00001392//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +00001393// Single Block Splitting
1394//===----------------------------------------------------------------------===//
1395
Jakob Stoklund Olesen8627ea92011-08-05 22:20:45 +00001396bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
1397 bool SingleInstrs) const {
1398 // Always split for multiple instructions.
1399 if (!BI.isOneInstr())
1400 return true;
1401 // Don't split for single instructions unless explicitly requested.
1402 if (!SingleInstrs)
1403 return false;
1404 // Splitting a live-through range always makes progress.
1405 if (BI.LiveIn && BI.LiveOut)
1406 return true;
1407 // No point in isolating a copy. It has no register class constraints.
1408 if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
1409 return false;
1410 // Finally, don't isolate an end point that was created by earlier splits.
1411 return isOriginalEndpoint(BI.FirstInstr);
1412}
1413
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001414void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1415 openIntv();
1416 SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001417 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001418 LastSplitPoint));
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001419 if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
1420 useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001421 } else {
1422 // The last use is after the last valid split point.
1423 SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1424 useIntv(SegStart, SegStop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001425 overlapIntv(SegStop, BI.LastInstr);
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001426 }
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001427}
1428
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001429
1430//===----------------------------------------------------------------------===//
1431// Global Live Range Splitting Support
1432//===----------------------------------------------------------------------===//
1433
1434// These methods support a method of global live range splitting that uses a
1435// global algorithm to decide intervals for CFG edges. They will insert split
1436// points and color intervals in basic blocks while avoiding interference.
1437//
1438// Note that splitSingleBlock is also useful for blocks where both CFG edges
1439// are on the stack.
1440
1441void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1442 unsigned IntvIn, SlotIndex LeaveBefore,
1443 unsigned IntvOut, SlotIndex EnterAfter){
1444 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001445 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001446
1447 DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
1448 << ") intf " << LeaveBefore << '-' << EnterAfter
1449 << ", live-through " << IntvIn << " -> " << IntvOut);
1450
1451 assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1452
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001453 assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
1454 assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
1455 assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
1456
1457 MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
1458
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001459 if (!IntvOut) {
1460 DEBUG(dbgs() << ", spill on entry.\n");
1461 //
1462 // <<<<<<<<< Possible LeaveBefore interference.
1463 // |-----------| Live through.
1464 // -____________ Spill on entry.
1465 //
1466 selectIntv(IntvIn);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001467 SlotIndex Idx = leaveIntvAtTop(*MBB);
1468 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1469 (void)Idx;
1470 return;
1471 }
1472
1473 if (!IntvIn) {
1474 DEBUG(dbgs() << ", reload on exit.\n");
1475 //
1476 // >>>>>>> Possible EnterAfter interference.
1477 // |-----------| Live through.
1478 // ___________-- Reload on exit.
1479 //
1480 selectIntv(IntvOut);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001481 SlotIndex Idx = enterIntvAtEnd(*MBB);
1482 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1483 (void)Idx;
1484 return;
1485 }
1486
1487 if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1488 DEBUG(dbgs() << ", straight through.\n");
1489 //
1490 // |-----------| Live through.
1491 // ------------- Straight through, same intv, no interference.
1492 //
1493 selectIntv(IntvOut);
1494 useIntv(Start, Stop);
1495 return;
1496 }
1497
1498 // We cannot legally insert splits after LSP.
1499 SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001500 assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001501
1502 if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1503 LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1504 DEBUG(dbgs() << ", switch avoiding interference.\n");
1505 //
1506 // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
1507 // |-----------| Live through.
1508 // ------======= Switch intervals between interference.
1509 //
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001510 selectIntv(IntvOut);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001511 SlotIndex Idx;
1512 if (LeaveBefore && LeaveBefore < LSP) {
1513 Idx = enterIntvBefore(LeaveBefore);
1514 useIntv(Idx, Stop);
1515 } else {
1516 Idx = enterIntvAtEnd(*MBB);
1517 }
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001518 selectIntv(IntvIn);
1519 useIntv(Start, Idx);
1520 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1521 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1522 return;
1523 }
1524
1525 DEBUG(dbgs() << ", create local intv for interference.\n");
1526 //
1527 // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
1528 // |-----------| Live through.
1529 // ==---------== Switch intervals before/after interference.
1530 //
1531 assert(LeaveBefore <= EnterAfter && "Missed case");
1532
1533 selectIntv(IntvOut);
1534 SlotIndex Idx = enterIntvAfter(EnterAfter);
1535 useIntv(Idx, Stop);
1536 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1537
1538 selectIntv(IntvIn);
1539 Idx = leaveIntvBefore(LeaveBefore);
1540 useIntv(Start, Idx);
1541 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1542}
1543
1544
1545void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1546 unsigned IntvIn, SlotIndex LeaveBefore) {
1547 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001548 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001549
1550 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001551 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001552 << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
1553 << (BI.LiveOut ? ", stack-out" : ", killed in block"));
1554
1555 assert(IntvIn && "Must have register in");
1556 assert(BI.LiveIn && "Must be live-in");
1557 assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1558
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001559 if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001560 DEBUG(dbgs() << " before interference.\n");
1561 //
1562 // <<< Interference after kill.
1563 // |---o---x | Killed in block.
1564 // ========= Use IntvIn everywhere.
1565 //
1566 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001567 useIntv(Start, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001568 return;
1569 }
1570
1571 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1572
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001573 if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001574 //
1575 // <<< Possible interference after last use.
1576 // |---o---o---| Live-out on stack.
1577 // =========____ Leave IntvIn after last use.
1578 //
1579 // < Interference after last use.
1580 // |---o---o--o| Live-out on stack, late last use.
1581 // ============ Copy to stack after LSP, overlap IntvIn.
1582 // \_____ Stack interval is live-out.
1583 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001584 if (BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001585 DEBUG(dbgs() << ", spill after last use before interference.\n");
1586 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001587 SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001588 useIntv(Start, Idx);
1589 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1590 } else {
1591 DEBUG(dbgs() << ", spill before last split point.\n");
1592 selectIntv(IntvIn);
Jakob Stoklund Olesen37e3a132011-07-16 00:13:30 +00001593 SlotIndex Idx = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001594 overlapIntv(Idx, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001595 useIntv(Start, Idx);
1596 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1597 }
1598 return;
1599 }
1600
1601 // The interference is overlapping somewhere we wanted to use IntvIn. That
1602 // means we need to create a local interval that can be allocated a
1603 // different register.
1604 unsigned LocalIntv = openIntv();
Matt Beaumont-Gay26909d82011-07-16 04:18:47 +00001605 (void)LocalIntv;
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001606 DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
1607
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001608 if (!BI.LiveOut || BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001609 //
1610 // <<<<<<< Interference overlapping uses.
1611 // |---o---o---| Live-out on stack.
1612 // =====----____ Leave IntvIn before interference, then spill.
1613 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001614 SlotIndex To = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001615 SlotIndex From = enterIntvBefore(LeaveBefore);
1616 useIntv(From, To);
1617 selectIntv(IntvIn);
1618 useIntv(Start, From);
1619 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1620 return;
1621 }
1622
1623 // <<<<<<< Interference overlapping uses.
1624 // |---o---o--o| Live-out on stack, late last use.
1625 // =====------- Copy to stack before LSP, overlap LocalIntv.
1626 // \_____ Stack interval is live-out.
1627 //
1628 SlotIndex To = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001629 overlapIntv(To, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001630 SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1631 useIntv(From, To);
1632 selectIntv(IntvIn);
1633 useIntv(Start, From);
1634 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1635}
1636
1637void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1638 unsigned IntvOut, SlotIndex EnterAfter) {
1639 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001640 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001641
1642 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001643 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001644 << ", reg-out " << IntvOut << ", enter after " << EnterAfter
1645 << (BI.LiveIn ? ", stack-in" : ", defined in block"));
1646
1647 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1648
1649 assert(IntvOut && "Must have register out");
1650 assert(BI.LiveOut && "Must be live-out");
1651 assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1652
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001653 if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001654 DEBUG(dbgs() << " after interference.\n");
1655 //
1656 // >>>> Interference before def.
1657 // | o---o---| Defined in block.
1658 // ========= Use IntvOut everywhere.
1659 //
1660 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001661 useIntv(BI.FirstInstr, Stop);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001662 return;
1663 }
1664
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001665 if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001666 DEBUG(dbgs() << ", reload after interference.\n");
1667 //
1668 // >>>> Interference before def.
1669 // |---o---o---| Live-through, stack-in.
1670 // ____========= Enter IntvOut before first use.
1671 //
1672 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001673 SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001674 useIntv(Idx, Stop);
1675 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1676 return;
1677 }
1678
1679 // The interference is overlapping somewhere we wanted to use IntvOut. That
1680 // means we need to create a local interval that can be allocated a
1681 // different register.
1682 DEBUG(dbgs() << ", interference overlaps uses.\n");
1683 //
1684 // >>>>>>> Interference overlapping uses.
1685 // |---o---o---| Live-through, stack-in.
1686 // ____---====== Create local interval for interference range.
1687 //
1688 selectIntv(IntvOut);
1689 SlotIndex Idx = enterIntvAfter(EnterAfter);
1690 useIntv(Idx, Stop);
1691 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1692
1693 openIntv();
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001694 SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001695 useIntv(From, Idx);
1696}