blob: 323045fd2aaae12b2fec94873f12a8c4a1e9187f [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"
Matthias Braunf0b68d32017-03-17 00:41:39 +000026#include "llvm/Support/MathExtras.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000027#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesened4075c2010-07-20 21:46:58 +000028#include "llvm/Target/TargetInstrInfo.h"
29#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000030
31using namespace llvm;
32
Chandler Carruth1b9dde02014-04-22 02:02:50 +000033#define DEBUG_TYPE "regalloc"
34
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +000035STATISTIC(NumFinished, "Number of splits finished");
36STATISTIC(NumSimple, "Number of splits that were simple");
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +000037STATISTIC(NumCopies, "Number of copies inserted for splitting");
38STATISTIC(NumRemats, "Number of rematerialized defs for splitting");
Jakob Stoklund Olesen50215af2011-05-10 17:37:41 +000039STATISTIC(NumRepairs, "Number of invalid live ranges repaired");
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +000040
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000041//===----------------------------------------------------------------------===//
Wei Mi35ee9332016-05-11 22:28:29 +000042// Last Insert Point Analysis
43//===----------------------------------------------------------------------===//
44
45InsertPointAnalysis::InsertPointAnalysis(const LiveIntervals &lis,
46 unsigned BBNum)
Wei Mif3c8f532016-05-23 19:39:19 +000047 : LIS(lis), LastInsertPoint(BBNum) {}
Wei Mi35ee9332016-05-11 22:28:29 +000048
49SlotIndex
Wei Mif3c8f532016-05-23 19:39:19 +000050InsertPointAnalysis::computeLastInsertPoint(const LiveInterval &CurLI,
51 const MachineBasicBlock &MBB) {
Wei Mi35ee9332016-05-11 22:28:29 +000052 unsigned Num = MBB.getNumber();
53 std::pair<SlotIndex, SlotIndex> &LIP = LastInsertPoint[Num];
54 SlotIndex MBBEnd = LIS.getMBBEndIdx(&MBB);
55
Hiroshi Inoue713b5ba2017-07-09 05:54:44 +000056 SmallVector<const MachineBasicBlock *, 1> EHPadSuccessors;
Wei Mi35ee9332016-05-11 22:28:29 +000057 for (const MachineBasicBlock *SMBB : MBB.successors())
58 if (SMBB->isEHPad())
Hiroshi Inoue713b5ba2017-07-09 05:54:44 +000059 EHPadSuccessors.push_back(SMBB);
Wei Mi35ee9332016-05-11 22:28:29 +000060
61 // Compute insert points on the first call. The pair is independent of the
62 // current live interval.
63 if (!LIP.first.isValid()) {
64 MachineBasicBlock::const_iterator FirstTerm = MBB.getFirstTerminator();
65 if (FirstTerm == MBB.end())
66 LIP.first = MBBEnd;
67 else
68 LIP.first = LIS.getInstructionIndex(*FirstTerm);
69
70 // If there is a landing pad successor, also find the call instruction.
Hiroshi Inoue713b5ba2017-07-09 05:54:44 +000071 if (EHPadSuccessors.empty())
Wei Mi35ee9332016-05-11 22:28:29 +000072 return LIP.first;
73 // There may not be a call instruction (?) in which case we ignore LPad.
74 LIP.second = LIP.first;
75 for (MachineBasicBlock::const_iterator I = MBB.end(), E = MBB.begin();
76 I != E;) {
77 --I;
78 if (I->isCall()) {
79 LIP.second = LIS.getInstructionIndex(*I);
80 break;
81 }
82 }
83 }
84
85 // If CurLI is live into a landing pad successor, move the last insert point
86 // back to the call that may throw.
87 if (!LIP.second)
88 return LIP.first;
89
Hiroshi Inoue713b5ba2017-07-09 05:54:44 +000090 if (none_of(EHPadSuccessors, [&](const MachineBasicBlock *EHPad) {
Wei Mif3c8f532016-05-23 19:39:19 +000091 return LIS.isLiveInToMBB(CurLI, EHPad);
Wei Mi35ee9332016-05-11 22:28:29 +000092 }))
93 return LIP.first;
94
95 // Find the value leaving MBB.
Wei Mif3c8f532016-05-23 19:39:19 +000096 const VNInfo *VNI = CurLI.getVNInfoBefore(MBBEnd);
Wei Mi35ee9332016-05-11 22:28:29 +000097 if (!VNI)
98 return LIP.first;
99
100 // If the value leaving MBB was defined after the call in MBB, it can't
101 // really be live-in to the landing pad. This can happen if the landing pad
102 // has a PHI, and this register is undef on the exceptional edge.
103 // <rdar://problem/10664933>
104 if (!SlotIndex::isEarlierInstr(VNI->def, LIP.second) && VNI->def < MBBEnd)
105 return LIP.first;
106
107 // Value is properly live-in to the landing pad.
108 // Only allow inserts before the call.
109 return LIP.second;
110}
111
112MachineBasicBlock::iterator
Wei Mif3c8f532016-05-23 19:39:19 +0000113InsertPointAnalysis::getLastInsertPointIter(const LiveInterval &CurLI,
114 MachineBasicBlock &MBB) {
115 SlotIndex LIP = getLastInsertPoint(CurLI, MBB);
Wei Mi35ee9332016-05-11 22:28:29 +0000116 if (LIP == LIS.getMBBEndIdx(&MBB))
117 return MBB.end();
118 return LIS.getInstructionFromIndex(LIP);
119}
120
121//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000122// Split Analysis
123//===----------------------------------------------------------------------===//
124
Eric Christopherd9134482014-08-04 21:25:23 +0000125SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
Jakob Stoklund Olesen0fef9dd2010-07-20 23:50:15 +0000126 const MachineLoopInfo &mli)
Eric Christopherd9134482014-08-04 21:25:23 +0000127 : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli),
Eric Christopherfc6de422014-08-05 02:39:49 +0000128 TII(*MF.getSubtarget().getInstrInfo()), CurLI(nullptr),
Wei Mi35ee9332016-05-11 22:28:29 +0000129 IPA(lis, MF.getNumBlockIDs()) {}
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000130
131void SplitAnalysis::clear() {
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000132 UseSlots.clear();
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000133 UseBlocks.clear();
134 ThroughBlocks.clear();
Craig Topperc0196b12014-04-14 00:51:57 +0000135 CurLI = nullptr;
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000136 DidRepairRange = false;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000137}
138
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000139/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000140void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000141 assert(UseSlots.empty() && "Call clear first");
142
143 // First get all the defs from the interval values. This provides the correct
144 // slots for early clobbers.
Matthias Braun96761952014-12-10 23:07:54 +0000145 for (const VNInfo *VNI : CurLI->valnos)
146 if (!VNI->isPHIDef() && !VNI->isUnused())
147 UseSlots.push_back(VNI->def);
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000148
149 // Get use slots form the use-def chain.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000150 const MachineRegisterInfo &MRI = MF.getRegInfo();
Owen Andersonb36376e2014-03-17 19:36:09 +0000151 for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg))
152 if (!MO.isUndef())
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000153 UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot());
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000154
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000155 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000156
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000157 // Remove duplicates, keeping the smaller slot for each instruction.
158 // That is what we want for early clobbers.
159 UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
160 SlotIndex::isSameInstr),
161 UseSlots.end());
162
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000163 // Compute per-live block info.
164 if (!calcLiveBlockInfo()) {
165 // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
Rafael Espindola676c4052011-06-26 22:34:10 +0000166 // I am looking at you, RegisterCoalescer!
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000167 DidRepairRange = true;
Jakob Stoklund Olesen50215af2011-05-10 17:37:41 +0000168 ++NumRepairs;
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000169 DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
170 const_cast<LiveIntervals&>(LIS)
171 .shrinkToUses(const_cast<LiveInterval*>(CurLI));
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000172 UseBlocks.clear();
173 ThroughBlocks.clear();
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000174 bool fixed = calcLiveBlockInfo();
175 (void)fixed;
176 assert(fixed && "Couldn't fix broken live interval");
177 }
178
Jakob Stoklund Olesenbd6b86e2011-03-27 22:49:23 +0000179 DEBUG(dbgs() << "Analyze counted "
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000180 << UseSlots.size() << " instrs in "
181 << UseBlocks.size() << " blocks, through "
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000182 << NumThroughBlocks << " blocks.\n");
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000183}
184
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000185/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
186/// where CurLI is live.
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000187bool SplitAnalysis::calcLiveBlockInfo() {
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000188 ThroughBlocks.resize(MF.getNumBlockIDs());
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000189 NumThroughBlocks = NumGapBlocks = 0;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000190 if (CurLI->empty())
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000191 return true;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000192
193 LiveInterval::const_iterator LVI = CurLI->begin();
194 LiveInterval::const_iterator LVE = CurLI->end();
195
196 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
197 UseI = UseSlots.begin();
198 UseE = UseSlots.end();
199
200 // Loop over basic blocks where CurLI is live.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000201 MachineFunction::iterator MFI =
202 LIS.getMBBFromIndex(LVI->start)->getIterator();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000203 for (;;) {
204 BlockInfo BI;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000205 BI.MBB = &*MFI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000206 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000207 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000208
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000209 // If the block contains no uses, the range must be live through. At one
Rafael Espindola676c4052011-06-26 22:34:10 +0000210 // point, RegisterCoalescer could create dangling ranges that ended
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000211 // mid-block.
212 if (UseI == UseE || *UseI >= Stop) {
213 ++NumThroughBlocks;
214 ThroughBlocks.set(BI.MBB->getNumber());
215 // The range shouldn't end mid-block if there are no uses. This shouldn't
216 // happen.
217 if (LVI->end < Stop)
218 return false;
219 } else {
220 // This block has uses. Find the first and last uses in the block.
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000221 BI.FirstInstr = *UseI;
222 assert(BI.FirstInstr >= Start);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000223 do ++UseI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000224 while (UseI != UseE && *UseI < Stop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000225 BI.LastInstr = UseI[-1];
226 assert(BI.LastInstr < Stop);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000227
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000228 // LVI is the first live segment overlapping MBB.
229 BI.LiveIn = LVI->start <= Start;
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000230
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000231 // When not live in, the first use should be a def.
232 if (!BI.LiveIn) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000233 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000234 assert(LVI->start == BI.FirstInstr && "First instr should be a def");
235 BI.FirstDef = BI.FirstInstr;
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000236 }
237
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000238 // Look for gaps in the live range.
239 BI.LiveOut = true;
240 while (LVI->end < Stop) {
241 SlotIndex LastStop = LVI->end;
242 if (++LVI == LVE || LVI->start >= Stop) {
243 BI.LiveOut = false;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000244 BI.LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000245 break;
246 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000247
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000248 if (LastStop < LVI->start) {
249 // There is a gap in the live range. Create duplicate entries for the
250 // live-in snippet and the live-out snippet.
251 ++NumGapBlocks;
252
253 // Push the Live-in part.
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000254 BI.LiveOut = false;
255 UseBlocks.push_back(BI);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000256 UseBlocks.back().LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000257
258 // Set up BI for the live-out part.
259 BI.LiveIn = false;
260 BI.LiveOut = true;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000261 BI.FirstInstr = BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000262 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000263
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000264 // A Segment that starts in the middle of the block must be a def.
265 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000266 if (!BI.FirstDef)
267 BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000268 }
269
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000270 UseBlocks.push_back(BI);
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000271
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000272 // LVI is now at LVE or LVI->end >= Stop.
273 if (LVI == LVE)
274 break;
275 }
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000276
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000277 // Live segment ends exactly at Stop. Move to the next segment.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000278 if (LVI->end == Stop && ++LVI == LVE)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000279 break;
280
281 // Pick the next basic block.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000282 if (LVI->start < Stop)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000283 ++MFI;
284 else
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000285 MFI = LIS.getMBBFromIndex(LVI->start)->getIterator();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000286 }
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000287
288 assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000289 return true;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000290}
291
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000292unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
293 if (cli->empty())
294 return 0;
295 LiveInterval *li = const_cast<LiveInterval*>(cli);
296 LiveInterval::iterator LVI = li->begin();
297 LiveInterval::iterator LVE = li->end();
298 unsigned Count = 0;
299
300 // Loop over basic blocks where li is live.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000301 MachineFunction::const_iterator MFI =
302 LIS.getMBBFromIndex(LVI->start)->getIterator();
303 SlotIndex Stop = LIS.getMBBEndIdx(&*MFI);
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000304 for (;;) {
305 ++Count;
306 LVI = li->advanceTo(LVI, Stop);
307 if (LVI == LVE)
308 return Count;
309 do {
310 ++MFI;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000311 Stop = LIS.getMBBEndIdx(&*MFI);
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000312 } while (Stop <= LVI->start);
313 }
314}
315
Jakob Stoklund Olesen60a26a62011-02-21 23:09:46 +0000316bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
317 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
318 const LiveInterval &Orig = LIS.getInterval(OrigReg);
319 assert(!Orig.empty() && "Splitting empty interval?");
320 LiveInterval::const_iterator I = Orig.find(Idx);
321
322 // Range containing Idx should begin at Idx.
323 if (I != Orig.end() && I->start <= Idx)
324 return I->start == Idx;
325
326 // Range does not contain Idx, previous must end at Idx.
327 return I != Orig.begin() && (--I)->end == Idx;
328}
329
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000330void SplitAnalysis::analyze(const LiveInterval *li) {
331 clear();
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000332 CurLI = li;
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000333 analyzeUses();
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000334}
335
Jakob Stoklund Olesen28e769c2010-12-15 17:49:52 +0000336
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000337//===----------------------------------------------------------------------===//
338// Split Editor
339//===----------------------------------------------------------------------===//
340
341/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Wei Mic0223702016-07-08 21:08:09 +0000342SplitEditor::SplitEditor(SplitAnalysis &sa, AliasAnalysis &aa,
343 LiveIntervals &lis, VirtRegMap &vrm,
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000344 MachineDominatorTree &mdt,
345 MachineBlockFrequencyInfo &mbfi)
Wei Mic0223702016-07-08 21:08:09 +0000346 : SA(sa), AA(aa), LIS(lis), VRM(vrm),
347 MRI(vrm.getMachineFunction().getRegInfo()), MDT(mdt),
348 TII(*vrm.getMachineFunction().getSubtarget().getInstrInfo()),
Eric Christopher60621802014-10-14 07:22:00 +0000349 TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()),
Eric Christopherd9134482014-08-04 21:25:23 +0000350 MBFI(mbfi), Edit(nullptr), OpenIdx(0), SpillMode(SM_Partition),
351 RegAssign(Allocator) {}
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000352
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000353void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
354 Edit = &LRE;
355 SpillMode = SM;
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000356 OpenIdx = 0;
357 RegAssign.clear();
358 Values.clear();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000359
360 // Reset the LiveRangeCalc instances needed for this spill mode.
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000361 LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
362 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000363 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000364 LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
365 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000366
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000367 // We don't need an AliasAnalysis since we will only be performing
368 // cheap-as-a-copy remats anyway.
Craig Topperc0196b12014-04-14 00:51:57 +0000369 Edit->anyRematerializable(nullptr);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000370}
371
Manman Ren19f49ac2012-09-11 22:23:19 +0000372#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000373LLVM_DUMP_METHOD void SplitEditor::dump() const {
Eric Christopherede62672011-02-03 06:18:29 +0000374 if (RegAssign.empty()) {
375 dbgs() << " empty\n";
376 return;
377 }
378
379 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
380 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
381 dbgs() << '\n';
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000382}
Manman Ren742534c2012-09-06 19:06:06 +0000383#endif
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000384
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000385LiveInterval::SubRange &SplitEditor::getSubRangeForMask(LaneBitmask LM,
386 LiveInterval &LI) {
387 for (LiveInterval::SubRange &S : LI.subranges())
388 if (S.LaneMask == LM)
389 return S;
390 llvm_unreachable("SubRange for this mask not found");
George Burgess IVb42e0e72016-08-25 02:15:54 +0000391}
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000392
393void SplitEditor::addDeadDef(LiveInterval &LI, VNInfo *VNI, bool Original) {
394 if (!LI.hasSubRanges()) {
395 LI.createDeadDef(VNI);
396 return;
397 }
398
399 SlotIndex Def = VNI->def;
400 if (Original) {
401 // If we are transferring a def from the original interval, make sure
402 // to only update the subranges for which the original subranges had
403 // a def at this location.
404 for (LiveInterval::SubRange &S : LI.subranges()) {
405 auto &PS = getSubRangeForMask(S.LaneMask, Edit->getParent());
406 VNInfo *PV = PS.getVNInfoAt(Def);
407 if (PV != nullptr && PV->def == Def)
408 S.createDeadDef(Def, LIS.getVNInfoAllocator());
409 }
410 } else {
411 // This is a new def: either from rematerialization, or from an inserted
412 // copy. Since rematerialization can regenerate a definition of a sub-
413 // register, we need to check which subranges need to be updated.
414 const MachineInstr *DefMI = LIS.getInstructionFromIndex(Def);
415 assert(DefMI != nullptr);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000416 LaneBitmask LM;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000417 for (const MachineOperand &DefOp : DefMI->defs()) {
418 unsigned R = DefOp.getReg();
419 if (R != LI.reg)
420 continue;
421 if (unsigned SR = DefOp.getSubReg())
422 LM |= TRI.getSubRegIndexLaneMask(SR);
423 else {
424 LM = MRI.getMaxLaneMaskForVReg(R);
425 break;
426 }
427 }
428 for (LiveInterval::SubRange &S : LI.subranges())
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000429 if ((S.LaneMask & LM).any())
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000430 S.createDeadDef(Def, LIS.getVNInfoAllocator());
431 }
432}
433
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000434VNInfo *SplitEditor::defValue(unsigned RegIdx,
435 const VNInfo *ParentVNI,
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000436 SlotIndex Idx,
437 bool Original) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000438 assert(ParentVNI && "Mapping NULL value");
439 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000440 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
Mark Laceyf9ea8852013-08-14 23:50:04 +0000441 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000442
443 // Create a new value.
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000444 VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000445
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000446 bool Force = LI->hasSubRanges();
447 ValueForcePair FP(Force ? nullptr : VNI, Force);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000448 // Use insert for lookup, so we can add missing values with a second lookup.
449 std::pair<ValueMap::iterator, bool> InsP =
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000450 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), FP));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000451
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000452 // This was the first time (RegIdx, ParentVNI) was mapped, and it is not
453 // forced. Keep it as a simple def without any liveness.
454 if (!Force && InsP.second)
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000455 return VNI;
456
457 // If the previous value was a simple mapping, add liveness for it now.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000458 if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000459 addDeadDef(*LI, OldVNI, Original);
460
461 // No longer a simple mapping. Switch to a complex mapping. If the
462 // interval has subranges, make it a forced mapping.
463 InsP.first->second = ValueForcePair(nullptr, Force);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000464 }
465
466 // This is a complex mapping, add liveness for VNI
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000467 addDeadDef(*LI, VNI, Original);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000468 return VNI;
469}
470
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000471void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000472 assert(ParentVNI && "Mapping NULL value");
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000473 ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
474 VNInfo *VNI = VFP.getPointer();
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000475
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000476 // ParentVNI was either unmapped or already complex mapped. Either way, just
477 // set the force bit.
478 if (!VNI) {
479 VFP.setInt(true);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000480 return;
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000481 }
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000482
483 // This was previously a single mapping. Make sure the old def is represented
484 // by a trivial live range.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000485 addDeadDef(LIS.getInterval(Edit->get(RegIdx)), VNI, false);
486
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000487 // Mark as complex mapped, forced.
Craig Topperc0196b12014-04-14 00:51:57 +0000488 VFP = ValueForcePair(nullptr, true);
Jakob Stoklund Olesen4484f992011-09-13 18:05:29 +0000489}
490
Matthias Braunf0b68d32017-03-17 00:41:39 +0000491SlotIndex SplitEditor::buildSingleSubRegCopy(unsigned FromReg, unsigned ToReg,
492 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
493 unsigned SubIdx, LiveInterval &DestLI, bool Late, SlotIndex Def) {
494 const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
495 bool FirstCopy = !Def.isValid();
496 MachineInstr *CopyMI = BuildMI(MBB, InsertBefore, DebugLoc(), Desc)
497 .addReg(ToReg, RegState::Define | getUndefRegState(FirstCopy)
498 | getInternalReadRegState(!FirstCopy), SubIdx)
499 .addReg(FromReg, 0, SubIdx);
500
501 BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator();
502 if (FirstCopy) {
503 SlotIndexes &Indexes = *LIS.getSlotIndexes();
504 Def = Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
Matthias Braunf0b68d32017-03-17 00:41:39 +0000505 } else {
506 CopyMI->bundleWithPred();
507 }
508 LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubIdx);
509 DestLI.refineSubRanges(Allocator, LaneMask,
510 [Def, &Allocator](LiveInterval::SubRange& SR) {
511 SR.createDeadDef(Def, Allocator);
512 });
513 return Def;
514}
515
516SlotIndex SplitEditor::buildCopy(unsigned FromReg, unsigned ToReg,
517 LaneBitmask LaneMask, MachineBasicBlock &MBB,
518 MachineBasicBlock::iterator InsertBefore, bool Late, unsigned RegIdx) {
519 const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
520 if (LaneMask.all() || LaneMask == MRI.getMaxLaneMaskForVReg(FromReg)) {
521 // The full vreg is copied.
522 MachineInstr *CopyMI =
523 BuildMI(MBB, InsertBefore, DebugLoc(), Desc, ToReg).addReg(FromReg);
524 SlotIndexes &Indexes = *LIS.getSlotIndexes();
525 return Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
526 }
527
528 // Only a subset of lanes needs to be copied. The following is a simple
529 // heuristic to construct a sequence of COPYs. We could add a target
530 // specific callback if this turns out to be suboptimal.
531 LiveInterval &DestLI = LIS.getInterval(Edit->get(RegIdx));
532
533 // First pass: Try to find a perfectly matching subregister index. If none
534 // exists find the one covering the most lanemask bits.
535 SmallVector<unsigned, 8> PossibleIndexes;
536 unsigned BestIdx = 0;
537 unsigned BestCover = 0;
538 const TargetRegisterClass *RC = MRI.getRegClass(FromReg);
539 assert(RC == MRI.getRegClass(ToReg) && "Should have same reg class");
540 for (unsigned Idx = 1, E = TRI.getNumSubRegIndices(); Idx < E; ++Idx) {
541 // Is this index even compatible with the given class?
542 if (TRI.getSubClassWithSubReg(RC, Idx) != RC)
543 continue;
544 LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(Idx);
545 // Early exit if we found a perfect match.
546 if (SubRegMask == LaneMask) {
547 BestIdx = Idx;
548 break;
549 }
550
551 // The index must not cover any lanes outside \p LaneMask.
552 if ((SubRegMask & ~LaneMask).any())
553 continue;
554
555 unsigned PopCount = countPopulation(SubRegMask.getAsInteger());
556 PossibleIndexes.push_back(Idx);
557 if (PopCount > BestCover) {
558 BestCover = PopCount;
559 BestIdx = Idx;
560 }
561 }
562
563 // Abort if we cannot possibly implement the COPY with the given indexes.
564 if (BestIdx == 0)
565 report_fatal_error("Impossible to implement partial COPY");
566
567 SlotIndex Def = buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore,
568 BestIdx, DestLI, Late, SlotIndex());
569
570 // Greedy heuristic: Keep iterating keeping the best covering subreg index
571 // each time.
Matthias Braun76f06302017-06-12 20:30:52 +0000572 LaneBitmask LanesLeft = LaneMask & ~(TRI.getSubRegIndexLaneMask(BestIdx));
Matthias Braunf0b68d32017-03-17 00:41:39 +0000573 while (LanesLeft.any()) {
574 unsigned BestIdx = 0;
575 int BestCover = INT_MIN;
576 for (unsigned Idx : PossibleIndexes) {
577 LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(Idx);
578 // Early exit if we found a perfect match.
579 if (SubRegMask == LanesLeft) {
580 BestIdx = Idx;
581 break;
582 }
583
584 // Try to cover as much of the remaining lanes as possible but
585 // as few of the already covered lanes as possible.
586 int Cover = countPopulation((SubRegMask & LanesLeft).getAsInteger())
587 - countPopulation((SubRegMask & ~LanesLeft).getAsInteger());
588 if (Cover > BestCover) {
589 BestCover = Cover;
590 BestIdx = Idx;
591 }
592 }
593
594 if (BestIdx == 0)
595 report_fatal_error("Impossible to implement partial COPY");
596
597 buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore, BestIdx,
598 DestLI, Late, Def);
599 LanesLeft &= ~TRI.getSubRegIndexLaneMask(BestIdx);
600 }
601
602 return Def;
603}
604
Eric Christopherede62672011-02-03 06:18:29 +0000605VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000606 VNInfo *ParentVNI,
607 SlotIndex UseIdx,
608 MachineBasicBlock &MBB,
609 MachineBasicBlock::iterator I) {
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000610 SlotIndex Def;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000611 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000612
Jakob Stoklund Olesen7d406792011-05-02 05:29:58 +0000613 // We may be trying to avoid interference that ends at a deleted instruction,
614 // so always begin RegIdx 0 early and all others late.
615 bool Late = RegIdx != 0;
616
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000617 // Attempt cheap-as-a-copy rematerialization.
Wei Mi9a16d652016-04-13 03:08:27 +0000618 unsigned Original = VRM.getOriginal(Edit->get(RegIdx));
619 LiveInterval &OrigLI = LIS.getInterval(Original);
620 VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
Wei Mi9a16d652016-04-13 03:08:27 +0000621
Matthias Braunf0b68d32017-03-17 00:41:39 +0000622 unsigned Reg = LI->reg;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000623 bool DidRemat = false;
624 if (OrigVNI) {
625 LiveRangeEdit::Remat RM(ParentVNI);
626 RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
627 if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) {
Matthias Braunf0b68d32017-03-17 00:41:39 +0000628 Def = Edit->rematerializeAt(MBB, I, Reg, RM, TRI, Late);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000629 ++NumRemats;
630 DidRemat = true;
631 }
632 }
633 if (!DidRemat) {
Matthias Braunf0b68d32017-03-17 00:41:39 +0000634 LaneBitmask LaneMask;
Stanislav Mekhanoshin70c245e2017-02-01 01:18:36 +0000635 if (LI->hasSubRanges()) {
Matthias Braunf0b68d32017-03-17 00:41:39 +0000636 LaneMask = LaneBitmask::getNone();
Stanislav Mekhanoshin70c245e2017-02-01 01:18:36 +0000637 for (LiveInterval::SubRange &S : LI->subranges())
Matthias Braunf0b68d32017-03-17 00:41:39 +0000638 LaneMask |= S.LaneMask;
639 } else {
640 LaneMask = LaneBitmask::getAll();
Stanislav Mekhanoshin70c245e2017-02-01 01:18:36 +0000641 }
Matthias Braunf0b68d32017-03-17 00:41:39 +0000642
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000643 ++NumCopies;
Matthias Braunf0b68d32017-03-17 00:41:39 +0000644 Def = buildCopy(Edit->getReg(), Reg, LaneMask, MBB, I, Late, RegIdx);
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000645 }
646
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000647 // Define the value in Reg.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000648 return defValue(RegIdx, ParentVNI, Def, false);
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000649}
650
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000651/// Create a new virtual register and live interval.
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000652unsigned SplitEditor::openIntv() {
Eric Christopherede62672011-02-03 06:18:29 +0000653 // Create the complement as index 0.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000654 if (Edit->empty())
Mark Lacey9d8103d2013-08-14 23:50:16 +0000655 Edit->createEmptyInterval();
Eric Christopherede62672011-02-03 06:18:29 +0000656
657 // Create the open interval.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000658 OpenIdx = Edit->size();
Mark Lacey9d8103d2013-08-14 23:50:16 +0000659 Edit->createEmptyInterval();
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000660 return OpenIdx;
661}
662
663void SplitEditor::selectIntv(unsigned Idx) {
664 assert(Idx != 0 && "Cannot select the complement interval");
665 assert(Idx < Edit->size() && "Can only select previously opened interval");
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000666 DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000667 OpenIdx = Idx;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000668}
669
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000670SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000671 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000672 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000673 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000674 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000675 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000676 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000677 return Idx;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000678 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000679 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000680 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000681 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000682
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000683 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
684 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000685}
686
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000687SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
688 assert(OpenIdx && "openIntv not called before enterIntvAfter");
689 DEBUG(dbgs() << " enterIntvAfter " << Idx);
690 Idx = Idx.getBoundaryIndex();
691 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
692 if (!ParentVNI) {
693 DEBUG(dbgs() << ": not live\n");
694 return Idx;
695 }
696 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
697 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
698 assert(MI && "enterIntvAfter called with invalid index");
699
700 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000701 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000702 return VNI->def;
703}
704
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000705SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000706 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000707 SlotIndex End = LIS.getMBBEndIdx(&MBB);
708 SlotIndex Last = End.getPrevSlot();
709 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000710 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000711 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000712 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000713 return End;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000714 }
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000715 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000716 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000717 SA.getLastSplitPointIter(&MBB));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000718 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopherede62672011-02-03 06:18:29 +0000719 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000720 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000721}
722
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000723/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000724void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000725 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000726}
727
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000728void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopherede62672011-02-03 06:18:29 +0000729 assert(OpenIdx && "openIntv not called before useIntv");
730 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
731 RegAssign.insert(Start, End, OpenIdx);
732 DEBUG(dump());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000733}
734
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000735SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000736 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000737 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000738
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000739 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000740 SlotIndex Boundary = Idx.getBoundaryIndex();
741 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000742 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000743 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000744 return Boundary.getNextSlot();
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000745 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000746 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000747 MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
Jakob Stoklund Olesen3d11c8e2011-02-08 18:50:18 +0000748 assert(MI && "No instruction at index");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000749
750 // In spill mode, make live ranges as short as possible by inserting the copy
751 // before MI. This is only possible if that instruction doesn't redefine the
752 // value. The inserted COPY is not a kill, and we don't need to recompute
753 // the source live range. The spiller also won't try to hoist this copy.
754 if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
755 MI->readsVirtualRegister(Edit->getReg())) {
756 forceRecompute(0, ParentVNI);
757 defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
758 return Idx;
759 }
760
761 VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000762 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000763 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000764}
765
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000766SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
767 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
768 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
769
770 // The interval must be live into the instruction at Idx.
Jakob Stoklund Olesenc45d38e2011-07-18 18:47:13 +0000771 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000772 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000773 if (!ParentVNI) {
774 DEBUG(dbgs() << ": not live\n");
775 return Idx.getNextSlot();
776 }
777 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
778
779 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
780 assert(MI && "No instruction at index");
781 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
782 return VNI->def;
783}
784
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000785SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000786 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000787 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000788 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000789
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000790 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000791 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000792 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000793 return Start;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000794 }
795
Eric Christopherede62672011-02-03 06:18:29 +0000796 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Keith Walker830a8c12016-09-16 14:07:29 +0000797 MBB.SkipPHIsLabelsAndDebug(MBB.begin()));
Eric Christopherede62672011-02-03 06:18:29 +0000798 RegAssign.insert(Start, VNI->def, OpenIdx);
799 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000800 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000801}
802
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000803void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
804 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000805 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesend7bcf432011-11-14 01:39:36 +0000806 assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000807 "Parent changes value in extended range");
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000808 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
809 "Range cannot span basic blocks");
810
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000811 // The complement interval will be extended as needed by LRCalc.extend().
Jakob Stoklund Olesen5c482cd2011-04-05 23:43:14 +0000812 if (ParentVNI)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000813 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000814 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
815 RegAssign.insert(Start, End, OpenIdx);
816 DEBUG(dump());
817}
818
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000819//===----------------------------------------------------------------------===//
820// Spill modes
821//===----------------------------------------------------------------------===//
822
823void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
Mark Laceyf9ea8852013-08-14 23:50:04 +0000824 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000825 DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
826 RegAssignMap::iterator AssignI;
827 AssignI.setMap(RegAssign);
828
829 for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
Matthias Braun311730a2015-01-21 19:02:30 +0000830 SlotIndex Def = Copies[i]->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000831 MachineInstr *MI = LIS.getInstructionFromIndex(Def);
832 assert(MI && "No instruction for back-copy");
833
834 MachineBasicBlock *MBB = MI->getParent();
835 MachineBasicBlock::iterator MBBI(MI);
836 bool AtBegin;
837 do AtBegin = MBBI == MBB->begin();
838 while (!AtBegin && (--MBBI)->isDebugValue());
839
840 DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
Matthias Braun311730a2015-01-21 19:02:30 +0000841 LIS.removeVRegDefAt(*LI, Def);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000842 LIS.RemoveMachineInstrFromMaps(*MI);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000843 MI->eraseFromParent();
844
Matthias Braun311730a2015-01-21 19:02:30 +0000845 // Adjust RegAssign if a register assignment is killed at Def. We want to
846 // avoid calculating the live range of the source register if possible.
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000847 AssignI.find(Def.getPrevSlot());
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000848 if (!AssignI.valid() || AssignI.start() >= Def)
849 continue;
850 // If MI doesn't kill the assigned register, just leave it.
851 if (AssignI.stop() != Def)
852 continue;
853 unsigned RegIdx = AssignI.value();
854 if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
855 DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n');
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000856 forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000857 } else {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000858 SlotIndex Kill = LIS.getInstructionIndex(*MBBI).getRegSlot();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000859 DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
860 AssignI.setStop(Kill);
861 }
862 }
863}
864
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000865MachineBasicBlock*
866SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
867 MachineBasicBlock *DefMBB) {
868 if (MBB == DefMBB)
869 return MBB;
870 assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
871
872 const MachineLoopInfo &Loops = SA.Loops;
873 const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
874 MachineDomTreeNode *DefDomNode = MDT[DefMBB];
875
876 // Best candidate so far.
877 MachineBasicBlock *BestMBB = MBB;
878 unsigned BestDepth = UINT_MAX;
879
880 for (;;) {
881 const MachineLoop *Loop = Loops.getLoopFor(MBB);
882
883 // MBB isn't in a loop, it doesn't get any better. All dominators have a
884 // higher frequency by definition.
885 if (!Loop) {
886 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
887 << MBB->getNumber() << " at depth 0\n");
888 return MBB;
889 }
890
891 // We'll never be able to exit the DefLoop.
892 if (Loop == DefLoop) {
893 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
894 << MBB->getNumber() << " in the same loop\n");
895 return MBB;
896 }
897
898 // Least busy dominator seen so far.
899 unsigned Depth = Loop->getLoopDepth();
900 if (Depth < BestDepth) {
901 BestMBB = MBB;
902 BestDepth = Depth;
903 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
904 << MBB->getNumber() << " at depth " << Depth << '\n');
905 }
906
907 // Leave loop by going to the immediate dominator of the loop header.
908 // This is a bigger stride than simply walking up the dominator tree.
909 MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
910
911 // Too far up the dominator tree?
912 if (!IDom || !MDT.dominates(DefDomNode, IDom))
913 return BestMBB;
914
915 MBB = IDom->getBlock();
916 }
917}
918
Wei Mi9a16d652016-04-13 03:08:27 +0000919void SplitEditor::computeRedundantBackCopies(
920 DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) {
921 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
922 LiveInterval *Parent = &Edit->getParent();
923 SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums());
924 SmallPtrSet<VNInfo *, 8> DominatedVNIs;
925
926 // Aggregate VNIs having the same value as ParentVNI.
927 for (VNInfo *VNI : LI->valnos) {
928 if (VNI->isUnused())
929 continue;
930 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
931 EqualVNs[ParentVNI->id].insert(VNI);
932 }
933
934 // For VNI aggregation of each ParentVNI, collect dominated, i.e.,
935 // redundant VNIs to BackCopies.
936 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
937 VNInfo *ParentVNI = Parent->getValNumInfo(i);
938 if (!NotToHoistSet.count(ParentVNI->id))
939 continue;
940 SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin();
941 SmallPtrSetIterator<VNInfo *> It2 = It1;
942 for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) {
943 It2 = It1;
944 for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) {
945 if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2))
946 continue;
947
948 MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def);
949 MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def);
950 if (MBB1 == MBB2) {
951 DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1));
952 } else if (MDT.dominates(MBB1, MBB2)) {
953 DominatedVNIs.insert(*It2);
954 } else if (MDT.dominates(MBB2, MBB1)) {
955 DominatedVNIs.insert(*It1);
956 }
957 }
958 }
959 if (!DominatedVNIs.empty()) {
960 forceRecompute(0, ParentVNI);
961 for (auto VNI : DominatedVNIs) {
962 BackCopies.push_back(VNI);
963 }
964 DominatedVNIs.clear();
965 }
966 }
967}
968
969/// For SM_Size mode, find a common dominator for all the back-copies for
970/// the same ParentVNI and hoist the backcopies to the dominator BB.
971/// For SM_Speed mode, if the common dominator is hot and it is not beneficial
972/// to do the hoisting, simply remove the dominated backcopies for the same
973/// ParentVNI.
974void SplitEditor::hoistCopies() {
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000975 // Get the complement interval, always RegIdx 0.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000976 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000977 LiveInterval *Parent = &Edit->getParent();
978
979 // Track the nearest common dominator for all back-copies for each ParentVNI,
980 // indexed by ParentVNI->id.
981 typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
982 SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
Wei Mi9a16d652016-04-13 03:08:27 +0000983 // The total cost of all the back-copies for each ParentVNI.
984 SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums());
985 // The ParentVNI->id set for which hoisting back-copies are not beneficial
986 // for Speed.
987 DenseSet<unsigned> NotToHoistSet;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000988
989 // Find the nearest common dominator for parent values with multiple
990 // back-copies. If a single back-copy dominates, put it in DomPair.second.
Matthias Braun96761952014-12-10 23:07:54 +0000991 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000992 if (VNI->isUnused())
993 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000994 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
995 assert(ParentVNI && "Parent not live at complement def");
996
997 // Don't hoist remats. The complement is probably going to disappear
998 // completely anyway.
999 if (Edit->didRematerialize(ParentVNI))
1000 continue;
1001
1002 MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
Wei Mi9a16d652016-04-13 03:08:27 +00001003
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001004 DomPair &Dom = NearestDom[ParentVNI->id];
1005
1006 // Keep directly defined parent values. This is either a PHI or an
1007 // instruction in the complement range. All other copies of ParentVNI
1008 // should be eliminated.
1009 if (VNI->def == ParentVNI->def) {
1010 DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
1011 Dom = DomPair(ValMBB, VNI->def);
1012 continue;
1013 }
1014 // Skip the singly mapped values. There is nothing to gain from hoisting a
1015 // single back-copy.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001016 if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001017 DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
1018 continue;
1019 }
1020
1021 if (!Dom.first) {
1022 // First time we see ParentVNI. VNI dominates itself.
1023 Dom = DomPair(ValMBB, VNI->def);
1024 } else if (Dom.first == ValMBB) {
1025 // Two defs in the same block. Pick the earlier def.
1026 if (!Dom.second.isValid() || VNI->def < Dom.second)
1027 Dom.second = VNI->def;
1028 } else {
1029 // Different basic blocks. Check if one dominates.
1030 MachineBasicBlock *Near =
1031 MDT.findNearestCommonDominator(Dom.first, ValMBB);
1032 if (Near == ValMBB)
1033 // Def ValMBB dominates.
1034 Dom = DomPair(ValMBB, VNI->def);
1035 else if (Near != Dom.first)
1036 // None dominate. Hoist to common dominator, need new def.
1037 Dom = DomPair(Near, SlotIndex());
Wei Mi9a16d652016-04-13 03:08:27 +00001038 Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001039 }
1040
1041 DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
1042 << " for parent " << ParentVNI->id << '@' << ParentVNI->def
1043 << " hoist to BB#" << Dom.first->getNumber() << ' '
1044 << Dom.second << '\n');
1045 }
1046
1047 // Insert the hoisted copies.
1048 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
1049 DomPair &Dom = NearestDom[i];
1050 if (!Dom.first || Dom.second.isValid())
1051 continue;
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +00001052 // This value needs a hoisted copy inserted at the end of Dom.first.
1053 VNInfo *ParentVNI = Parent->getValNumInfo(i);
1054 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
1055 // Get a less loopy dominator than Dom.first.
1056 Dom.first = findShallowDominator(Dom.first, DefMBB);
Wei Mi9a16d652016-04-13 03:08:27 +00001057 if (SpillMode == SM_Speed &&
1058 MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) {
1059 NotToHoistSet.insert(ParentVNI->id);
1060 continue;
1061 }
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001062 SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
1063 Dom.second =
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +00001064 defFromParent(0, ParentVNI, Last, *Dom.first,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +00001065 SA.getLastSplitPointIter(Dom.first))->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001066 }
1067
1068 // Remove redundant back-copies that are now known to be dominated by another
1069 // def with the same value.
1070 SmallVector<VNInfo*, 8> BackCopies;
Matthias Braun96761952014-12-10 23:07:54 +00001071 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +00001072 if (VNI->isUnused())
1073 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001074 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
1075 const DomPair &Dom = NearestDom[ParentVNI->id];
Wei Mi9a16d652016-04-13 03:08:27 +00001076 if (!Dom.first || Dom.second == VNI->def ||
1077 NotToHoistSet.count(ParentVNI->id))
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001078 continue;
1079 BackCopies.push_back(VNI);
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001080 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001081 }
Wei Mi9a16d652016-04-13 03:08:27 +00001082
1083 // If it is not beneficial to hoist all the BackCopies, simply remove
1084 // redundant BackCopies in speed mode.
1085 if (SpillMode == SM_Speed && !NotToHoistSet.empty())
1086 computeRedundantBackCopies(NotToHoistSet, BackCopies);
1087
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001088 removeBackCopies(BackCopies);
1089}
1090
1091
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001092/// transferValues - Transfer all possible values to the new live ranges.
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001093/// Values that were rematerialized are left alone, they need LRCalc.extend().
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001094bool SplitEditor::transferValues() {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001095 bool Skipped = false;
1096 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Matthias Braun96761952014-12-10 23:07:54 +00001097 for (const LiveRange::Segment &S : Edit->getParent()) {
1098 DEBUG(dbgs() << " blit " << S << ':');
1099 VNInfo *ParentVNI = S.valno;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001100 // RegAssign has holes where RegIdx 0 should be used.
Matthias Braun96761952014-12-10 23:07:54 +00001101 SlotIndex Start = S.start;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001102 AssignI.advanceTo(Start);
1103 do {
1104 unsigned RegIdx;
Matthias Braun96761952014-12-10 23:07:54 +00001105 SlotIndex End = S.end;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001106 if (!AssignI.valid()) {
1107 RegIdx = 0;
1108 } else if (AssignI.start() <= Start) {
1109 RegIdx = AssignI.value();
1110 if (AssignI.stop() < End) {
1111 End = AssignI.stop();
1112 ++AssignI;
1113 }
1114 } else {
1115 RegIdx = 0;
1116 End = std::min(End, AssignI.start());
1117 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001118
1119 // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001120 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx
1121 << '(' << PrintReg(Edit->get(RegIdx)) << ')');
1122 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001123
1124 // Check for a simply defined value that can be blitted directly.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001125 ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
1126 if (VNInfo *VNI = VFP.getPointer()) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001127 DEBUG(dbgs() << ':' << VNI->id);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001128 LI.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001129 Start = End;
1130 continue;
1131 }
1132
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001133 // Skip values with forced recomputation.
1134 if (VFP.getInt()) {
1135 DEBUG(dbgs() << "(recalc)");
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001136 Skipped = true;
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001137 Start = End;
1138 continue;
1139 }
1140
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +00001141 LiveRangeCalc &LRC = getLRCalc(RegIdx);
1142
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001143 // This value has multiple defs in RegIdx, but it wasn't rematerialized,
1144 // so the live range is accurate. Add live-in blocks in [Start;End) to the
1145 // LiveInBlocks.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001146 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001147 SlotIndex BlockStart, BlockEnd;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001148 std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001149
1150 // The first block may be live-in, or it may have its own def.
1151 if (Start != BlockStart) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001152 VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001153 assert(VNI && "Missing def for complex mapped value");
1154 DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
1155 // MBB has its own def. Is it also live-out?
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001156 if (BlockEnd <= End)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001157 LRC.setLiveOutValue(&*MBB, VNI);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001158
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001159 // Skip to the next block for live-in.
1160 ++MBB;
1161 BlockStart = BlockEnd;
1162 }
1163
1164 // Handle the live-in blocks covered by [Start;End).
1165 assert(Start <= BlockStart && "Expected live-in block");
1166 while (BlockStart < End) {
1167 DEBUG(dbgs() << ">BB#" << MBB->getNumber());
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001168 BlockEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001169 if (BlockStart == ParentVNI->def) {
1170 // This block has the def of a parent PHI, so it isn't live-in.
1171 assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001172 VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001173 assert(VNI && "Missing def for complex mapped parent PHI");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001174 if (End >= BlockEnd)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001175 LRC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001176 } else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001177 // This block needs a live-in value. The last block covered may not
1178 // be live-out.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001179 if (End < BlockEnd)
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001180 LRC.addLiveInBlock(LI, MDT[&*MBB], End);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001181 else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001182 // Live-through, and we don't know the value.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001183 LRC.addLiveInBlock(LI, MDT[&*MBB]);
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001184 LRC.setLiveOutValue(&*MBB, nullptr);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001185 }
1186 }
1187 BlockStart = BlockEnd;
1188 ++MBB;
1189 }
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001190 Start = End;
Matthias Braun96761952014-12-10 23:07:54 +00001191 } while (Start != S.end);
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001192 DEBUG(dbgs() << '\n');
1193 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001194
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +00001195 LRCalc[0].calculateValues();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +00001196 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +00001197 LRCalc[1].calculateValues();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001198
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001199 return Skipped;
1200}
1201
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001202static bool removeDeadSegment(SlotIndex Def, LiveRange &LR) {
1203 const LiveRange::Segment *Seg = LR.getSegmentContaining(Def);
1204 if (Seg == nullptr)
1205 return true;
1206 if (Seg->end != Def.getDeadSlot())
1207 return false;
1208 // This is a dead PHI. Remove it.
1209 LR.removeSegment(*Seg, true);
1210 return true;
1211}
1212
1213void SplitEditor::extendPHIRange(MachineBasicBlock &B, LiveRangeCalc &LRC,
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +00001214 LiveRange &LR, LaneBitmask LM,
1215 ArrayRef<SlotIndex> Undefs) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001216 for (MachineBasicBlock *P : B.predecessors()) {
1217 SlotIndex End = LIS.getMBBEndIdx(P);
1218 SlotIndex LastUse = End.getPrevSlot();
1219 // The predecessor may not have a live-out value. That is OK, like an
1220 // undef PHI operand.
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +00001221 LiveInterval &PLI = Edit->getParent();
1222 // Need the cast because the inputs to ?: would otherwise be deemed
1223 // "incompatible": SubRange vs LiveInterval.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001224 LiveRange &PSR = !LM.all() ? getSubRangeForMask(LM, PLI)
1225 : static_cast<LiveRange&>(PLI);
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +00001226 if (PSR.liveAt(LastUse))
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001227 LRC.extend(LR, End, /*PhysReg=*/0, Undefs);
1228 }
1229}
1230
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001231void SplitEditor::extendPHIKillRanges() {
Wei Mi9a16d652016-04-13 03:08:27 +00001232 // Extend live ranges to be live-out for successor PHI values.
Wei Mi9a16d652016-04-13 03:08:27 +00001233
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001234 // Visit each PHI def slot in the parent live interval. If the def is dead,
1235 // remove it. Otherwise, extend the live interval to reach the end indexes
1236 // of all predecessor blocks.
Wei Mi9a16d652016-04-13 03:08:27 +00001237
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001238 LiveInterval &ParentLI = Edit->getParent();
1239 for (const VNInfo *V : ParentLI.valnos) {
1240 if (V->isUnused() || !V->isPHIDef())
1241 continue;
1242
1243 unsigned RegIdx = RegAssign.lookup(V->def);
1244 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001245 LiveRangeCalc &LRC = getLRCalc(RegIdx);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001246 MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
1247 if (!removeDeadSegment(V->def, LI))
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001248 extendPHIRange(B, LRC, LI, LaneBitmask::getAll(), /*Undefs=*/{});
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001249 }
1250
1251 SmallVector<SlotIndex, 4> Undefs;
1252 LiveRangeCalc SubLRC;
1253
1254 for (LiveInterval::SubRange &PS : ParentLI.subranges()) {
1255 for (const VNInfo *V : PS.valnos) {
1256 if (V->isUnused() || !V->isPHIDef())
1257 continue;
1258 unsigned RegIdx = RegAssign.lookup(V->def);
1259 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1260 LiveInterval::SubRange &S = getSubRangeForMask(PS.LaneMask, LI);
1261 if (removeDeadSegment(V->def, S))
1262 continue;
1263
1264 MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
1265 SubLRC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
1266 &LIS.getVNInfoAllocator());
1267 Undefs.clear();
1268 LI.computeSubRangeUndefs(Undefs, PS.LaneMask, MRI, *LIS.getSlotIndexes());
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +00001269 extendPHIRange(B, SubLRC, S, PS.LaneMask, Undefs);
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001270 }
1271 }
1272}
1273
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001274/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001275void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001276 struct ExtPoint {
1277 ExtPoint(const MachineOperand &O, unsigned R, SlotIndex N)
1278 : MO(O), RegIdx(R), Next(N) {}
1279 MachineOperand MO;
1280 unsigned RegIdx;
1281 SlotIndex Next;
1282 };
1283
1284 SmallVector<ExtPoint,4> ExtPoints;
1285
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001286 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +00001287 RE = MRI.reg_end(); RI != RE;) {
Owen Anderson16c6bf42014-03-13 23:12:04 +00001288 MachineOperand &MO = *RI;
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001289 MachineInstr *MI = MO.getParent();
1290 ++RI;
Eric Christopherede62672011-02-03 06:18:29 +00001291 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001292 if (MI->isDebugValue()) {
1293 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001294 MO.setReg(0);
1295 continue;
1296 }
Jakob Stoklund Olesenf6e03942011-02-09 21:52:09 +00001297
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +00001298 // <undef> operands don't really read the register, so it doesn't matter
1299 // which register we choose. When the use operand is tied to a def, we must
1300 // use the same register as the def, so just do that always.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001301 SlotIndex Idx = LIS.getInstructionIndex(*MI);
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +00001302 if (MO.isDef() || MO.isUndef())
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +00001303 Idx = Idx.getRegSlot(MO.isEarlyClobber());
Eric Christopherede62672011-02-03 06:18:29 +00001304
1305 // Rewrite to the mapped register at Idx.
1306 unsigned RegIdx = RegAssign.lookup(Idx);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001307 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1308 MO.setReg(LI.reg);
Eric Christopherede62672011-02-03 06:18:29 +00001309 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
1310 << Idx << ':' << RegIdx << '\t' << *MI);
1311
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001312 // Extend liveness to Idx if the instruction reads reg.
Jakob Stoklund Olesen73a9eb92011-07-24 20:33:23 +00001313 if (!ExtendRanges || MO.isUndef())
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001314 continue;
1315
1316 // Skip instructions that don't read Reg.
1317 if (MO.isDef()) {
1318 if (!MO.getSubReg() && !MO.isEarlyClobber())
1319 continue;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001320 // We may want to extend a live range for a partial redef, or for a use
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001321 // tied to an early clobber.
1322 Idx = Idx.getPrevSlot();
1323 if (!Edit->getParent().liveAt(Idx))
1324 continue;
1325 } else
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +00001326 Idx = Idx.getRegSlot(true);
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001327
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001328 SlotIndex Next = Idx.getNextSlot();
1329 if (LI.hasSubRanges()) {
1330 // We have to delay extending subranges until we have seen all operands
1331 // defining the register. This is because a <def,read-undef> operand
1332 // will create an "undef" point, and we cannot extend any subranges
1333 // until all of them have been accounted for.
Krzysztof Parzyszek3bf4aec2016-09-02 19:48:55 +00001334 if (MO.isUse())
1335 ExtPoints.push_back(ExtPoint(MO, RegIdx, Next));
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001336 } else {
1337 LiveRangeCalc &LRC = getLRCalc(RegIdx);
1338 LRC.extend(LI, Next, 0, ArrayRef<SlotIndex>());
1339 }
1340 }
1341
1342 for (ExtPoint &EP : ExtPoints) {
1343 LiveInterval &LI = LIS.getInterval(Edit->get(EP.RegIdx));
1344 assert(LI.hasSubRanges());
1345
1346 LiveRangeCalc SubLRC;
1347 unsigned Reg = EP.MO.getReg(), Sub = EP.MO.getSubReg();
1348 LaneBitmask LM = Sub != 0 ? TRI.getSubRegIndexLaneMask(Sub)
1349 : MRI.getMaxLaneMaskForVReg(Reg);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001350 for (LiveInterval::SubRange &S : LI.subranges()) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001351 if ((S.LaneMask & LM).none())
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001352 continue;
1353 // The problem here can be that the new register may have been created
1354 // for a partially defined original register. For example:
1355 // %vreg827:subreg_hireg<def,read-undef> = ...
1356 // ...
1357 // %vreg828<def> = COPY %vreg827
1358 if (S.empty())
1359 continue;
1360 SubLRC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
1361 &LIS.getVNInfoAllocator());
1362 SmallVector<SlotIndex, 4> Undefs;
1363 LI.computeSubRangeUndefs(Undefs, S.LaneMask, MRI, *LIS.getSlotIndexes());
1364 SubLRC.extend(S, EP.Next, 0, Undefs);
1365 }
1366 }
1367
1368 for (unsigned R : *Edit) {
1369 LiveInterval &LI = LIS.getInterval(R);
1370 if (!LI.hasSubRanges())
1371 continue;
1372 LI.clear();
1373 LI.removeEmptySubRanges();
1374 LIS.constructMainRangeFromSubranges(LI);
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001375 }
1376}
1377
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001378void SplitEditor::deleteRematVictims() {
1379 SmallVector<MachineInstr*, 8> Dead;
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001380 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
Mark Laceyf9ea8852013-08-14 23:50:04 +00001381 LiveInterval *LI = &LIS.getInterval(*I);
Matthias Braun96761952014-12-10 23:07:54 +00001382 for (const LiveRange::Segment &S : LI->segments) {
Jakob Stoklund Olesend8f24052011-11-13 22:42:13 +00001383 // Dead defs end at the dead slot.
Matthias Braun96761952014-12-10 23:07:54 +00001384 if (S.end != S.valno->def.getDeadSlot())
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001385 continue;
Wei Mi9a16d652016-04-13 03:08:27 +00001386 if (S.valno->isPHIDef())
1387 continue;
Matthias Braun96761952014-12-10 23:07:54 +00001388 MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001389 assert(MI && "Missing instruction for dead def");
1390 MI->addRegisterDead(LI->reg, &TRI);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001391
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001392 if (!MI->allDefsAreDead())
1393 continue;
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001394
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001395 DEBUG(dbgs() << "All defs dead: " << *MI);
1396 Dead.push_back(MI);
1397 }
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001398 }
1399
1400 if (Dead.empty())
1401 return;
1402
Wei Mic0223702016-07-08 21:08:09 +00001403 Edit->eliminateDeadDefs(Dead, None, &AA);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001404}
1405
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001406void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001407 ++NumFinished;
Eric Christopher21933532011-02-03 05:40:54 +00001408
Eric Christopherede62672011-02-03 06:18:29 +00001409 // At this point, the live intervals in Edit contain VNInfos corresponding to
1410 // the inserted copies.
1411
1412 // Add the original defs from the parent interval.
Matthias Braun96761952014-12-10 23:07:54 +00001413 for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen3295a992011-02-04 00:59:23 +00001414 if (ParentVNI->isUnused())
1415 continue;
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +00001416 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001417 defValue(RegIdx, ParentVNI, ParentVNI->def, true);
Jakob Stoklund Olesen32210de2011-03-15 21:13:22 +00001418
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001419 // Force rematted values to be recomputed everywhere.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001420 // The new live ranges may be truncated.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001421 if (Edit->didRematerialize(ParentVNI))
1422 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001423 forceRecompute(i, ParentVNI);
Eric Christopherede62672011-02-03 06:18:29 +00001424 }
1425
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001426 // Hoist back-copies to the complement interval when in spill mode.
1427 switch (SpillMode) {
1428 case SM_Partition:
1429 // Leave all back-copies as is.
1430 break;
1431 case SM_Size:
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001432 case SM_Speed:
Wei Mi9a16d652016-04-13 03:08:27 +00001433 // hoistCopies will behave differently between size and speed.
1434 hoistCopies();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001435 }
1436
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001437 // Transfer the simply mapped values, check if any are skipped.
1438 bool Skipped = transferValues();
Wei Mi9a16d652016-04-13 03:08:27 +00001439
1440 // Rewrite virtual registers, possibly extending ranges.
1441 rewriteAssigned(Skipped);
1442
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001443 if (Skipped)
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001444 extendPHIKillRanges();
1445 else
1446 ++NumSimple;
Eric Christopherede62672011-02-03 06:18:29 +00001447
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001448 // Delete defs that were rematted everywhere.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001449 if (Skipped)
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001450 deleteRematVictims();
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001451
Jakob Stoklund Olesen0f1677e2010-10-07 23:34:34 +00001452 // Get rid of unused values and set phi-kill flags.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001453 for (unsigned Reg : *Edit) {
1454 LiveInterval &LI = LIS.getInterval(Reg);
1455 LI.removeEmptySubRanges();
Mark Laceyf9ea8852013-08-14 23:50:04 +00001456 LI.RenumberValues();
1457 }
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001458
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001459 // Provide a reverse mapping from original indices to Edit ranges.
1460 if (LRMap) {
1461 LRMap->clear();
1462 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1463 LRMap->push_back(i);
1464 }
1465
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001466 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +00001467 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001468 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001469 // Don't use iterators, they are invalidated by create() below.
Matthias Braund3dd1352015-09-22 03:44:41 +00001470 unsigned VReg = Edit->get(i);
1471 LiveInterval &LI = LIS.getInterval(VReg);
1472 SmallVector<LiveInterval*, 8> SplitLIs;
1473 LIS.splitSeparateComponents(LI, SplitLIs);
1474 unsigned Original = VRM.getOriginal(VReg);
1475 for (LiveInterval *SplitLI : SplitLIs)
1476 VRM.setIsSplitFromReg(SplitLI->reg, Original);
1477
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001478 // The new intervals all map back to i.
1479 if (LRMap)
1480 LRMap->resize(Edit->size(), i);
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001481 }
1482
Jakob Stoklund Olesen284c2db2010-08-10 17:07:22 +00001483 // Calculate spill weight and allocation hints for new intervals.
Benjamin Kramere2a1d892013-06-17 19:00:36 +00001484 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001485
1486 assert(!LRMap || LRMap->size() == Edit->size());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +00001487}
1488
1489
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +00001490//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +00001491// Single Block Splitting
1492//===----------------------------------------------------------------------===//
1493
Jakob Stoklund Olesen8627ea92011-08-05 22:20:45 +00001494bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
1495 bool SingleInstrs) const {
1496 // Always split for multiple instructions.
1497 if (!BI.isOneInstr())
1498 return true;
1499 // Don't split for single instructions unless explicitly requested.
1500 if (!SingleInstrs)
1501 return false;
1502 // Splitting a live-through range always makes progress.
1503 if (BI.LiveIn && BI.LiveOut)
1504 return true;
1505 // No point in isolating a copy. It has no register class constraints.
1506 if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
1507 return false;
1508 // Finally, don't isolate an end point that was created by earlier splits.
1509 return isOriginalEndpoint(BI.FirstInstr);
1510}
1511
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001512void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1513 openIntv();
1514 SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001515 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001516 LastSplitPoint));
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001517 if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
1518 useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001519 } else {
1520 // The last use is after the last valid split point.
1521 SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1522 useIntv(SegStart, SegStop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001523 overlapIntv(SegStop, BI.LastInstr);
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001524 }
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001525}
1526
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001527
1528//===----------------------------------------------------------------------===//
1529// Global Live Range Splitting Support
1530//===----------------------------------------------------------------------===//
1531
1532// These methods support a method of global live range splitting that uses a
1533// global algorithm to decide intervals for CFG edges. They will insert split
1534// points and color intervals in basic blocks while avoiding interference.
1535//
1536// Note that splitSingleBlock is also useful for blocks where both CFG edges
1537// are on the stack.
1538
1539void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1540 unsigned IntvIn, SlotIndex LeaveBefore,
1541 unsigned IntvOut, SlotIndex EnterAfter){
1542 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001543 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001544
1545 DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
1546 << ") intf " << LeaveBefore << '-' << EnterAfter
1547 << ", live-through " << IntvIn << " -> " << IntvOut);
1548
1549 assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1550
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001551 assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
1552 assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
1553 assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
1554
1555 MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
1556
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001557 if (!IntvOut) {
1558 DEBUG(dbgs() << ", spill on entry.\n");
1559 //
1560 // <<<<<<<<< Possible LeaveBefore interference.
1561 // |-----------| Live through.
1562 // -____________ Spill on entry.
1563 //
1564 selectIntv(IntvIn);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001565 SlotIndex Idx = leaveIntvAtTop(*MBB);
1566 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1567 (void)Idx;
1568 return;
1569 }
1570
1571 if (!IntvIn) {
1572 DEBUG(dbgs() << ", reload on exit.\n");
1573 //
1574 // >>>>>>> Possible EnterAfter interference.
1575 // |-----------| Live through.
1576 // ___________-- Reload on exit.
1577 //
1578 selectIntv(IntvOut);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001579 SlotIndex Idx = enterIntvAtEnd(*MBB);
1580 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1581 (void)Idx;
1582 return;
1583 }
1584
1585 if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1586 DEBUG(dbgs() << ", straight through.\n");
1587 //
1588 // |-----------| Live through.
1589 // ------------- Straight through, same intv, no interference.
1590 //
1591 selectIntv(IntvOut);
1592 useIntv(Start, Stop);
1593 return;
1594 }
1595
1596 // We cannot legally insert splits after LSP.
1597 SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001598 assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001599
1600 if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1601 LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1602 DEBUG(dbgs() << ", switch avoiding interference.\n");
1603 //
1604 // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
1605 // |-----------| Live through.
1606 // ------======= Switch intervals between interference.
1607 //
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001608 selectIntv(IntvOut);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001609 SlotIndex Idx;
1610 if (LeaveBefore && LeaveBefore < LSP) {
1611 Idx = enterIntvBefore(LeaveBefore);
1612 useIntv(Idx, Stop);
1613 } else {
1614 Idx = enterIntvAtEnd(*MBB);
1615 }
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001616 selectIntv(IntvIn);
1617 useIntv(Start, Idx);
1618 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1619 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1620 return;
1621 }
1622
1623 DEBUG(dbgs() << ", create local intv for interference.\n");
1624 //
1625 // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
1626 // |-----------| Live through.
1627 // ==---------== Switch intervals before/after interference.
1628 //
1629 assert(LeaveBefore <= EnterAfter && "Missed case");
1630
1631 selectIntv(IntvOut);
1632 SlotIndex Idx = enterIntvAfter(EnterAfter);
1633 useIntv(Idx, Stop);
1634 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1635
1636 selectIntv(IntvIn);
1637 Idx = leaveIntvBefore(LeaveBefore);
1638 useIntv(Start, Idx);
1639 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1640}
1641
1642
1643void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1644 unsigned IntvIn, SlotIndex LeaveBefore) {
1645 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001646 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001647
1648 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001649 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001650 << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
1651 << (BI.LiveOut ? ", stack-out" : ", killed in block"));
1652
1653 assert(IntvIn && "Must have register in");
1654 assert(BI.LiveIn && "Must be live-in");
1655 assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1656
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001657 if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001658 DEBUG(dbgs() << " before interference.\n");
1659 //
1660 // <<< Interference after kill.
1661 // |---o---x | Killed in block.
1662 // ========= Use IntvIn everywhere.
1663 //
1664 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001665 useIntv(Start, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001666 return;
1667 }
1668
1669 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1670
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001671 if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001672 //
1673 // <<< Possible interference after last use.
1674 // |---o---o---| Live-out on stack.
1675 // =========____ Leave IntvIn after last use.
1676 //
1677 // < Interference after last use.
1678 // |---o---o--o| Live-out on stack, late last use.
1679 // ============ Copy to stack after LSP, overlap IntvIn.
1680 // \_____ Stack interval is live-out.
1681 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001682 if (BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001683 DEBUG(dbgs() << ", spill after last use before interference.\n");
1684 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001685 SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001686 useIntv(Start, Idx);
1687 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1688 } else {
1689 DEBUG(dbgs() << ", spill before last split point.\n");
1690 selectIntv(IntvIn);
Jakob Stoklund Olesen37e3a132011-07-16 00:13:30 +00001691 SlotIndex Idx = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001692 overlapIntv(Idx, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001693 useIntv(Start, Idx);
1694 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1695 }
1696 return;
1697 }
1698
1699 // The interference is overlapping somewhere we wanted to use IntvIn. That
1700 // means we need to create a local interval that can be allocated a
1701 // different register.
1702 unsigned LocalIntv = openIntv();
Matt Beaumont-Gay26909d82011-07-16 04:18:47 +00001703 (void)LocalIntv;
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001704 DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
1705
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001706 if (!BI.LiveOut || BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001707 //
1708 // <<<<<<< Interference overlapping uses.
1709 // |---o---o---| Live-out on stack.
1710 // =====----____ Leave IntvIn before interference, then spill.
1711 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001712 SlotIndex To = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001713 SlotIndex From = enterIntvBefore(LeaveBefore);
1714 useIntv(From, To);
1715 selectIntv(IntvIn);
1716 useIntv(Start, From);
1717 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1718 return;
1719 }
1720
1721 // <<<<<<< Interference overlapping uses.
1722 // |---o---o--o| Live-out on stack, late last use.
1723 // =====------- Copy to stack before LSP, overlap LocalIntv.
1724 // \_____ Stack interval is live-out.
1725 //
1726 SlotIndex To = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001727 overlapIntv(To, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001728 SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1729 useIntv(From, To);
1730 selectIntv(IntvIn);
1731 useIntv(Start, From);
1732 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1733}
1734
1735void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1736 unsigned IntvOut, SlotIndex EnterAfter) {
1737 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001738 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001739
1740 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001741 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001742 << ", reg-out " << IntvOut << ", enter after " << EnterAfter
1743 << (BI.LiveIn ? ", stack-in" : ", defined in block"));
1744
1745 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1746
1747 assert(IntvOut && "Must have register out");
1748 assert(BI.LiveOut && "Must be live-out");
1749 assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1750
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001751 if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001752 DEBUG(dbgs() << " after interference.\n");
1753 //
1754 // >>>> Interference before def.
1755 // | o---o---| Defined in block.
1756 // ========= Use IntvOut everywhere.
1757 //
1758 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001759 useIntv(BI.FirstInstr, Stop);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001760 return;
1761 }
1762
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001763 if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001764 DEBUG(dbgs() << ", reload after interference.\n");
1765 //
1766 // >>>> Interference before def.
1767 // |---o---o---| Live-through, stack-in.
1768 // ____========= Enter IntvOut before first use.
1769 //
1770 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001771 SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001772 useIntv(Idx, Stop);
1773 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1774 return;
1775 }
1776
1777 // The interference is overlapping somewhere we wanted to use IntvOut. That
1778 // means we need to create a local interval that can be allocated a
1779 // different register.
1780 DEBUG(dbgs() << ", interference overlaps uses.\n");
1781 //
1782 // >>>>>>> Interference overlapping uses.
1783 // |---o---o---| Live-through, stack-in.
1784 // ____---====== Create local interval for interference range.
1785 //
1786 selectIntv(IntvOut);
1787 SlotIndex Idx = enterIntvAfter(EnterAfter);
1788 useIntv(Idx, Stop);
1789 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1790
1791 openIntv();
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001792 SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001793 useIntv(From, Idx);
1794}