blob: c99c3b09d88af66549b17459cdaf3a96bf946973 [file] [log] [blame]
Eugene Zelenko900b6332017-08-29 22:32:07 +00001//===- SplitKit.cpp - Toolkit for splitting live ranges -------------------===//
Jakob Stoklund Olesen36d12c62010-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 Olesen36d12c62010-07-20 15:41:07 +000015#include "SplitKit.h"
Eugene Zelenko900b6332017-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 Olesen503b1432011-03-02 23:05:19 +000023#include "llvm/ADT/Statistic.h"
Eugene Zelenko900b6332017-08-29 22:32:07 +000024#include "llvm/CodeGen/LiveInterval.h"
Matthias Braunf8422972017-12-13 02:51:04 +000025#include "llvm/CodeGen/LiveIntervals.h"
Pete Cooper3ca96f92012-04-02 22:44:18 +000026#include "llvm/CodeGen/LiveRangeEdit.h"
Eugene Zelenko900b6332017-08-29 22:32:07 +000027#include "llvm/CodeGen/MachineBasicBlock.h"
Wei Mi9a16d652016-04-13 03:08:27 +000028#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Jakob Stoklund Olesene172a8b2010-10-28 20:34:50 +000029#include "llvm/CodeGen/MachineDominators.h"
Eugene Zelenko900b6332017-08-29 22:32:07 +000030#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstr.h"
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +000032#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +000033#include "llvm/CodeGen/MachineLoopInfo.h"
Eugene Zelenko900b6332017-08-29 22:32:07 +000034#include "llvm/CodeGen/MachineOperand.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko900b6332017-08-29 22:32:07 +000036#include "llvm/CodeGen/SlotIndexes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000037#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-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 Olesen26c9d702012-11-28 19:13:06 +000041#include "llvm/CodeGen/VirtRegMap.h"
Eugene Zelenko900b6332017-08-29 22:32:07 +000042#include "llvm/IR/DebugLoc.h"
43#include "llvm/MC/LaneBitmask.h"
44#include "llvm/Support/Allocator.h"
45#include "llvm/Support/BlockFrequency.h"
46#include "llvm/Support/Compiler.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000047#include "llvm/Support/Debug.h"
Eugene Zelenko900b6332017-08-29 22:32:07 +000048#include "llvm/Support/ErrorHandling.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000049#include "llvm/Support/raw_ostream.h"
Eugene Zelenko900b6332017-08-29 22:32:07 +000050#include <algorithm>
51#include <cassert>
52#include <iterator>
53#include <limits>
54#include <tuple>
55#include <utility>
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000056
57using namespace llvm;
58
Chandler Carruth1b9dde02014-04-22 02:02:50 +000059#define DEBUG_TYPE "regalloc"
60
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +000061STATISTIC(NumFinished, "Number of splits finished");
62STATISTIC(NumSimple, "Number of splits that were simple");
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +000063STATISTIC(NumCopies, "Number of copies inserted for splitting");
64STATISTIC(NumRemats, "Number of rematerialized defs for splitting");
Jakob Stoklund Olesen50215af2011-05-10 17:37:41 +000065STATISTIC(NumRepairs, "Number of invalid live ranges repaired");
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +000066
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000067//===----------------------------------------------------------------------===//
Wei Mi35ee9332016-05-11 22:28:29 +000068// Last Insert Point Analysis
69//===----------------------------------------------------------------------===//
70
71InsertPointAnalysis::InsertPointAnalysis(const LiveIntervals &lis,
72 unsigned BBNum)
Wei Mif3c8f532016-05-23 19:39:19 +000073 : LIS(lis), LastInsertPoint(BBNum) {}
Wei Mi35ee9332016-05-11 22:28:29 +000074
75SlotIndex
Wei Mif3c8f532016-05-23 19:39:19 +000076InsertPointAnalysis::computeLastInsertPoint(const LiveInterval &CurLI,
77 const MachineBasicBlock &MBB) {
Wei Mi35ee9332016-05-11 22:28:29 +000078 unsigned Num = MBB.getNumber();
79 std::pair<SlotIndex, SlotIndex> &LIP = LastInsertPoint[Num];
80 SlotIndex MBBEnd = LIS.getMBBEndIdx(&MBB);
81
Hiroshi Inoue713b5ba2017-07-09 05:54:44 +000082 SmallVector<const MachineBasicBlock *, 1> EHPadSuccessors;
Wei Mi35ee9332016-05-11 22:28:29 +000083 for (const MachineBasicBlock *SMBB : MBB.successors())
84 if (SMBB->isEHPad())
Hiroshi Inoue713b5ba2017-07-09 05:54:44 +000085 EHPadSuccessors.push_back(SMBB);
Wei Mi35ee9332016-05-11 22:28:29 +000086
87 // Compute insert points on the first call. The pair is independent of the
88 // current live interval.
89 if (!LIP.first.isValid()) {
90 MachineBasicBlock::const_iterator FirstTerm = MBB.getFirstTerminator();
91 if (FirstTerm == MBB.end())
92 LIP.first = MBBEnd;
93 else
94 LIP.first = LIS.getInstructionIndex(*FirstTerm);
95
96 // If there is a landing pad successor, also find the call instruction.
Hiroshi Inoue713b5ba2017-07-09 05:54:44 +000097 if (EHPadSuccessors.empty())
Wei Mi35ee9332016-05-11 22:28:29 +000098 return LIP.first;
99 // There may not be a call instruction (?) in which case we ignore LPad.
100 LIP.second = LIP.first;
101 for (MachineBasicBlock::const_iterator I = MBB.end(), E = MBB.begin();
102 I != E;) {
103 --I;
104 if (I->isCall()) {
105 LIP.second = LIS.getInstructionIndex(*I);
106 break;
107 }
108 }
109 }
110
111 // If CurLI is live into a landing pad successor, move the last insert point
112 // back to the call that may throw.
113 if (!LIP.second)
114 return LIP.first;
115
Hiroshi Inoue713b5ba2017-07-09 05:54:44 +0000116 if (none_of(EHPadSuccessors, [&](const MachineBasicBlock *EHPad) {
Wei Mif3c8f532016-05-23 19:39:19 +0000117 return LIS.isLiveInToMBB(CurLI, EHPad);
Wei Mi35ee9332016-05-11 22:28:29 +0000118 }))
119 return LIP.first;
120
121 // Find the value leaving MBB.
Wei Mif3c8f532016-05-23 19:39:19 +0000122 const VNInfo *VNI = CurLI.getVNInfoBefore(MBBEnd);
Wei Mi35ee9332016-05-11 22:28:29 +0000123 if (!VNI)
124 return LIP.first;
125
126 // If the value leaving MBB was defined after the call in MBB, it can't
127 // really be live-in to the landing pad. This can happen if the landing pad
128 // has a PHI, and this register is undef on the exceptional edge.
129 // <rdar://problem/10664933>
130 if (!SlotIndex::isEarlierInstr(VNI->def, LIP.second) && VNI->def < MBBEnd)
131 return LIP.first;
132
133 // Value is properly live-in to the landing pad.
134 // Only allow inserts before the call.
135 return LIP.second;
136}
137
138MachineBasicBlock::iterator
Wei Mif3c8f532016-05-23 19:39:19 +0000139InsertPointAnalysis::getLastInsertPointIter(const LiveInterval &CurLI,
140 MachineBasicBlock &MBB) {
141 SlotIndex LIP = getLastInsertPoint(CurLI, MBB);
Wei Mi35ee9332016-05-11 22:28:29 +0000142 if (LIP == LIS.getMBBEndIdx(&MBB))
143 return MBB.end();
144 return LIS.getInstructionFromIndex(LIP);
145}
146
147//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000148// Split Analysis
149//===----------------------------------------------------------------------===//
150
Eric Christopherd9134482014-08-04 21:25:23 +0000151SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
Jakob Stoklund Olesen0fef9dd2010-07-20 23:50:15 +0000152 const MachineLoopInfo &mli)
Eric Christopherd9134482014-08-04 21:25:23 +0000153 : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli),
Eugene Zelenko900b6332017-08-29 22:32:07 +0000154 TII(*MF.getSubtarget().getInstrInfo()), IPA(lis, MF.getNumBlockIDs()) {}
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000155
156void SplitAnalysis::clear() {
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000157 UseSlots.clear();
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000158 UseBlocks.clear();
159 ThroughBlocks.clear();
Craig Topperc0196b12014-04-14 00:51:57 +0000160 CurLI = nullptr;
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000161 DidRepairRange = false;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000162}
163
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000164/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000165void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000166 assert(UseSlots.empty() && "Call clear first");
167
168 // First get all the defs from the interval values. This provides the correct
169 // slots for early clobbers.
Matthias Braun96761952014-12-10 23:07:54 +0000170 for (const VNInfo *VNI : CurLI->valnos)
171 if (!VNI->isPHIDef() && !VNI->isUnused())
172 UseSlots.push_back(VNI->def);
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000173
174 // Get use slots form the use-def chain.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000175 const MachineRegisterInfo &MRI = MF.getRegInfo();
Owen Andersonb36376e2014-03-17 19:36:09 +0000176 for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg))
177 if (!MO.isUndef())
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000178 UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot());
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000179
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000180 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000181
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000182 // Remove duplicates, keeping the smaller slot for each instruction.
183 // That is what we want for early clobbers.
184 UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
185 SlotIndex::isSameInstr),
186 UseSlots.end());
187
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000188 // Compute per-live block info.
189 if (!calcLiveBlockInfo()) {
190 // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
Rafael Espindola676c4052011-06-26 22:34:10 +0000191 // I am looking at you, RegisterCoalescer!
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000192 DidRepairRange = true;
Jakob Stoklund Olesen50215af2011-05-10 17:37:41 +0000193 ++NumRepairs;
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000194 DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
195 const_cast<LiveIntervals&>(LIS)
196 .shrinkToUses(const_cast<LiveInterval*>(CurLI));
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000197 UseBlocks.clear();
198 ThroughBlocks.clear();
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000199 bool fixed = calcLiveBlockInfo();
200 (void)fixed;
201 assert(fixed && "Couldn't fix broken live interval");
202 }
203
Jakob Stoklund Olesenbd6b86e2011-03-27 22:49:23 +0000204 DEBUG(dbgs() << "Analyze counted "
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000205 << UseSlots.size() << " instrs in "
206 << UseBlocks.size() << " blocks, through "
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000207 << NumThroughBlocks << " blocks.\n");
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000208}
209
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000210/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
211/// where CurLI is live.
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000212bool SplitAnalysis::calcLiveBlockInfo() {
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000213 ThroughBlocks.resize(MF.getNumBlockIDs());
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000214 NumThroughBlocks = NumGapBlocks = 0;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000215 if (CurLI->empty())
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000216 return true;
Jakob Stoklund Olesenb1b76ad2011-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 Smithf1ff53e2015-10-09 22:56:24 +0000226 MachineFunction::iterator MFI =
227 LIS.getMBBFromIndex(LVI->start)->getIterator();
Eugene Zelenko900b6332017-08-29 22:32:07 +0000228 while (true) {
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000229 BlockInfo BI;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000230 BI.MBB = &*MFI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000231 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000232 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000233
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000234 // If the block contains no uses, the range must be live through. At one
Rafael Espindola676c4052011-06-26 22:34:10 +0000235 // point, RegisterCoalescer could create dangling ranges that ended
Jakob Stoklund Olesenec43d5d2011-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 Olesen43859a62011-08-02 22:54:14 +0000246 BI.FirstInstr = *UseI;
247 assert(BI.FirstInstr >= Start);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000248 do ++UseI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000249 while (UseI != UseE && *UseI < Stop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000250 BI.LastInstr = UseI[-1];
251 assert(BI.LastInstr < Stop);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000252
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000253 // LVI is the first live segment overlapping MBB.
254 BI.LiveIn = LVI->start <= Start;
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000255
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000256 // When not live in, the first use should be a def.
257 if (!BI.LiveIn) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000258 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000259 assert(LVI->start == BI.FirstInstr && "First instr should be a def");
260 BI.FirstDef = BI.FirstInstr;
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000261 }
262
Jakob Stoklund Olesenec43d5d2011-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 Olesen43859a62011-08-02 22:54:14 +0000269 BI.LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000270 break;
271 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000272
Jakob Stoklund Olesenec43d5d2011-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 Olesenec43d5d2011-05-30 01:33:26 +0000279 BI.LiveOut = false;
280 UseBlocks.push_back(BI);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000281 UseBlocks.back().LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-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 Olesen43859a62011-08-02 22:54:14 +0000286 BI.FirstInstr = BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000287 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000288
Matthias Braun13ddb7c2013-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 Olesenae8027c2011-08-02 22:37:22 +0000291 if (!BI.FirstDef)
292 BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000293 }
294
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000295 UseBlocks.push_back(BI);
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000296
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000297 // LVI is now at LVE or LVI->end >= Stop.
298 if (LVI == LVE)
299 break;
300 }
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000301
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000302 // Live segment ends exactly at Stop. Move to the next segment.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000303 if (LVI->end == Stop && ++LVI == LVE)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000304 break;
305
306 // Pick the next basic block.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000307 if (LVI->start < Stop)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000308 ++MFI;
309 else
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000310 MFI = LIS.getMBBFromIndex(LVI->start)->getIterator();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000311 }
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000312
313 assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000314 return true;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000315}
316
Jakob Stoklund Oleseneef23272011-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 Smithf1ff53e2015-10-09 22:56:24 +0000326 MachineFunction::const_iterator MFI =
327 LIS.getMBBFromIndex(LVI->start)->getIterator();
328 SlotIndex Stop = LIS.getMBBEndIdx(&*MFI);
Eugene Zelenko900b6332017-08-29 22:32:07 +0000329 while (true) {
Jakob Stoklund Oleseneef23272011-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 Smithf1ff53e2015-10-09 22:56:24 +0000336 Stop = LIS.getMBBEndIdx(&*MFI);
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000337 } while (Stop <= LVI->start);
338 }
339}
340
Jakob Stoklund Olesen60a26a62011-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 Olesen36d12c62010-07-20 15:41:07 +0000355void SplitAnalysis::analyze(const LiveInterval *li) {
356 clear();
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000357 CurLI = li;
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000358 analyzeUses();
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000359}
360
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000361//===----------------------------------------------------------------------===//
362// Split Editor
363//===----------------------------------------------------------------------===//
364
365/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Wei Mic0223702016-07-08 21:08:09 +0000366SplitEditor::SplitEditor(SplitAnalysis &sa, AliasAnalysis &aa,
367 LiveIntervals &lis, VirtRegMap &vrm,
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000368 MachineDominatorTree &mdt,
369 MachineBlockFrequencyInfo &mbfi)
Wei Mic0223702016-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 Christopher60621802014-10-14 07:22:00 +0000373 TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()),
Eugene Zelenko900b6332017-08-29 22:32:07 +0000374 MBFI(mbfi), RegAssign(Allocator) {}
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000375
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000376void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
377 Edit = &LRE;
378 SpillMode = SM;
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000379 OpenIdx = 0;
380 RegAssign.clear();
381 Values.clear();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000382
383 // Reset the LiveRangeCalc instances needed for this spill mode.
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000384 LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
385 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000386 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000387 LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
388 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000389
Jakob Stoklund Olesen6ee7d9aa2010-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 Topperc0196b12014-04-14 00:51:57 +0000392 Edit->anyRematerializable(nullptr);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000393}
394
Aaron Ballman615eb472017-10-15 14:32:27 +0000395#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000396LLVM_DUMP_METHOD void SplitEditor::dump() const {
Eric Christopherede62672011-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 Olesen6f8bd422010-09-21 22:32:21 +0000405}
Manman Ren742534c2012-09-06 19:06:06 +0000406#endif
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000407
Krzysztof Parzyszeka7ed0902016-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 IVb42e0e72016-08-25 02:15:54 +0000414}
Krzysztof Parzyszeka7ed0902016-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 Parzyszek91b5cf82016-12-15 14:36:06 +0000439 LaneBitmask LM;
Krzysztof Parzyszeka7ed0902016-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 Parzyszekea9f8ce2016-12-16 19:11:56 +0000452 if ((S.LaneMask & LM).any())
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000453 S.createDeadDef(Def, LIS.getVNInfoAllocator());
454 }
455}
456
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000457VNInfo *SplitEditor::defValue(unsigned RegIdx,
458 const VNInfo *ParentVNI,
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000459 SlotIndex Idx,
460 bool Original) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000461 assert(ParentVNI && "Mapping NULL value");
462 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000463 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
Mark Laceyf9ea8852013-08-14 23:50:04 +0000464 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000465
466 // Create a new value.
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000467 VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000468
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000469 bool Force = LI->hasSubRanges();
470 ValueForcePair FP(Force ? nullptr : VNI, Force);
Jakob Stoklund Olesen8ef91fc2011-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 Parzyszeka7ed0902016-08-24 13:37:55 +0000473 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), FP));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000474
Krzysztof Parzyszeka7ed0902016-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 Olesen8ef91fc2011-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 Olesen5d4277d2011-09-13 23:09:04 +0000481 if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
Krzysztof Parzyszeka7ed0902016-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 Olesen8ef91fc2011-03-01 23:14:53 +0000487 }
488
489 // This is a complex mapping, add liveness for VNI
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000490 addDeadDef(*LI, VNI, Original);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000491 return VNI;
492}
493
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000494void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000495 assert(ParentVNI && "Mapping NULL value");
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000496 ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
497 VNInfo *VNI = VFP.getPointer();
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000498
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000499 // ParentVNI was either unmapped or already complex mapped. Either way, just
500 // set the force bit.
501 if (!VNI) {
502 VFP.setInt(true);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000503 return;
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000504 }
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000505
506 // This was previously a single mapping. Make sure the old def is represented
507 // by a trivial live range.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000508 addDeadDef(LIS.getInterval(Edit->get(RegIdx)), VNI, false);
509
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000510 // Mark as complex mapped, forced.
Craig Topperc0196b12014-04-14 00:51:57 +0000511 VFP = ValueForcePair(nullptr, true);
Jakob Stoklund Olesen4484f992011-09-13 18:05:29 +0000512}
513
Matthias Braunf0b68d32017-03-17 00:41:39 +0000514SlotIndex SplitEditor::buildSingleSubRegCopy(unsigned FromReg, unsigned ToReg,
515 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
516 unsigned SubIdx, LiveInterval &DestLI, bool Late, SlotIndex Def) {
517 const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
518 bool FirstCopy = !Def.isValid();
519 MachineInstr *CopyMI = BuildMI(MBB, InsertBefore, DebugLoc(), Desc)
520 .addReg(ToReg, RegState::Define | getUndefRegState(FirstCopy)
521 | getInternalReadRegState(!FirstCopy), SubIdx)
522 .addReg(FromReg, 0, SubIdx);
523
524 BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator();
525 if (FirstCopy) {
526 SlotIndexes &Indexes = *LIS.getSlotIndexes();
527 Def = Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
Matthias Braunf0b68d32017-03-17 00:41:39 +0000528 } else {
529 CopyMI->bundleWithPred();
530 }
531 LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubIdx);
532 DestLI.refineSubRanges(Allocator, LaneMask,
533 [Def, &Allocator](LiveInterval::SubRange& SR) {
534 SR.createDeadDef(Def, Allocator);
535 });
536 return Def;
537}
538
539SlotIndex SplitEditor::buildCopy(unsigned FromReg, unsigned ToReg,
540 LaneBitmask LaneMask, MachineBasicBlock &MBB,
541 MachineBasicBlock::iterator InsertBefore, bool Late, unsigned RegIdx) {
542 const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
543 if (LaneMask.all() || LaneMask == MRI.getMaxLaneMaskForVReg(FromReg)) {
544 // The full vreg is copied.
545 MachineInstr *CopyMI =
546 BuildMI(MBB, InsertBefore, DebugLoc(), Desc, ToReg).addReg(FromReg);
547 SlotIndexes &Indexes = *LIS.getSlotIndexes();
548 return Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
549 }
550
551 // Only a subset of lanes needs to be copied. The following is a simple
552 // heuristic to construct a sequence of COPYs. We could add a target
553 // specific callback if this turns out to be suboptimal.
554 LiveInterval &DestLI = LIS.getInterval(Edit->get(RegIdx));
555
556 // First pass: Try to find a perfectly matching subregister index. If none
557 // exists find the one covering the most lanemask bits.
558 SmallVector<unsigned, 8> PossibleIndexes;
559 unsigned BestIdx = 0;
560 unsigned BestCover = 0;
561 const TargetRegisterClass *RC = MRI.getRegClass(FromReg);
562 assert(RC == MRI.getRegClass(ToReg) && "Should have same reg class");
563 for (unsigned Idx = 1, E = TRI.getNumSubRegIndices(); Idx < E; ++Idx) {
564 // Is this index even compatible with the given class?
565 if (TRI.getSubClassWithSubReg(RC, Idx) != RC)
566 continue;
567 LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(Idx);
568 // Early exit if we found a perfect match.
569 if (SubRegMask == LaneMask) {
570 BestIdx = Idx;
571 break;
572 }
573
574 // The index must not cover any lanes outside \p LaneMask.
575 if ((SubRegMask & ~LaneMask).any())
576 continue;
577
Krzysztof Parzyszekf3a778d2017-07-20 19:43:19 +0000578 unsigned PopCount = SubRegMask.getNumLanes();
Matthias Braunf0b68d32017-03-17 00:41:39 +0000579 PossibleIndexes.push_back(Idx);
580 if (PopCount > BestCover) {
581 BestCover = PopCount;
582 BestIdx = Idx;
583 }
584 }
585
586 // Abort if we cannot possibly implement the COPY with the given indexes.
587 if (BestIdx == 0)
588 report_fatal_error("Impossible to implement partial COPY");
589
590 SlotIndex Def = buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore,
591 BestIdx, DestLI, Late, SlotIndex());
592
593 // Greedy heuristic: Keep iterating keeping the best covering subreg index
594 // each time.
Matthias Braun76f06302017-06-12 20:30:52 +0000595 LaneBitmask LanesLeft = LaneMask & ~(TRI.getSubRegIndexLaneMask(BestIdx));
Matthias Braunf0b68d32017-03-17 00:41:39 +0000596 while (LanesLeft.any()) {
597 unsigned BestIdx = 0;
Eugene Zelenko900b6332017-08-29 22:32:07 +0000598 int BestCover = std::numeric_limits<int>::min();
Matthias Braunf0b68d32017-03-17 00:41:39 +0000599 for (unsigned Idx : PossibleIndexes) {
600 LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(Idx);
601 // Early exit if we found a perfect match.
602 if (SubRegMask == LanesLeft) {
603 BestIdx = Idx;
604 break;
605 }
606
607 // Try to cover as much of the remaining lanes as possible but
608 // as few of the already covered lanes as possible.
Krzysztof Parzyszekf3a778d2017-07-20 19:43:19 +0000609 int Cover = (SubRegMask & LanesLeft).getNumLanes()
610 - (SubRegMask & ~LanesLeft).getNumLanes();
Matthias Braunf0b68d32017-03-17 00:41:39 +0000611 if (Cover > BestCover) {
612 BestCover = Cover;
613 BestIdx = Idx;
614 }
615 }
616
617 if (BestIdx == 0)
618 report_fatal_error("Impossible to implement partial COPY");
619
620 buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore, BestIdx,
621 DestLI, Late, Def);
622 LanesLeft &= ~TRI.getSubRegIndexLaneMask(BestIdx);
623 }
624
625 return Def;
626}
627
Eric Christopherede62672011-02-03 06:18:29 +0000628VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000629 VNInfo *ParentVNI,
630 SlotIndex UseIdx,
631 MachineBasicBlock &MBB,
632 MachineBasicBlock::iterator I) {
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000633 SlotIndex Def;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000634 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000635
Jakob Stoklund Olesen7d406792011-05-02 05:29:58 +0000636 // We may be trying to avoid interference that ends at a deleted instruction,
637 // so always begin RegIdx 0 early and all others late.
638 bool Late = RegIdx != 0;
639
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000640 // Attempt cheap-as-a-copy rematerialization.
Wei Mi9a16d652016-04-13 03:08:27 +0000641 unsigned Original = VRM.getOriginal(Edit->get(RegIdx));
642 LiveInterval &OrigLI = LIS.getInterval(Original);
643 VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
Wei Mi9a16d652016-04-13 03:08:27 +0000644
Matthias Braunf0b68d32017-03-17 00:41:39 +0000645 unsigned Reg = LI->reg;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000646 bool DidRemat = false;
647 if (OrigVNI) {
648 LiveRangeEdit::Remat RM(ParentVNI);
649 RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
650 if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) {
Matthias Braunf0b68d32017-03-17 00:41:39 +0000651 Def = Edit->rematerializeAt(MBB, I, Reg, RM, TRI, Late);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000652 ++NumRemats;
653 DidRemat = true;
654 }
655 }
656 if (!DidRemat) {
Matthias Braunf0b68d32017-03-17 00:41:39 +0000657 LaneBitmask LaneMask;
Stanislav Mekhanoshin70c245e2017-02-01 01:18:36 +0000658 if (LI->hasSubRanges()) {
Matthias Braunf0b68d32017-03-17 00:41:39 +0000659 LaneMask = LaneBitmask::getNone();
Stanislav Mekhanoshin70c245e2017-02-01 01:18:36 +0000660 for (LiveInterval::SubRange &S : LI->subranges())
Matthias Braunf0b68d32017-03-17 00:41:39 +0000661 LaneMask |= S.LaneMask;
662 } else {
663 LaneMask = LaneBitmask::getAll();
Stanislav Mekhanoshin70c245e2017-02-01 01:18:36 +0000664 }
Matthias Braunf0b68d32017-03-17 00:41:39 +0000665
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000666 ++NumCopies;
Matthias Braunf0b68d32017-03-17 00:41:39 +0000667 Def = buildCopy(Edit->getReg(), Reg, LaneMask, MBB, I, Late, RegIdx);
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000668 }
669
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000670 // Define the value in Reg.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000671 return defValue(RegIdx, ParentVNI, Def, false);
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000672}
673
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000674/// Create a new virtual register and live interval.
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000675unsigned SplitEditor::openIntv() {
Eric Christopherede62672011-02-03 06:18:29 +0000676 // Create the complement as index 0.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000677 if (Edit->empty())
Mark Lacey9d8103d2013-08-14 23:50:16 +0000678 Edit->createEmptyInterval();
Eric Christopherede62672011-02-03 06:18:29 +0000679
680 // Create the open interval.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000681 OpenIdx = Edit->size();
Mark Lacey9d8103d2013-08-14 23:50:16 +0000682 Edit->createEmptyInterval();
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000683 return OpenIdx;
684}
685
686void SplitEditor::selectIntv(unsigned Idx) {
687 assert(Idx != 0 && "Cannot select the complement interval");
688 assert(Idx < Edit->size() && "Can only select previously opened interval");
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000689 DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000690 OpenIdx = Idx;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000691}
692
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000693SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000694 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000695 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000696 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000697 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000698 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000699 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000700 return Idx;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000701 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000702 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000703 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000704 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000705
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000706 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
707 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000708}
709
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000710SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
711 assert(OpenIdx && "openIntv not called before enterIntvAfter");
712 DEBUG(dbgs() << " enterIntvAfter " << Idx);
713 Idx = Idx.getBoundaryIndex();
714 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
715 if (!ParentVNI) {
716 DEBUG(dbgs() << ": not live\n");
717 return Idx;
718 }
719 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
720 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
721 assert(MI && "enterIntvAfter called with invalid index");
722
723 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000724 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000725 return VNI->def;
726}
727
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000728SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000729 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000730 SlotIndex End = LIS.getMBBEndIdx(&MBB);
731 SlotIndex Last = End.getPrevSlot();
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000732 DEBUG(dbgs() << " enterIntvAtEnd " << printMBBReference(MBB) << ", "
733 << Last);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000734 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000735 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000736 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000737 return End;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000738 }
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000739 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000740 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000741 SA.getLastSplitPointIter(&MBB));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000742 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopherede62672011-02-03 06:18:29 +0000743 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000744 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000745}
746
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000747/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000748void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000749 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000750}
751
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000752void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopherede62672011-02-03 06:18:29 +0000753 assert(OpenIdx && "openIntv not called before useIntv");
754 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
755 RegAssign.insert(Start, End, OpenIdx);
756 DEBUG(dump());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000757}
758
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000759SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000760 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000761 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000762
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000763 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000764 SlotIndex Boundary = Idx.getBoundaryIndex();
765 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000766 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000767 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000768 return Boundary.getNextSlot();
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000769 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000770 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000771 MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
Jakob Stoklund Olesen3d11c8e2011-02-08 18:50:18 +0000772 assert(MI && "No instruction at index");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000773
774 // In spill mode, make live ranges as short as possible by inserting the copy
775 // before MI. This is only possible if that instruction doesn't redefine the
776 // value. The inserted COPY is not a kill, and we don't need to recompute
777 // the source live range. The spiller also won't try to hoist this copy.
778 if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
779 MI->readsVirtualRegister(Edit->getReg())) {
780 forceRecompute(0, ParentVNI);
781 defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
782 return Idx;
783 }
784
785 VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000786 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000787 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000788}
789
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000790SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
791 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
792 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
793
794 // The interval must be live into the instruction at Idx.
Jakob Stoklund Olesenc45d38e2011-07-18 18:47:13 +0000795 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000796 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000797 if (!ParentVNI) {
798 DEBUG(dbgs() << ": not live\n");
799 return Idx.getNextSlot();
800 }
801 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
802
803 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
804 assert(MI && "No instruction at index");
805 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
806 return VNI->def;
807}
808
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000809SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000810 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000811 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000812 DEBUG(dbgs() << " leaveIntvAtTop " << printMBBReference(MBB) << ", "
813 << Start);
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000814
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000815 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000816 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000817 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000818 return Start;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000819 }
820
Eric Christopherede62672011-02-03 06:18:29 +0000821 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Keith Walker830a8c12016-09-16 14:07:29 +0000822 MBB.SkipPHIsLabelsAndDebug(MBB.begin()));
Eric Christopherede62672011-02-03 06:18:29 +0000823 RegAssign.insert(Start, VNI->def, OpenIdx);
824 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000825 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000826}
827
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000828void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
829 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000830 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesend7bcf432011-11-14 01:39:36 +0000831 assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000832 "Parent changes value in extended range");
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000833 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
834 "Range cannot span basic blocks");
835
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000836 // The complement interval will be extended as needed by LRCalc.extend().
Jakob Stoklund Olesen5c482cd2011-04-05 23:43:14 +0000837 if (ParentVNI)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000838 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000839 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
840 RegAssign.insert(Start, End, OpenIdx);
841 DEBUG(dump());
842}
843
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000844//===----------------------------------------------------------------------===//
845// Spill modes
846//===----------------------------------------------------------------------===//
847
848void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
Mark Laceyf9ea8852013-08-14 23:50:04 +0000849 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000850 DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
851 RegAssignMap::iterator AssignI;
852 AssignI.setMap(RegAssign);
853
854 for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
Matthias Braun311730a2015-01-21 19:02:30 +0000855 SlotIndex Def = Copies[i]->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000856 MachineInstr *MI = LIS.getInstructionFromIndex(Def);
857 assert(MI && "No instruction for back-copy");
858
859 MachineBasicBlock *MBB = MI->getParent();
860 MachineBasicBlock::iterator MBBI(MI);
861 bool AtBegin;
862 do AtBegin = MBBI == MBB->begin();
863 while (!AtBegin && (--MBBI)->isDebugValue());
864
865 DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
Matthias Braun311730a2015-01-21 19:02:30 +0000866 LIS.removeVRegDefAt(*LI, Def);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000867 LIS.RemoveMachineInstrFromMaps(*MI);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000868 MI->eraseFromParent();
869
Matthias Braun311730a2015-01-21 19:02:30 +0000870 // Adjust RegAssign if a register assignment is killed at Def. We want to
871 // avoid calculating the live range of the source register if possible.
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000872 AssignI.find(Def.getPrevSlot());
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000873 if (!AssignI.valid() || AssignI.start() >= Def)
874 continue;
875 // If MI doesn't kill the assigned register, just leave it.
876 if (AssignI.stop() != Def)
877 continue;
878 unsigned RegIdx = AssignI.value();
879 if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
880 DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n');
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000881 forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000882 } else {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000883 SlotIndex Kill = LIS.getInstructionIndex(*MBBI).getRegSlot();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000884 DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
885 AssignI.setStop(Kill);
886 }
887 }
888}
889
Jakob Stoklund Olesena98af392011-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 Zelenko900b6332017-08-29 22:32:07 +0000903 unsigned BestDepth = std::numeric_limits<unsigned>::max();
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000904
Eugene Zelenko900b6332017-08-29 22:32:07 +0000905 while (true) {
Jakob Stoklund Olesena98af392011-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) {
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000911 DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB) << " dominates "
912 << printMBBReference(*MBB) << " at depth 0\n");
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000913 return MBB;
914 }
915
916 // We'll never be able to exit the DefLoop.
917 if (Loop == DefLoop) {
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000918 DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB) << " dominates "
919 << printMBBReference(*MBB) << " in the same loop\n");
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000920 return MBB;
921 }
922
923 // Least busy dominator seen so far.
924 unsigned Depth = Loop->getLoopDepth();
925 if (Depth < BestDepth) {
926 BestMBB = MBB;
927 BestDepth = Depth;
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000928 DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB) << " dominates "
929 << printMBBReference(*MBB) << " at depth " << Depth << '\n');
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000930 }
931
932 // Leave loop by going to the immediate dominator of the loop header.
933 // This is a bigger stride than simply walking up the dominator tree.
934 MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
935
936 // Too far up the dominator tree?
937 if (!IDom || !MDT.dominates(DefDomNode, IDom))
938 return BestMBB;
939
940 MBB = IDom->getBlock();
941 }
942}
943
Wei Mi9a16d652016-04-13 03:08:27 +0000944void SplitEditor::computeRedundantBackCopies(
945 DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) {
946 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
947 LiveInterval *Parent = &Edit->getParent();
948 SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums());
949 SmallPtrSet<VNInfo *, 8> DominatedVNIs;
950
951 // Aggregate VNIs having the same value as ParentVNI.
952 for (VNInfo *VNI : LI->valnos) {
953 if (VNI->isUnused())
954 continue;
955 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
956 EqualVNs[ParentVNI->id].insert(VNI);
957 }
958
959 // For VNI aggregation of each ParentVNI, collect dominated, i.e.,
960 // redundant VNIs to BackCopies.
961 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
962 VNInfo *ParentVNI = Parent->getValNumInfo(i);
963 if (!NotToHoistSet.count(ParentVNI->id))
964 continue;
965 SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin();
966 SmallPtrSetIterator<VNInfo *> It2 = It1;
967 for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) {
968 It2 = It1;
969 for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) {
970 if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2))
971 continue;
972
973 MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def);
974 MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def);
975 if (MBB1 == MBB2) {
976 DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1));
977 } else if (MDT.dominates(MBB1, MBB2)) {
978 DominatedVNIs.insert(*It2);
979 } else if (MDT.dominates(MBB2, MBB1)) {
980 DominatedVNIs.insert(*It1);
981 }
982 }
983 }
984 if (!DominatedVNIs.empty()) {
985 forceRecompute(0, ParentVNI);
986 for (auto VNI : DominatedVNIs) {
987 BackCopies.push_back(VNI);
988 }
989 DominatedVNIs.clear();
990 }
991 }
992}
993
994/// For SM_Size mode, find a common dominator for all the back-copies for
995/// the same ParentVNI and hoist the backcopies to the dominator BB.
996/// For SM_Speed mode, if the common dominator is hot and it is not beneficial
997/// to do the hoisting, simply remove the dominated backcopies for the same
998/// ParentVNI.
999void SplitEditor::hoistCopies() {
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001000 // Get the complement interval, always RegIdx 0.
Mark Laceyf9ea8852013-08-14 23:50:04 +00001001 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001002 LiveInterval *Parent = &Edit->getParent();
1003
1004 // Track the nearest common dominator for all back-copies for each ParentVNI,
1005 // indexed by ParentVNI->id.
Eugene Zelenko900b6332017-08-29 22:32:07 +00001006 using DomPair = std::pair<MachineBasicBlock *, SlotIndex>;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001007 SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
Wei Mi9a16d652016-04-13 03:08:27 +00001008 // The total cost of all the back-copies for each ParentVNI.
1009 SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums());
1010 // The ParentVNI->id set for which hoisting back-copies are not beneficial
1011 // for Speed.
1012 DenseSet<unsigned> NotToHoistSet;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001013
1014 // Find the nearest common dominator for parent values with multiple
1015 // back-copies. If a single back-copy dominates, put it in DomPair.second.
Matthias Braun96761952014-12-10 23:07:54 +00001016 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +00001017 if (VNI->isUnused())
1018 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001019 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
1020 assert(ParentVNI && "Parent not live at complement def");
1021
1022 // Don't hoist remats. The complement is probably going to disappear
1023 // completely anyway.
1024 if (Edit->didRematerialize(ParentVNI))
1025 continue;
1026
1027 MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
Wei Mi9a16d652016-04-13 03:08:27 +00001028
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001029 DomPair &Dom = NearestDom[ParentVNI->id];
1030
1031 // Keep directly defined parent values. This is either a PHI or an
1032 // instruction in the complement range. All other copies of ParentVNI
1033 // should be eliminated.
1034 if (VNI->def == ParentVNI->def) {
1035 DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
1036 Dom = DomPair(ValMBB, VNI->def);
1037 continue;
1038 }
1039 // Skip the singly mapped values. There is nothing to gain from hoisting a
1040 // single back-copy.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001041 if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001042 DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
1043 continue;
1044 }
1045
1046 if (!Dom.first) {
1047 // First time we see ParentVNI. VNI dominates itself.
1048 Dom = DomPair(ValMBB, VNI->def);
1049 } else if (Dom.first == ValMBB) {
1050 // Two defs in the same block. Pick the earlier def.
1051 if (!Dom.second.isValid() || VNI->def < Dom.second)
1052 Dom.second = VNI->def;
1053 } else {
1054 // Different basic blocks. Check if one dominates.
1055 MachineBasicBlock *Near =
1056 MDT.findNearestCommonDominator(Dom.first, ValMBB);
1057 if (Near == ValMBB)
1058 // Def ValMBB dominates.
1059 Dom = DomPair(ValMBB, VNI->def);
1060 else if (Near != Dom.first)
1061 // None dominate. Hoist to common dominator, need new def.
1062 Dom = DomPair(Near, SlotIndex());
Wei Mi9a16d652016-04-13 03:08:27 +00001063 Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001064 }
1065
1066 DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
1067 << " for parent " << ParentVNI->id << '@' << ParentVNI->def
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00001068 << " hoist to " << printMBBReference(*Dom.first) << ' '
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001069 << Dom.second << '\n');
1070 }
1071
1072 // Insert the hoisted copies.
1073 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
1074 DomPair &Dom = NearestDom[i];
1075 if (!Dom.first || Dom.second.isValid())
1076 continue;
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +00001077 // This value needs a hoisted copy inserted at the end of Dom.first.
1078 VNInfo *ParentVNI = Parent->getValNumInfo(i);
1079 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
1080 // Get a less loopy dominator than Dom.first.
1081 Dom.first = findShallowDominator(Dom.first, DefMBB);
Wei Mi9a16d652016-04-13 03:08:27 +00001082 if (SpillMode == SM_Speed &&
1083 MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) {
1084 NotToHoistSet.insert(ParentVNI->id);
1085 continue;
1086 }
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001087 SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
1088 Dom.second =
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +00001089 defFromParent(0, ParentVNI, Last, *Dom.first,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +00001090 SA.getLastSplitPointIter(Dom.first))->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001091 }
1092
1093 // Remove redundant back-copies that are now known to be dominated by another
1094 // def with the same value.
1095 SmallVector<VNInfo*, 8> BackCopies;
Matthias Braun96761952014-12-10 23:07:54 +00001096 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +00001097 if (VNI->isUnused())
1098 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001099 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
1100 const DomPair &Dom = NearestDom[ParentVNI->id];
Wei Mi9a16d652016-04-13 03:08:27 +00001101 if (!Dom.first || Dom.second == VNI->def ||
1102 NotToHoistSet.count(ParentVNI->id))
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001103 continue;
1104 BackCopies.push_back(VNI);
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001105 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001106 }
Wei Mi9a16d652016-04-13 03:08:27 +00001107
1108 // If it is not beneficial to hoist all the BackCopies, simply remove
1109 // redundant BackCopies in speed mode.
1110 if (SpillMode == SM_Speed && !NotToHoistSet.empty())
1111 computeRedundantBackCopies(NotToHoistSet, BackCopies);
1112
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001113 removeBackCopies(BackCopies);
1114}
1115
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001116/// transferValues - Transfer all possible values to the new live ranges.
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001117/// Values that were rematerialized are left alone, they need LRCalc.extend().
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001118bool SplitEditor::transferValues() {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001119 bool Skipped = false;
1120 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Matthias Braun96761952014-12-10 23:07:54 +00001121 for (const LiveRange::Segment &S : Edit->getParent()) {
1122 DEBUG(dbgs() << " blit " << S << ':');
1123 VNInfo *ParentVNI = S.valno;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001124 // RegAssign has holes where RegIdx 0 should be used.
Matthias Braun96761952014-12-10 23:07:54 +00001125 SlotIndex Start = S.start;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001126 AssignI.advanceTo(Start);
1127 do {
1128 unsigned RegIdx;
Matthias Braun96761952014-12-10 23:07:54 +00001129 SlotIndex End = S.end;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001130 if (!AssignI.valid()) {
1131 RegIdx = 0;
1132 } else if (AssignI.start() <= Start) {
1133 RegIdx = AssignI.value();
1134 if (AssignI.stop() < End) {
1135 End = AssignI.stop();
1136 ++AssignI;
1137 }
1138 } else {
1139 RegIdx = 0;
1140 End = std::min(End, AssignI.start());
1141 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001142
1143 // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001144 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001145 << '(' << printReg(Edit->get(RegIdx)) << ')');
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001146 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001147
1148 // Check for a simply defined value that can be blitted directly.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001149 ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
1150 if (VNInfo *VNI = VFP.getPointer()) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001151 DEBUG(dbgs() << ':' << VNI->id);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001152 LI.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001153 Start = End;
1154 continue;
1155 }
1156
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001157 // Skip values with forced recomputation.
1158 if (VFP.getInt()) {
1159 DEBUG(dbgs() << "(recalc)");
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001160 Skipped = true;
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001161 Start = End;
1162 continue;
1163 }
1164
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +00001165 LiveRangeCalc &LRC = getLRCalc(RegIdx);
1166
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001167 // This value has multiple defs in RegIdx, but it wasn't rematerialized,
1168 // so the live range is accurate. Add live-in blocks in [Start;End) to the
1169 // LiveInBlocks.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001170 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001171 SlotIndex BlockStart, BlockEnd;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001172 std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001173
1174 // The first block may be live-in, or it may have its own def.
1175 if (Start != BlockStart) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001176 VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001177 assert(VNI && "Missing def for complex mapped value");
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00001178 DEBUG(dbgs() << ':' << VNI->id << "*" << printMBBReference(*MBB));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001179 // MBB has its own def. Is it also live-out?
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001180 if (BlockEnd <= End)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001181 LRC.setLiveOutValue(&*MBB, VNI);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001182
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001183 // Skip to the next block for live-in.
1184 ++MBB;
1185 BlockStart = BlockEnd;
1186 }
1187
1188 // Handle the live-in blocks covered by [Start;End).
1189 assert(Start <= BlockStart && "Expected live-in block");
1190 while (BlockStart < End) {
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00001191 DEBUG(dbgs() << ">" << printMBBReference(*MBB));
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001192 BlockEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001193 if (BlockStart == ParentVNI->def) {
1194 // This block has the def of a parent PHI, so it isn't live-in.
1195 assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001196 VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001197 assert(VNI && "Missing def for complex mapped parent PHI");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001198 if (End >= BlockEnd)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001199 LRC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001200 } else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001201 // This block needs a live-in value. The last block covered may not
1202 // be live-out.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001203 if (End < BlockEnd)
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001204 LRC.addLiveInBlock(LI, MDT[&*MBB], End);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001205 else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001206 // Live-through, and we don't know the value.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001207 LRC.addLiveInBlock(LI, MDT[&*MBB]);
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +00001208 LRC.setLiveOutValue(&*MBB, nullptr);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001209 }
1210 }
1211 BlockStart = BlockEnd;
1212 ++MBB;
1213 }
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001214 Start = End;
Matthias Braun96761952014-12-10 23:07:54 +00001215 } while (Start != S.end);
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001216 DEBUG(dbgs() << '\n');
1217 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001218
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +00001219 LRCalc[0].calculateValues();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +00001220 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +00001221 LRCalc[1].calculateValues();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001222
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001223 return Skipped;
1224}
1225
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001226static bool removeDeadSegment(SlotIndex Def, LiveRange &LR) {
1227 const LiveRange::Segment *Seg = LR.getSegmentContaining(Def);
1228 if (Seg == nullptr)
1229 return true;
1230 if (Seg->end != Def.getDeadSlot())
1231 return false;
1232 // This is a dead PHI. Remove it.
1233 LR.removeSegment(*Seg, true);
1234 return true;
1235}
1236
1237void SplitEditor::extendPHIRange(MachineBasicBlock &B, LiveRangeCalc &LRC,
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +00001238 LiveRange &LR, LaneBitmask LM,
1239 ArrayRef<SlotIndex> Undefs) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001240 for (MachineBasicBlock *P : B.predecessors()) {
1241 SlotIndex End = LIS.getMBBEndIdx(P);
1242 SlotIndex LastUse = End.getPrevSlot();
1243 // The predecessor may not have a live-out value. That is OK, like an
1244 // undef PHI operand.
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +00001245 LiveInterval &PLI = Edit->getParent();
1246 // Need the cast because the inputs to ?: would otherwise be deemed
1247 // "incompatible": SubRange vs LiveInterval.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001248 LiveRange &PSR = !LM.all() ? getSubRangeForMask(LM, PLI)
1249 : static_cast<LiveRange&>(PLI);
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +00001250 if (PSR.liveAt(LastUse))
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001251 LRC.extend(LR, End, /*PhysReg=*/0, Undefs);
1252 }
1253}
1254
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001255void SplitEditor::extendPHIKillRanges() {
Wei Mi9a16d652016-04-13 03:08:27 +00001256 // Extend live ranges to be live-out for successor PHI values.
Wei Mi9a16d652016-04-13 03:08:27 +00001257
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001258 // Visit each PHI def slot in the parent live interval. If the def is dead,
1259 // remove it. Otherwise, extend the live interval to reach the end indexes
1260 // of all predecessor blocks.
Wei Mi9a16d652016-04-13 03:08:27 +00001261
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001262 LiveInterval &ParentLI = Edit->getParent();
1263 for (const VNInfo *V : ParentLI.valnos) {
1264 if (V->isUnused() || !V->isPHIDef())
1265 continue;
1266
1267 unsigned RegIdx = RegAssign.lookup(V->def);
1268 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +00001269 LiveRangeCalc &LRC = getLRCalc(RegIdx);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001270 MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
1271 if (!removeDeadSegment(V->def, LI))
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001272 extendPHIRange(B, LRC, LI, LaneBitmask::getAll(), /*Undefs=*/{});
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001273 }
1274
1275 SmallVector<SlotIndex, 4> Undefs;
1276 LiveRangeCalc SubLRC;
1277
1278 for (LiveInterval::SubRange &PS : ParentLI.subranges()) {
1279 for (const VNInfo *V : PS.valnos) {
1280 if (V->isUnused() || !V->isPHIDef())
1281 continue;
1282 unsigned RegIdx = RegAssign.lookup(V->def);
1283 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1284 LiveInterval::SubRange &S = getSubRangeForMask(PS.LaneMask, LI);
1285 if (removeDeadSegment(V->def, S))
1286 continue;
1287
1288 MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
1289 SubLRC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
1290 &LIS.getVNInfoAllocator());
1291 Undefs.clear();
1292 LI.computeSubRangeUndefs(Undefs, PS.LaneMask, MRI, *LIS.getSlotIndexes());
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +00001293 extendPHIRange(B, SubLRC, S, PS.LaneMask, Undefs);
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +00001294 }
1295 }
1296}
1297
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001298/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001299void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001300 struct ExtPoint {
1301 ExtPoint(const MachineOperand &O, unsigned R, SlotIndex N)
1302 : MO(O), RegIdx(R), Next(N) {}
Eugene Zelenko900b6332017-08-29 22:32:07 +00001303
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001304 MachineOperand MO;
1305 unsigned RegIdx;
1306 SlotIndex Next;
1307 };
1308
1309 SmallVector<ExtPoint,4> ExtPoints;
1310
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001311 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +00001312 RE = MRI.reg_end(); RI != RE;) {
Owen Anderson16c6bf42014-03-13 23:12:04 +00001313 MachineOperand &MO = *RI;
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001314 MachineInstr *MI = MO.getParent();
1315 ++RI;
Eric Christopherede62672011-02-03 06:18:29 +00001316 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001317 if (MI->isDebugValue()) {
1318 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001319 MO.setReg(0);
1320 continue;
1321 }
Jakob Stoklund Olesenf6e03942011-02-09 21:52:09 +00001322
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +00001323 // <undef> operands don't really read the register, so it doesn't matter
1324 // which register we choose. When the use operand is tied to a def, we must
1325 // use the same register as the def, so just do that always.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +00001326 SlotIndex Idx = LIS.getInstructionIndex(*MI);
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +00001327 if (MO.isDef() || MO.isUndef())
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +00001328 Idx = Idx.getRegSlot(MO.isEarlyClobber());
Eric Christopherede62672011-02-03 06:18:29 +00001329
1330 // Rewrite to the mapped register at Idx.
1331 unsigned RegIdx = RegAssign.lookup(Idx);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001332 LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
1333 MO.setReg(LI.reg);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00001334 DEBUG(dbgs() << " rewr " << printMBBReference(*MI->getParent()) << '\t'
Eric Christopherede62672011-02-03 06:18:29 +00001335 << Idx << ':' << RegIdx << '\t' << *MI);
1336
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001337 // Extend liveness to Idx if the instruction reads reg.
Jakob Stoklund Olesen73a9eb92011-07-24 20:33:23 +00001338 if (!ExtendRanges || MO.isUndef())
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001339 continue;
1340
1341 // Skip instructions that don't read Reg.
1342 if (MO.isDef()) {
1343 if (!MO.getSubReg() && !MO.isEarlyClobber())
1344 continue;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001345 // We may want to extend a live range for a partial redef, or for a use
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001346 // tied to an early clobber.
1347 Idx = Idx.getPrevSlot();
1348 if (!Edit->getParent().liveAt(Idx))
1349 continue;
1350 } else
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +00001351 Idx = Idx.getRegSlot(true);
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +00001352
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001353 SlotIndex Next = Idx.getNextSlot();
1354 if (LI.hasSubRanges()) {
1355 // We have to delay extending subranges until we have seen all operands
1356 // defining the register. This is because a <def,read-undef> operand
1357 // will create an "undef" point, and we cannot extend any subranges
1358 // until all of them have been accounted for.
Krzysztof Parzyszek3bf4aec2016-09-02 19:48:55 +00001359 if (MO.isUse())
1360 ExtPoints.push_back(ExtPoint(MO, RegIdx, Next));
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001361 } else {
1362 LiveRangeCalc &LRC = getLRCalc(RegIdx);
1363 LRC.extend(LI, Next, 0, ArrayRef<SlotIndex>());
1364 }
1365 }
1366
1367 for (ExtPoint &EP : ExtPoints) {
1368 LiveInterval &LI = LIS.getInterval(Edit->get(EP.RegIdx));
1369 assert(LI.hasSubRanges());
1370
1371 LiveRangeCalc SubLRC;
1372 unsigned Reg = EP.MO.getReg(), Sub = EP.MO.getSubReg();
1373 LaneBitmask LM = Sub != 0 ? TRI.getSubRegIndexLaneMask(Sub)
1374 : MRI.getMaxLaneMaskForVReg(Reg);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001375 for (LiveInterval::SubRange &S : LI.subranges()) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001376 if ((S.LaneMask & LM).none())
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001377 continue;
1378 // The problem here can be that the new register may have been created
1379 // for a partially defined original register. For example:
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +00001380 // %0:subreg_hireg<def,read-undef> = ...
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001381 // ...
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001382 // %1 = COPY %0
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001383 if (S.empty())
1384 continue;
1385 SubLRC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
1386 &LIS.getVNInfoAllocator());
1387 SmallVector<SlotIndex, 4> Undefs;
1388 LI.computeSubRangeUndefs(Undefs, S.LaneMask, MRI, *LIS.getSlotIndexes());
1389 SubLRC.extend(S, EP.Next, 0, Undefs);
1390 }
1391 }
1392
1393 for (unsigned R : *Edit) {
1394 LiveInterval &LI = LIS.getInterval(R);
1395 if (!LI.hasSubRanges())
1396 continue;
1397 LI.clear();
1398 LI.removeEmptySubRanges();
1399 LIS.constructMainRangeFromSubranges(LI);
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +00001400 }
1401}
1402
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001403void SplitEditor::deleteRematVictims() {
1404 SmallVector<MachineInstr*, 8> Dead;
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001405 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
Mark Laceyf9ea8852013-08-14 23:50:04 +00001406 LiveInterval *LI = &LIS.getInterval(*I);
Matthias Braun96761952014-12-10 23:07:54 +00001407 for (const LiveRange::Segment &S : LI->segments) {
Jakob Stoklund Olesend8f24052011-11-13 22:42:13 +00001408 // Dead defs end at the dead slot.
Matthias Braun96761952014-12-10 23:07:54 +00001409 if (S.end != S.valno->def.getDeadSlot())
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001410 continue;
Wei Mi9a16d652016-04-13 03:08:27 +00001411 if (S.valno->isPHIDef())
1412 continue;
Matthias Braun96761952014-12-10 23:07:54 +00001413 MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001414 assert(MI && "Missing instruction for dead def");
1415 MI->addRegisterDead(LI->reg, &TRI);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001416
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001417 if (!MI->allDefsAreDead())
1418 continue;
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001419
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001420 DEBUG(dbgs() << "All defs dead: " << *MI);
1421 Dead.push_back(MI);
1422 }
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001423 }
1424
1425 if (Dead.empty())
1426 return;
1427
Wei Mic0223702016-07-08 21:08:09 +00001428 Edit->eliminateDeadDefs(Dead, None, &AA);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001429}
1430
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001431void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001432 ++NumFinished;
Eric Christopher21933532011-02-03 05:40:54 +00001433
Eric Christopherede62672011-02-03 06:18:29 +00001434 // At this point, the live intervals in Edit contain VNInfos corresponding to
1435 // the inserted copies.
1436
1437 // Add the original defs from the parent interval.
Matthias Braun96761952014-12-10 23:07:54 +00001438 for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen3295a992011-02-04 00:59:23 +00001439 if (ParentVNI->isUnused())
1440 continue;
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +00001441 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001442 defValue(RegIdx, ParentVNI, ParentVNI->def, true);
Jakob Stoklund Olesen32210de2011-03-15 21:13:22 +00001443
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001444 // Force rematted values to be recomputed everywhere.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001445 // The new live ranges may be truncated.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001446 if (Edit->didRematerialize(ParentVNI))
1447 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001448 forceRecompute(i, ParentVNI);
Eric Christopherede62672011-02-03 06:18:29 +00001449 }
1450
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001451 // Hoist back-copies to the complement interval when in spill mode.
1452 switch (SpillMode) {
1453 case SM_Partition:
1454 // Leave all back-copies as is.
1455 break;
1456 case SM_Size:
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001457 case SM_Speed:
Wei Mi9a16d652016-04-13 03:08:27 +00001458 // hoistCopies will behave differently between size and speed.
1459 hoistCopies();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001460 }
1461
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001462 // Transfer the simply mapped values, check if any are skipped.
1463 bool Skipped = transferValues();
Wei Mi9a16d652016-04-13 03:08:27 +00001464
1465 // Rewrite virtual registers, possibly extending ranges.
1466 rewriteAssigned(Skipped);
1467
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001468 if (Skipped)
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001469 extendPHIKillRanges();
1470 else
1471 ++NumSimple;
Eric Christopherede62672011-02-03 06:18:29 +00001472
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001473 // Delete defs that were rematted everywhere.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001474 if (Skipped)
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001475 deleteRematVictims();
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001476
Jakob Stoklund Olesen0f1677e2010-10-07 23:34:34 +00001477 // Get rid of unused values and set phi-kill flags.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +00001478 for (unsigned Reg : *Edit) {
1479 LiveInterval &LI = LIS.getInterval(Reg);
1480 LI.removeEmptySubRanges();
Mark Laceyf9ea8852013-08-14 23:50:04 +00001481 LI.RenumberValues();
1482 }
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001483
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001484 // Provide a reverse mapping from original indices to Edit ranges.
1485 if (LRMap) {
1486 LRMap->clear();
1487 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1488 LRMap->push_back(i);
1489 }
1490
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001491 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +00001492 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001493 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001494 // Don't use iterators, they are invalidated by create() below.
Matthias Braund3dd1352015-09-22 03:44:41 +00001495 unsigned VReg = Edit->get(i);
1496 LiveInterval &LI = LIS.getInterval(VReg);
1497 SmallVector<LiveInterval*, 8> SplitLIs;
1498 LIS.splitSeparateComponents(LI, SplitLIs);
1499 unsigned Original = VRM.getOriginal(VReg);
1500 for (LiveInterval *SplitLI : SplitLIs)
1501 VRM.setIsSplitFromReg(SplitLI->reg, Original);
1502
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001503 // The new intervals all map back to i.
1504 if (LRMap)
1505 LRMap->resize(Edit->size(), i);
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001506 }
1507
Jakob Stoklund Olesen284c2db2010-08-10 17:07:22 +00001508 // Calculate spill weight and allocation hints for new intervals.
Benjamin Kramere2a1d892013-06-17 19:00:36 +00001509 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001510
1511 assert(!LRMap || LRMap->size() == Edit->size());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +00001512}
1513
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +00001514//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +00001515// Single Block Splitting
1516//===----------------------------------------------------------------------===//
1517
Jakob Stoklund Olesen8627ea92011-08-05 22:20:45 +00001518bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
1519 bool SingleInstrs) const {
1520 // Always split for multiple instructions.
1521 if (!BI.isOneInstr())
1522 return true;
1523 // Don't split for single instructions unless explicitly requested.
1524 if (!SingleInstrs)
1525 return false;
1526 // Splitting a live-through range always makes progress.
1527 if (BI.LiveIn && BI.LiveOut)
1528 return true;
1529 // No point in isolating a copy. It has no register class constraints.
1530 if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
1531 return false;
1532 // Finally, don't isolate an end point that was created by earlier splits.
1533 return isOriginalEndpoint(BI.FirstInstr);
1534}
1535
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001536void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1537 openIntv();
1538 SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001539 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001540 LastSplitPoint));
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001541 if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
1542 useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001543 } else {
1544 // The last use is after the last valid split point.
1545 SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1546 useIntv(SegStart, SegStop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001547 overlapIntv(SegStop, BI.LastInstr);
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001548 }
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001549}
1550
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001551//===----------------------------------------------------------------------===//
1552// Global Live Range Splitting Support
1553//===----------------------------------------------------------------------===//
1554
1555// These methods support a method of global live range splitting that uses a
1556// global algorithm to decide intervals for CFG edges. They will insert split
1557// points and color intervals in basic blocks while avoiding interference.
1558//
1559// Note that splitSingleBlock is also useful for blocks where both CFG edges
1560// are on the stack.
1561
1562void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1563 unsigned IntvIn, SlotIndex LeaveBefore,
1564 unsigned IntvOut, SlotIndex EnterAfter){
1565 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001566 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001567
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00001568 DEBUG(dbgs() << "%bb." << MBBNum << " [" << Start << ';' << Stop << ") intf "
1569 << LeaveBefore << '-' << EnterAfter << ", live-through "
1570 << IntvIn << " -> " << IntvOut);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001571
1572 assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1573
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001574 assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
1575 assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
1576 assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
1577
1578 MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
1579
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001580 if (!IntvOut) {
1581 DEBUG(dbgs() << ", spill on entry.\n");
1582 //
1583 // <<<<<<<<< Possible LeaveBefore interference.
1584 // |-----------| Live through.
1585 // -____________ Spill on entry.
1586 //
1587 selectIntv(IntvIn);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001588 SlotIndex Idx = leaveIntvAtTop(*MBB);
1589 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1590 (void)Idx;
1591 return;
1592 }
1593
1594 if (!IntvIn) {
1595 DEBUG(dbgs() << ", reload on exit.\n");
1596 //
1597 // >>>>>>> Possible EnterAfter interference.
1598 // |-----------| Live through.
1599 // ___________-- Reload on exit.
1600 //
1601 selectIntv(IntvOut);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001602 SlotIndex Idx = enterIntvAtEnd(*MBB);
1603 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1604 (void)Idx;
1605 return;
1606 }
1607
1608 if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1609 DEBUG(dbgs() << ", straight through.\n");
1610 //
1611 // |-----------| Live through.
1612 // ------------- Straight through, same intv, no interference.
1613 //
1614 selectIntv(IntvOut);
1615 useIntv(Start, Stop);
1616 return;
1617 }
1618
1619 // We cannot legally insert splits after LSP.
1620 SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001621 assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001622
1623 if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1624 LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1625 DEBUG(dbgs() << ", switch avoiding interference.\n");
1626 //
1627 // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
1628 // |-----------| Live through.
1629 // ------======= Switch intervals between interference.
1630 //
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001631 selectIntv(IntvOut);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001632 SlotIndex Idx;
1633 if (LeaveBefore && LeaveBefore < LSP) {
1634 Idx = enterIntvBefore(LeaveBefore);
1635 useIntv(Idx, Stop);
1636 } else {
1637 Idx = enterIntvAtEnd(*MBB);
1638 }
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001639 selectIntv(IntvIn);
1640 useIntv(Start, Idx);
1641 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1642 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1643 return;
1644 }
1645
1646 DEBUG(dbgs() << ", create local intv for interference.\n");
1647 //
1648 // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
1649 // |-----------| Live through.
1650 // ==---------== Switch intervals before/after interference.
1651 //
1652 assert(LeaveBefore <= EnterAfter && "Missed case");
1653
1654 selectIntv(IntvOut);
1655 SlotIndex Idx = enterIntvAfter(EnterAfter);
1656 useIntv(Idx, Stop);
1657 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1658
1659 selectIntv(IntvIn);
1660 Idx = leaveIntvBefore(LeaveBefore);
1661 useIntv(Start, Idx);
1662 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1663}
1664
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001665void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1666 unsigned IntvIn, SlotIndex LeaveBefore) {
1667 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001668 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001669
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00001670 DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001671 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001672 << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
1673 << (BI.LiveOut ? ", stack-out" : ", killed in block"));
1674
1675 assert(IntvIn && "Must have register in");
1676 assert(BI.LiveIn && "Must be live-in");
1677 assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1678
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001679 if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001680 DEBUG(dbgs() << " before interference.\n");
1681 //
1682 // <<< Interference after kill.
1683 // |---o---x | Killed in block.
1684 // ========= Use IntvIn everywhere.
1685 //
1686 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001687 useIntv(Start, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001688 return;
1689 }
1690
1691 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1692
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001693 if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001694 //
1695 // <<< Possible interference after last use.
1696 // |---o---o---| Live-out on stack.
1697 // =========____ Leave IntvIn after last use.
1698 //
1699 // < Interference after last use.
1700 // |---o---o--o| Live-out on stack, late last use.
1701 // ============ Copy to stack after LSP, overlap IntvIn.
1702 // \_____ Stack interval is live-out.
1703 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001704 if (BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001705 DEBUG(dbgs() << ", spill after last use before interference.\n");
1706 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001707 SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001708 useIntv(Start, Idx);
1709 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1710 } else {
1711 DEBUG(dbgs() << ", spill before last split point.\n");
1712 selectIntv(IntvIn);
Jakob Stoklund Olesen37e3a132011-07-16 00:13:30 +00001713 SlotIndex Idx = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001714 overlapIntv(Idx, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001715 useIntv(Start, Idx);
1716 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1717 }
1718 return;
1719 }
1720
1721 // The interference is overlapping somewhere we wanted to use IntvIn. That
1722 // means we need to create a local interval that can be allocated a
1723 // different register.
1724 unsigned LocalIntv = openIntv();
Matt Beaumont-Gay26909d82011-07-16 04:18:47 +00001725 (void)LocalIntv;
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001726 DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
1727
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001728 if (!BI.LiveOut || BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001729 //
1730 // <<<<<<< Interference overlapping uses.
1731 // |---o---o---| Live-out on stack.
1732 // =====----____ Leave IntvIn before interference, then spill.
1733 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001734 SlotIndex To = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001735 SlotIndex From = enterIntvBefore(LeaveBefore);
1736 useIntv(From, To);
1737 selectIntv(IntvIn);
1738 useIntv(Start, From);
1739 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1740 return;
1741 }
1742
1743 // <<<<<<< Interference overlapping uses.
1744 // |---o---o--o| Live-out on stack, late last use.
1745 // =====------- Copy to stack before LSP, overlap LocalIntv.
1746 // \_____ Stack interval is live-out.
1747 //
1748 SlotIndex To = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001749 overlapIntv(To, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001750 SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1751 useIntv(From, To);
1752 selectIntv(IntvIn);
1753 useIntv(Start, From);
1754 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1755}
1756
1757void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1758 unsigned IntvOut, SlotIndex EnterAfter) {
1759 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001760 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001761
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00001762 DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001763 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001764 << ", reg-out " << IntvOut << ", enter after " << EnterAfter
1765 << (BI.LiveIn ? ", stack-in" : ", defined in block"));
1766
1767 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1768
1769 assert(IntvOut && "Must have register out");
1770 assert(BI.LiveOut && "Must be live-out");
1771 assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1772
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001773 if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001774 DEBUG(dbgs() << " after interference.\n");
1775 //
1776 // >>>> Interference before def.
1777 // | o---o---| Defined in block.
1778 // ========= Use IntvOut everywhere.
1779 //
1780 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001781 useIntv(BI.FirstInstr, Stop);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001782 return;
1783 }
1784
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001785 if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001786 DEBUG(dbgs() << ", reload after interference.\n");
1787 //
1788 // >>>> Interference before def.
1789 // |---o---o---| Live-through, stack-in.
1790 // ____========= Enter IntvOut before first use.
1791 //
1792 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001793 SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001794 useIntv(Idx, Stop);
1795 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1796 return;
1797 }
1798
1799 // The interference is overlapping somewhere we wanted to use IntvOut. That
1800 // means we need to create a local interval that can be allocated a
1801 // different register.
1802 DEBUG(dbgs() << ", interference overlaps uses.\n");
1803 //
1804 // >>>>>>> Interference overlapping uses.
1805 // |---o---o---| Live-through, stack-in.
1806 // ____---====== Create local interval for interference range.
1807 //
1808 selectIntv(IntvOut);
1809 SlotIndex Idx = enterIntvAfter(EnterAfter);
1810 useIntv(Idx, Stop);
1811 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1812
1813 openIntv();
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001814 SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001815 useIntv(From, Idx);
1816}