blob: 51dddabed2d9ea7bb1892d44283563ffda3bb27f [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.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000180 MachineFunction::iterator MFI =
181 LIS.getMBBFromIndex(LVI->start)->getIterator();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000182 for (;;) {
183 BlockInfo BI;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000184 BI.MBB = &*MFI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000185 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000186 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000187
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000188 // If the block contains no uses, the range must be live through. At one
Rafael Espindola676c4052011-06-26 22:34:10 +0000189 // point, RegisterCoalescer could create dangling ranges that ended
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000190 // mid-block.
191 if (UseI == UseE || *UseI >= Stop) {
192 ++NumThroughBlocks;
193 ThroughBlocks.set(BI.MBB->getNumber());
194 // The range shouldn't end mid-block if there are no uses. This shouldn't
195 // happen.
196 if (LVI->end < Stop)
197 return false;
198 } else {
199 // This block has uses. Find the first and last uses in the block.
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000200 BI.FirstInstr = *UseI;
201 assert(BI.FirstInstr >= Start);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000202 do ++UseI;
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000203 while (UseI != UseE && *UseI < Stop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000204 BI.LastInstr = UseI[-1];
205 assert(BI.LastInstr < Stop);
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000206
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000207 // LVI is the first live segment overlapping MBB.
208 BI.LiveIn = LVI->start <= Start;
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000209
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000210 // When not live in, the first use should be a def.
211 if (!BI.LiveIn) {
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000212 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000213 assert(LVI->start == BI.FirstInstr && "First instr should be a def");
214 BI.FirstDef = BI.FirstInstr;
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000215 }
216
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000217 // Look for gaps in the live range.
218 BI.LiveOut = true;
219 while (LVI->end < Stop) {
220 SlotIndex LastStop = LVI->end;
221 if (++LVI == LVE || LVI->start >= Stop) {
222 BI.LiveOut = false;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000223 BI.LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000224 break;
225 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000226
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000227 if (LastStop < LVI->start) {
228 // There is a gap in the live range. Create duplicate entries for the
229 // live-in snippet and the live-out snippet.
230 ++NumGapBlocks;
231
232 // Push the Live-in part.
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000233 BI.LiveOut = false;
234 UseBlocks.push_back(BI);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000235 UseBlocks.back().LastInstr = LastStop;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000236
237 // Set up BI for the live-out part.
238 BI.LiveIn = false;
239 BI.LiveOut = true;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000240 BI.FirstInstr = BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000241 }
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000242
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000243 // A Segment that starts in the middle of the block must be a def.
244 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000245 if (!BI.FirstDef)
246 BI.FirstDef = LVI->start;
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000247 }
248
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000249 UseBlocks.push_back(BI);
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000250
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000251 // LVI is now at LVE or LVI->end >= Stop.
252 if (LVI == LVE)
253 break;
254 }
Jakob Stoklund Olesenca6a4d82011-05-29 21:24:39 +0000255
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000256 // Live segment ends exactly at Stop. Move to the next segment.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000257 if (LVI->end == Stop && ++LVI == LVE)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000258 break;
259
260 // Pick the next basic block.
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000261 if (LVI->start < Stop)
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000262 ++MFI;
263 else
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000264 MFI = LIS.getMBBFromIndex(LVI->start)->getIterator();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000265 }
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000266
267 assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000268 return true;
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000269}
270
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000271unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
272 if (cli->empty())
273 return 0;
274 LiveInterval *li = const_cast<LiveInterval*>(cli);
275 LiveInterval::iterator LVI = li->begin();
276 LiveInterval::iterator LVE = li->end();
277 unsigned Count = 0;
278
279 // Loop over basic blocks where li is live.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000280 MachineFunction::const_iterator MFI =
281 LIS.getMBBFromIndex(LVI->start)->getIterator();
282 SlotIndex Stop = LIS.getMBBEndIdx(&*MFI);
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000283 for (;;) {
284 ++Count;
285 LVI = li->advanceTo(LVI, Stop);
286 if (LVI == LVE)
287 return Count;
288 do {
289 ++MFI;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000290 Stop = LIS.getMBBEndIdx(&*MFI);
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000291 } while (Stop <= LVI->start);
292 }
293}
294
Jakob Stoklund Olesen60a26a62011-02-21 23:09:46 +0000295bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
296 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
297 const LiveInterval &Orig = LIS.getInterval(OrigReg);
298 assert(!Orig.empty() && "Splitting empty interval?");
299 LiveInterval::const_iterator I = Orig.find(Idx);
300
301 // Range containing Idx should begin at Idx.
302 if (I != Orig.end() && I->start <= Idx)
303 return I->start == Idx;
304
305 // Range does not contain Idx, previous must end at Idx.
306 return I != Orig.begin() && (--I)->end == Idx;
307}
308
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000309void SplitAnalysis::analyze(const LiveInterval *li) {
310 clear();
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000311 CurLI = li;
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000312 analyzeUses();
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000313}
314
Jakob Stoklund Olesen28e769c2010-12-15 17:49:52 +0000315
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000316//===----------------------------------------------------------------------===//
317// Split Editor
318//===----------------------------------------------------------------------===//
319
320/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Eric Christopherd9134482014-08-04 21:25:23 +0000321SplitEditor::SplitEditor(SplitAnalysis &sa, LiveIntervals &lis, VirtRegMap &vrm,
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000322 MachineDominatorTree &mdt,
323 MachineBlockFrequencyInfo &mbfi)
Eric Christopherd9134482014-08-04 21:25:23 +0000324 : SA(sa), LIS(lis), VRM(vrm), MRI(vrm.getMachineFunction().getRegInfo()),
Eric Christopher60621802014-10-14 07:22:00 +0000325 MDT(mdt), TII(*vrm.getMachineFunction().getSubtarget().getInstrInfo()),
326 TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()),
Eric Christopherd9134482014-08-04 21:25:23 +0000327 MBFI(mbfi), Edit(nullptr), OpenIdx(0), SpillMode(SM_Partition),
328 RegAssign(Allocator) {}
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000329
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000330void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
331 Edit = &LRE;
332 SpillMode = SM;
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000333 OpenIdx = 0;
334 RegAssign.clear();
335 Values.clear();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000336
337 // Reset the LiveRangeCalc instances needed for this spill mode.
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000338 LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
339 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000340 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000341 LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
342 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000343
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000344 // We don't need an AliasAnalysis since we will only be performing
345 // cheap-as-a-copy remats anyway.
Craig Topperc0196b12014-04-14 00:51:57 +0000346 Edit->anyRematerializable(nullptr);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000347}
348
Manman Ren19f49ac2012-09-11 22:23:19 +0000349#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Eric Christopherede62672011-02-03 06:18:29 +0000350void SplitEditor::dump() const {
351 if (RegAssign.empty()) {
352 dbgs() << " empty\n";
353 return;
354 }
355
356 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
357 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
358 dbgs() << '\n';
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000359}
Manman Ren742534c2012-09-06 19:06:06 +0000360#endif
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000361
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000362VNInfo *SplitEditor::defValue(unsigned RegIdx,
363 const VNInfo *ParentVNI,
364 SlotIndex Idx) {
365 assert(ParentVNI && "Mapping NULL value");
366 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000367 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
Mark Laceyf9ea8852013-08-14 23:50:04 +0000368 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000369
370 // Create a new value.
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000371 VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000372
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000373 // Use insert for lookup, so we can add missing values with a second lookup.
374 std::pair<ValueMap::iterator, bool> InsP =
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000375 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id),
376 ValueForcePair(VNI, false)));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000377
378 // This was the first time (RegIdx, ParentVNI) was mapped.
379 // Keep it as a simple def without any liveness.
380 if (InsP.second)
381 return VNI;
382
383 // If the previous value was a simple mapping, add liveness for it now.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000384 if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000385 SlotIndex Def = OldVNI->def;
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000386 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), OldVNI));
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000387 // No longer a simple mapping. Switch to a complex, non-forced mapping.
388 InsP.first->second = ValueForcePair();
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000389 }
390
391 // This is a complex mapping, add liveness for VNI
392 SlotIndex Def = VNI->def;
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000393 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000394
395 return VNI;
396}
397
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000398void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000399 assert(ParentVNI && "Mapping NULL value");
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000400 ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
401 VNInfo *VNI = VFP.getPointer();
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000402
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000403 // ParentVNI was either unmapped or already complex mapped. Either way, just
404 // set the force bit.
405 if (!VNI) {
406 VFP.setInt(true);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000407 return;
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000408 }
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000409
410 // This was previously a single mapping. Make sure the old def is represented
411 // by a trivial live range.
412 SlotIndex Def = VNI->def;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000413 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Matthias Braun13ddb7c2013-10-10 21:28:43 +0000414 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000415 // Mark as complex mapped, forced.
Craig Topperc0196b12014-04-14 00:51:57 +0000416 VFP = ValueForcePair(nullptr, true);
Jakob Stoklund Olesen4484f992011-09-13 18:05:29 +0000417}
418
Eric Christopherede62672011-02-03 06:18:29 +0000419VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000420 VNInfo *ParentVNI,
421 SlotIndex UseIdx,
422 MachineBasicBlock &MBB,
423 MachineBasicBlock::iterator I) {
Craig Topperc0196b12014-04-14 00:51:57 +0000424 MachineInstr *CopyMI = nullptr;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000425 SlotIndex Def;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000426 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000427
Jakob Stoklund Olesen7d406792011-05-02 05:29:58 +0000428 // We may be trying to avoid interference that ends at a deleted instruction,
429 // so always begin RegIdx 0 early and all others late.
430 bool Late = RegIdx != 0;
431
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000432 // Attempt cheap-as-a-copy rematerialization.
433 LiveRangeEdit::Remat RM(ParentVNI);
Pete Cooper2bde2f42012-04-02 22:22:53 +0000434 if (Edit->canRematerializeAt(RM, UseIdx, true)) {
435 Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late);
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000436 ++NumRemats;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000437 } else {
438 // Can't remat, just insert a copy from parent.
Eric Christopherede62672011-02-03 06:18:29 +0000439 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000440 .addReg(Edit->getReg());
Jakob Stoklund Olesen7d406792011-05-02 05:29:58 +0000441 Def = LIS.getSlotIndexes()->insertMachineInstrInMaps(CopyMI, Late)
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000442 .getRegSlot();
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000443 ++NumCopies;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000444 }
445
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000446 // Define the value in Reg.
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000447 return defValue(RegIdx, ParentVNI, Def);
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000448}
449
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000450/// Create a new virtual register and live interval.
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000451unsigned SplitEditor::openIntv() {
Eric Christopherede62672011-02-03 06:18:29 +0000452 // Create the complement as index 0.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000453 if (Edit->empty())
Mark Lacey9d8103d2013-08-14 23:50:16 +0000454 Edit->createEmptyInterval();
Eric Christopherede62672011-02-03 06:18:29 +0000455
456 // Create the open interval.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000457 OpenIdx = Edit->size();
Mark Lacey9d8103d2013-08-14 23:50:16 +0000458 Edit->createEmptyInterval();
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000459 return OpenIdx;
460}
461
462void SplitEditor::selectIntv(unsigned Idx) {
463 assert(Idx != 0 && "Cannot select the complement interval");
464 assert(Idx < Edit->size() && "Can only select previously opened interval");
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000465 DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000466 OpenIdx = Idx;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000467}
468
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000469SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000470 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000471 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000472 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000473 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000474 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000475 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000476 return Idx;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000477 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000478 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000479 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000480 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000481
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000482 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
483 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000484}
485
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000486SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
487 assert(OpenIdx && "openIntv not called before enterIntvAfter");
488 DEBUG(dbgs() << " enterIntvAfter " << Idx);
489 Idx = Idx.getBoundaryIndex();
490 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
491 if (!ParentVNI) {
492 DEBUG(dbgs() << ": not live\n");
493 return Idx;
494 }
495 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
496 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
497 assert(MI && "enterIntvAfter called with invalid index");
498
499 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000500 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000501 return VNI->def;
502}
503
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000504SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000505 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000506 SlotIndex End = LIS.getMBBEndIdx(&MBB);
507 SlotIndex Last = End.getPrevSlot();
508 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000509 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000510 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000511 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000512 return End;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000513 }
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000514 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000515 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000516 SA.getLastSplitPointIter(&MBB));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000517 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopherede62672011-02-03 06:18:29 +0000518 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000519 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000520}
521
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000522/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000523void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000524 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000525}
526
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000527void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopherede62672011-02-03 06:18:29 +0000528 assert(OpenIdx && "openIntv not called before useIntv");
529 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
530 RegAssign.insert(Start, End, OpenIdx);
531 DEBUG(dump());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000532}
533
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000534SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopherede62672011-02-03 06:18:29 +0000535 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000536 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000537
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000538 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000539 SlotIndex Boundary = Idx.getBoundaryIndex();
540 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000541 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000542 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000543 return Boundary.getNextSlot();
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000544 }
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000545 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000546 MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
Jakob Stoklund Olesen3d11c8e2011-02-08 18:50:18 +0000547 assert(MI && "No instruction at index");
Jakob Stoklund Olesene2c92a32011-09-16 00:03:35 +0000548
549 // In spill mode, make live ranges as short as possible by inserting the copy
550 // before MI. This is only possible if that instruction doesn't redefine the
551 // value. The inserted COPY is not a kill, and we don't need to recompute
552 // the source live range. The spiller also won't try to hoist this copy.
553 if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
554 MI->readsVirtualRegister(Edit->getReg())) {
555 forceRecompute(0, ParentVNI);
556 defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
557 return Idx;
558 }
559
560 VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000561 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000562 return VNI->def;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000563}
564
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000565SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
566 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
567 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
568
569 // The interval must be live into the instruction at Idx.
Jakob Stoklund Olesenc45d38e2011-07-18 18:47:13 +0000570 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000571 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000572 if (!ParentVNI) {
573 DEBUG(dbgs() << ": not live\n");
574 return Idx.getNextSlot();
575 }
576 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
577
578 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
579 assert(MI && "No instruction at index");
580 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
581 return VNI->def;
582}
583
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000584SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopherede62672011-02-03 06:18:29 +0000585 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000586 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000587 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000588
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000589 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesen98551092010-09-16 00:01:36 +0000590 if (!ParentVNI) {
Jakob Stoklund Olesen9575af42010-10-07 17:56:35 +0000591 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000592 return Start;
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000593 }
594
Eric Christopherede62672011-02-03 06:18:29 +0000595 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000596 MBB.SkipPHIsAndLabels(MBB.begin()));
Eric Christopherede62672011-02-03 06:18:29 +0000597 RegAssign.insert(Start, VNI->def, OpenIdx);
598 DEBUG(dump());
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000599 return VNI->def;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000600}
601
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000602void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
603 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000604 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesend7bcf432011-11-14 01:39:36 +0000605 assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000606 "Parent changes value in extended range");
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000607 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
608 "Range cannot span basic blocks");
609
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000610 // The complement interval will be extended as needed by LRCalc.extend().
Jakob Stoklund Olesen5c482cd2011-04-05 23:43:14 +0000611 if (ParentVNI)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000612 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000613 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
614 RegAssign.insert(Start, End, OpenIdx);
615 DEBUG(dump());
616}
617
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000618//===----------------------------------------------------------------------===//
619// Spill modes
620//===----------------------------------------------------------------------===//
621
622void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
Mark Laceyf9ea8852013-08-14 23:50:04 +0000623 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000624 DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
625 RegAssignMap::iterator AssignI;
626 AssignI.setMap(RegAssign);
627
628 for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
Matthias Braun311730a2015-01-21 19:02:30 +0000629 SlotIndex Def = Copies[i]->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000630 MachineInstr *MI = LIS.getInstructionFromIndex(Def);
631 assert(MI && "No instruction for back-copy");
632
633 MachineBasicBlock *MBB = MI->getParent();
634 MachineBasicBlock::iterator MBBI(MI);
635 bool AtBegin;
636 do AtBegin = MBBI == MBB->begin();
637 while (!AtBegin && (--MBBI)->isDebugValue());
638
639 DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
Matthias Braun311730a2015-01-21 19:02:30 +0000640 LIS.removeVRegDefAt(*LI, Def);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000641 LIS.RemoveMachineInstrFromMaps(MI);
642 MI->eraseFromParent();
643
Matthias Braun311730a2015-01-21 19:02:30 +0000644 // Adjust RegAssign if a register assignment is killed at Def. We want to
645 // avoid calculating the live range of the source register if possible.
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000646 AssignI.find(Def.getPrevSlot());
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000647 if (!AssignI.valid() || AssignI.start() >= Def)
648 continue;
649 // If MI doesn't kill the assigned register, just leave it.
650 if (AssignI.stop() != Def)
651 continue;
652 unsigned RegIdx = AssignI.value();
653 if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
654 DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n');
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000655 forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000656 } else {
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000657 SlotIndex Kill = LIS.getInstructionIndex(MBBI).getRegSlot();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000658 DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
659 AssignI.setStop(Kill);
660 }
661 }
662}
663
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000664MachineBasicBlock*
665SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
666 MachineBasicBlock *DefMBB) {
667 if (MBB == DefMBB)
668 return MBB;
669 assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
670
671 const MachineLoopInfo &Loops = SA.Loops;
672 const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
673 MachineDomTreeNode *DefDomNode = MDT[DefMBB];
674
675 // Best candidate so far.
676 MachineBasicBlock *BestMBB = MBB;
677 unsigned BestDepth = UINT_MAX;
678
679 for (;;) {
680 const MachineLoop *Loop = Loops.getLoopFor(MBB);
681
682 // MBB isn't in a loop, it doesn't get any better. All dominators have a
683 // higher frequency by definition.
684 if (!Loop) {
685 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
686 << MBB->getNumber() << " at depth 0\n");
687 return MBB;
688 }
689
690 // We'll never be able to exit the DefLoop.
691 if (Loop == DefLoop) {
692 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
693 << MBB->getNumber() << " in the same loop\n");
694 return MBB;
695 }
696
697 // Least busy dominator seen so far.
698 unsigned Depth = Loop->getLoopDepth();
699 if (Depth < BestDepth) {
700 BestMBB = MBB;
701 BestDepth = Depth;
702 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
703 << MBB->getNumber() << " at depth " << Depth << '\n');
704 }
705
706 // Leave loop by going to the immediate dominator of the loop header.
707 // This is a bigger stride than simply walking up the dominator tree.
708 MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
709
710 // Too far up the dominator tree?
711 if (!IDom || !MDT.dominates(DefDomNode, IDom))
712 return BestMBB;
713
714 MBB = IDom->getBlock();
715 }
716}
717
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000718void SplitEditor::hoistCopiesForSize() {
719 // Get the complement interval, always RegIdx 0.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000720 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000721 LiveInterval *Parent = &Edit->getParent();
722
723 // Track the nearest common dominator for all back-copies for each ParentVNI,
724 // indexed by ParentVNI->id.
725 typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
726 SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
727
728 // Find the nearest common dominator for parent values with multiple
729 // back-copies. If a single back-copy dominates, put it in DomPair.second.
Matthias Braun96761952014-12-10 23:07:54 +0000730 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000731 if (VNI->isUnused())
732 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000733 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
734 assert(ParentVNI && "Parent not live at complement def");
735
736 // Don't hoist remats. The complement is probably going to disappear
737 // completely anyway.
738 if (Edit->didRematerialize(ParentVNI))
739 continue;
740
741 MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
742 DomPair &Dom = NearestDom[ParentVNI->id];
743
744 // Keep directly defined parent values. This is either a PHI or an
745 // instruction in the complement range. All other copies of ParentVNI
746 // should be eliminated.
747 if (VNI->def == ParentVNI->def) {
748 DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
749 Dom = DomPair(ValMBB, VNI->def);
750 continue;
751 }
752 // Skip the singly mapped values. There is nothing to gain from hoisting a
753 // single back-copy.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000754 if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000755 DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
756 continue;
757 }
758
759 if (!Dom.first) {
760 // First time we see ParentVNI. VNI dominates itself.
761 Dom = DomPair(ValMBB, VNI->def);
762 } else if (Dom.first == ValMBB) {
763 // Two defs in the same block. Pick the earlier def.
764 if (!Dom.second.isValid() || VNI->def < Dom.second)
765 Dom.second = VNI->def;
766 } else {
767 // Different basic blocks. Check if one dominates.
768 MachineBasicBlock *Near =
769 MDT.findNearestCommonDominator(Dom.first, ValMBB);
770 if (Near == ValMBB)
771 // Def ValMBB dominates.
772 Dom = DomPair(ValMBB, VNI->def);
773 else if (Near != Dom.first)
774 // None dominate. Hoist to common dominator, need new def.
775 Dom = DomPair(Near, SlotIndex());
776 }
777
778 DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
779 << " for parent " << ParentVNI->id << '@' << ParentVNI->def
780 << " hoist to BB#" << Dom.first->getNumber() << ' '
781 << Dom.second << '\n');
782 }
783
784 // Insert the hoisted copies.
785 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
786 DomPair &Dom = NearestDom[i];
787 if (!Dom.first || Dom.second.isValid())
788 continue;
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000789 // This value needs a hoisted copy inserted at the end of Dom.first.
790 VNInfo *ParentVNI = Parent->getValNumInfo(i);
791 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
792 // Get a less loopy dominator than Dom.first.
793 Dom.first = findShallowDominator(Dom.first, DefMBB);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000794 SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
795 Dom.second =
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000796 defFromParent(0, ParentVNI, Last, *Dom.first,
Jakob Stoklund Olesen67aec122012-01-11 02:07:00 +0000797 SA.getLastSplitPointIter(Dom.first))->def;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000798 }
799
800 // Remove redundant back-copies that are now known to be dominated by another
801 // def with the same value.
802 SmallVector<VNInfo*, 8> BackCopies;
Matthias Braun96761952014-12-10 23:07:54 +0000803 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen21809382012-08-03 20:59:29 +0000804 if (VNI->isUnused())
805 continue;
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000806 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
807 const DomPair &Dom = NearestDom[ParentVNI->id];
808 if (!Dom.first || Dom.second == VNI->def)
809 continue;
810 BackCopies.push_back(VNI);
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000811 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000812 }
813 removeBackCopies(BackCopies);
814}
815
816
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000817/// transferValues - Transfer all possible values to the new live ranges.
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000818/// Values that were rematerialized are left alone, they need LRCalc.extend().
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000819bool SplitEditor::transferValues() {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000820 bool Skipped = false;
821 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Matthias Braun96761952014-12-10 23:07:54 +0000822 for (const LiveRange::Segment &S : Edit->getParent()) {
823 DEBUG(dbgs() << " blit " << S << ':');
824 VNInfo *ParentVNI = S.valno;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000825 // RegAssign has holes where RegIdx 0 should be used.
Matthias Braun96761952014-12-10 23:07:54 +0000826 SlotIndex Start = S.start;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000827 AssignI.advanceTo(Start);
828 do {
829 unsigned RegIdx;
Matthias Braun96761952014-12-10 23:07:54 +0000830 SlotIndex End = S.end;
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000831 if (!AssignI.valid()) {
832 RegIdx = 0;
833 } else if (AssignI.start() <= Start) {
834 RegIdx = AssignI.value();
835 if (AssignI.stop() < End) {
836 End = AssignI.stop();
837 ++AssignI;
838 }
839 } else {
840 RegIdx = 0;
841 End = std::min(End, AssignI.start());
842 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000843
844 // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000845 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000846 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000847
848 // Check for a simply defined value that can be blitted directly.
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000849 ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
850 if (VNInfo *VNI = VFP.getPointer()) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000851 DEBUG(dbgs() << ':' << VNI->id);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000852 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000853 Start = End;
854 continue;
855 }
856
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000857 // Skip values with forced recomputation.
858 if (VFP.getInt()) {
859 DEBUG(dbgs() << "(recalc)");
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000860 Skipped = true;
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000861 Start = End;
862 continue;
863 }
864
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000865 LiveRangeCalc &LRC = getLRCalc(RegIdx);
866
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000867 // This value has multiple defs in RegIdx, but it wasn't rematerialized,
868 // so the live range is accurate. Add live-in blocks in [Start;End) to the
869 // LiveInBlocks.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000870 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000871 SlotIndex BlockStart, BlockEnd;
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000872 std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000873
874 // The first block may be live-in, or it may have its own def.
875 if (Start != BlockStart) {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000876 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000877 assert(VNI && "Missing def for complex mapped value");
878 DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
879 // MBB has its own def. Is it also live-out?
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000880 if (BlockEnd <= End)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000881 LRC.setLiveOutValue(&*MBB, VNI);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000882
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000883 // Skip to the next block for live-in.
884 ++MBB;
885 BlockStart = BlockEnd;
886 }
887
888 // Handle the live-in blocks covered by [Start;End).
889 assert(Start <= BlockStart && "Expected live-in block");
890 while (BlockStart < End) {
891 DEBUG(dbgs() << ">BB#" << MBB->getNumber());
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000892 BlockEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000893 if (BlockStart == ParentVNI->def) {
894 // This block has the def of a parent PHI, so it isn't live-in.
895 assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000896 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000897 assert(VNI && "Missing def for complex mapped parent PHI");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000898 if (End >= BlockEnd)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000899 LRC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000900 } else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000901 // This block needs a live-in value. The last block covered may not
902 // be live-out.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000903 if (End < BlockEnd)
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000904 LRC.addLiveInBlock(LR, MDT[&*MBB], End);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000905 else {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000906 // Live-through, and we don't know the value.
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000907 LRC.addLiveInBlock(LR, MDT[&*MBB]);
908 LRC.setLiveOutValue(&*MBB, nullptr);
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000909 }
910 }
911 BlockStart = BlockEnd;
912 ++MBB;
913 }
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000914 Start = End;
Matthias Braun96761952014-12-10 23:07:54 +0000915 } while (Start != S.end);
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000916 DEBUG(dbgs() << '\n');
917 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000918
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000919 LRCalc[0].calculateValues();
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000920 if (SpillMode)
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000921 LRCalc[1].calculateValues();
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000922
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000923 return Skipped;
924}
925
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +0000926void SplitEditor::extendPHIKillRanges() {
927 // Extend live ranges to be live-out for successor PHI values.
Matthias Braun96761952014-12-10 23:07:54 +0000928 for (const VNInfo *PHIVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +0000929 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
930 continue;
931 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000932 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000933 LiveRangeCalc &LRC = getLRCalc(RegIdx);
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +0000934 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
935 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
936 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000937 SlotIndex End = LIS.getMBBEndIdx(*PI);
938 SlotIndex LastUse = End.getPrevSlot();
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +0000939 // The predecessor may not have a live-out value. That is OK, like an
940 // undef PHI operand.
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000941 if (Edit->getParent().liveAt(LastUse)) {
942 assert(RegAssign.lookup(LastUse) == RegIdx &&
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +0000943 "Different register assignment in phi predecessor");
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000944 LRC.extend(LR, End);
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +0000945 }
946 }
947 }
948}
949
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000950/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000951void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000952 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000953 RE = MRI.reg_end(); RI != RE;) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000954 MachineOperand &MO = *RI;
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +0000955 MachineInstr *MI = MO.getParent();
956 ++RI;
Eric Christopherede62672011-02-03 06:18:29 +0000957 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +0000958 if (MI->isDebugValue()) {
959 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +0000960 MO.setReg(0);
961 continue;
962 }
Jakob Stoklund Olesenf6e03942011-02-09 21:52:09 +0000963
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +0000964 // <undef> operands don't really read the register, so it doesn't matter
965 // which register we choose. When the use operand is tied to a def, we must
966 // use the same register as the def, so just do that always.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000967 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen56a56eb2011-07-24 20:23:50 +0000968 if (MO.isDef() || MO.isUndef())
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000969 Idx = Idx.getRegSlot(MO.isEarlyClobber());
Eric Christopherede62672011-02-03 06:18:29 +0000970
971 // Rewrite to the mapped register at Idx.
972 unsigned RegIdx = RegAssign.lookup(Idx);
Mark Laceyf9ea8852013-08-14 23:50:04 +0000973 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen820c8fd02011-09-13 17:38:57 +0000974 MO.setReg(LI->reg);
Eric Christopherede62672011-02-03 06:18:29 +0000975 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
976 << Idx << ':' << RegIdx << '\t' << *MI);
977
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +0000978 // Extend liveness to Idx if the instruction reads reg.
Jakob Stoklund Olesen73a9eb92011-07-24 20:33:23 +0000979 if (!ExtendRanges || MO.isUndef())
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +0000980 continue;
981
982 // Skip instructions that don't read Reg.
983 if (MO.isDef()) {
984 if (!MO.getSubReg() && !MO.isEarlyClobber())
985 continue;
986 // We may wan't to extend a live range for a partial redef, or for a use
987 // tied to an early clobber.
988 Idx = Idx.getPrevSlot();
989 if (!Edit->getParent().liveAt(Idx))
990 continue;
991 } else
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000992 Idx = Idx.getRegSlot(true);
Jakob Stoklund Olesenc099dde2011-03-18 03:06:02 +0000993
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000994 getLRCalc(RegIdx).extend(*LI, Idx.getNextSlot());
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +0000995 }
996}
997
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000998void SplitEditor::deleteRematVictims() {
999 SmallVector<MachineInstr*, 8> Dead;
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001000 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
Mark Laceyf9ea8852013-08-14 23:50:04 +00001001 LiveInterval *LI = &LIS.getInterval(*I);
Matthias Braun96761952014-12-10 23:07:54 +00001002 for (const LiveRange::Segment &S : LI->segments) {
Jakob Stoklund Olesend8f24052011-11-13 22:42:13 +00001003 // Dead defs end at the dead slot.
Matthias Braun96761952014-12-10 23:07:54 +00001004 if (S.end != S.valno->def.getDeadSlot())
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001005 continue;
Matthias Braun96761952014-12-10 23:07:54 +00001006 MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001007 assert(MI && "Missing instruction for dead def");
1008 MI->addRegisterDead(LI->reg, &TRI);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001009
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001010 if (!MI->allDefsAreDead())
1011 continue;
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001012
Jakob Stoklund Olesen35502422011-03-20 19:46:23 +00001013 DEBUG(dbgs() << "All defs dead: " << *MI);
1014 Dead.push_back(MI);
1015 }
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001016 }
1017
1018 if (Dead.empty())
1019 return;
1020
Pete Cooper2bde2f42012-04-02 22:22:53 +00001021 Edit->eliminateDeadDefs(Dead);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001022}
1023
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001024void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001025 ++NumFinished;
Eric Christopher21933532011-02-03 05:40:54 +00001026
Eric Christopherede62672011-02-03 06:18:29 +00001027 // At this point, the live intervals in Edit contain VNInfos corresponding to
1028 // the inserted copies.
1029
1030 // Add the original defs from the parent interval.
Matthias Braun96761952014-12-10 23:07:54 +00001031 for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen3295a992011-02-04 00:59:23 +00001032 if (ParentVNI->isUnused())
1033 continue;
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +00001034 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Jakob Stoklund Olesen97e14e02012-07-27 21:11:14 +00001035 defValue(RegIdx, ParentVNI, ParentVNI->def);
Jakob Stoklund Olesen32210de2011-03-15 21:13:22 +00001036
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001037 // Force rematted values to be recomputed everywhere.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001038 // The new live ranges may be truncated.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001039 if (Edit->didRematerialize(ParentVNI))
1040 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +00001041 forceRecompute(i, ParentVNI);
Eric Christopherede62672011-02-03 06:18:29 +00001042 }
1043
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001044 // Hoist back-copies to the complement interval when in spill mode.
1045 switch (SpillMode) {
1046 case SM_Partition:
1047 // Leave all back-copies as is.
1048 break;
1049 case SM_Size:
1050 hoistCopiesForSize();
1051 break;
1052 case SM_Speed:
1053 llvm_unreachable("Spill mode 'speed' not implemented yet");
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +00001054 }
1055
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001056 // Transfer the simply mapped values, check if any are skipped.
1057 bool Skipped = transferValues();
1058 if (Skipped)
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001059 extendPHIKillRanges();
1060 else
1061 ++NumSimple;
Eric Christopherede62672011-02-03 06:18:29 +00001062
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +00001063 // Rewrite virtual registers, possibly extending ranges.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001064 rewriteAssigned(Skipped);
Eric Christopherede62672011-02-03 06:18:29 +00001065
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001066 // Delete defs that were rematted everywhere.
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +00001067 if (Skipped)
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +00001068 deleteRematVictims();
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001069
Jakob Stoklund Olesen0f1677e2010-10-07 23:34:34 +00001070 // Get rid of unused values and set phi-kill flags.
Mark Laceyf9ea8852013-08-14 23:50:04 +00001071 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) {
1072 LiveInterval &LI = LIS.getInterval(*I);
1073 LI.RenumberValues();
1074 }
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +00001075
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001076 // Provide a reverse mapping from original indices to Edit ranges.
1077 if (LRMap) {
1078 LRMap->clear();
1079 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1080 LRMap->push_back(i);
1081 }
1082
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001083 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +00001084 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +00001085 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001086 // Don't use iterators, they are invalidated by create() below.
Matthias Braund3dd1352015-09-22 03:44:41 +00001087 unsigned VReg = Edit->get(i);
1088 LiveInterval &LI = LIS.getInterval(VReg);
1089 SmallVector<LiveInterval*, 8> SplitLIs;
1090 LIS.splitSeparateComponents(LI, SplitLIs);
1091 unsigned Original = VRM.getOriginal(VReg);
1092 for (LiveInterval *SplitLI : SplitLIs)
1093 VRM.setIsSplitFromReg(SplitLI->reg, Original);
1094
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001095 // The new intervals all map back to i.
1096 if (LRMap)
1097 LRMap->resize(Edit->size(), i);
Jakob Stoklund Olesene4f33172010-10-26 22:36:09 +00001098 }
1099
Jakob Stoklund Olesen284c2db2010-08-10 17:07:22 +00001100 // Calculate spill weight and allocation hints for new intervals.
Benjamin Kramere2a1d892013-06-17 19:00:36 +00001101 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001102
1103 assert(!LRMap || LRMap->size() == Edit->size());
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +00001104}
1105
1106
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +00001107//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +00001108// Single Block Splitting
1109//===----------------------------------------------------------------------===//
1110
Jakob Stoklund Olesen8627ea92011-08-05 22:20:45 +00001111bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
1112 bool SingleInstrs) const {
1113 // Always split for multiple instructions.
1114 if (!BI.isOneInstr())
1115 return true;
1116 // Don't split for single instructions unless explicitly requested.
1117 if (!SingleInstrs)
1118 return false;
1119 // Splitting a live-through range always makes progress.
1120 if (BI.LiveIn && BI.LiveOut)
1121 return true;
1122 // No point in isolating a copy. It has no register class constraints.
1123 if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
1124 return false;
1125 // Finally, don't isolate an end point that was created by earlier splits.
1126 return isOriginalEndpoint(BI.FirstInstr);
1127}
1128
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001129void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1130 openIntv();
1131 SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001132 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001133 LastSplitPoint));
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001134 if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
1135 useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001136 } else {
1137 // The last use is after the last valid split point.
1138 SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1139 useIntv(SegStart, SegStop);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001140 overlapIntv(SegStop, BI.LastInstr);
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001141 }
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001142}
1143
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001144
1145//===----------------------------------------------------------------------===//
1146// Global Live Range Splitting Support
1147//===----------------------------------------------------------------------===//
1148
1149// These methods support a method of global live range splitting that uses a
1150// global algorithm to decide intervals for CFG edges. They will insert split
1151// points and color intervals in basic blocks while avoiding interference.
1152//
1153// Note that splitSingleBlock is also useful for blocks where both CFG edges
1154// are on the stack.
1155
1156void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1157 unsigned IntvIn, SlotIndex LeaveBefore,
1158 unsigned IntvOut, SlotIndex EnterAfter){
1159 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001160 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001161
1162 DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
1163 << ") intf " << LeaveBefore << '-' << EnterAfter
1164 << ", live-through " << IntvIn << " -> " << IntvOut);
1165
1166 assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1167
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001168 assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
1169 assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
1170 assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
1171
1172 MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
1173
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001174 if (!IntvOut) {
1175 DEBUG(dbgs() << ", spill on entry.\n");
1176 //
1177 // <<<<<<<<< Possible LeaveBefore interference.
1178 // |-----------| Live through.
1179 // -____________ Spill on entry.
1180 //
1181 selectIntv(IntvIn);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001182 SlotIndex Idx = leaveIntvAtTop(*MBB);
1183 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1184 (void)Idx;
1185 return;
1186 }
1187
1188 if (!IntvIn) {
1189 DEBUG(dbgs() << ", reload on exit.\n");
1190 //
1191 // >>>>>>> Possible EnterAfter interference.
1192 // |-----------| Live through.
1193 // ___________-- Reload on exit.
1194 //
1195 selectIntv(IntvOut);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001196 SlotIndex Idx = enterIntvAtEnd(*MBB);
1197 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1198 (void)Idx;
1199 return;
1200 }
1201
1202 if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1203 DEBUG(dbgs() << ", straight through.\n");
1204 //
1205 // |-----------| Live through.
1206 // ------------- Straight through, same intv, no interference.
1207 //
1208 selectIntv(IntvOut);
1209 useIntv(Start, Stop);
1210 return;
1211 }
1212
1213 // We cannot legally insert splits after LSP.
1214 SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001215 assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001216
1217 if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1218 LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1219 DEBUG(dbgs() << ", switch avoiding interference.\n");
1220 //
1221 // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
1222 // |-----------| Live through.
1223 // ------======= Switch intervals between interference.
1224 //
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001225 selectIntv(IntvOut);
Jakob Stoklund Olesenf500cce2011-07-23 03:32:26 +00001226 SlotIndex Idx;
1227 if (LeaveBefore && LeaveBefore < LSP) {
1228 Idx = enterIntvBefore(LeaveBefore);
1229 useIntv(Idx, Stop);
1230 } else {
1231 Idx = enterIntvAtEnd(*MBB);
1232 }
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001233 selectIntv(IntvIn);
1234 useIntv(Start, Idx);
1235 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1236 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1237 return;
1238 }
1239
1240 DEBUG(dbgs() << ", create local intv for interference.\n");
1241 //
1242 // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
1243 // |-----------| Live through.
1244 // ==---------== Switch intervals before/after interference.
1245 //
1246 assert(LeaveBefore <= EnterAfter && "Missed case");
1247
1248 selectIntv(IntvOut);
1249 SlotIndex Idx = enterIntvAfter(EnterAfter);
1250 useIntv(Idx, Stop);
1251 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1252
1253 selectIntv(IntvIn);
1254 Idx = leaveIntvBefore(LeaveBefore);
1255 useIntv(Start, Idx);
1256 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1257}
1258
1259
1260void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1261 unsigned IntvIn, SlotIndex LeaveBefore) {
1262 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001263 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001264
1265 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001266 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001267 << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
1268 << (BI.LiveOut ? ", stack-out" : ", killed in block"));
1269
1270 assert(IntvIn && "Must have register in");
1271 assert(BI.LiveIn && "Must be live-in");
1272 assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1273
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001274 if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001275 DEBUG(dbgs() << " before interference.\n");
1276 //
1277 // <<< Interference after kill.
1278 // |---o---x | Killed in block.
1279 // ========= Use IntvIn everywhere.
1280 //
1281 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001282 useIntv(Start, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001283 return;
1284 }
1285
1286 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1287
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001288 if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001289 //
1290 // <<< Possible interference after last use.
1291 // |---o---o---| Live-out on stack.
1292 // =========____ Leave IntvIn after last use.
1293 //
1294 // < Interference after last use.
1295 // |---o---o--o| Live-out on stack, late last use.
1296 // ============ Copy to stack after LSP, overlap IntvIn.
1297 // \_____ Stack interval is live-out.
1298 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001299 if (BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001300 DEBUG(dbgs() << ", spill after last use before interference.\n");
1301 selectIntv(IntvIn);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001302 SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001303 useIntv(Start, Idx);
1304 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1305 } else {
1306 DEBUG(dbgs() << ", spill before last split point.\n");
1307 selectIntv(IntvIn);
Jakob Stoklund Olesen37e3a132011-07-16 00:13:30 +00001308 SlotIndex Idx = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001309 overlapIntv(Idx, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001310 useIntv(Start, Idx);
1311 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1312 }
1313 return;
1314 }
1315
1316 // The interference is overlapping somewhere we wanted to use IntvIn. That
1317 // means we need to create a local interval that can be allocated a
1318 // different register.
1319 unsigned LocalIntv = openIntv();
Matt Beaumont-Gay26909d82011-07-16 04:18:47 +00001320 (void)LocalIntv;
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001321 DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
1322
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001323 if (!BI.LiveOut || BI.LastInstr < LSP) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001324 //
1325 // <<<<<<< Interference overlapping uses.
1326 // |---o---o---| Live-out on stack.
1327 // =====----____ Leave IntvIn before interference, then spill.
1328 //
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001329 SlotIndex To = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001330 SlotIndex From = enterIntvBefore(LeaveBefore);
1331 useIntv(From, To);
1332 selectIntv(IntvIn);
1333 useIntv(Start, From);
1334 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1335 return;
1336 }
1337
1338 // <<<<<<< Interference overlapping uses.
1339 // |---o---o--o| Live-out on stack, late last use.
1340 // =====------- Copy to stack before LSP, overlap LocalIntv.
1341 // \_____ Stack interval is live-out.
1342 //
1343 SlotIndex To = leaveIntvBefore(LSP);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001344 overlapIntv(To, BI.LastInstr);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001345 SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1346 useIntv(From, To);
1347 selectIntv(IntvIn);
1348 useIntv(Start, From);
1349 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1350}
1351
1352void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1353 unsigned IntvOut, SlotIndex EnterAfter) {
1354 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00001355 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001356
1357 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001358 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001359 << ", reg-out " << IntvOut << ", enter after " << EnterAfter
1360 << (BI.LiveIn ? ", stack-in" : ", defined in block"));
1361
1362 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1363
1364 assert(IntvOut && "Must have register out");
1365 assert(BI.LiveOut && "Must be live-out");
1366 assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1367
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001368 if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001369 DEBUG(dbgs() << " after interference.\n");
1370 //
1371 // >>>> Interference before def.
1372 // | o---o---| Defined in block.
1373 // ========= Use IntvOut everywhere.
1374 //
1375 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001376 useIntv(BI.FirstInstr, Stop);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001377 return;
1378 }
1379
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001380 if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001381 DEBUG(dbgs() << ", reload after interference.\n");
1382 //
1383 // >>>> Interference before def.
1384 // |---o---o---| Live-through, stack-in.
1385 // ____========= Enter IntvOut before first use.
1386 //
1387 selectIntv(IntvOut);
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001388 SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001389 useIntv(Idx, Stop);
1390 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1391 return;
1392 }
1393
1394 // The interference is overlapping somewhere we wanted to use IntvOut. That
1395 // means we need to create a local interval that can be allocated a
1396 // different register.
1397 DEBUG(dbgs() << ", interference overlaps uses.\n");
1398 //
1399 // >>>>>>> Interference overlapping uses.
1400 // |---o---o---| Live-through, stack-in.
1401 // ____---====== Create local interval for interference range.
1402 //
1403 selectIntv(IntvOut);
1404 SlotIndex Idx = enterIntvAfter(EnterAfter);
1405 useIntv(Idx, Stop);
1406 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1407
1408 openIntv();
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001409 SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001410 useIntv(From, Idx);
1411}