blob: 665ed3b0d23a6a3fae08c2a626e6386df74ae99b [file] [log] [blame]
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +00001//===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the SplitAnalysis class as well as mutator functions for
11// live range splitting.
12//
13//===----------------------------------------------------------------------===//
14
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000015#include "SplitKit.h"
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +000016#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000017#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper3ca96f92012-04-02 22:44:18 +000018#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesene172a8b2010-10-28 20:34:50 +000019#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +000021#include "llvm/CodeGen/MachineLoopInfo.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000023#include "llvm/CodeGen/VirtRegMap.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesened4075c2010-07-20 21:46:58 +000026#include "llvm/Target/TargetInstrInfo.h"
27#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000028
29using namespace llvm;
30
Chandler Carruth1b9dde02014-04-22 02:02:50 +000031#define DEBUG_TYPE "regalloc"
32
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +000033STATISTIC(NumFinished, "Number of splits finished");
34STATISTIC(NumSimple, "Number of splits that were simple");
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +000035STATISTIC(NumCopies, "Number of copies inserted for splitting");
36STATISTIC(NumRemats, "Number of rematerialized defs for splitting");
Jakob Stoklund Olesen50215af2011-05-10 17:37:41 +000037STATISTIC(NumRepairs, "Number of invalid live ranges repaired");
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +000038
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000039//===----------------------------------------------------------------------===//
40// Split Analysis
41//===----------------------------------------------------------------------===//
42
Eric Christopherd9134482014-08-04 21:25:23 +000043SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
Jakob Stoklund Olesen0fef9dd2010-07-20 23:50:15 +000044 const MachineLoopInfo &mli)
Eric Christopherd9134482014-08-04 21:25:23 +000045 : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli),
Eric Christopherfc6de422014-08-05 02:39:49 +000046 TII(*MF.getSubtarget().getInstrInfo()), CurLI(nullptr),
Eric Christopherd9134482014-08-04 21:25:23 +000047 LastSplitPoint(MF.getNumBlockIDs()) {}
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000048
49void SplitAnalysis::clear() {
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +000050 UseSlots.clear();
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +000051 UseBlocks.clear();
52 ThroughBlocks.clear();
Craig Topperc0196b12014-04-14 00:51:57 +000053 CurLI = nullptr;
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +000054 DidRepairRange = false;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000055}
56
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000057SlotIndex SplitAnalysis::computeLastSplitPoint(unsigned Num) {
58 const MachineBasicBlock *MBB = MF.getBlockNumbered(Num);
Reid Klecknered170792015-09-17 17:19:40 +000059 // FIXME: Handle multiple EH pad successors.
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000060 const MachineBasicBlock *LPad = MBB->getLandingPadSuccessor();
61 std::pair<SlotIndex, SlotIndex> &LSP = LastSplitPoint[Num];
Jakob Stoklund Olesen8b1d0232012-01-11 02:07:05 +000062 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000063
64 // Compute split points on the first call. The pair is independent of the
65 // current live interval.
66 if (!LSP.first.isValid()) {
67 MachineBasicBlock::const_iterator FirstTerm = MBB->getFirstTerminator();
68 if (FirstTerm == MBB->end())
Jakob Stoklund Olesen8b1d0232012-01-11 02:07:05 +000069 LSP.first = MBBEnd;
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000070 else
71 LSP.first = LIS.getInstructionIndex(FirstTerm);
72
73 // If there is a landing pad successor, also find the call instruction.
74 if (!LPad)
75 return LSP.first;
76 // There may not be a call instruction (?) in which case we ignore LPad.
77 LSP.second = LSP.first;
Jakob Stoklund Olesen040d6592011-06-28 01:18:58 +000078 for (MachineBasicBlock::const_iterator I = MBB->end(), E = MBB->begin();
79 I != E;) {
80 --I;
Evan Cheng7f8e5632011-12-07 07:15:52 +000081 if (I->isCall()) {
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000082 LSP.second = LIS.getInstructionIndex(I);
83 break;
84 }
Jakob Stoklund Olesen040d6592011-06-28 01:18:58 +000085 }
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000086 }
87
88 // If CurLI is live into a landing pad successor, move the last split point
89 // back to the call that may throw.
Jakob Stoklund Olesen8b1d0232012-01-11 02:07:05 +000090 if (!LPad || !LSP.second || !LIS.isLiveInToMBB(*CurLI, LPad))
Jakob Stoklund Olesen50b2db82011-04-05 04:20:27 +000091 return LSP.first;
Jakob Stoklund Olesen8b1d0232012-01-11 02:07:05 +000092
93 // Find the value leaving MBB.
94 const VNInfo *VNI = CurLI->getVNInfoBefore(MBBEnd);
95 if (!VNI)
96 return LSP.first;
97
98 // If the value leaving MBB was defined after the call in MBB, it can't
99 // really be live-in to the landing pad. This can happen if the landing pad
100 // has a PHI, and this register is undef on the exceptional edge.
101 // <rdar://problem/10664933>
102 if (!SlotIndex::isEarlierInstr(VNI->def, LSP.second) && VNI->def < MBBEnd)
103 return LSP.first;
104
105 // Value is properly live-in to the landing pad.
106 // Only allow splits before the call.
107 return LSP.second;
Jakob Stoklund Olesened4075c2010-07-20 21:46:58 +0000108}
109
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000110MachineBasicBlock::iterator
111SplitAnalysis::getLastSplitPointIter(MachineBasicBlock *MBB) {
112 SlotIndex LSP = getLastSplitPoint(MBB->getNumber());
113 if (LSP == LIS.getMBBEndIdx(MBB))
114 return MBB->end();
115 return LIS.getInstructionFromIndex(LSP);
116}
117
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000118/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000119void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000120 assert(UseSlots.empty() && "Call clear first");
121
122 // First get all the defs from the interval values. This provides the correct
123 // slots for early clobbers.
Matthias Braun96761952014-12-10 23:07:54 +0000124 for (const VNInfo *VNI : CurLI->valnos)
125 if (!VNI->isPHIDef() && !VNI->isUnused())
126 UseSlots.push_back(VNI->def);
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000127
128 // Get use slots form the use-def chain.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000129 const MachineRegisterInfo &MRI = MF.getRegInfo();
Owen Andersonb36376e2014-03-17 19:36:09 +0000130 for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg))
131 if (!MO.isUndef())
132 UseSlots.push_back(LIS.getInstructionIndex(MO.getParent()).getRegSlot());
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000133
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000134 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000135
Jakob Stoklund Olesenfe6e07f2011-04-05 15:18:18 +0000136 // Remove duplicates, keeping the smaller slot for each instruction.
137 // That is what we want for early clobbers.
138 UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
139 SlotIndex::isSameInstr),
140 UseSlots.end());
141
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000142 // Compute per-live block info.
143 if (!calcLiveBlockInfo()) {
144 // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
Rafael Espindola676c4052011-06-26 22:34:10 +0000145 // I am looking at you, RegisterCoalescer!
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000146 DidRepairRange = true;
Jakob Stoklund Olesen50215af2011-05-10 17:37:41 +0000147 ++NumRepairs;
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000148 DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
149 const_cast<LiveIntervals&>(LIS)
150 .shrinkToUses(const_cast<LiveInterval*>(CurLI));
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000151 UseBlocks.clear();
152 ThroughBlocks.clear();
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000153 bool fixed = calcLiveBlockInfo();
154 (void)fixed;
155 assert(fixed && "Couldn't fix broken live interval");
156 }
157
Jakob Stoklund Olesenbd6b86e2011-03-27 22:49:23 +0000158 DEBUG(dbgs() << "Analyze counted "
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000159 << UseSlots.size() << " instrs in "
160 << UseBlocks.size() << " blocks, through "
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000161 << NumThroughBlocks << " blocks.\n");
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000162}
163
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000164/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
165/// where CurLI is live.
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000166bool SplitAnalysis::calcLiveBlockInfo() {
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000167 ThroughBlocks.resize(MF.getNumBlockIDs());
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000168 NumThroughBlocks = NumGapBlocks = 0;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000169 if (CurLI->empty())
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000170 return true;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000171
172 LiveInterval::const_iterator LVI = CurLI->begin();
173 LiveInterval::const_iterator LVE = CurLI->end();
174
175 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
176 UseI = UseSlots.begin();
177 UseE = UseSlots.end();
178
179 // Loop over basic blocks where CurLI is live.
180 MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start);
181 for (;;) {
182 BlockInfo BI;
183 BI.MBB = MFI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000184 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000185 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000186
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000187 // If the block contains no uses, the range must be live through. At one
Rafael Espindola676c4052011-06-26 22:34:10 +0000188 // point, RegisterCoalescer could create dangling ranges that ended
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000189 // mid-block.
190 if (UseI == UseE || *UseI >= Stop) {
191 ++NumThroughBlocks;
192 ThroughBlocks.set(BI.MBB->getNumber());
193 // The range shouldn't end mid-block if there are no uses. This shouldn't
194 // happen.
195 if (LVI->end < Stop)
196 return false;
197 } else {
198 // This block has uses. Find the first and last uses in the block.
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000199 BI.FirstInstr = *UseI;
200 assert(BI.FirstInstr >= Start);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000201 do ++UseI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000202 while (UseI != UseE && *UseI < Stop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000203 BI.LastInstr = UseI[-1];
204 assert(BI.LastInstr < Stop);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000205
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000206 // LVI is the first live segment overlapping MBB.
207 BI.LiveIn = LVI->start <= Start;
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000208
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000209 // When not live in, the first use should be a def.
210 if (!BI.LiveIn) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000211 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000212 assert(LVI->start == BI.FirstInstr && "First instr should be a def");
213 BI.FirstDef = BI.FirstInstr;
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000214 }
215
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000216 // Look for gaps in the live range.
217 BI.LiveOut = true;
218 while (LVI->end < Stop) {
219 SlotIndex LastStop = LVI->end;
220 if (++LVI == LVE || LVI->start >= Stop) {
221 BI.LiveOut = false;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000222 BI.LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000223 break;
224 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000225
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000226 if (LastStop < LVI->start) {
227 // There is a gap in the live range. Create duplicate entries for the
228 // live-in snippet and the live-out snippet.
229 ++NumGapBlocks;
230
231 // Push the Live-in part.
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000232 BI.LiveOut = false;
233 UseBlocks.push_back(BI);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000234 UseBlocks.back().LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000235
236 // Set up BI for the live-out part.
237 BI.LiveIn = false;
238 BI.LiveOut = true;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000239 BI.FirstInstr = BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000240 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000241
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000242 // A Segment that starts in the middle of the block must be a def.
243 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000244 if (!BI.FirstDef)
245 BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000246 }
247
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000248 UseBlocks.push_back(BI);
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000249
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000250 // LVI is now at LVE or LVI->end >= Stop.
251 if (LVI == LVE)
252 break;
253 }
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000254
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000255 // Live segment ends exactly at Stop. Move to the next segment.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000256 if (LVI->end == Stop && ++LVI == LVE)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000257 break;
258
259 // Pick the next basic block.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000260 if (LVI->start < Stop)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000261 ++MFI;
262 else
263 MFI = LIS.getMBBFromIndex(LVI->start);
264 }
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000265
266 assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000267 return true;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000268}
269
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000270unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
271 if (cli->empty())
272 return 0;
273 LiveInterval *li = const_cast<LiveInterval*>(cli);
274 LiveInterval::iterator LVI = li->begin();
275 LiveInterval::iterator LVE = li->end();
276 unsigned Count = 0;
277
278 // Loop over basic blocks where li is live.
279 MachineFunction::const_iterator MFI = LIS.getMBBFromIndex(LVI->start);
280 SlotIndex Stop = LIS.getMBBEndIdx(MFI);
281 for (;;) {
282 ++Count;
283 LVI = li->advanceTo(LVI, Stop);
284 if (LVI == LVE)
285 return Count;
286 do {
287 ++MFI;
288 Stop = LIS.getMBBEndIdx(MFI);
289 } while (Stop <= LVI->start);
290 }
291}
292
Jakob Stoklund Olesen60a26a62011-02-21 23:09:46 +0000293bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
294 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
295 const LiveInterval &Orig = LIS.getInterval(OrigReg);
296 assert(!Orig.empty() && "Splitting empty interval?");
297 LiveInterval::const_iterator I = Orig.find(Idx);
298
299 // Range containing Idx should begin at Idx.
300 if (I != Orig.end() && I->start <= Idx)
301 return I->start == Idx;
302
303 // Range does not contain Idx, previous must end at Idx.
304 return I != Orig.begin() && (--I)->end == Idx;
305}
306
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000307void SplitAnalysis::analyze(const LiveInterval *li) {
308 clear();
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000309 CurLI = li;
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000310 analyzeUses();
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000311}
312
Jakob Stoklund Olesen28e769c2010-12-15 17:49:52 +0000313
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000314//===----------------------------------------------------------------------===//
315// Split Editor
316//===----------------------------------------------------------------------===//
317
318/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Eric Christopherd9134482014-08-04 21:25:23 +0000319SplitEditor::SplitEditor(SplitAnalysis &sa, LiveIntervals &lis, VirtRegMap &vrm,
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000320 MachineDominatorTree &mdt,
321 MachineBlockFrequencyInfo &mbfi)
Eric Christopherd9134482014-08-04 21:25:23 +0000322 : SA(sa), LIS(lis), VRM(vrm), MRI(vrm.getMachineFunction().getRegInfo()),
Eric Christopher60621802014-10-14 07:22:00 +0000323 MDT(mdt), TII(*vrm.getMachineFunction().getSubtarget().getInstrInfo()),
324 TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()),
Eric Christopherd9134482014-08-04 21:25:23 +0000325 MBFI(mbfi), Edit(nullptr), OpenIdx(0), SpillMode(SM_Partition),
326 RegAssign(Allocator) {}
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000327
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000328void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
329 Edit = &LRE;
330 SpillMode = SM;
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000331 OpenIdx = 0;
332 RegAssign.clear();
333 Values.clear();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000334
335 // Reset the LiveRangeCalc instances needed for this spill mode.
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000336 LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
337 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000338 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000339 LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
340 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000341
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000342 // We don't need an AliasAnalysis since we will only be performing
343 // cheap-as-a-copy remats anyway.
Craig Topperc0196b12014-04-14 00:51:57 +0000344 Edit->anyRematerializable(nullptr);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000345}
346
Manman Ren19f49ac2012-09-11 22:23:19 +0000347#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Eric Christopherede62672011-02-03 06:18:29 +0000348void SplitEditor::dump() const {
349 if (RegAssign.empty()) {
350 dbgs() << " empty\n";
351 return;
352 }
353
354 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
355 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
356 dbgs() << '\n';
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000357}
Manman Ren742534c2012-09-06 19:06:06 +0000358#endif
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000359
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000360VNInfo *SplitEditor::defValue(unsigned RegIdx,
361 const VNInfo *ParentVNI,
362 SlotIndex Idx) {
363 assert(ParentVNI && "Mapping NULL value");
364 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000365 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
Mark Laceyf9ea8852013-08-14 23:50:04 +0000366 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000367
368 // Create a new value.
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000369 VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000370
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000371 // Use insert for lookup, so we can add missing values with a second lookup.
372 std::pair<ValueMap::iterator, bool> InsP =
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000373 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id),
374 ValueForcePair(VNI, false)));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000375
376 // This was the first time (RegIdx, ParentVNI) was mapped.
377 // Keep it as a simple def without any liveness.
378 if (InsP.second)
379 return VNI;
380
381 // If the previous value was a simple mapping, add liveness for it now.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000382 if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000383 SlotIndex Def = OldVNI->def;
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000384 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), OldVNI));
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000385 // No longer a simple mapping. Switch to a complex, non-forced mapping.
386 InsP.first->second = ValueForcePair();
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000387 }
388
389 // This is a complex mapping, add liveness for VNI
390 SlotIndex Def = VNI->def;
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000391 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000392
393 return VNI;
394}
395
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000396void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000397 assert(ParentVNI && "Mapping NULL value");
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000398 ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
399 VNInfo *VNI = VFP.getPointer();
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000400
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000401 // ParentVNI was either unmapped or already complex mapped. Either way, just
402 // set the force bit.
403 if (!VNI) {
404 VFP.setInt(true);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000405 return;
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000406 }
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000407
408 // This was previously a single mapping. Make sure the old def is represented
409 // by a trivial live range.
410 SlotIndex Def = VNI->def;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000411 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000412 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000413 // Mark as complex mapped, forced.
Craig Topperc0196b12014-04-14 00:51:57 +0000414 VFP = ValueForcePair(nullptr, true);
Jakob Stoklund Olesen4484f992011-09-13 18:05:29 +0000415}
416
Eric Christopherede62672011-02-03 06:18:29 +0000417VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000418 VNInfo *ParentVNI,
419 SlotIndex UseIdx,
420 MachineBasicBlock &MBB,
421 MachineBasicBlock::iterator I) {
Craig Topperc0196b12014-04-14 00:51:57 +0000422 MachineInstr *CopyMI = nullptr;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000423 SlotIndex Def;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000424 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000425
Jakob Stoklund Olesen7d406792011-05-02 05:29:58 +0000426 // We may be trying to avoid interference that ends at a deleted instruction,
427 // so always begin RegIdx 0 early and all others late.
428 bool Late = RegIdx != 0;
429
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000430 // Attempt cheap-as-a-copy rematerialization.
431 LiveRangeEdit::Remat RM(ParentVNI);
Pete Cooper2bde2f42012-04-02 22:22:53 +0000432 if (Edit->canRematerializeAt(RM, UseIdx, true)) {
433 Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late);
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000434 ++NumRemats;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000435 } else {
436 // Can't remat, just insert a copy from parent.
Eric Christopherede62672011-02-03 06:18:29 +0000437 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000438 .addReg(Edit->getReg());
Jakob Stoklund Olesen7d406792011-05-02 05:29:58 +0000439 Def = LIS.getSlotIndexes()->insertMachineInstrInMaps(CopyMI, Late)
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000440 .getRegSlot();
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000441 ++NumCopies;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000442 }
443
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000444 // Define the value in Reg.
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000445 return defValue(RegIdx, ParentVNI, Def);
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000446}
447
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000448/// Create a new virtual register and live interval.
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000449unsigned SplitEditor::openIntv() {
Eric Christopherede62672011-02-03 06:18:29 +0000450 // Create the complement as index 0.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000451 if (Edit->empty())
Mark Lacey9d8103d2013-08-14 23:50:16 +0000452 Edit->createEmptyInterval();
Eric Christopherede62672011-02-03 06:18:29 +0000453
454 // Create the open interval.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000455 OpenIdx = Edit->size();
Mark Lacey9d8103d2013-08-14 23:50:16 +0000456 Edit->createEmptyInterval();
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000457 return OpenIdx;
458}
459
460void SplitEditor::selectIntv(unsigned Idx) {
461 assert(Idx != 0 && "Cannot select the complement interval");
462 assert(Idx < Edit->size() && "Can only select previously opened interval");
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000463 DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000464 OpenIdx = Idx;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000465}
466
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000467SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000468 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000469 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000470 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000471 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000472 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000473 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000474 return Idx;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000475 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000476 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000477 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000478 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000479
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000480 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
481 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000482}
483
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000484SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
485 assert(OpenIdx && "openIntv not called before enterIntvAfter");
486 DEBUG(dbgs() << " enterIntvAfter " << Idx);
487 Idx = Idx.getBoundaryIndex();
488 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
489 if (!ParentVNI) {
490 DEBUG(dbgs() << ": not live\n");
491 return Idx;
492 }
493 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
494 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
495 assert(MI && "enterIntvAfter called with invalid index");
496
497 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000498 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000499 return VNI->def;
500}
501
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000502SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000503 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000504 SlotIndex End = LIS.getMBBEndIdx(&MBB);
505 SlotIndex Last = End.getPrevSlot();
506 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000507 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000508 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000509 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000510 return End;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000511 }
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000512 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000513 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000514 SA.getLastSplitPointIter(&MBB));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000515 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopherede62672011-02-03 06:18:29 +0000516 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000517 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000518}
519
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000520/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000521void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000522 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000523}
524
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000525void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopherede62672011-02-03 06:18:29 +0000526 assert(OpenIdx && "openIntv not called before useIntv");
527 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
528 RegAssign.insert(Start, End, OpenIdx);
529 DEBUG(dump());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000530}
531
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000532SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000533 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000534 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000535
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000536 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000537 SlotIndex Boundary = Idx.getBoundaryIndex();
538 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000539 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000540 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000541 return Boundary.getNextSlot();
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000542 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000543 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000544 MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
Jakob Stoklund Olesen3d11c8e2011-02-08 18:50:18 +0000545 assert(MI && "No instruction at index");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000546
547 // In spill mode, make live ranges as short as possible by inserting the copy
548 // before MI. This is only possible if that instruction doesn't redefine the
549 // value. The inserted COPY is not a kill, and we don't need to recompute
550 // the source live range. The spiller also won't try to hoist this copy.
551 if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
552 MI->readsVirtualRegister(Edit->getReg())) {
553 forceRecompute(0, ParentVNI);
554 defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
555 return Idx;
556 }
557
558 VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000559 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000560 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000561}
562
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000563SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
564 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
565 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
566
567 // The interval must be live into the instruction at Idx.
Jakob Stoklund Olesenc45d38e2011-07-18 18:47:13 +0000568 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000569 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000570 if (!ParentVNI) {
571 DEBUG(dbgs() << ": not live\n");
572 return Idx.getNextSlot();
573 }
574 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
575
576 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
577 assert(MI && "No instruction at index");
578 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
579 return VNI->def;
580}
581
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000582SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000583 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000584 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000585 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000586
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000587 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000588 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000589 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000590 return Start;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000591 }
592
Eric Christopherede62672011-02-03 06:18:29 +0000593 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000594 MBB.SkipPHIsAndLabels(MBB.begin()));
Eric Christopherede62672011-02-03 06:18:29 +0000595 RegAssign.insert(Start, VNI->def, OpenIdx);
596 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000597 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000598}
599
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000600void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
601 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000602 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesend7bcf432011-11-14 01:39:36 +0000603 assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000604 "Parent changes value in extended range");
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000605 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
606 "Range cannot span basic blocks");
607
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000608 // The complement interval will be extended as needed by LRCalc.extend().
Jakob Stoklund Olesen5c482cd2011-04-05 23:43:14 +0000609 if (ParentVNI)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000610 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000611 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
612 RegAssign.insert(Start, End, OpenIdx);
613 DEBUG(dump());
614}
615
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000616//===----------------------------------------------------------------------===//
617// Spill modes
618//===----------------------------------------------------------------------===//
619
620void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
Mark Laceyf9ea8852013-08-14 23:50:04 +0000621 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000622 DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
623 RegAssignMap::iterator AssignI;
624 AssignI.setMap(RegAssign);
625
626 for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
Matthias Braun311730a2015-01-21 19:02:30 +0000627 SlotIndex Def = Copies[i]->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000628 MachineInstr *MI = LIS.getInstructionFromIndex(Def);
629 assert(MI && "No instruction for back-copy");
630
631 MachineBasicBlock *MBB = MI->getParent();
632 MachineBasicBlock::iterator MBBI(MI);
633 bool AtBegin;
634 do AtBegin = MBBI == MBB->begin();
635 while (!AtBegin && (--MBBI)->isDebugValue());
636
637 DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
Matthias Braun311730a2015-01-21 19:02:30 +0000638 LIS.removeVRegDefAt(*LI, Def);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000639 LIS.RemoveMachineInstrFromMaps(MI);
640 MI->eraseFromParent();
641
Matthias Braun311730a2015-01-21 19:02:30 +0000642 // Adjust RegAssign if a register assignment is killed at Def. We want to
643 // avoid calculating the live range of the source register if possible.
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000644 AssignI.find(Def.getPrevSlot());
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000645 if (!AssignI.valid() || AssignI.start() >= Def)
646 continue;
647 // If MI doesn't kill the assigned register, just leave it.
648 if (AssignI.stop() != Def)
649 continue;
650 unsigned RegIdx = AssignI.value();
651 if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
652 DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n');
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000653 forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000654 } else {
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000655 SlotIndex Kill = LIS.getInstructionIndex(MBBI).getRegSlot();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000656 DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
657 AssignI.setStop(Kill);
658 }
659 }
660}
661
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000662MachineBasicBlock*
663SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
664 MachineBasicBlock *DefMBB) {
665 if (MBB == DefMBB)
666 return MBB;
667 assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
668
669 const MachineLoopInfo &Loops = SA.Loops;
670 const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
671 MachineDomTreeNode *DefDomNode = MDT[DefMBB];
672
673 // Best candidate so far.
674 MachineBasicBlock *BestMBB = MBB;
675 unsigned BestDepth = UINT_MAX;
676
677 for (;;) {
678 const MachineLoop *Loop = Loops.getLoopFor(MBB);
679
680 // MBB isn't in a loop, it doesn't get any better. All dominators have a
681 // higher frequency by definition.
682 if (!Loop) {
683 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
684 << MBB->getNumber() << " at depth 0\n");
685 return MBB;
686 }
687
688 // We'll never be able to exit the DefLoop.
689 if (Loop == DefLoop) {
690 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
691 << MBB->getNumber() << " in the same loop\n");
692 return MBB;
693 }
694
695 // Least busy dominator seen so far.
696 unsigned Depth = Loop->getLoopDepth();
697 if (Depth < BestDepth) {
698 BestMBB = MBB;
699 BestDepth = Depth;
700 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
701 << MBB->getNumber() << " at depth " << Depth << '\n');
702 }
703
704 // Leave loop by going to the immediate dominator of the loop header.
705 // This is a bigger stride than simply walking up the dominator tree.
706 MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
707
708 // Too far up the dominator tree?
709 if (!IDom || !MDT.dominates(DefDomNode, IDom))
710 return BestMBB;
711
712 MBB = IDom->getBlock();
713 }
714}
715
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000716void SplitEditor::hoistCopiesForSize() {
717 // Get the complement interval, always RegIdx 0.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000718 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000719 LiveInterval *Parent = &Edit->getParent();
720
721 // Track the nearest common dominator for all back-copies for each ParentVNI,
722 // indexed by ParentVNI->id.
723 typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
724 SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
725
726 // Find the nearest common dominator for parent values with multiple
727 // back-copies. If a single back-copy dominates, put it in DomPair.second.
Matthias Braun96761952014-12-10 23:07:54 +0000728 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000729 if (VNI->isUnused())
730 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000731 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
732 assert(ParentVNI && "Parent not live at complement def");
733
734 // Don't hoist remats. The complement is probably going to disappear
735 // completely anyway.
736 if (Edit->didRematerialize(ParentVNI))
737 continue;
738
739 MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
740 DomPair &Dom = NearestDom[ParentVNI->id];
741
742 // Keep directly defined parent values. This is either a PHI or an
743 // instruction in the complement range. All other copies of ParentVNI
744 // should be eliminated.
745 if (VNI->def == ParentVNI->def) {
746 DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
747 Dom = DomPair(ValMBB, VNI->def);
748 continue;
749 }
750 // Skip the singly mapped values. There is nothing to gain from hoisting a
751 // single back-copy.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000752 if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000753 DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
754 continue;
755 }
756
757 if (!Dom.first) {
758 // First time we see ParentVNI. VNI dominates itself.
759 Dom = DomPair(ValMBB, VNI->def);
760 } else if (Dom.first == ValMBB) {
761 // Two defs in the same block. Pick the earlier def.
762 if (!Dom.second.isValid() || VNI->def < Dom.second)
763 Dom.second = VNI->def;
764 } else {
765 // Different basic blocks. Check if one dominates.
766 MachineBasicBlock *Near =
767 MDT.findNearestCommonDominator(Dom.first, ValMBB);
768 if (Near == ValMBB)
769 // Def ValMBB dominates.
770 Dom = DomPair(ValMBB, VNI->def);
771 else if (Near != Dom.first)
772 // None dominate. Hoist to common dominator, need new def.
773 Dom = DomPair(Near, SlotIndex());
774 }
775
776 DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
777 << " for parent " << ParentVNI->id << '@' << ParentVNI->def
778 << " hoist to BB#" << Dom.first->getNumber() << ' '
779 << Dom.second << '\n');
780 }
781
782 // Insert the hoisted copies.
783 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
784 DomPair &Dom = NearestDom[i];
785 if (!Dom.first || Dom.second.isValid())
786 continue;
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000787 // This value needs a hoisted copy inserted at the end of Dom.first.
788 VNInfo *ParentVNI = Parent->getValNumInfo(i);
789 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
790 // Get a less loopy dominator than Dom.first.
791 Dom.first = findShallowDominator(Dom.first, DefMBB);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000792 SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
793 Dom.second =
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000794 defFromParent(0, ParentVNI, Last, *Dom.first,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000795 SA.getLastSplitPointIter(Dom.first))->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000796 }
797
798 // Remove redundant back-copies that are now known to be dominated by another
799 // def with the same value.
800 SmallVector<VNInfo*, 8> BackCopies;
Matthias Braun96761952014-12-10 23:07:54 +0000801 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000802 if (VNI->isUnused())
803 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000804 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
805 const DomPair &Dom = NearestDom[ParentVNI->id];
806 if (!Dom.first || Dom.second == VNI->def)
807 continue;
808 BackCopies.push_back(VNI);
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000809 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000810 }
811 removeBackCopies(BackCopies);
812}
813
814
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000815/// transferValues - Transfer all possible values to the new live ranges.
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000816/// Values that were rematerialized are left alone, they need LRCalc.extend().
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000817bool SplitEditor::transferValues() {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000818 bool Skipped = false;
819 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Matthias Braun96761952014-12-10 23:07:54 +0000820 for (const LiveRange::Segment &S : Edit->getParent()) {
821 DEBUG(dbgs() << " blit " << S << ':');
822 VNInfo *ParentVNI = S.valno;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000823 // RegAssign has holes where RegIdx 0 should be used.
Matthias Braun96761952014-12-10 23:07:54 +0000824 SlotIndex Start = S.start;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000825 AssignI.advanceTo(Start);
826 do {
827 unsigned RegIdx;
Matthias Braun96761952014-12-10 23:07:54 +0000828 SlotIndex End = S.end;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000829 if (!AssignI.valid()) {
830 RegIdx = 0;
831 } else if (AssignI.start() <= Start) {
832 RegIdx = AssignI.value();
833 if (AssignI.stop() < End) {
834 End = AssignI.stop();
835 ++AssignI;
836 }
837 } else {
838 RegIdx = 0;
839 End = std::min(End, AssignI.start());
840 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000841
842 // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000843 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000844 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000845
846 // Check for a simply defined value that can be blitted directly.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000847 ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
848 if (VNInfo *VNI = VFP.getPointer()) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000849 DEBUG(dbgs() << ':' << VNI->id);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000850 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000851 Start = End;
852 continue;
853 }
854
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000855 // Skip values with forced recomputation.
856 if (VFP.getInt()) {
857 DEBUG(dbgs() << "(recalc)");
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000858 Skipped = true;
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000859 Start = End;
860 continue;
861 }
862
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000863 LiveRangeCalc &LRC = getLRCalc(RegIdx);
864
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000865 // This value has multiple defs in RegIdx, but it wasn't rematerialized,
866 // so the live range is accurate. Add live-in blocks in [Start;End) to the
867 // LiveInBlocks.
868 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
869 SlotIndex BlockStart, BlockEnd;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000870 std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000871
872 // The first block may be live-in, or it may have its own def.
873 if (Start != BlockStart) {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000874 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000875 assert(VNI && "Missing def for complex mapped value");
876 DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
877 // MBB has its own def. Is it also live-out?
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000878 if (BlockEnd <= End)
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000879 LRC.setLiveOutValue(MBB, VNI);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000880
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000881 // Skip to the next block for live-in.
882 ++MBB;
883 BlockStart = BlockEnd;
884 }
885
886 // Handle the live-in blocks covered by [Start;End).
887 assert(Start <= BlockStart && "Expected live-in block");
888 while (BlockStart < End) {
889 DEBUG(dbgs() << ">BB#" << MBB->getNumber());
890 BlockEnd = LIS.getMBBEndIdx(MBB);
891 if (BlockStart == ParentVNI->def) {
892 // This block has the def of a parent PHI, so it isn't live-in.
893 assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000894 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000895 assert(VNI && "Missing def for complex mapped parent PHI");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000896 if (End >= BlockEnd)
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000897 LRC.setLiveOutValue(MBB, VNI); // Live-out as well.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000898 } else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000899 // This block needs a live-in value. The last block covered may not
900 // be live-out.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000901 if (End < BlockEnd)
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000902 LRC.addLiveInBlock(LR, MDT[MBB], End);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000903 else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000904 // Live-through, and we don't know the value.
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000905 LRC.addLiveInBlock(LR, MDT[MBB]);
Craig Topperc0196b12014-04-14 00:51:57 +0000906 LRC.setLiveOutValue(MBB, nullptr);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000907 }
908 }
909 BlockStart = BlockEnd;
910 ++MBB;
911 }
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000912 Start = End;
Matthias Braun96761952014-12-10 23:07:54 +0000913 } while (Start != S.end);
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000914 DEBUG(dbgs() << '\n');
915 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000916
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000917 LRCalc[0].calculateValues();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000918 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000919 LRCalc[1].calculateValues();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000920
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000921 return Skipped;
922}
923
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +0000924void SplitEditor::extendPHIKillRanges() {
925 // Extend live ranges to be live-out for successor PHI values.
Matthias Braun96761952014-12-10 23:07:54 +0000926 for (const VNInfo *PHIVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +0000927 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
928 continue;
929 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000930 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000931 LiveRangeCalc &LRC = getLRCalc(RegIdx);
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +0000932 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
933 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
934 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000935 SlotIndex End = LIS.getMBBEndIdx(*PI);
936 SlotIndex LastUse = End.getPrevSlot();
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +0000937 // The predecessor may not have a live-out value. That is OK, like an
938 // undef PHI operand.
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000939 if (Edit->getParent().liveAt(LastUse)) {
940 assert(RegAssign.lookup(LastUse) == RegIdx &&
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +0000941 "Different register assignment in phi predecessor");
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000942 LRC.extend(LR, End);
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +0000943 }
944 }
945 }
946}
947
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000948/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000949void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000950 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000951 RE = MRI.reg_end(); RI != RE;) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000952 MachineOperand &MO = *RI;
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +0000953 MachineInstr *MI = MO.getParent();
954 ++RI;
Eric Christopherede62672011-02-03 06:18:29 +0000955 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +0000956 if (MI->isDebugValue()) {
957 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +0000958 MO.setReg(0);
959 continue;
960 }
Jakob Stoklund Olesenf6e03942011-02-09 21:52:09 +0000961
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +0000962 // <undef> operands don't really read the register, so it doesn't matter
963 // which register we choose. When the use operand is tied to a def, we must
964 // use the same register as the def, so just do that always.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000965 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +0000966 if (MO.isDef() || MO.isUndef())
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000967 Idx = Idx.getRegSlot(MO.isEarlyClobber());
Eric Christopherede62672011-02-03 06:18:29 +0000968
969 // Rewrite to the mapped register at Idx.
970 unsigned RegIdx = RegAssign.lookup(Idx);
Mark Laceyf9ea8852013-08-14 23:50:04 +0000971 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000972 MO.setReg(LI->reg);
Eric Christopherede62672011-02-03 06:18:29 +0000973 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
974 << Idx << ':' << RegIdx << '\t' << *MI);
975
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +0000976 // Extend liveness to Idx if the instruction reads reg.
Jakob Stoklund Olesen73a9eb92011-07-24 20:33:23 +0000977 if (!ExtendRanges || MO.isUndef())
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +0000978 continue;
979
980 // Skip instructions that don't read Reg.
981 if (MO.isDef()) {
982 if (!MO.getSubReg() && !MO.isEarlyClobber())
983 continue;
984 // We may wan't to extend a live range for a partial redef, or for a use
985 // tied to an early clobber.
986 Idx = Idx.getPrevSlot();
987 if (!Edit->getParent().liveAt(Idx))
988 continue;
989 } else
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000990 Idx = Idx.getRegSlot(true);
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +0000991
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000992 getLRCalc(RegIdx).extend(*LI, Idx.getNextSlot());
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +0000993 }
994}
995
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000996void SplitEditor::deleteRematVictims() {
997 SmallVector<MachineInstr*, 8> Dead;
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +0000998 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
Mark Laceyf9ea8852013-08-14 23:50:04 +0000999 LiveInterval *LI = &LIS.getInterval(*I);
Matthias Braun96761952014-12-10 23:07:54 +00001000 for (const LiveRange::Segment &S : LI->segments) {
Jakob Stoklund Olesend8f24052011-11-13 22:42:13 +00001001 // Dead defs end at the dead slot.
Matthias Braun96761952014-12-10 23:07:54 +00001002 if (S.end != S.valno->def.getDeadSlot())
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001003 continue;
Matthias Braun96761952014-12-10 23:07:54 +00001004 MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001005 assert(MI && "Missing instruction for dead def");
1006 MI->addRegisterDead(LI->reg, &TRI);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001007
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001008 if (!MI->allDefsAreDead())
1009 continue;
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001010
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001011 DEBUG(dbgs() << "All defs dead: " << *MI);
1012 Dead.push_back(MI);
1013 }
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001014 }
1015
1016 if (Dead.empty())
1017 return;
1018
Pete Cooper2bde2f42012-04-02 22:22:53 +00001019 Edit->eliminateDeadDefs(Dead);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001020}
1021
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001022void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001023 ++NumFinished;
Eric Christopher21933532011-02-03 05:40:54 +00001024
Eric Christopherede62672011-02-03 06:18:29 +00001025 // At this point, the live intervals in Edit contain VNInfos corresponding to
1026 // the inserted copies.
1027
1028 // Add the original defs from the parent interval.
Matthias Braun96761952014-12-10 23:07:54 +00001029 for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen3295a992011-02-04 00:59:23 +00001030 if (ParentVNI->isUnused())
1031 continue;
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +00001032 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Jakob Stoklund Olesen97e14e02012-07-27 21:11:14 +00001033 defValue(RegIdx, ParentVNI, ParentVNI->def);
Jakob Stoklund Olesen32210de2011-03-15 21:13:22 +00001034
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001035 // Force rematted values to be recomputed everywhere.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001036 // The new live ranges may be truncated.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001037 if (Edit->didRematerialize(ParentVNI))
1038 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001039 forceRecompute(i, ParentVNI);
Eric Christopherede62672011-02-03 06:18:29 +00001040 }
1041
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001042 // Hoist back-copies to the complement interval when in spill mode.
1043 switch (SpillMode) {
1044 case SM_Partition:
1045 // Leave all back-copies as is.
1046 break;
1047 case SM_Size:
1048 hoistCopiesForSize();
1049 break;
1050 case SM_Speed:
1051 llvm_unreachable("Spill mode 'speed' not implemented yet");
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001052 }
1053
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001054 // Transfer the simply mapped values, check if any are skipped.
1055 bool Skipped = transferValues();
1056 if (Skipped)
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001057 extendPHIKillRanges();
1058 else
1059 ++NumSimple;
Eric Christopherede62672011-02-03 06:18:29 +00001060
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001061 // Rewrite virtual registers, possibly extending ranges.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001062 rewriteAssigned(Skipped);
Eric Christopherede62672011-02-03 06:18:29 +00001063
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001064 // Delete defs that were rematted everywhere.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001065 if (Skipped)
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001066 deleteRematVictims();
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001067
Jakob Stoklund Olesen0f1677e2010-10-07 23:34:34 +00001068 // Get rid of unused values and set phi-kill flags.
Mark Laceyf9ea8852013-08-14 23:50:04 +00001069 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) {
1070 LiveInterval &LI = LIS.getInterval(*I);
1071 LI.RenumberValues();
1072 }
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001073
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001074 // Provide a reverse mapping from original indices to Edit ranges.
1075 if (LRMap) {
1076 LRMap->clear();
1077 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1078 LRMap->push_back(i);
1079 }
1080
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001081 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +00001082 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001083 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001084 // Don't use iterators, they are invalidated by create() below.
Matthias Braund3dd1352015-09-22 03:44:41 +00001085 unsigned VReg = Edit->get(i);
1086 LiveInterval &LI = LIS.getInterval(VReg);
1087 SmallVector<LiveInterval*, 8> SplitLIs;
1088 LIS.splitSeparateComponents(LI, SplitLIs);
1089 unsigned Original = VRM.getOriginal(VReg);
1090 for (LiveInterval *SplitLI : SplitLIs)
1091 VRM.setIsSplitFromReg(SplitLI->reg, Original);
1092
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001093 // The new intervals all map back to i.
1094 if (LRMap)
1095 LRMap->resize(Edit->size(), i);
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001096 }
1097
Jakob Stoklund Olesen284c2db2010-08-10 17:07:22 +00001098 // Calculate spill weight and allocation hints for new intervals.
Benjamin Kramere2a1d892013-06-17 19:00:36 +00001099 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001100
1101 assert(!LRMap || LRMap->size() == Edit->size());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +00001102}
1103
1104
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +00001105//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +00001106// Single Block Splitting
1107//===----------------------------------------------------------------------===//
1108
Jakob Stoklund Olesen8627ea92011-08-05 22:20:45 +00001109bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
1110 bool SingleInstrs) const {
1111 // Always split for multiple instructions.
1112 if (!BI.isOneInstr())
1113 return true;
1114 // Don't split for single instructions unless explicitly requested.
1115 if (!SingleInstrs)
1116 return false;
1117 // Splitting a live-through range always makes progress.
1118 if (BI.LiveIn && BI.LiveOut)
1119 return true;
1120 // No point in isolating a copy. It has no register class constraints.
1121 if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
1122 return false;
1123 // Finally, don't isolate an end point that was created by earlier splits.
1124 return isOriginalEndpoint(BI.FirstInstr);
1125}
1126
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001127void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1128 openIntv();
1129 SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001130 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001131 LastSplitPoint));
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001132 if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
1133 useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001134 } else {
1135 // The last use is after the last valid split point.
1136 SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1137 useIntv(SegStart, SegStop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001138 overlapIntv(SegStop, BI.LastInstr);
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001139 }
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001140}
1141
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001142
1143//===----------------------------------------------------------------------===//
1144// Global Live Range Splitting Support
1145//===----------------------------------------------------------------------===//
1146
1147// These methods support a method of global live range splitting that uses a
1148// global algorithm to decide intervals for CFG edges. They will insert split
1149// points and color intervals in basic blocks while avoiding interference.
1150//
1151// Note that splitSingleBlock is also useful for blocks where both CFG edges
1152// are on the stack.
1153
1154void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1155 unsigned IntvIn, SlotIndex LeaveBefore,
1156 unsigned IntvOut, SlotIndex EnterAfter){
1157 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001158 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001159
1160 DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
1161 << ") intf " << LeaveBefore << '-' << EnterAfter
1162 << ", live-through " << IntvIn << " -> " << IntvOut);
1163
1164 assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1165
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001166 assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
1167 assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
1168 assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
1169
1170 MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
1171
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001172 if (!IntvOut) {
1173 DEBUG(dbgs() << ", spill on entry.\n");
1174 //
1175 // <<<<<<<<< Possible LeaveBefore interference.
1176 // |-----------| Live through.
1177 // -____________ Spill on entry.
1178 //
1179 selectIntv(IntvIn);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001180 SlotIndex Idx = leaveIntvAtTop(*MBB);
1181 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1182 (void)Idx;
1183 return;
1184 }
1185
1186 if (!IntvIn) {
1187 DEBUG(dbgs() << ", reload on exit.\n");
1188 //
1189 // >>>>>>> Possible EnterAfter interference.
1190 // |-----------| Live through.
1191 // ___________-- Reload on exit.
1192 //
1193 selectIntv(IntvOut);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001194 SlotIndex Idx = enterIntvAtEnd(*MBB);
1195 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1196 (void)Idx;
1197 return;
1198 }
1199
1200 if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1201 DEBUG(dbgs() << ", straight through.\n");
1202 //
1203 // |-----------| Live through.
1204 // ------------- Straight through, same intv, no interference.
1205 //
1206 selectIntv(IntvOut);
1207 useIntv(Start, Stop);
1208 return;
1209 }
1210
1211 // We cannot legally insert splits after LSP.
1212 SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001213 assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001214
1215 if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1216 LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1217 DEBUG(dbgs() << ", switch avoiding interference.\n");
1218 //
1219 // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
1220 // |-----------| Live through.
1221 // ------======= Switch intervals between interference.
1222 //
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001223 selectIntv(IntvOut);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001224 SlotIndex Idx;
1225 if (LeaveBefore && LeaveBefore < LSP) {
1226 Idx = enterIntvBefore(LeaveBefore);
1227 useIntv(Idx, Stop);
1228 } else {
1229 Idx = enterIntvAtEnd(*MBB);
1230 }
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001231 selectIntv(IntvIn);
1232 useIntv(Start, Idx);
1233 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1234 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1235 return;
1236 }
1237
1238 DEBUG(dbgs() << ", create local intv for interference.\n");
1239 //
1240 // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
1241 // |-----------| Live through.
1242 // ==---------== Switch intervals before/after interference.
1243 //
1244 assert(LeaveBefore <= EnterAfter && "Missed case");
1245
1246 selectIntv(IntvOut);
1247 SlotIndex Idx = enterIntvAfter(EnterAfter);
1248 useIntv(Idx, Stop);
1249 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1250
1251 selectIntv(IntvIn);
1252 Idx = leaveIntvBefore(LeaveBefore);
1253 useIntv(Start, Idx);
1254 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1255}
1256
1257
1258void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1259 unsigned IntvIn, SlotIndex LeaveBefore) {
1260 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001261 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001262
1263 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001264 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001265 << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
1266 << (BI.LiveOut ? ", stack-out" : ", killed in block"));
1267
1268 assert(IntvIn && "Must have register in");
1269 assert(BI.LiveIn && "Must be live-in");
1270 assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1271
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001272 if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001273 DEBUG(dbgs() << " before interference.\n");
1274 //
1275 // <<< Interference after kill.
1276 // |---o---x | Killed in block.
1277 // ========= Use IntvIn everywhere.
1278 //
1279 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001280 useIntv(Start, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001281 return;
1282 }
1283
1284 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1285
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001286 if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001287 //
1288 // <<< Possible interference after last use.
1289 // |---o---o---| Live-out on stack.
1290 // =========____ Leave IntvIn after last use.
1291 //
1292 // < Interference after last use.
1293 // |---o---o--o| Live-out on stack, late last use.
1294 // ============ Copy to stack after LSP, overlap IntvIn.
1295 // \_____ Stack interval is live-out.
1296 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001297 if (BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001298 DEBUG(dbgs() << ", spill after last use before interference.\n");
1299 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001300 SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001301 useIntv(Start, Idx);
1302 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1303 } else {
1304 DEBUG(dbgs() << ", spill before last split point.\n");
1305 selectIntv(IntvIn);
Jakob Stoklund Olesen37e3a132011-07-16 00:13:30 +00001306 SlotIndex Idx = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001307 overlapIntv(Idx, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001308 useIntv(Start, Idx);
1309 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1310 }
1311 return;
1312 }
1313
1314 // The interference is overlapping somewhere we wanted to use IntvIn. That
1315 // means we need to create a local interval that can be allocated a
1316 // different register.
1317 unsigned LocalIntv = openIntv();
Matt Beaumont-Gay26909d82011-07-16 04:18:47 +00001318 (void)LocalIntv;
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001319 DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
1320
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001321 if (!BI.LiveOut || BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001322 //
1323 // <<<<<<< Interference overlapping uses.
1324 // |---o---o---| Live-out on stack.
1325 // =====----____ Leave IntvIn before interference, then spill.
1326 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001327 SlotIndex To = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001328 SlotIndex From = enterIntvBefore(LeaveBefore);
1329 useIntv(From, To);
1330 selectIntv(IntvIn);
1331 useIntv(Start, From);
1332 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1333 return;
1334 }
1335
1336 // <<<<<<< Interference overlapping uses.
1337 // |---o---o--o| Live-out on stack, late last use.
1338 // =====------- Copy to stack before LSP, overlap LocalIntv.
1339 // \_____ Stack interval is live-out.
1340 //
1341 SlotIndex To = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001342 overlapIntv(To, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001343 SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1344 useIntv(From, To);
1345 selectIntv(IntvIn);
1346 useIntv(Start, From);
1347 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1348}
1349
1350void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1351 unsigned IntvOut, SlotIndex EnterAfter) {
1352 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001353 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001354
1355 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001356 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001357 << ", reg-out " << IntvOut << ", enter after " << EnterAfter
1358 << (BI.LiveIn ? ", stack-in" : ", defined in block"));
1359
1360 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1361
1362 assert(IntvOut && "Must have register out");
1363 assert(BI.LiveOut && "Must be live-out");
1364 assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1365
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001366 if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001367 DEBUG(dbgs() << " after interference.\n");
1368 //
1369 // >>>> Interference before def.
1370 // | o---o---| Defined in block.
1371 // ========= Use IntvOut everywhere.
1372 //
1373 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001374 useIntv(BI.FirstInstr, Stop);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001375 return;
1376 }
1377
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001378 if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001379 DEBUG(dbgs() << ", reload after interference.\n");
1380 //
1381 // >>>> Interference before def.
1382 // |---o---o---| Live-through, stack-in.
1383 // ____========= Enter IntvOut before first use.
1384 //
1385 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001386 SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001387 useIntv(Idx, Stop);
1388 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1389 return;
1390 }
1391
1392 // The interference is overlapping somewhere we wanted to use IntvOut. That
1393 // means we need to create a local interval that can be allocated a
1394 // different register.
1395 DEBUG(dbgs() << ", interference overlaps uses.\n");
1396 //
1397 // >>>>>>> Interference overlapping uses.
1398 // |---o---o---| Live-through, stack-in.
1399 // ____---====== Create local interval for interference range.
1400 //
1401 selectIntv(IntvOut);
1402 SlotIndex Idx = enterIntvAfter(EnterAfter);
1403 useIntv(Idx, Stop);
1404 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1405
1406 openIntv();
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001407 SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001408 useIntv(From, Idx);
1409}