blob: d639f44753015049f688fb3bdf99cbf326799371 [file] [log] [blame]
Eugene Zelenko7cf6af52017-08-29 22:32:07 +00001//===- SplitKit.cpp - Toolkit for splitting live ranges -------------------===//
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +00002//
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 Olesen8ae02632010-07-20 15:41:07 +000015#include "SplitKit.h"
Eugene Zelenko7cf6af52017-08-29 22:32:07 +000016#include "LiveRangeCalc.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/DenseSet.h"
19#include "llvm/ADT/None.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SmallPtrSet.h"
22#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000023#include "llvm/ADT/Statistic.h"
Eugene Zelenko7cf6af52017-08-29 22:32:07 +000024#include "llvm/CodeGen/LiveInterval.h"
Matthias Braunfa621d22017-12-13 02:51:04 +000025#include "llvm/CodeGen/LiveIntervals.h"
Pete Cooper789d5d82012-04-02 22:44:18 +000026#include "llvm/CodeGen/LiveRangeEdit.h"
Eugene Zelenko7cf6af52017-08-29 22:32:07 +000027#include "llvm/CodeGen/MachineBasicBlock.h"
Wei Mi815b02e2016-04-13 03:08:27 +000028#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +000029#include "llvm/CodeGen/MachineDominators.h"
Eugene Zelenko7cf6af52017-08-29 22:32:07 +000030#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstr.h"
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000032#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +000033#include "llvm/CodeGen/MachineLoopInfo.h"
Eugene Zelenko7cf6af52017-08-29 22:32:07 +000034#include "llvm/CodeGen/MachineOperand.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko7cf6af52017-08-29 22:32:07 +000036#include "llvm/CodeGen/SlotIndexes.h"
David Blaikie48319232017-11-08 01:01:31 +000037#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000038#include "llvm/CodeGen/TargetOpcodes.h"
39#include "llvm/CodeGen/TargetRegisterInfo.h"
40#include "llvm/CodeGen/TargetSubtargetInfo.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000041#include "llvm/CodeGen/VirtRegMap.h"
Nico Weber0f38c602018-04-30 14:59:11 +000042#include "llvm/Config/llvm-config.h"
Eugene Zelenko7cf6af52017-08-29 22:32:07 +000043#include "llvm/IR/DebugLoc.h"
44#include "llvm/MC/LaneBitmask.h"
45#include "llvm/Support/Allocator.h"
46#include "llvm/Support/BlockFrequency.h"
47#include "llvm/Support/Compiler.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000048#include "llvm/Support/Debug.h"
Eugene Zelenko7cf6af52017-08-29 22:32:07 +000049#include "llvm/Support/ErrorHandling.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000050#include "llvm/Support/raw_ostream.h"
Eugene Zelenko7cf6af52017-08-29 22:32:07 +000051#include <algorithm>
52#include <cassert>
53#include <iterator>
54#include <limits>
55#include <tuple>
56#include <utility>
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000057
58using namespace llvm;
59
Chandler Carruth8677f2f2014-04-22 02:02:50 +000060#define DEBUG_TYPE "regalloc"
61
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000062STATISTIC(NumFinished, "Number of splits finished");
63STATISTIC(NumSimple, "Number of splits that were simple");
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000064STATISTIC(NumCopies, "Number of copies inserted for splitting");
65STATISTIC(NumRemats, "Number of rematerialized defs for splitting");
Jakob Stoklund Olesenbdda37d2011-05-10 17:37:41 +000066STATISTIC(NumRepairs, "Number of invalid live ranges repaired");
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000067
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000068//===----------------------------------------------------------------------===//
Wei Mi723d45a2016-05-11 22:28:29 +000069// Last Insert Point Analysis
70//===----------------------------------------------------------------------===//
71
72InsertPointAnalysis::InsertPointAnalysis(const LiveIntervals &lis,
73 unsigned BBNum)
Wei Mi14a29ca2016-05-23 19:39:19 +000074 : LIS(lis), LastInsertPoint(BBNum) {}
Wei Mi723d45a2016-05-11 22:28:29 +000075
76SlotIndex
Wei Mi14a29ca2016-05-23 19:39:19 +000077InsertPointAnalysis::computeLastInsertPoint(const LiveInterval &CurLI,
78 const MachineBasicBlock &MBB) {
Wei Mi723d45a2016-05-11 22:28:29 +000079 unsigned Num = MBB.getNumber();
80 std::pair<SlotIndex, SlotIndex> &LIP = LastInsertPoint[Num];
81 SlotIndex MBBEnd = LIS.getMBBEndIdx(&MBB);
82
Hiroshi Inoue5c99c6a2017-07-09 05:54:44 +000083 SmallVector<const MachineBasicBlock *, 1> EHPadSuccessors;
Wei Mi723d45a2016-05-11 22:28:29 +000084 for (const MachineBasicBlock *SMBB : MBB.successors())
85 if (SMBB->isEHPad())
Hiroshi Inoue5c99c6a2017-07-09 05:54:44 +000086 EHPadSuccessors.push_back(SMBB);
Wei Mi723d45a2016-05-11 22:28:29 +000087
88 // Compute insert points on the first call. The pair is independent of the
89 // current live interval.
90 if (!LIP.first.isValid()) {
91 MachineBasicBlock::const_iterator FirstTerm = MBB.getFirstTerminator();
92 if (FirstTerm == MBB.end())
93 LIP.first = MBBEnd;
94 else
95 LIP.first = LIS.getInstructionIndex(*FirstTerm);
96
97 // If there is a landing pad successor, also find the call instruction.
Hiroshi Inoue5c99c6a2017-07-09 05:54:44 +000098 if (EHPadSuccessors.empty())
Wei Mi723d45a2016-05-11 22:28:29 +000099 return LIP.first;
100 // There may not be a call instruction (?) in which case we ignore LPad.
101 LIP.second = LIP.first;
102 for (MachineBasicBlock::const_iterator I = MBB.end(), E = MBB.begin();
103 I != E;) {
104 --I;
105 if (I->isCall()) {
106 LIP.second = LIS.getInstructionIndex(*I);
107 break;
108 }
109 }
110 }
111
112 // If CurLI is live into a landing pad successor, move the last insert point
113 // back to the call that may throw.
114 if (!LIP.second)
115 return LIP.first;
116
Hiroshi Inoue5c99c6a2017-07-09 05:54:44 +0000117 if (none_of(EHPadSuccessors, [&](const MachineBasicBlock *EHPad) {
Wei Mi14a29ca2016-05-23 19:39:19 +0000118 return LIS.isLiveInToMBB(CurLI, EHPad);
Wei Mi723d45a2016-05-11 22:28:29 +0000119 }))
120 return LIP.first;
121
122 // Find the value leaving MBB.
Wei Mi14a29ca2016-05-23 19:39:19 +0000123 const VNInfo *VNI = CurLI.getVNInfoBefore(MBBEnd);
Wei Mi723d45a2016-05-11 22:28:29 +0000124 if (!VNI)
125 return LIP.first;
126
127 // If the value leaving MBB was defined after the call in MBB, it can't
128 // really be live-in to the landing pad. This can happen if the landing pad
129 // has a PHI, and this register is undef on the exceptional edge.
130 // <rdar://problem/10664933>
131 if (!SlotIndex::isEarlierInstr(VNI->def, LIP.second) && VNI->def < MBBEnd)
132 return LIP.first;
133
134 // Value is properly live-in to the landing pad.
135 // Only allow inserts before the call.
136 return LIP.second;
137}
138
139MachineBasicBlock::iterator
Wei Mi14a29ca2016-05-23 19:39:19 +0000140InsertPointAnalysis::getLastInsertPointIter(const LiveInterval &CurLI,
141 MachineBasicBlock &MBB) {
142 SlotIndex LIP = getLastInsertPoint(CurLI, MBB);
Wei Mi723d45a2016-05-11 22:28:29 +0000143 if (LIP == LIS.getMBBEndIdx(&MBB))
144 return MBB.end();
145 return LIS.getInstructionFromIndex(LIP);
146}
147
148//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000149// Split Analysis
150//===----------------------------------------------------------------------===//
151
Eric Christopher9f85dcc2014-08-04 21:25:23 +0000152SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000153 const MachineLoopInfo &mli)
Eric Christopher9f85dcc2014-08-04 21:25:23 +0000154 : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli),
Eugene Zelenko7cf6af52017-08-29 22:32:07 +0000155 TII(*MF.getSubtarget().getInstrInfo()), IPA(lis, MF.getNumBlockIDs()) {}
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000156
157void SplitAnalysis::clear() {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000158 UseSlots.clear();
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000159 UseBlocks.clear();
160 ThroughBlocks.clear();
Craig Topper4ba84432014-04-14 00:51:57 +0000161 CurLI = nullptr;
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +0000162 DidRepairRange = false;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000163}
164
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000165/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000166void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000167 assert(UseSlots.empty() && "Call clear first");
168
169 // First get all the defs from the interval values. This provides the correct
170 // slots for early clobbers.
Matthias Braun218d20a2014-12-10 23:07:54 +0000171 for (const VNInfo *VNI : CurLI->valnos)
172 if (!VNI->isPHIDef() && !VNI->isUnused())
173 UseSlots.push_back(VNI->def);
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000174
175 // Get use slots form the use-def chain.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000176 const MachineRegisterInfo &MRI = MF.getRegInfo();
Owen Anderson92fca732014-03-17 19:36:09 +0000177 for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg))
178 if (!MO.isUndef())
Duncan P. N. Exon Smith42e18352016-02-27 06:40:41 +0000179 UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot());
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000180
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000181 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000182
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000183 // Remove duplicates, keeping the smaller slot for each instruction.
184 // That is what we want for early clobbers.
185 UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
186 SlotIndex::isSameInstr),
187 UseSlots.end());
188
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000189 // Compute per-live block info.
190 if (!calcLiveBlockInfo()) {
191 // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
Rafael Espindola5b220212011-06-26 22:34:10 +0000192 // I am looking at you, RegisterCoalescer!
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +0000193 DidRepairRange = true;
Jakob Stoklund Olesenbdda37d2011-05-10 17:37:41 +0000194 ++NumRepairs;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000195 LLVM_DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000196 const_cast<LiveIntervals&>(LIS)
197 .shrinkToUses(const_cast<LiveInterval*>(CurLI));
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000198 UseBlocks.clear();
199 ThroughBlocks.clear();
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000200 bool fixed = calcLiveBlockInfo();
201 (void)fixed;
202 assert(fixed && "Couldn't fix broken live interval");
203 }
204
Nicola Zaghen0818e782018-05-14 12:53:11 +0000205 LLVM_DEBUG(dbgs() << "Analyze counted " << UseSlots.size() << " instrs in "
206 << UseBlocks.size() << " blocks, through "
207 << NumThroughBlocks << " blocks.\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000208}
209
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000210/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
211/// where CurLI is live.
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000212bool SplitAnalysis::calcLiveBlockInfo() {
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000213 ThroughBlocks.resize(MF.getNumBlockIDs());
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000214 NumThroughBlocks = NumGapBlocks = 0;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000215 if (CurLI->empty())
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000216 return true;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000217
218 LiveInterval::const_iterator LVI = CurLI->begin();
219 LiveInterval::const_iterator LVE = CurLI->end();
220
221 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
222 UseI = UseSlots.begin();
223 UseE = UseSlots.end();
224
225 // Loop over basic blocks where CurLI is live.
Duncan P. N. Exon Smithac4d7b62015-10-09 22:56:24 +0000226 MachineFunction::iterator MFI =
227 LIS.getMBBFromIndex(LVI->start)->getIterator();
Eugene Zelenko7cf6af52017-08-29 22:32:07 +0000228 while (true) {
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000229 BlockInfo BI;
Duncan P. N. Exon Smithac4d7b62015-10-09 22:56:24 +0000230 BI.MBB = &*MFI;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000231 SlotIndex Start, Stop;
Benjamin Kramera4f0aad2014-03-02 13:30:33 +0000232 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000233
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000234 // If the block contains no uses, the range must be live through. At one
Rafael Espindola5b220212011-06-26 22:34:10 +0000235 // point, RegisterCoalescer could create dangling ranges that ended
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000236 // mid-block.
237 if (UseI == UseE || *UseI >= Stop) {
238 ++NumThroughBlocks;
239 ThroughBlocks.set(BI.MBB->getNumber());
240 // The range shouldn't end mid-block if there are no uses. This shouldn't
241 // happen.
242 if (LVI->end < Stop)
243 return false;
244 } else {
245 // This block has uses. Find the first and last uses in the block.
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000246 BI.FirstInstr = *UseI;
247 assert(BI.FirstInstr >= Start);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000248 do ++UseI;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000249 while (UseI != UseE && *UseI < Stop);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000250 BI.LastInstr = UseI[-1];
251 assert(BI.LastInstr < Stop);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000252
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000253 // LVI is the first live segment overlapping MBB.
254 BI.LiveIn = LVI->start <= Start;
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000255
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000256 // When not live in, the first use should be a def.
257 if (!BI.LiveIn) {
Matthias Braun331de112013-10-10 21:28:43 +0000258 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000259 assert(LVI->start == BI.FirstInstr && "First instr should be a def");
260 BI.FirstDef = BI.FirstInstr;
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000261 }
262
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000263 // Look for gaps in the live range.
264 BI.LiveOut = true;
265 while (LVI->end < Stop) {
266 SlotIndex LastStop = LVI->end;
267 if (++LVI == LVE || LVI->start >= Stop) {
268 BI.LiveOut = false;
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000269 BI.LastInstr = LastStop;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000270 break;
271 }
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000272
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000273 if (LastStop < LVI->start) {
274 // There is a gap in the live range. Create duplicate entries for the
275 // live-in snippet and the live-out snippet.
276 ++NumGapBlocks;
277
278 // Push the Live-in part.
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000279 BI.LiveOut = false;
280 UseBlocks.push_back(BI);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000281 UseBlocks.back().LastInstr = LastStop;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000282
283 // Set up BI for the live-out part.
284 BI.LiveIn = false;
285 BI.LiveOut = true;
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000286 BI.FirstInstr = BI.FirstDef = LVI->start;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000287 }
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000288
Matthias Braun331de112013-10-10 21:28:43 +0000289 // A Segment that starts in the middle of the block must be a def.
290 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000291 if (!BI.FirstDef)
292 BI.FirstDef = LVI->start;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000293 }
294
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000295 UseBlocks.push_back(BI);
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000296
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000297 // LVI is now at LVE or LVI->end >= Stop.
298 if (LVI == LVE)
299 break;
300 }
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000301
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000302 // Live segment ends exactly at Stop. Move to the next segment.
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000303 if (LVI->end == Stop && ++LVI == LVE)
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000304 break;
305
306 // Pick the next basic block.
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000307 if (LVI->start < Stop)
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000308 ++MFI;
309 else
Duncan P. N. Exon Smithac4d7b62015-10-09 22:56:24 +0000310 MFI = LIS.getMBBFromIndex(LVI->start)->getIterator();
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000311 }
Jakob Stoklund Olesenb2abfa02011-05-28 02:32:57 +0000312
313 assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000314 return true;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000315}
316
Jakob Stoklund Olesen9f4b8932011-04-26 22:33:12 +0000317unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
318 if (cli->empty())
319 return 0;
320 LiveInterval *li = const_cast<LiveInterval*>(cli);
321 LiveInterval::iterator LVI = li->begin();
322 LiveInterval::iterator LVE = li->end();
323 unsigned Count = 0;
324
325 // Loop over basic blocks where li is live.
Duncan P. N. Exon Smithac4d7b62015-10-09 22:56:24 +0000326 MachineFunction::const_iterator MFI =
327 LIS.getMBBFromIndex(LVI->start)->getIterator();
328 SlotIndex Stop = LIS.getMBBEndIdx(&*MFI);
Eugene Zelenko7cf6af52017-08-29 22:32:07 +0000329 while (true) {
Jakob Stoklund Olesen9f4b8932011-04-26 22:33:12 +0000330 ++Count;
331 LVI = li->advanceTo(LVI, Stop);
332 if (LVI == LVE)
333 return Count;
334 do {
335 ++MFI;
Duncan P. N. Exon Smithac4d7b62015-10-09 22:56:24 +0000336 Stop = LIS.getMBBEndIdx(&*MFI);
Jakob Stoklund Olesen9f4b8932011-04-26 22:33:12 +0000337 } while (Stop <= LVI->start);
338 }
339}
340
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000341bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
342 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
343 const LiveInterval &Orig = LIS.getInterval(OrigReg);
344 assert(!Orig.empty() && "Splitting empty interval?");
345 LiveInterval::const_iterator I = Orig.find(Idx);
346
347 // Range containing Idx should begin at Idx.
348 if (I != Orig.end() && I->start <= Idx)
349 return I->start == Idx;
350
351 // Range does not contain Idx, previous must end at Idx.
352 return I != Orig.begin() && (--I)->end == Idx;
353}
354
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000355void SplitAnalysis::analyze(const LiveInterval *li) {
356 clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000357 CurLI = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000358 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000359}
360
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000361//===----------------------------------------------------------------------===//
362// Split Editor
363//===----------------------------------------------------------------------===//
364
365/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Wei Mi4eae2782016-07-08 21:08:09 +0000366SplitEditor::SplitEditor(SplitAnalysis &sa, AliasAnalysis &aa,
367 LiveIntervals &lis, VirtRegMap &vrm,
Benjamin Kramer4eed7562013-06-17 19:00:36 +0000368 MachineDominatorTree &mdt,
369 MachineBlockFrequencyInfo &mbfi)
Wei Mi4eae2782016-07-08 21:08:09 +0000370 : SA(sa), AA(aa), LIS(lis), VRM(vrm),
371 MRI(vrm.getMachineFunction().getRegInfo()), MDT(mdt),
372 TII(*vrm.getMachineFunction().getSubtarget().getInstrInfo()),
Eric Christopher1dd55ba2014-10-14 07:22:00 +0000373 TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()),
Eugene Zelenko7cf6af52017-08-29 22:32:07 +0000374 MBFI(mbfi), RegAssign(Allocator) {}
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000375
Jakob Stoklund Olesen708d06f2011-09-12 16:49:21 +0000376void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
377 Edit = &LRE;
378 SpillMode = SM;
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000379 OpenIdx = 0;
380 RegAssign.clear();
381 Values.clear();
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000382
383 // Reset the LiveRangeCalc instances needed for this spill mode.
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000384 LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
385 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000386 if (SpillMode)
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000387 LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
388 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000389
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000390 // We don't need an AliasAnalysis since we will only be performing
391 // cheap-as-a-copy remats anyway.
Craig Topper4ba84432014-04-14 00:51:57 +0000392 Edit->anyRematerializable(nullptr);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000393}
394
Aaron Ballman1d03d382017-10-15 14:32:27 +0000395#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +0000396LLVM_DUMP_METHOD void SplitEditor::dump() const {
Eric Christopher0f438112011-02-03 06:18:29 +0000397 if (RegAssign.empty()) {
398 dbgs() << " empty\n";
399 return;
400 }
401
402 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
403 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
404 dbgs() << '\n';
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000405}
Manman Ren77e300e2012-09-06 19:06:06 +0000406#endif
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000407
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000408LiveInterval::SubRange &SplitEditor::getSubRangeForMask(LaneBitmask LM,
409 LiveInterval &LI) {
410 for (LiveInterval::SubRange &S : LI.subranges())
411 if (S.LaneMask == LM)
412 return S;
413 llvm_unreachable("SubRange for this mask not found");
George Burgess IV29b947d2016-08-25 02:15:54 +0000414}
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000415
416void SplitEditor::addDeadDef(LiveInterval &LI, VNInfo *VNI, bool Original) {
417 if (!LI.hasSubRanges()) {
418 LI.createDeadDef(VNI);
419 return;
420 }
421
422 SlotIndex Def = VNI->def;
423 if (Original) {
424 // If we are transferring a def from the original interval, make sure
425 // to only update the subranges for which the original subranges had
426 // a def at this location.
427 for (LiveInterval::SubRange &S : LI.subranges()) {
428 auto &PS = getSubRangeForMask(S.LaneMask, Edit->getParent());
429 VNInfo *PV = PS.getVNInfoAt(Def);
430 if (PV != nullptr && PV->def == Def)
431 S.createDeadDef(Def, LIS.getVNInfoAllocator());
432 }
433 } else {
434 // This is a new def: either from rematerialization, or from an inserted
435 // copy. Since rematerialization can regenerate a definition of a sub-
436 // register, we need to check which subranges need to be updated.
437 const MachineInstr *DefMI = LIS.getInstructionFromIndex(Def);
438 assert(DefMI != nullptr);
Krzysztof Parzyszekd6ca3f02016-12-15 14:36:06 +0000439 LaneBitmask LM;
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000440 for (const MachineOperand &DefOp : DefMI->defs()) {
441 unsigned R = DefOp.getReg();
442 if (R != LI.reg)
443 continue;
444 if (unsigned SR = DefOp.getSubReg())
445 LM |= TRI.getSubRegIndexLaneMask(SR);
446 else {
447 LM = MRI.getMaxLaneMaskForVReg(R);
448 break;
449 }
450 }
451 for (LiveInterval::SubRange &S : LI.subranges())
Krzysztof Parzyszek308c60d2016-12-16 19:11:56 +0000452 if ((S.LaneMask & LM).any())
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000453 S.createDeadDef(Def, LIS.getVNInfoAllocator());
454 }
455}
456
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000457VNInfo *SplitEditor::defValue(unsigned RegIdx,
458 const VNInfo *ParentVNI,
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000459 SlotIndex Idx,
460 bool Original) {
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000461 assert(ParentVNI && "Mapping NULL value");
462 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000463 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
Mark Lacey1feb5852013-08-14 23:50:04 +0000464 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000465
466 // Create a new value.
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000467 VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000468
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000469 bool Force = LI->hasSubRanges();
470 ValueForcePair FP(Force ? nullptr : VNI, Force);
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000471 // Use insert for lookup, so we can add missing values with a second lookup.
472 std::pair<ValueMap::iterator, bool> InsP =
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000473 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), FP));
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000474
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000475 // This was the first time (RegIdx, ParentVNI) was mapped, and it is not
476 // forced. Keep it as a simple def without any liveness.
477 if (!Force && InsP.second)
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000478 return VNI;
479
480 // If the previous value was a simple mapping, add liveness for it now.
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000481 if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000482 addDeadDef(*LI, OldVNI, Original);
483
484 // No longer a simple mapping. Switch to a complex mapping. If the
485 // interval has subranges, make it a forced mapping.
486 InsP.first->second = ValueForcePair(nullptr, Force);
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000487 }
488
489 // This is a complex mapping, add liveness for VNI
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000490 addDeadDef(*LI, VNI, Original);
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000491 return VNI;
492}
493
Matthias Braunffc6b462018-02-02 00:08:19 +0000494void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo &ParentVNI) {
495 ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI.id)];
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000496 VNInfo *VNI = VFP.getPointer();
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000497
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000498 // ParentVNI was either unmapped or already complex mapped. Either way, just
499 // set the force bit.
500 if (!VNI) {
501 VFP.setInt(true);
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000502 return;
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000503 }
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000504
505 // This was previously a single mapping. Make sure the old def is represented
506 // by a trivial live range.
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000507 addDeadDef(LIS.getInterval(Edit->get(RegIdx)), VNI, false);
508
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000509 // Mark as complex mapped, forced.
Craig Topper4ba84432014-04-14 00:51:57 +0000510 VFP = ValueForcePair(nullptr, true);
Jakob Stoklund Olesene5a2e362011-09-13 18:05:29 +0000511}
512
Matthias Braun94ebfcb2017-03-17 00:41:39 +0000513SlotIndex SplitEditor::buildSingleSubRegCopy(unsigned FromReg, unsigned ToReg,
514 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
515 unsigned SubIdx, LiveInterval &DestLI, bool Late, SlotIndex Def) {
516 const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
517 bool FirstCopy = !Def.isValid();
518 MachineInstr *CopyMI = BuildMI(MBB, InsertBefore, DebugLoc(), Desc)
519 .addReg(ToReg, RegState::Define | getUndefRegState(FirstCopy)
520 | getInternalReadRegState(!FirstCopy), SubIdx)
521 .addReg(FromReg, 0, SubIdx);
522
523 BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator();
524 if (FirstCopy) {
525 SlotIndexes &Indexes = *LIS.getSlotIndexes();
526 Def = Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
Matthias Braun94ebfcb2017-03-17 00:41:39 +0000527 } else {
528 CopyMI->bundleWithPred();
529 }
530 LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubIdx);
531 DestLI.refineSubRanges(Allocator, LaneMask,
532 [Def, &Allocator](LiveInterval::SubRange& SR) {
533 SR.createDeadDef(Def, Allocator);
534 });
535 return Def;
536}
537
538SlotIndex SplitEditor::buildCopy(unsigned FromReg, unsigned ToReg,
539 LaneBitmask LaneMask, MachineBasicBlock &MBB,
540 MachineBasicBlock::iterator InsertBefore, bool Late, unsigned RegIdx) {
541 const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
542 if (LaneMask.all() || LaneMask == MRI.getMaxLaneMaskForVReg(FromReg)) {
543 // The full vreg is copied.
544 MachineInstr *CopyMI =
545 BuildMI(MBB, InsertBefore, DebugLoc(), Desc, ToReg).addReg(FromReg);
546 SlotIndexes &Indexes = *LIS.getSlotIndexes();
547 return Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
548 }
549
550 // Only a subset of lanes needs to be copied. The following is a simple
551 // heuristic to construct a sequence of COPYs. We could add a target
552 // specific callback if this turns out to be suboptimal.
553 LiveInterval &DestLI = LIS.getInterval(Edit->get(RegIdx));
554
555 // First pass: Try to find a perfectly matching subregister index. If none
556 // exists find the one covering the most lanemask bits.
557 SmallVector<unsigned, 8> PossibleIndexes;
558 unsigned BestIdx = 0;
559 unsigned BestCover = 0;
560 const TargetRegisterClass *RC = MRI.getRegClass(FromReg);
561 assert(RC == MRI.getRegClass(ToReg) && "Should have same reg class");
562 for (unsigned Idx = 1, E = TRI.getNumSubRegIndices(); Idx < E; ++Idx) {
563 // Is this index even compatible with the given class?
564 if (TRI.getSubClassWithSubReg(RC, Idx) != RC)
565 continue;
566 LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(Idx);
567 // Early exit if we found a perfect match.
568 if (SubRegMask == LaneMask) {
569 BestIdx = Idx;
570 break;
571 }
572
573 // The index must not cover any lanes outside \p LaneMask.
574 if ((SubRegMask & ~LaneMask).any())
575 continue;
576
Krzysztof Parzyszekcf138ec2017-07-20 19:43:19 +0000577 unsigned PopCount = SubRegMask.getNumLanes();
Matthias Braun94ebfcb2017-03-17 00:41:39 +0000578 PossibleIndexes.push_back(Idx);
579 if (PopCount > BestCover) {
580 BestCover = PopCount;
581 BestIdx = Idx;
582 }
583 }
584
585 // Abort if we cannot possibly implement the COPY with the given indexes.
586 if (BestIdx == 0)
587 report_fatal_error("Impossible to implement partial COPY");
588
589 SlotIndex Def = buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore,
590 BestIdx, DestLI, Late, SlotIndex());
591
592 // Greedy heuristic: Keep iterating keeping the best covering subreg index
593 // each time.
Matthias Braunc37ab5f2017-06-12 20:30:52 +0000594 LaneBitmask LanesLeft = LaneMask & ~(TRI.getSubRegIndexLaneMask(BestIdx));
Matthias Braun94ebfcb2017-03-17 00:41:39 +0000595 while (LanesLeft.any()) {
596 unsigned BestIdx = 0;
Eugene Zelenko7cf6af52017-08-29 22:32:07 +0000597 int BestCover = std::numeric_limits<int>::min();
Matthias Braun94ebfcb2017-03-17 00:41:39 +0000598 for (unsigned Idx : PossibleIndexes) {
599 LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(Idx);
600 // Early exit if we found a perfect match.
601 if (SubRegMask == LanesLeft) {
602 BestIdx = Idx;
603 break;
604 }
605
606 // Try to cover as much of the remaining lanes as possible but
607 // as few of the already covered lanes as possible.
Krzysztof Parzyszekcf138ec2017-07-20 19:43:19 +0000608 int Cover = (SubRegMask & LanesLeft).getNumLanes()
609 - (SubRegMask & ~LanesLeft).getNumLanes();
Matthias Braun94ebfcb2017-03-17 00:41:39 +0000610 if (Cover > BestCover) {
611 BestCover = Cover;
612 BestIdx = Idx;
613 }
614 }
615
616 if (BestIdx == 0)
617 report_fatal_error("Impossible to implement partial COPY");
618
619 buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore, BestIdx,
620 DestLI, Late, Def);
621 LanesLeft &= ~TRI.getSubRegIndexLaneMask(BestIdx);
622 }
623
624 return Def;
625}
626
Eric Christopher0f438112011-02-03 06:18:29 +0000627VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000628 VNInfo *ParentVNI,
629 SlotIndex UseIdx,
630 MachineBasicBlock &MBB,
631 MachineBasicBlock::iterator I) {
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000632 SlotIndex Def;
Mark Lacey1feb5852013-08-14 23:50:04 +0000633 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000634
Jakob Stoklund Olesenbb30dd42011-05-02 05:29:58 +0000635 // We may be trying to avoid interference that ends at a deleted instruction,
636 // so always begin RegIdx 0 early and all others late.
637 bool Late = RegIdx != 0;
638
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000639 // Attempt cheap-as-a-copy rematerialization.
Wei Mi815b02e2016-04-13 03:08:27 +0000640 unsigned Original = VRM.getOriginal(Edit->get(RegIdx));
641 LiveInterval &OrigLI = LIS.getInterval(Original);
642 VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
Wei Mi815b02e2016-04-13 03:08:27 +0000643
Matthias Braun94ebfcb2017-03-17 00:41:39 +0000644 unsigned Reg = LI->reg;
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000645 bool DidRemat = false;
646 if (OrigVNI) {
647 LiveRangeEdit::Remat RM(ParentVNI);
648 RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
649 if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) {
Matthias Braun94ebfcb2017-03-17 00:41:39 +0000650 Def = Edit->rematerializeAt(MBB, I, Reg, RM, TRI, Late);
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000651 ++NumRemats;
652 DidRemat = true;
653 }
654 }
655 if (!DidRemat) {
Matthias Braun94ebfcb2017-03-17 00:41:39 +0000656 LaneBitmask LaneMask;
Stanislav Mekhanoshin036be2d2017-02-01 01:18:36 +0000657 if (LI->hasSubRanges()) {
Matthias Braun94ebfcb2017-03-17 00:41:39 +0000658 LaneMask = LaneBitmask::getNone();
Stanislav Mekhanoshin036be2d2017-02-01 01:18:36 +0000659 for (LiveInterval::SubRange &S : LI->subranges())
Matthias Braun94ebfcb2017-03-17 00:41:39 +0000660 LaneMask |= S.LaneMask;
661 } else {
662 LaneMask = LaneBitmask::getAll();
Stanislav Mekhanoshin036be2d2017-02-01 01:18:36 +0000663 }
Matthias Braun94ebfcb2017-03-17 00:41:39 +0000664
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000665 ++NumCopies;
Matthias Braun94ebfcb2017-03-17 00:41:39 +0000666 Def = buildCopy(Edit->getReg(), Reg, LaneMask, MBB, I, Late, RegIdx);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000667 }
668
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000669 // Define the value in Reg.
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000670 return defValue(RegIdx, ParentVNI, Def, false);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000671}
672
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000673/// Create a new virtual register and live interval.
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000674unsigned SplitEditor::openIntv() {
Eric Christopher0f438112011-02-03 06:18:29 +0000675 // Create the complement as index 0.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000676 if (Edit->empty())
Mark Laceye742d682013-08-14 23:50:16 +0000677 Edit->createEmptyInterval();
Eric Christopher0f438112011-02-03 06:18:29 +0000678
679 // Create the open interval.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000680 OpenIdx = Edit->size();
Mark Laceye742d682013-08-14 23:50:16 +0000681 Edit->createEmptyInterval();
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000682 return OpenIdx;
683}
684
685void SplitEditor::selectIntv(unsigned Idx) {
686 assert(Idx != 0 && "Cannot select the complement interval");
687 assert(Idx < Edit->size() && "Can only select previously opened interval");
Nicola Zaghen0818e782018-05-14 12:53:11 +0000688 LLVM_DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000689 OpenIdx = Idx;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000690}
691
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000692SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000693 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Nicola Zaghen0818e782018-05-14 12:53:11 +0000694 LLVM_DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000695 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000696 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000697 if (!ParentVNI) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000698 LLVM_DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000699 return Idx;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000700 }
Nicola Zaghen0818e782018-05-14 12:53:11 +0000701 LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000702 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000703 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000704
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000705 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
706 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000707}
708
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000709SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
710 assert(OpenIdx && "openIntv not called before enterIntvAfter");
Nicola Zaghen0818e782018-05-14 12:53:11 +0000711 LLVM_DEBUG(dbgs() << " enterIntvAfter " << Idx);
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000712 Idx = Idx.getBoundaryIndex();
713 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
714 if (!ParentVNI) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000715 LLVM_DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000716 return Idx;
717 }
Nicola Zaghen0818e782018-05-14 12:53:11 +0000718 LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000719 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
720 assert(MI && "enterIntvAfter called with invalid index");
721
722 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
Benjamin Kramerd628f192014-03-02 12:27:27 +0000723 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000724 return VNI->def;
725}
726
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000727SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000728 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000729 SlotIndex End = LIS.getMBBEndIdx(&MBB);
730 SlotIndex Last = End.getPrevSlot();
Nicola Zaghen0818e782018-05-14 12:53:11 +0000731 LLVM_DEBUG(dbgs() << " enterIntvAtEnd " << printMBBReference(MBB) << ", "
732 << Last);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000733 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000734 if (!ParentVNI) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000735 LLVM_DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000736 return End;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000737 }
Nicola Zaghen0818e782018-05-14 12:53:11 +0000738 LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000739 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesen74c4f972012-01-11 02:07:00 +0000740 SA.getLastSplitPointIter(&MBB));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000741 RegAssign.insert(VNI->def, End, OpenIdx);
Nicola Zaghen0818e782018-05-14 12:53:11 +0000742 LLVM_DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000743 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000744}
745
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000746/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000747void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000748 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000749}
750
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000751void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopher0f438112011-02-03 06:18:29 +0000752 assert(OpenIdx && "openIntv not called before useIntv");
Nicola Zaghen0818e782018-05-14 12:53:11 +0000753 LLVM_DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
Eric Christopher0f438112011-02-03 06:18:29 +0000754 RegAssign.insert(Start, End, OpenIdx);
Nicola Zaghen0818e782018-05-14 12:53:11 +0000755 LLVM_DEBUG(dump());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000756}
757
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000758SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000759 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Nicola Zaghen0818e782018-05-14 12:53:11 +0000760 LLVM_DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000761
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000762 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000763 SlotIndex Boundary = Idx.getBoundaryIndex();
764 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000765 if (!ParentVNI) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000766 LLVM_DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000767 return Boundary.getNextSlot();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000768 }
Nicola Zaghen0818e782018-05-14 12:53:11 +0000769 LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000770 MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
Jakob Stoklund Olesen01cb34b2011-02-08 18:50:18 +0000771 assert(MI && "No instruction at index");
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000772
773 // In spill mode, make live ranges as short as possible by inserting the copy
774 // before MI. This is only possible if that instruction doesn't redefine the
775 // value. The inserted COPY is not a kill, and we don't need to recompute
776 // the source live range. The spiller also won't try to hoist this copy.
777 if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
778 MI->readsVirtualRegister(Edit->getReg())) {
Matthias Braunffc6b462018-02-02 00:08:19 +0000779 forceRecompute(0, *ParentVNI);
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000780 defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
781 return Idx;
782 }
783
784 VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
Benjamin Kramerd628f192014-03-02 12:27:27 +0000785 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000786 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000787}
788
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000789SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
790 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
Nicola Zaghen0818e782018-05-14 12:53:11 +0000791 LLVM_DEBUG(dbgs() << " leaveIntvBefore " << Idx);
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000792
793 // The interval must be live into the instruction at Idx.
Jakob Stoklund Olesenfc479332011-07-18 18:47:13 +0000794 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000795 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000796 if (!ParentVNI) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000797 LLVM_DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000798 return Idx.getNextSlot();
799 }
Nicola Zaghen0818e782018-05-14 12:53:11 +0000800 LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000801
802 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
803 assert(MI && "No instruction at index");
804 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
805 return VNI->def;
806}
807
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000808SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000809 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000810 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Nicola Zaghen0818e782018-05-14 12:53:11 +0000811 LLVM_DEBUG(dbgs() << " leaveIntvAtTop " << printMBBReference(MBB) << ", "
812 << Start);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000813
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000814 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000815 if (!ParentVNI) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000816 LLVM_DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000817 return Start;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000818 }
819
Eric Christopher0f438112011-02-03 06:18:29 +0000820 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Keith Walker7435b282016-09-16 14:07:29 +0000821 MBB.SkipPHIsLabelsAndDebug(MBB.begin()));
Eric Christopher0f438112011-02-03 06:18:29 +0000822 RegAssign.insert(Start, VNI->def, OpenIdx);
Nicola Zaghen0818e782018-05-14 12:53:11 +0000823 LLVM_DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000824 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000825}
826
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000827void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
828 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000829 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +0000830 assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000831 "Parent changes value in extended range");
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000832 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
833 "Range cannot span basic blocks");
834
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000835 // The complement interval will be extended as needed by LRCalc.extend().
Jakob Stoklund Olesenb3dd8262011-04-05 23:43:14 +0000836 if (ParentVNI)
Matthias Braunffc6b462018-02-02 00:08:19 +0000837 forceRecompute(0, *ParentVNI);
Nicola Zaghen0818e782018-05-14 12:53:11 +0000838 LLVM_DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000839 RegAssign.insert(Start, End, OpenIdx);
Nicola Zaghen0818e782018-05-14 12:53:11 +0000840 LLVM_DEBUG(dump());
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000841}
842
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000843//===----------------------------------------------------------------------===//
844// Spill modes
845//===----------------------------------------------------------------------===//
846
847void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
Mark Lacey1feb5852013-08-14 23:50:04 +0000848 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Nicola Zaghen0818e782018-05-14 12:53:11 +0000849 LLVM_DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000850 RegAssignMap::iterator AssignI;
851 AssignI.setMap(RegAssign);
852
853 for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
Matthias Braun7d3ec5a2015-01-21 19:02:30 +0000854 SlotIndex Def = Copies[i]->def;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000855 MachineInstr *MI = LIS.getInstructionFromIndex(Def);
856 assert(MI && "No instruction for back-copy");
857
858 MachineBasicBlock *MBB = MI->getParent();
859 MachineBasicBlock::iterator MBBI(MI);
860 bool AtBegin;
861 do AtBegin = MBBI == MBB->begin();
Shiva Chen24abe712018-05-09 02:42:00 +0000862 while (!AtBegin && (--MBBI)->isDebugInstr());
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000863
Nicola Zaghen0818e782018-05-14 12:53:11 +0000864 LLVM_DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
Matthias Braun7d3ec5a2015-01-21 19:02:30 +0000865 LIS.removeVRegDefAt(*LI, Def);
Duncan P. N. Exon Smith42e18352016-02-27 06:40:41 +0000866 LIS.RemoveMachineInstrFromMaps(*MI);
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000867 MI->eraseFromParent();
868
Matthias Braun7d3ec5a2015-01-21 19:02:30 +0000869 // Adjust RegAssign if a register assignment is killed at Def. We want to
870 // avoid calculating the live range of the source register if possible.
Jakob Stoklund Olesen1599a642012-08-03 20:59:29 +0000871 AssignI.find(Def.getPrevSlot());
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000872 if (!AssignI.valid() || AssignI.start() >= Def)
873 continue;
874 // If MI doesn't kill the assigned register, just leave it.
875 if (AssignI.stop() != Def)
876 continue;
877 unsigned RegIdx = AssignI.value();
878 if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000879 LLVM_DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx
880 << '\n');
Matthias Braunffc6b462018-02-02 00:08:19 +0000881 forceRecompute(RegIdx, *Edit->getParent().getVNInfoAt(Def));
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000882 } else {
Duncan P. N. Exon Smith42e18352016-02-27 06:40:41 +0000883 SlotIndex Kill = LIS.getInstructionIndex(*MBBI).getRegSlot();
Nicola Zaghen0818e782018-05-14 12:53:11 +0000884 LLVM_DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000885 AssignI.setStop(Kill);
886 }
887 }
888}
889
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000890MachineBasicBlock*
891SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
892 MachineBasicBlock *DefMBB) {
893 if (MBB == DefMBB)
894 return MBB;
895 assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
896
897 const MachineLoopInfo &Loops = SA.Loops;
898 const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
899 MachineDomTreeNode *DefDomNode = MDT[DefMBB];
900
901 // Best candidate so far.
902 MachineBasicBlock *BestMBB = MBB;
Eugene Zelenko7cf6af52017-08-29 22:32:07 +0000903 unsigned BestDepth = std::numeric_limits<unsigned>::max();
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000904
Eugene Zelenko7cf6af52017-08-29 22:32:07 +0000905 while (true) {
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000906 const MachineLoop *Loop = Loops.getLoopFor(MBB);
907
908 // MBB isn't in a loop, it doesn't get any better. All dominators have a
909 // higher frequency by definition.
910 if (!Loop) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000911 LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
912 << " dominates " << printMBBReference(*MBB)
913 << " at depth 0\n");
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000914 return MBB;
915 }
916
917 // We'll never be able to exit the DefLoop.
918 if (Loop == DefLoop) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000919 LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
920 << " dominates " << printMBBReference(*MBB)
921 << " in the same loop\n");
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000922 return MBB;
923 }
924
925 // Least busy dominator seen so far.
926 unsigned Depth = Loop->getLoopDepth();
927 if (Depth < BestDepth) {
928 BestMBB = MBB;
929 BestDepth = Depth;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000930 LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
931 << " dominates " << printMBBReference(*MBB)
932 << " at depth " << Depth << '\n');
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000933 }
934
935 // Leave loop by going to the immediate dominator of the loop header.
936 // This is a bigger stride than simply walking up the dominator tree.
937 MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
938
939 // Too far up the dominator tree?
940 if (!IDom || !MDT.dominates(DefDomNode, IDom))
941 return BestMBB;
942
943 MBB = IDom->getBlock();
944 }
945}
946
Wei Mi815b02e2016-04-13 03:08:27 +0000947void SplitEditor::computeRedundantBackCopies(
948 DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) {
949 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
950 LiveInterval *Parent = &Edit->getParent();
951 SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums());
952 SmallPtrSet<VNInfo *, 8> DominatedVNIs;
953
954 // Aggregate VNIs having the same value as ParentVNI.
955 for (VNInfo *VNI : LI->valnos) {
956 if (VNI->isUnused())
957 continue;
958 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
959 EqualVNs[ParentVNI->id].insert(VNI);
960 }
961
962 // For VNI aggregation of each ParentVNI, collect dominated, i.e.,
963 // redundant VNIs to BackCopies.
964 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
965 VNInfo *ParentVNI = Parent->getValNumInfo(i);
966 if (!NotToHoistSet.count(ParentVNI->id))
967 continue;
968 SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin();
969 SmallPtrSetIterator<VNInfo *> It2 = It1;
970 for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) {
971 It2 = It1;
972 for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) {
973 if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2))
974 continue;
975
976 MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def);
977 MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def);
978 if (MBB1 == MBB2) {
979 DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1));
980 } else if (MDT.dominates(MBB1, MBB2)) {
981 DominatedVNIs.insert(*It2);
982 } else if (MDT.dominates(MBB2, MBB1)) {
983 DominatedVNIs.insert(*It1);
984 }
985 }
986 }
987 if (!DominatedVNIs.empty()) {
Matthias Braunffc6b462018-02-02 00:08:19 +0000988 forceRecompute(0, *ParentVNI);
Wei Mi815b02e2016-04-13 03:08:27 +0000989 for (auto VNI : DominatedVNIs) {
990 BackCopies.push_back(VNI);
991 }
992 DominatedVNIs.clear();
993 }
994 }
995}
996
997/// For SM_Size mode, find a common dominator for all the back-copies for
998/// the same ParentVNI and hoist the backcopies to the dominator BB.
999/// For SM_Speed mode, if the common dominator is hot and it is not beneficial
1000/// to do the hoisting, simply remove the dominated backcopies for the same
1001/// ParentVNI.
1002void SplitEditor::hoistCopies() {
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001003 // Get the complement interval, always RegIdx 0.
Mark Lacey1feb5852013-08-14 23:50:04 +00001004 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001005 LiveInterval *Parent = &Edit->getParent();
1006
1007 // Track the nearest common dominator for all back-copies for each ParentVNI,
1008 // indexed by ParentVNI->id.
Eugene Zelenko7cf6af52017-08-29 22:32:07 +00001009 using DomPair = std::pair<MachineBasicBlock *, SlotIndex>;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001010 SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
Wei Mi815b02e2016-04-13 03:08:27 +00001011 // The total cost of all the back-copies for each ParentVNI.
1012 SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums());
1013 // The ParentVNI->id set for which hoisting back-copies are not beneficial
1014 // for Speed.
1015 DenseSet<unsigned> NotToHoistSet;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001016
1017 // Find the nearest common dominator for parent values with multiple
1018 // back-copies. If a single back-copy dominates, put it in DomPair.second.
Matthias Braun218d20a2014-12-10 23:07:54 +00001019 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen1599a642012-08-03 20:59:29 +00001020 if (VNI->isUnused())
1021 continue;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001022 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
1023 assert(ParentVNI && "Parent not live at complement def");
1024
1025 // Don't hoist remats. The complement is probably going to disappear
1026 // completely anyway.
1027 if (Edit->didRematerialize(ParentVNI))
1028 continue;
1029
1030 MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
Wei Mi815b02e2016-04-13 03:08:27 +00001031
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001032 DomPair &Dom = NearestDom[ParentVNI->id];
1033
1034 // Keep directly defined parent values. This is either a PHI or an
1035 // instruction in the complement range. All other copies of ParentVNI
1036 // should be eliminated.
1037 if (VNI->def == ParentVNI->def) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001038 LLVM_DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001039 Dom = DomPair(ValMBB, VNI->def);
1040 continue;
1041 }
1042 // Skip the singly mapped values. There is nothing to gain from hoisting a
1043 // single back-copy.
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +00001044 if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001045 LLVM_DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001046 continue;
1047 }
1048
1049 if (!Dom.first) {
1050 // First time we see ParentVNI. VNI dominates itself.
1051 Dom = DomPair(ValMBB, VNI->def);
1052 } else if (Dom.first == ValMBB) {
1053 // Two defs in the same block. Pick the earlier def.
1054 if (!Dom.second.isValid() || VNI->def < Dom.second)
1055 Dom.second = VNI->def;
1056 } else {
1057 // Different basic blocks. Check if one dominates.
1058 MachineBasicBlock *Near =
1059 MDT.findNearestCommonDominator(Dom.first, ValMBB);
1060 if (Near == ValMBB)
1061 // Def ValMBB dominates.
1062 Dom = DomPair(ValMBB, VNI->def);
1063 else if (Near != Dom.first)
1064 // None dominate. Hoist to common dominator, need new def.
1065 Dom = DomPair(Near, SlotIndex());
Wei Mi815b02e2016-04-13 03:08:27 +00001066 Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB);
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001067 }
1068
Nicola Zaghen0818e782018-05-14 12:53:11 +00001069 LLVM_DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@'
1070 << VNI->def << " for parent " << ParentVNI->id << '@'
1071 << ParentVNI->def << " hoist to "
1072 << printMBBReference(*Dom.first) << ' ' << Dom.second
1073 << '\n');
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001074 }
1075
1076 // Insert the hoisted copies.
1077 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
1078 DomPair &Dom = NearestDom[i];
1079 if (!Dom.first || Dom.second.isValid())
1080 continue;
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +00001081 // This value needs a hoisted copy inserted at the end of Dom.first.
1082 VNInfo *ParentVNI = Parent->getValNumInfo(i);
1083 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
1084 // Get a less loopy dominator than Dom.first.
1085 Dom.first = findShallowDominator(Dom.first, DefMBB);
Wei Mi815b02e2016-04-13 03:08:27 +00001086 if (SpillMode == SM_Speed &&
1087 MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) {
1088 NotToHoistSet.insert(ParentVNI->id);
1089 continue;
1090 }
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001091 SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
1092 Dom.second =
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +00001093 defFromParent(0, ParentVNI, Last, *Dom.first,
Jakob Stoklund Olesen74c4f972012-01-11 02:07:00 +00001094 SA.getLastSplitPointIter(Dom.first))->def;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001095 }
1096
1097 // Remove redundant back-copies that are now known to be dominated by another
1098 // def with the same value.
1099 SmallVector<VNInfo*, 8> BackCopies;
Matthias Braun218d20a2014-12-10 23:07:54 +00001100 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen1599a642012-08-03 20:59:29 +00001101 if (VNI->isUnused())
1102 continue;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001103 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
1104 const DomPair &Dom = NearestDom[ParentVNI->id];
Wei Mi815b02e2016-04-13 03:08:27 +00001105 if (!Dom.first || Dom.second == VNI->def ||
1106 NotToHoistSet.count(ParentVNI->id))
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001107 continue;
1108 BackCopies.push_back(VNI);
Matthias Braunffc6b462018-02-02 00:08:19 +00001109 forceRecompute(0, *ParentVNI);
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001110 }
Wei Mi815b02e2016-04-13 03:08:27 +00001111
1112 // If it is not beneficial to hoist all the BackCopies, simply remove
1113 // redundant BackCopies in speed mode.
1114 if (SpillMode == SM_Speed && !NotToHoistSet.empty())
1115 computeRedundantBackCopies(NotToHoistSet, BackCopies);
1116
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001117 removeBackCopies(BackCopies);
1118}
1119
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001120/// transferValues - Transfer all possible values to the new live ranges.
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +00001121/// Values that were rematerialized are left alone, they need LRCalc.extend().
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001122bool SplitEditor::transferValues() {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001123 bool Skipped = false;
1124 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Matthias Braun218d20a2014-12-10 23:07:54 +00001125 for (const LiveRange::Segment &S : Edit->getParent()) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001126 LLVM_DEBUG(dbgs() << " blit " << S << ':');
Matthias Braun218d20a2014-12-10 23:07:54 +00001127 VNInfo *ParentVNI = S.valno;
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001128 // RegAssign has holes where RegIdx 0 should be used.
Matthias Braun218d20a2014-12-10 23:07:54 +00001129 SlotIndex Start = S.start;
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001130 AssignI.advanceTo(Start);
1131 do {
1132 unsigned RegIdx;
Matthias Braun218d20a2014-12-10 23:07:54 +00001133 SlotIndex End = S.end;
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001134 if (!AssignI.valid()) {
1135 RegIdx = 0;
1136 } else if (AssignI.start() <= Start) {
1137 RegIdx = AssignI.value();
1138 if (AssignI.stop() < End) {
1139 End = AssignI.stop();
1140 ++AssignI;
1141 }
1142 } else {
1143 RegIdx = 0;
1144 End = std::min(End, AssignI.start());
1145 }
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001146
1147 // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
Nicola Zaghen0818e782018-05-14 12:53:11 +00001148 LLVM_DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx << '('
1149 << printReg(Edit->get(RegIdx)) << ')');
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001150 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001151
1152 // Check for a simply defined value that can be blitted directly.
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +00001153 ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
1154 if (VNInfo *VNI = VFP.getPointer()) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001155 LLVM_DEBUG(dbgs() << ':' << VNI->id);
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001156 LI.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001157 Start = End;
1158 continue;
1159 }
1160
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +00001161 // Skip values with forced recomputation.
1162 if (VFP.getInt()) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001163 LLVM_DEBUG(dbgs() << "(recalc)");
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001164 Skipped = true;
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001165 Start = End;
1166 continue;
1167 }
1168
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +00001169 LiveRangeCalc &LRC = getLRCalc(RegIdx);
1170
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001171 // This value has multiple defs in RegIdx, but it wasn't rematerialized,
1172 // so the live range is accurate. Add live-in blocks in [Start;End) to the
1173 // LiveInBlocks.
Duncan P. N. Exon Smithac4d7b62015-10-09 22:56:24 +00001174 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001175 SlotIndex BlockStart, BlockEnd;
Duncan P. N. Exon Smithac4d7b62015-10-09 22:56:24 +00001176 std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001177
1178 // The first block may be live-in, or it may have its own def.
1179 if (Start != BlockStart) {
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001180 VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001181 assert(VNI && "Missing def for complex mapped value");
Nicola Zaghen0818e782018-05-14 12:53:11 +00001182 LLVM_DEBUG(dbgs() << ':' << VNI->id << "*" << printMBBReference(*MBB));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001183 // MBB has its own def. Is it also live-out?
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +00001184 if (BlockEnd <= End)
Duncan P. N. Exon Smithac4d7b62015-10-09 22:56:24 +00001185 LRC.setLiveOutValue(&*MBB, VNI);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +00001186
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001187 // Skip to the next block for live-in.
1188 ++MBB;
1189 BlockStart = BlockEnd;
1190 }
1191
1192 // Handle the live-in blocks covered by [Start;End).
1193 assert(Start <= BlockStart && "Expected live-in block");
1194 while (BlockStart < End) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001195 LLVM_DEBUG(dbgs() << ">" << printMBBReference(*MBB));
Duncan P. N. Exon Smithac4d7b62015-10-09 22:56:24 +00001196 BlockEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001197 if (BlockStart == ParentVNI->def) {
1198 // This block has the def of a parent PHI, so it isn't live-in.
1199 assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001200 VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001201 assert(VNI && "Missing def for complex mapped parent PHI");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +00001202 if (End >= BlockEnd)
Duncan P. N. Exon Smithac4d7b62015-10-09 22:56:24 +00001203 LRC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001204 } else {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +00001205 // This block needs a live-in value. The last block covered may not
1206 // be live-out.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001207 if (End < BlockEnd)
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001208 LRC.addLiveInBlock(LI, MDT[&*MBB], End);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001209 else {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +00001210 // Live-through, and we don't know the value.
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001211 LRC.addLiveInBlock(LI, MDT[&*MBB]);
Duncan P. N. Exon Smithac4d7b62015-10-09 22:56:24 +00001212 LRC.setLiveOutValue(&*MBB, nullptr);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001213 }
1214 }
1215 BlockStart = BlockEnd;
1216 ++MBB;
1217 }
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001218 Start = End;
Matthias Braun218d20a2014-12-10 23:07:54 +00001219 } while (Start != S.end);
Nicola Zaghen0818e782018-05-14 12:53:11 +00001220 LLVM_DEBUG(dbgs() << '\n');
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001221 }
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001222
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +00001223 LRCalc[0].calculateValues();
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +00001224 if (SpillMode)
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +00001225 LRCalc[1].calculateValues();
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001226
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001227 return Skipped;
1228}
1229
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001230static bool removeDeadSegment(SlotIndex Def, LiveRange &LR) {
1231 const LiveRange::Segment *Seg = LR.getSegmentContaining(Def);
1232 if (Seg == nullptr)
1233 return true;
1234 if (Seg->end != Def.getDeadSlot())
1235 return false;
1236 // This is a dead PHI. Remove it.
1237 LR.removeSegment(*Seg, true);
1238 return true;
1239}
1240
1241void SplitEditor::extendPHIRange(MachineBasicBlock &B, LiveRangeCalc &LRC,
Krzysztof Parzyszekf5fef362016-11-21 20:24:12 +00001242 LiveRange &LR, LaneBitmask LM,
1243 ArrayRef<SlotIndex> Undefs) {
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001244 for (MachineBasicBlock *P : B.predecessors()) {
1245 SlotIndex End = LIS.getMBBEndIdx(P);
1246 SlotIndex LastUse = End.getPrevSlot();
1247 // The predecessor may not have a live-out value. That is OK, like an
1248 // undef PHI operand.
Krzysztof Parzyszekf5fef362016-11-21 20:24:12 +00001249 LiveInterval &PLI = Edit->getParent();
1250 // Need the cast because the inputs to ?: would otherwise be deemed
1251 // "incompatible": SubRange vs LiveInterval.
Krzysztof Parzyszekd6ca3f02016-12-15 14:36:06 +00001252 LiveRange &PSR = !LM.all() ? getSubRangeForMask(LM, PLI)
1253 : static_cast<LiveRange&>(PLI);
Krzysztof Parzyszekf5fef362016-11-21 20:24:12 +00001254 if (PSR.liveAt(LastUse))
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001255 LRC.extend(LR, End, /*PhysReg=*/0, Undefs);
1256 }
1257}
1258
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +00001259void SplitEditor::extendPHIKillRanges() {
Wei Mi815b02e2016-04-13 03:08:27 +00001260 // Extend live ranges to be live-out for successor PHI values.
Wei Mi815b02e2016-04-13 03:08:27 +00001261
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001262 // Visit each PHI def slot in the parent live interval. If the def is dead,
1263 // remove it. Otherwise, extend the live interval to reach the end indexes
1264 // of all predecessor blocks.
Wei Mi815b02e2016-04-13 03:08:27 +00001265
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001266 LiveInterval &ParentLI = Edit->getParent();
1267 for (const VNInfo *V : ParentLI.valnos) {
1268 if (V->isUnused() || !V->isPHIDef())
1269 continue;
1270
1271 unsigned RegIdx = RegAssign.lookup(V->def);
1272 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +00001273 LiveRangeCalc &LRC = getLRCalc(RegIdx);
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001274 MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
1275 if (!removeDeadSegment(V->def, LI))
Krzysztof Parzyszekd6ca3f02016-12-15 14:36:06 +00001276 extendPHIRange(B, LRC, LI, LaneBitmask::getAll(), /*Undefs=*/{});
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001277 }
1278
1279 SmallVector<SlotIndex, 4> Undefs;
1280 LiveRangeCalc SubLRC;
1281
1282 for (LiveInterval::SubRange &PS : ParentLI.subranges()) {
1283 for (const VNInfo *V : PS.valnos) {
1284 if (V->isUnused() || !V->isPHIDef())
1285 continue;
1286 unsigned RegIdx = RegAssign.lookup(V->def);
1287 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1288 LiveInterval::SubRange &S = getSubRangeForMask(PS.LaneMask, LI);
1289 if (removeDeadSegment(V->def, S))
1290 continue;
1291
1292 MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
1293 SubLRC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
1294 &LIS.getVNInfoAllocator());
1295 Undefs.clear();
1296 LI.computeSubRangeUndefs(Undefs, PS.LaneMask, MRI, *LIS.getSlotIndexes());
Krzysztof Parzyszekf5fef362016-11-21 20:24:12 +00001297 extendPHIRange(B, SubLRC, S, PS.LaneMask, Undefs);
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +00001298 }
1299 }
1300}
1301
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001302/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001303void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001304 struct ExtPoint {
1305 ExtPoint(const MachineOperand &O, unsigned R, SlotIndex N)
1306 : MO(O), RegIdx(R), Next(N) {}
Eugene Zelenko7cf6af52017-08-29 22:32:07 +00001307
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001308 MachineOperand MO;
1309 unsigned RegIdx;
1310 SlotIndex Next;
1311 };
1312
1313 SmallVector<ExtPoint,4> ExtPoints;
1314
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001315 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001316 RE = MRI.reg_end(); RI != RE;) {
Owen Andersonbf630222014-03-13 23:12:04 +00001317 MachineOperand &MO = *RI;
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001318 MachineInstr *MI = MO.getParent();
1319 ++RI;
Eric Christopher0f438112011-02-03 06:18:29 +00001320 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001321 if (MI->isDebugValue()) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001322 LLVM_DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001323 MO.setReg(0);
1324 continue;
1325 }
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +00001326
Jakob Stoklund Olesenb09701d2011-07-24 20:23:50 +00001327 // <undef> operands don't really read the register, so it doesn't matter
1328 // which register we choose. When the use operand is tied to a def, we must
1329 // use the same register as the def, so just do that always.
Duncan P. N. Exon Smith42e18352016-02-27 06:40:41 +00001330 SlotIndex Idx = LIS.getInstructionIndex(*MI);
Jakob Stoklund Olesenb09701d2011-07-24 20:23:50 +00001331 if (MO.isDef() || MO.isUndef())
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001332 Idx = Idx.getRegSlot(MO.isEarlyClobber());
Eric Christopher0f438112011-02-03 06:18:29 +00001333
1334 // Rewrite to the mapped register at Idx.
1335 unsigned RegIdx = RegAssign.lookup(Idx);
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001336 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1337 MO.setReg(LI.reg);
Nicola Zaghen0818e782018-05-14 12:53:11 +00001338 LLVM_DEBUG(dbgs() << " rewr " << printMBBReference(*MI->getParent())
1339 << '\t' << Idx << ':' << RegIdx << '\t' << *MI);
Eric Christopher0f438112011-02-03 06:18:29 +00001340
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +00001341 // Extend liveness to Idx if the instruction reads reg.
Jakob Stoklund Olesen81d686e2011-07-24 20:33:23 +00001342 if (!ExtendRanges || MO.isUndef())
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +00001343 continue;
1344
1345 // Skip instructions that don't read Reg.
1346 if (MO.isDef()) {
1347 if (!MO.getSubReg() && !MO.isEarlyClobber())
1348 continue;
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001349 // We may want to extend a live range for a partial redef, or for a use
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +00001350 // tied to an early clobber.
1351 Idx = Idx.getPrevSlot();
1352 if (!Edit->getParent().liveAt(Idx))
1353 continue;
1354 } else
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001355 Idx = Idx.getRegSlot(true);
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +00001356
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001357 SlotIndex Next = Idx.getNextSlot();
1358 if (LI.hasSubRanges()) {
1359 // We have to delay extending subranges until we have seen all operands
1360 // defining the register. This is because a <def,read-undef> operand
1361 // will create an "undef" point, and we cannot extend any subranges
1362 // until all of them have been accounted for.
Krzysztof Parzyszek57c58d32016-09-02 19:48:55 +00001363 if (MO.isUse())
1364 ExtPoints.push_back(ExtPoint(MO, RegIdx, Next));
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001365 } else {
1366 LiveRangeCalc &LRC = getLRCalc(RegIdx);
1367 LRC.extend(LI, Next, 0, ArrayRef<SlotIndex>());
1368 }
1369 }
1370
1371 for (ExtPoint &EP : ExtPoints) {
1372 LiveInterval &LI = LIS.getInterval(Edit->get(EP.RegIdx));
1373 assert(LI.hasSubRanges());
1374
1375 LiveRangeCalc SubLRC;
1376 unsigned Reg = EP.MO.getReg(), Sub = EP.MO.getSubReg();
1377 LaneBitmask LM = Sub != 0 ? TRI.getSubRegIndexLaneMask(Sub)
1378 : MRI.getMaxLaneMaskForVReg(Reg);
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001379 for (LiveInterval::SubRange &S : LI.subranges()) {
Krzysztof Parzyszekd6ca3f02016-12-15 14:36:06 +00001380 if ((S.LaneMask & LM).none())
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001381 continue;
1382 // The problem here can be that the new register may have been created
1383 // for a partially defined original register. For example:
Francis Visoiu Mistrih73846522017-11-30 12:12:19 +00001384 // %0:subreg_hireg<def,read-undef> = ...
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001385 // ...
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +00001386 // %1 = COPY %0
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001387 if (S.empty())
1388 continue;
1389 SubLRC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
1390 &LIS.getVNInfoAllocator());
1391 SmallVector<SlotIndex, 4> Undefs;
1392 LI.computeSubRangeUndefs(Undefs, S.LaneMask, MRI, *LIS.getSlotIndexes());
1393 SubLRC.extend(S, EP.Next, 0, Undefs);
1394 }
1395 }
1396
1397 for (unsigned R : *Edit) {
1398 LiveInterval &LI = LIS.getInterval(R);
1399 if (!LI.hasSubRanges())
1400 continue;
1401 LI.clear();
1402 LI.removeEmptySubRanges();
1403 LIS.constructMainRangeFromSubranges(LI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001404 }
1405}
1406
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001407void SplitEditor::deleteRematVictims() {
1408 SmallVector<MachineInstr*, 8> Dead;
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001409 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
Mark Lacey1feb5852013-08-14 23:50:04 +00001410 LiveInterval *LI = &LIS.getInterval(*I);
Matthias Braun218d20a2014-12-10 23:07:54 +00001411 for (const LiveRange::Segment &S : LI->segments) {
Jakob Stoklund Olesen1f81e312011-11-13 22:42:13 +00001412 // Dead defs end at the dead slot.
Matthias Braun218d20a2014-12-10 23:07:54 +00001413 if (S.end != S.valno->def.getDeadSlot())
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001414 continue;
Wei Mi815b02e2016-04-13 03:08:27 +00001415 if (S.valno->isPHIDef())
1416 continue;
Matthias Braun218d20a2014-12-10 23:07:54 +00001417 MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001418 assert(MI && "Missing instruction for dead def");
1419 MI->addRegisterDead(LI->reg, &TRI);
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001420
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001421 if (!MI->allDefsAreDead())
1422 continue;
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001423
Nicola Zaghen0818e782018-05-14 12:53:11 +00001424 LLVM_DEBUG(dbgs() << "All defs dead: " << *MI);
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001425 Dead.push_back(MI);
1426 }
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001427 }
1428
1429 if (Dead.empty())
1430 return;
1431
Wei Mi4eae2782016-07-08 21:08:09 +00001432 Edit->eliminateDeadDefs(Dead, None, &AA);
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001433}
1434
Matthias Braunffc6b462018-02-02 00:08:19 +00001435void SplitEditor::forceRecomputeVNI(const VNInfo &ParentVNI) {
1436 // Fast-path for common case.
1437 if (!ParentVNI.isPHIDef()) {
1438 for (unsigned I = 0, E = Edit->size(); I != E; ++I)
1439 forceRecompute(I, ParentVNI);
1440 return;
1441 }
1442
1443 // Trace value through phis.
1444 SmallPtrSet<const VNInfo *, 8> Visited; ///< whether VNI was/is in worklist.
1445 SmallVector<const VNInfo *, 4> WorkList;
1446 Visited.insert(&ParentVNI);
1447 WorkList.push_back(&ParentVNI);
1448
1449 const LiveInterval &ParentLI = Edit->getParent();
1450 const SlotIndexes &Indexes = *LIS.getSlotIndexes();
1451 do {
1452 const VNInfo &VNI = *WorkList.back();
1453 WorkList.pop_back();
1454 for (unsigned I = 0, E = Edit->size(); I != E; ++I)
1455 forceRecompute(I, VNI);
1456 if (!VNI.isPHIDef())
1457 continue;
1458
1459 MachineBasicBlock &MBB = *Indexes.getMBBFromIndex(VNI.def);
1460 for (const MachineBasicBlock *Pred : MBB.predecessors()) {
1461 SlotIndex PredEnd = Indexes.getMBBEndIdx(Pred);
1462 VNInfo *PredVNI = ParentLI.getVNInfoBefore(PredEnd);
1463 assert(PredVNI && "Value available in PhiVNI predecessor");
1464 if (Visited.insert(PredVNI).second)
1465 WorkList.push_back(PredVNI);
1466 }
1467 } while(!WorkList.empty());
1468}
1469
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001470void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001471 ++NumFinished;
Eric Christopher463a2972011-02-03 05:40:54 +00001472
Eric Christopher0f438112011-02-03 06:18:29 +00001473 // At this point, the live intervals in Edit contain VNInfos corresponding to
1474 // the inserted copies.
1475
1476 // Add the original defs from the parent interval.
Matthias Braun218d20a2014-12-10 23:07:54 +00001477 for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen9ecd1e72011-02-04 00:59:23 +00001478 if (ParentVNI->isUnused())
1479 continue;
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +00001480 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001481 defValue(RegIdx, ParentVNI, ParentVNI->def, true);
Jakob Stoklund Olesen29ef8752011-03-15 21:13:22 +00001482
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +00001483 // Force rematted values to be recomputed everywhere.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001484 // The new live ranges may be truncated.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001485 if (Edit->didRematerialize(ParentVNI))
Matthias Braunffc6b462018-02-02 00:08:19 +00001486 forceRecomputeVNI(*ParentVNI);
Eric Christopher0f438112011-02-03 06:18:29 +00001487 }
1488
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001489 // Hoist back-copies to the complement interval when in spill mode.
1490 switch (SpillMode) {
1491 case SM_Partition:
1492 // Leave all back-copies as is.
1493 break;
1494 case SM_Size:
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001495 case SM_Speed:
Wei Mi815b02e2016-04-13 03:08:27 +00001496 // hoistCopies will behave differently between size and speed.
1497 hoistCopies();
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001498 }
1499
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001500 // Transfer the simply mapped values, check if any are skipped.
1501 bool Skipped = transferValues();
Wei Mi815b02e2016-04-13 03:08:27 +00001502
1503 // Rewrite virtual registers, possibly extending ranges.
1504 rewriteAssigned(Skipped);
1505
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001506 if (Skipped)
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001507 extendPHIKillRanges();
1508 else
1509 ++NumSimple;
Eric Christopher0f438112011-02-03 06:18:29 +00001510
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001511 // Delete defs that were rematted everywhere.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001512 if (Skipped)
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001513 deleteRematVictims();
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001514
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001515 // Get rid of unused values and set phi-kill flags.
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +00001516 for (unsigned Reg : *Edit) {
1517 LiveInterval &LI = LIS.getInterval(Reg);
1518 LI.removeEmptySubRanges();
Mark Lacey1feb5852013-08-14 23:50:04 +00001519 LI.RenumberValues();
1520 }
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001521
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001522 // Provide a reverse mapping from original indices to Edit ranges.
1523 if (LRMap) {
1524 LRMap->clear();
1525 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1526 LRMap->push_back(i);
1527 }
1528
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001529 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001530 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001531 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001532 // Don't use iterators, they are invalidated by create() below.
Matthias Braun95e05dd2015-09-22 03:44:41 +00001533 unsigned VReg = Edit->get(i);
1534 LiveInterval &LI = LIS.getInterval(VReg);
1535 SmallVector<LiveInterval*, 8> SplitLIs;
1536 LIS.splitSeparateComponents(LI, SplitLIs);
1537 unsigned Original = VRM.getOriginal(VReg);
1538 for (LiveInterval *SplitLI : SplitLIs)
1539 VRM.setIsSplitFromReg(SplitLI->reg, Original);
1540
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001541 // The new intervals all map back to i.
1542 if (LRMap)
1543 LRMap->resize(Edit->size(), i);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001544 }
1545
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +00001546 // Calculate spill weight and allocation hints for new intervals.
Benjamin Kramer4eed7562013-06-17 19:00:36 +00001547 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001548
1549 assert(!LRMap || LRMap->size() == Edit->size());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001550}
1551
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +00001552//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001553// Single Block Splitting
1554//===----------------------------------------------------------------------===//
1555
Jakob Stoklund Olesen2d6d86b2011-08-05 22:20:45 +00001556bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
1557 bool SingleInstrs) const {
1558 // Always split for multiple instructions.
1559 if (!BI.isOneInstr())
1560 return true;
1561 // Don't split for single instructions unless explicitly requested.
1562 if (!SingleInstrs)
1563 return false;
1564 // Splitting a live-through range always makes progress.
1565 if (BI.LiveIn && BI.LiveOut)
1566 return true;
1567 // No point in isolating a copy. It has no register class constraints.
1568 if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
1569 return false;
1570 // Finally, don't isolate an end point that was created by earlier splits.
1571 return isOriginalEndpoint(BI.FirstInstr);
1572}
1573
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001574void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1575 openIntv();
1576 SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001577 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001578 LastSplitPoint));
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001579 if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
1580 useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001581 } else {
1582 // The last use is after the last valid split point.
1583 SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1584 useIntv(SegStart, SegStop);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001585 overlapIntv(SegStop, BI.LastInstr);
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001586 }
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001587}
1588
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001589//===----------------------------------------------------------------------===//
1590// Global Live Range Splitting Support
1591//===----------------------------------------------------------------------===//
1592
1593// These methods support a method of global live range splitting that uses a
1594// global algorithm to decide intervals for CFG edges. They will insert split
1595// points and color intervals in basic blocks while avoiding interference.
1596//
1597// Note that splitSingleBlock is also useful for blocks where both CFG edges
1598// are on the stack.
1599
1600void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1601 unsigned IntvIn, SlotIndex LeaveBefore,
1602 unsigned IntvOut, SlotIndex EnterAfter){
1603 SlotIndex Start, Stop;
Benjamin Kramera4f0aad2014-03-02 13:30:33 +00001604 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001605
Nicola Zaghen0818e782018-05-14 12:53:11 +00001606 LLVM_DEBUG(dbgs() << "%bb." << MBBNum << " [" << Start << ';' << Stop
1607 << ") intf " << LeaveBefore << '-' << EnterAfter
1608 << ", live-through " << IntvIn << " -> " << IntvOut);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001609
1610 assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1611
Jakob Stoklund Olesenfe9b2d12011-07-23 03:32:26 +00001612 assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
1613 assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
1614 assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
1615
1616 MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
1617
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001618 if (!IntvOut) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001619 LLVM_DEBUG(dbgs() << ", spill on entry.\n");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001620 //
1621 // <<<<<<<<< Possible LeaveBefore interference.
1622 // |-----------| Live through.
1623 // -____________ Spill on entry.
1624 //
1625 selectIntv(IntvIn);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001626 SlotIndex Idx = leaveIntvAtTop(*MBB);
1627 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1628 (void)Idx;
1629 return;
1630 }
1631
1632 if (!IntvIn) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001633 LLVM_DEBUG(dbgs() << ", reload on exit.\n");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001634 //
1635 // >>>>>>> Possible EnterAfter interference.
1636 // |-----------| Live through.
1637 // ___________-- Reload on exit.
1638 //
1639 selectIntv(IntvOut);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001640 SlotIndex Idx = enterIntvAtEnd(*MBB);
1641 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1642 (void)Idx;
1643 return;
1644 }
1645
1646 if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001647 LLVM_DEBUG(dbgs() << ", straight through.\n");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001648 //
1649 // |-----------| Live through.
1650 // ------------- Straight through, same intv, no interference.
1651 //
1652 selectIntv(IntvOut);
1653 useIntv(Start, Stop);
1654 return;
1655 }
1656
1657 // We cannot legally insert splits after LSP.
1658 SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
Jakob Stoklund Olesenfe9b2d12011-07-23 03:32:26 +00001659 assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001660
1661 if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1662 LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001663 LLVM_DEBUG(dbgs() << ", switch avoiding interference.\n");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001664 //
1665 // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
1666 // |-----------| Live through.
1667 // ------======= Switch intervals between interference.
1668 //
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001669 selectIntv(IntvOut);
Jakob Stoklund Olesenfe9b2d12011-07-23 03:32:26 +00001670 SlotIndex Idx;
1671 if (LeaveBefore && LeaveBefore < LSP) {
1672 Idx = enterIntvBefore(LeaveBefore);
1673 useIntv(Idx, Stop);
1674 } else {
1675 Idx = enterIntvAtEnd(*MBB);
1676 }
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001677 selectIntv(IntvIn);
1678 useIntv(Start, Idx);
1679 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1680 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1681 return;
1682 }
1683
Nicola Zaghen0818e782018-05-14 12:53:11 +00001684 LLVM_DEBUG(dbgs() << ", create local intv for interference.\n");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001685 //
1686 // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
1687 // |-----------| Live through.
1688 // ==---------== Switch intervals before/after interference.
1689 //
1690 assert(LeaveBefore <= EnterAfter && "Missed case");
1691
1692 selectIntv(IntvOut);
1693 SlotIndex Idx = enterIntvAfter(EnterAfter);
1694 useIntv(Idx, Stop);
1695 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1696
1697 selectIntv(IntvIn);
1698 Idx = leaveIntvBefore(LeaveBefore);
1699 useIntv(Start, Idx);
1700 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1701}
1702
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001703void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1704 unsigned IntvIn, SlotIndex LeaveBefore) {
1705 SlotIndex Start, Stop;
Benjamin Kramera4f0aad2014-03-02 13:30:33 +00001706 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001707
Nicola Zaghen0818e782018-05-14 12:53:11 +00001708 LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';'
1709 << Stop << "), uses " << BI.FirstInstr << '-'
1710 << BI.LastInstr << ", reg-in " << IntvIn
1711 << ", leave before " << LeaveBefore
1712 << (BI.LiveOut ? ", stack-out" : ", killed in block"));
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001713
1714 assert(IntvIn && "Must have register in");
1715 assert(BI.LiveIn && "Must be live-in");
1716 assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1717
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001718 if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001719 LLVM_DEBUG(dbgs() << " before interference.\n");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001720 //
1721 // <<< Interference after kill.
1722 // |---o---x | Killed in block.
1723 // ========= Use IntvIn everywhere.
1724 //
1725 selectIntv(IntvIn);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001726 useIntv(Start, BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001727 return;
1728 }
1729
1730 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1731
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001732 if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001733 //
1734 // <<< Possible interference after last use.
1735 // |---o---o---| Live-out on stack.
1736 // =========____ Leave IntvIn after last use.
1737 //
1738 // < Interference after last use.
1739 // |---o---o--o| Live-out on stack, late last use.
1740 // ============ Copy to stack after LSP, overlap IntvIn.
1741 // \_____ Stack interval is live-out.
1742 //
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001743 if (BI.LastInstr < LSP) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001744 LLVM_DEBUG(dbgs() << ", spill after last use before interference.\n");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001745 selectIntv(IntvIn);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001746 SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001747 useIntv(Start, Idx);
1748 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1749 } else {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001750 LLVM_DEBUG(dbgs() << ", spill before last split point.\n");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001751 selectIntv(IntvIn);
Jakob Stoklund Olesenaf4e40c2011-07-16 00:13:30 +00001752 SlotIndex Idx = leaveIntvBefore(LSP);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001753 overlapIntv(Idx, BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001754 useIntv(Start, Idx);
1755 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1756 }
1757 return;
1758 }
1759
1760 // The interference is overlapping somewhere we wanted to use IntvIn. That
1761 // means we need to create a local interval that can be allocated a
1762 // different register.
1763 unsigned LocalIntv = openIntv();
Matt Beaumont-Gayf9d7fb62011-07-16 04:18:47 +00001764 (void)LocalIntv;
Nicola Zaghen0818e782018-05-14 12:53:11 +00001765 LLVM_DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001766
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001767 if (!BI.LiveOut || BI.LastInstr < LSP) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001768 //
1769 // <<<<<<< Interference overlapping uses.
1770 // |---o---o---| Live-out on stack.
1771 // =====----____ Leave IntvIn before interference, then spill.
1772 //
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001773 SlotIndex To = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001774 SlotIndex From = enterIntvBefore(LeaveBefore);
1775 useIntv(From, To);
1776 selectIntv(IntvIn);
1777 useIntv(Start, From);
1778 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1779 return;
1780 }
1781
1782 // <<<<<<< Interference overlapping uses.
1783 // |---o---o--o| Live-out on stack, late last use.
1784 // =====------- Copy to stack before LSP, overlap LocalIntv.
1785 // \_____ Stack interval is live-out.
1786 //
1787 SlotIndex To = leaveIntvBefore(LSP);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001788 overlapIntv(To, BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001789 SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1790 useIntv(From, To);
1791 selectIntv(IntvIn);
1792 useIntv(Start, From);
1793 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1794}
1795
1796void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1797 unsigned IntvOut, SlotIndex EnterAfter) {
1798 SlotIndex Start, Stop;
Benjamin Kramera4f0aad2014-03-02 13:30:33 +00001799 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001800
Nicola Zaghen0818e782018-05-14 12:53:11 +00001801 LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';'
1802 << Stop << "), uses " << BI.FirstInstr << '-'
1803 << BI.LastInstr << ", reg-out " << IntvOut
1804 << ", enter after " << EnterAfter
1805 << (BI.LiveIn ? ", stack-in" : ", defined in block"));
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001806
1807 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1808
1809 assert(IntvOut && "Must have register out");
1810 assert(BI.LiveOut && "Must be live-out");
1811 assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1812
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001813 if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001814 LLVM_DEBUG(dbgs() << " after interference.\n");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001815 //
1816 // >>>> Interference before def.
1817 // | o---o---| Defined in block.
1818 // ========= Use IntvOut everywhere.
1819 //
1820 selectIntv(IntvOut);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001821 useIntv(BI.FirstInstr, Stop);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001822 return;
1823 }
1824
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001825 if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
Nicola Zaghen0818e782018-05-14 12:53:11 +00001826 LLVM_DEBUG(dbgs() << ", reload after interference.\n");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001827 //
1828 // >>>> Interference before def.
1829 // |---o---o---| Live-through, stack-in.
1830 // ____========= Enter IntvOut before first use.
1831 //
1832 selectIntv(IntvOut);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001833 SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001834 useIntv(Idx, Stop);
1835 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1836 return;
1837 }
1838
1839 // The interference is overlapping somewhere we wanted to use IntvOut. That
1840 // means we need to create a local interval that can be allocated a
1841 // different register.
Nicola Zaghen0818e782018-05-14 12:53:11 +00001842 LLVM_DEBUG(dbgs() << ", interference overlaps uses.\n");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001843 //
1844 // >>>>>>> Interference overlapping uses.
1845 // |---o---o---| Live-through, stack-in.
1846 // ____---====== Create local interval for interference range.
1847 //
1848 selectIntv(IntvOut);
1849 SlotIndex Idx = enterIntvAfter(EnterAfter);
1850 useIntv(Idx, Stop);
1851 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1852
1853 openIntv();
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001854 SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001855 useIntv(From, Idx);
1856}