blob: 7d4f568df27dfe8aa5b4442e9b96e99479d092da [file] [log] [blame]
Jakob Stoklund Olesen8ae02632010-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 Olesen8ae02632010-07-20 15:41:07 +000015#include "SplitKit.h"
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000016#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000017#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper789d5d82012-04-02 22:44:18 +000018#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +000019#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +000021#include "llvm/CodeGen/MachineLoopInfo.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000023#include "llvm/CodeGen/VirtRegMap.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000026#include "llvm/Target/TargetInstrInfo.h"
27#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000028
29using namespace llvm;
30
Stephen Hinesdce4a402014-05-29 02:49:00 -070031#define DEBUG_TYPE "regalloc"
32
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000033STATISTIC(NumFinished, "Number of splits finished");
34STATISTIC(NumSimple, "Number of splits that were simple");
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000035STATISTIC(NumCopies, "Number of copies inserted for splitting");
36STATISTIC(NumRemats, "Number of rematerialized defs for splitting");
Jakob Stoklund Olesenbdda37d2011-05-10 17:37:41 +000037STATISTIC(NumRepairs, "Number of invalid live ranges repaired");
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000038
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000039//===----------------------------------------------------------------------===//
40// Split Analysis
41//===----------------------------------------------------------------------===//
42
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000043SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm,
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000044 const LiveIntervals &lis,
45 const MachineLoopInfo &mli)
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000046 : MF(vrm.getMachineFunction()),
47 VRM(vrm),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000048 LIS(lis),
49 Loops(mli),
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000050 TII(*MF.getTarget().getInstrInfo()),
Stephen Hinesdce4a402014-05-29 02:49:00 -070051 CurLI(nullptr),
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000052 LastSplitPoint(MF.getNumBlockIDs()) {}
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000053
54void SplitAnalysis::clear() {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000055 UseSlots.clear();
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +000056 UseBlocks.clear();
57 ThroughBlocks.clear();
Stephen Hinesdce4a402014-05-29 02:49:00 -070058 CurLI = nullptr;
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +000059 DidRepairRange = false;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000060}
61
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000062SlotIndex SplitAnalysis::computeLastSplitPoint(unsigned Num) {
63 const MachineBasicBlock *MBB = MF.getBlockNumbered(Num);
64 const MachineBasicBlock *LPad = MBB->getLandingPadSuccessor();
65 std::pair<SlotIndex, SlotIndex> &LSP = LastSplitPoint[Num];
Jakob Stoklund Olesen2aad2f62012-01-11 02:07:05 +000066 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000067
68 // Compute split points on the first call. The pair is independent of the
69 // current live interval.
70 if (!LSP.first.isValid()) {
71 MachineBasicBlock::const_iterator FirstTerm = MBB->getFirstTerminator();
72 if (FirstTerm == MBB->end())
Jakob Stoklund Olesen2aad2f62012-01-11 02:07:05 +000073 LSP.first = MBBEnd;
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000074 else
75 LSP.first = LIS.getInstructionIndex(FirstTerm);
76
77 // If there is a landing pad successor, also find the call instruction.
78 if (!LPad)
79 return LSP.first;
80 // There may not be a call instruction (?) in which case we ignore LPad.
81 LSP.second = LSP.first;
Jakob Stoklund Olesen1e0bd632011-06-28 01:18:58 +000082 for (MachineBasicBlock::const_iterator I = MBB->end(), E = MBB->begin();
83 I != E;) {
84 --I;
Evan Cheng5a96b3d2011-12-07 07:15:52 +000085 if (I->isCall()) {
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000086 LSP.second = LIS.getInstructionIndex(I);
87 break;
88 }
Jakob Stoklund Olesen1e0bd632011-06-28 01:18:58 +000089 }
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000090 }
91
92 // If CurLI is live into a landing pad successor, move the last split point
93 // back to the call that may throw.
Jakob Stoklund Olesen2aad2f62012-01-11 02:07:05 +000094 if (!LPad || !LSP.second || !LIS.isLiveInToMBB(*CurLI, LPad))
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000095 return LSP.first;
Jakob Stoklund Olesen2aad2f62012-01-11 02:07:05 +000096
97 // Find the value leaving MBB.
98 const VNInfo *VNI = CurLI->getVNInfoBefore(MBBEnd);
99 if (!VNI)
100 return LSP.first;
101
102 // If the value leaving MBB was defined after the call in MBB, it can't
103 // really be live-in to the landing pad. This can happen if the landing pad
104 // has a PHI, and this register is undef on the exceptional edge.
105 // <rdar://problem/10664933>
106 if (!SlotIndex::isEarlierInstr(VNI->def, LSP.second) && VNI->def < MBBEnd)
107 return LSP.first;
108
109 // Value is properly live-in to the landing pad.
110 // Only allow splits before the call.
111 return LSP.second;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000112}
113
Jakob Stoklund Olesen74c4f972012-01-11 02:07:00 +0000114MachineBasicBlock::iterator
115SplitAnalysis::getLastSplitPointIter(MachineBasicBlock *MBB) {
116 SlotIndex LSP = getLastSplitPoint(MBB->getNumber());
117 if (LSP == LIS.getMBBEndIdx(MBB))
118 return MBB->end();
119 return LIS.getInstructionFromIndex(LSP);
120}
121
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000122/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000123void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000124 assert(UseSlots.empty() && "Call clear first");
125
126 // First get all the defs from the interval values. This provides the correct
127 // slots for early clobbers.
128 for (LiveInterval::const_vni_iterator I = CurLI->vni_begin(),
129 E = CurLI->vni_end(); I != E; ++I)
130 if (!(*I)->isPHIDef() && !(*I)->isUnused())
131 UseSlots.push_back((*I)->def);
132
133 // Get use slots form the use-def chain.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000134 const MachineRegisterInfo &MRI = MF.getRegInfo();
Stephen Hines36b56882014-04-23 16:57:46 -0700135 for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg))
136 if (!MO.isUndef())
137 UseSlots.push_back(LIS.getInstructionIndex(MO.getParent()).getRegSlot());
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000138
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000139 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000140
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000141 // Remove duplicates, keeping the smaller slot for each instruction.
142 // That is what we want for early clobbers.
143 UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
144 SlotIndex::isSameInstr),
145 UseSlots.end());
146
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000147 // Compute per-live block info.
148 if (!calcLiveBlockInfo()) {
149 // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
Rafael Espindola5b220212011-06-26 22:34:10 +0000150 // I am looking at you, RegisterCoalescer!
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +0000151 DidRepairRange = true;
Jakob Stoklund Olesenbdda37d2011-05-10 17:37:41 +0000152 ++NumRepairs;
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000153 DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
154 const_cast<LiveIntervals&>(LIS)
155 .shrinkToUses(const_cast<LiveInterval*>(CurLI));
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000156 UseBlocks.clear();
157 ThroughBlocks.clear();
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000158 bool fixed = calcLiveBlockInfo();
159 (void)fixed;
160 assert(fixed && "Couldn't fix broken live interval");
161 }
162
Jakob Stoklund Olesenef1f5cc2011-03-27 22:49:23 +0000163 DEBUG(dbgs() << "Analyze counted "
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000164 << UseSlots.size() << " instrs in "
165 << UseBlocks.size() << " blocks, through "
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000166 << NumThroughBlocks << " blocks.\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000167}
168
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000169/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
170/// where CurLI is live.
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000171bool SplitAnalysis::calcLiveBlockInfo() {
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000172 ThroughBlocks.resize(MF.getNumBlockIDs());
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000173 NumThroughBlocks = NumGapBlocks = 0;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000174 if (CurLI->empty())
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000175 return true;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000176
177 LiveInterval::const_iterator LVI = CurLI->begin();
178 LiveInterval::const_iterator LVE = CurLI->end();
179
180 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
181 UseI = UseSlots.begin();
182 UseE = UseSlots.end();
183
184 // Loop over basic blocks where CurLI is live.
185 MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start);
186 for (;;) {
187 BlockInfo BI;
188 BI.MBB = MFI;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000189 SlotIndex Start, Stop;
Stephen Hines36b56882014-04-23 16:57:46 -0700190 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000191
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000192 // If the block contains no uses, the range must be live through. At one
Rafael Espindola5b220212011-06-26 22:34:10 +0000193 // point, RegisterCoalescer could create dangling ranges that ended
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000194 // mid-block.
195 if (UseI == UseE || *UseI >= Stop) {
196 ++NumThroughBlocks;
197 ThroughBlocks.set(BI.MBB->getNumber());
198 // The range shouldn't end mid-block if there are no uses. This shouldn't
199 // happen.
200 if (LVI->end < Stop)
201 return false;
202 } else {
203 // This block has uses. Find the first and last uses in the block.
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000204 BI.FirstInstr = *UseI;
205 assert(BI.FirstInstr >= Start);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000206 do ++UseI;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000207 while (UseI != UseE && *UseI < Stop);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000208 BI.LastInstr = UseI[-1];
209 assert(BI.LastInstr < Stop);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000210
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000211 // LVI is the first live segment overlapping MBB.
212 BI.LiveIn = LVI->start <= Start;
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000213
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000214 // When not live in, the first use should be a def.
215 if (!BI.LiveIn) {
Matthias Braun331de112013-10-10 21:28:43 +0000216 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000217 assert(LVI->start == BI.FirstInstr && "First instr should be a def");
218 BI.FirstDef = BI.FirstInstr;
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000219 }
220
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000221 // Look for gaps in the live range.
222 BI.LiveOut = true;
223 while (LVI->end < Stop) {
224 SlotIndex LastStop = LVI->end;
225 if (++LVI == LVE || LVI->start >= Stop) {
226 BI.LiveOut = false;
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000227 BI.LastInstr = LastStop;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000228 break;
229 }
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000230
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000231 if (LastStop < LVI->start) {
232 // There is a gap in the live range. Create duplicate entries for the
233 // live-in snippet and the live-out snippet.
234 ++NumGapBlocks;
235
236 // Push the Live-in part.
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000237 BI.LiveOut = false;
238 UseBlocks.push_back(BI);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000239 UseBlocks.back().LastInstr = LastStop;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000240
241 // Set up BI for the live-out part.
242 BI.LiveIn = false;
243 BI.LiveOut = true;
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000244 BI.FirstInstr = BI.FirstDef = LVI->start;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000245 }
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000246
Matthias Braun331de112013-10-10 21:28:43 +0000247 // A Segment that starts in the middle of the block must be a def.
248 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000249 if (!BI.FirstDef)
250 BI.FirstDef = LVI->start;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000251 }
252
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000253 UseBlocks.push_back(BI);
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000254
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000255 // LVI is now at LVE or LVI->end >= Stop.
256 if (LVI == LVE)
257 break;
258 }
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000259
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000260 // Live segment ends exactly at Stop. Move to the next segment.
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000261 if (LVI->end == Stop && ++LVI == LVE)
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000262 break;
263
264 // Pick the next basic block.
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000265 if (LVI->start < Stop)
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000266 ++MFI;
267 else
268 MFI = LIS.getMBBFromIndex(LVI->start);
269 }
Jakob Stoklund Olesenb2abfa02011-05-28 02:32:57 +0000270
271 assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000272 return true;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000273}
274
Jakob Stoklund Olesen9f4b8932011-04-26 22:33:12 +0000275unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
276 if (cli->empty())
277 return 0;
278 LiveInterval *li = const_cast<LiveInterval*>(cli);
279 LiveInterval::iterator LVI = li->begin();
280 LiveInterval::iterator LVE = li->end();
281 unsigned Count = 0;
282
283 // Loop over basic blocks where li is live.
284 MachineFunction::const_iterator MFI = LIS.getMBBFromIndex(LVI->start);
285 SlotIndex Stop = LIS.getMBBEndIdx(MFI);
286 for (;;) {
287 ++Count;
288 LVI = li->advanceTo(LVI, Stop);
289 if (LVI == LVE)
290 return Count;
291 do {
292 ++MFI;
293 Stop = LIS.getMBBEndIdx(MFI);
294 } while (Stop <= LVI->start);
295 }
296}
297
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000298bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
299 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
300 const LiveInterval &Orig = LIS.getInterval(OrigReg);
301 assert(!Orig.empty() && "Splitting empty interval?");
302 LiveInterval::const_iterator I = Orig.find(Idx);
303
304 // Range containing Idx should begin at Idx.
305 if (I != Orig.end() && I->start <= Idx)
306 return I->start == Idx;
307
308 // Range does not contain Idx, previous must end at Idx.
309 return I != Orig.begin() && (--I)->end == Idx;
310}
311
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000312void SplitAnalysis::analyze(const LiveInterval *li) {
313 clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000314 CurLI = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000315 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000316}
317
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000318
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000319//===----------------------------------------------------------------------===//
320// Split Editor
321//===----------------------------------------------------------------------===//
322
323/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000324SplitEditor::SplitEditor(SplitAnalysis &sa,
325 LiveIntervals &lis,
326 VirtRegMap &vrm,
Benjamin Kramer4eed7562013-06-17 19:00:36 +0000327 MachineDominatorTree &mdt,
328 MachineBlockFrequencyInfo &mbfi)
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000329 : SA(sa), LIS(lis), VRM(vrm),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000330 MRI(vrm.getMachineFunction().getRegInfo()),
Eric Christopher0f438112011-02-03 06:18:29 +0000331 MDT(mdt),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000332 TII(*vrm.getMachineFunction().getTarget().getInstrInfo()),
333 TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()),
Benjamin Kramer4eed7562013-06-17 19:00:36 +0000334 MBFI(mbfi),
Stephen Hinesdce4a402014-05-29 02:49:00 -0700335 Edit(nullptr),
Eric Christopher0f438112011-02-03 06:18:29 +0000336 OpenIdx(0),
Jakob Stoklund Olesen708d06f2011-09-12 16:49:21 +0000337 SpillMode(SM_Partition),
Eric Christopher0f438112011-02-03 06:18:29 +0000338 RegAssign(Allocator)
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000339{}
340
Jakob Stoklund Olesen708d06f2011-09-12 16:49:21 +0000341void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
342 Edit = &LRE;
343 SpillMode = SM;
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000344 OpenIdx = 0;
345 RegAssign.clear();
346 Values.clear();
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000347
348 // Reset the LiveRangeCalc instances needed for this spill mode.
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000349 LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
350 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000351 if (SpillMode)
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000352 LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
353 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000354
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000355 // We don't need an AliasAnalysis since we will only be performing
356 // cheap-as-a-copy remats anyway.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700357 Edit->anyRematerializable(nullptr);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000358}
359
Manman Renb720be62012-09-11 22:23:19 +0000360#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Eric Christopher0f438112011-02-03 06:18:29 +0000361void SplitEditor::dump() const {
362 if (RegAssign.empty()) {
363 dbgs() << " empty\n";
364 return;
365 }
366
367 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
368 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
369 dbgs() << '\n';
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000370}
Manman Ren77e300e2012-09-06 19:06:06 +0000371#endif
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000372
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000373VNInfo *SplitEditor::defValue(unsigned RegIdx,
374 const VNInfo *ParentVNI,
375 SlotIndex Idx) {
376 assert(ParentVNI && "Mapping NULL value");
377 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000378 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
Mark Lacey1feb5852013-08-14 23:50:04 +0000379 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000380
381 // Create a new value.
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000382 VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000383
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000384 // Use insert for lookup, so we can add missing values with a second lookup.
385 std::pair<ValueMap::iterator, bool> InsP =
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000386 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id),
387 ValueForcePair(VNI, false)));
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000388
389 // This was the first time (RegIdx, ParentVNI) was mapped.
390 // Keep it as a simple def without any liveness.
391 if (InsP.second)
392 return VNI;
393
394 // If the previous value was a simple mapping, add liveness for it now.
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000395 if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000396 SlotIndex Def = OldVNI->def;
Matthias Braun331de112013-10-10 21:28:43 +0000397 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), OldVNI));
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000398 // No longer a simple mapping. Switch to a complex, non-forced mapping.
399 InsP.first->second = ValueForcePair();
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000400 }
401
402 // This is a complex mapping, add liveness for VNI
403 SlotIndex Def = VNI->def;
Matthias Braun331de112013-10-10 21:28:43 +0000404 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000405
406 return VNI;
407}
408
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000409void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000410 assert(ParentVNI && "Mapping NULL value");
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000411 ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
412 VNInfo *VNI = VFP.getPointer();
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000413
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000414 // ParentVNI was either unmapped or already complex mapped. Either way, just
415 // set the force bit.
416 if (!VNI) {
417 VFP.setInt(true);
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000418 return;
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000419 }
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000420
421 // This was previously a single mapping. Make sure the old def is represented
422 // by a trivial live range.
423 SlotIndex Def = VNI->def;
Mark Lacey1feb5852013-08-14 23:50:04 +0000424 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Matthias Braun331de112013-10-10 21:28:43 +0000425 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000426 // Mark as complex mapped, forced.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700427 VFP = ValueForcePair(nullptr, true);
Jakob Stoklund Olesene5a2e362011-09-13 18:05:29 +0000428}
429
Eric Christopher0f438112011-02-03 06:18:29 +0000430VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000431 VNInfo *ParentVNI,
432 SlotIndex UseIdx,
433 MachineBasicBlock &MBB,
434 MachineBasicBlock::iterator I) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700435 MachineInstr *CopyMI = nullptr;
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000436 SlotIndex Def;
Mark Lacey1feb5852013-08-14 23:50:04 +0000437 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000438
Jakob Stoklund Olesenbb30dd42011-05-02 05:29:58 +0000439 // We may be trying to avoid interference that ends at a deleted instruction,
440 // so always begin RegIdx 0 early and all others late.
441 bool Late = RegIdx != 0;
442
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000443 // Attempt cheap-as-a-copy rematerialization.
444 LiveRangeEdit::Remat RM(ParentVNI);
Pete Cooper8a06af92012-04-02 22:22:53 +0000445 if (Edit->canRematerializeAt(RM, UseIdx, true)) {
446 Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000447 ++NumRemats;
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000448 } else {
449 // Can't remat, just insert a copy from parent.
Eric Christopher0f438112011-02-03 06:18:29 +0000450 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000451 .addReg(Edit->getReg());
Jakob Stoklund Olesenbb30dd42011-05-02 05:29:58 +0000452 Def = LIS.getSlotIndexes()->insertMachineInstrInMaps(CopyMI, Late)
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000453 .getRegSlot();
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000454 ++NumCopies;
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000455 }
456
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000457 // Define the value in Reg.
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000458 return defValue(RegIdx, ParentVNI, Def);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000459}
460
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000461/// Create a new virtual register and live interval.
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000462unsigned SplitEditor::openIntv() {
Eric Christopher0f438112011-02-03 06:18:29 +0000463 // Create the complement as index 0.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000464 if (Edit->empty())
Mark Laceye742d682013-08-14 23:50:16 +0000465 Edit->createEmptyInterval();
Eric Christopher0f438112011-02-03 06:18:29 +0000466
467 // Create the open interval.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000468 OpenIdx = Edit->size();
Mark Laceye742d682013-08-14 23:50:16 +0000469 Edit->createEmptyInterval();
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000470 return OpenIdx;
471}
472
473void SplitEditor::selectIntv(unsigned Idx) {
474 assert(Idx != 0 && "Cannot select the complement interval");
475 assert(Idx < Edit->size() && "Can only select previously opened interval");
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000476 DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000477 OpenIdx = Idx;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000478}
479
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000480SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000481 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000482 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000483 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000484 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000485 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000486 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000487 return Idx;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000488 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000489 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000490 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000491 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000492
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000493 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
494 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000495}
496
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000497SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
498 assert(OpenIdx && "openIntv not called before enterIntvAfter");
499 DEBUG(dbgs() << " enterIntvAfter " << Idx);
500 Idx = Idx.getBoundaryIndex();
501 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
502 if (!ParentVNI) {
503 DEBUG(dbgs() << ": not live\n");
504 return Idx;
505 }
506 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
507 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
508 assert(MI && "enterIntvAfter called with invalid index");
509
510 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
Stephen Hines36b56882014-04-23 16:57:46 -0700511 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000512 return VNI->def;
513}
514
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000515SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000516 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000517 SlotIndex End = LIS.getMBBEndIdx(&MBB);
518 SlotIndex Last = End.getPrevSlot();
519 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000520 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000521 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000522 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000523 return End;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000524 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000525 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000526 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesen74c4f972012-01-11 02:07:00 +0000527 SA.getLastSplitPointIter(&MBB));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000528 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopher0f438112011-02-03 06:18:29 +0000529 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000530 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000531}
532
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000533/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000534void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000535 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000536}
537
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000538void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopher0f438112011-02-03 06:18:29 +0000539 assert(OpenIdx && "openIntv not called before useIntv");
540 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
541 RegAssign.insert(Start, End, OpenIdx);
542 DEBUG(dump());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000543}
544
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000545SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000546 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000547 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000548
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000549 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000550 SlotIndex Boundary = Idx.getBoundaryIndex();
551 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000552 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000553 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000554 return Boundary.getNextSlot();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000555 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000556 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000557 MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
Jakob Stoklund Olesen01cb34b2011-02-08 18:50:18 +0000558 assert(MI && "No instruction at index");
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000559
560 // In spill mode, make live ranges as short as possible by inserting the copy
561 // before MI. This is only possible if that instruction doesn't redefine the
562 // value. The inserted COPY is not a kill, and we don't need to recompute
563 // the source live range. The spiller also won't try to hoist this copy.
564 if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
565 MI->readsVirtualRegister(Edit->getReg())) {
566 forceRecompute(0, ParentVNI);
567 defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
568 return Idx;
569 }
570
571 VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
Stephen Hines36b56882014-04-23 16:57:46 -0700572 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000573 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000574}
575
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000576SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
577 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
578 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
579
580 // The interval must be live into the instruction at Idx.
Jakob Stoklund Olesenfc479332011-07-18 18:47:13 +0000581 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000582 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000583 if (!ParentVNI) {
584 DEBUG(dbgs() << ": not live\n");
585 return Idx.getNextSlot();
586 }
587 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
588
589 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
590 assert(MI && "No instruction at index");
591 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
592 return VNI->def;
593}
594
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000595SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000596 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000597 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000598 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000599
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000600 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000601 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000602 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000603 return Start;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000604 }
605
Eric Christopher0f438112011-02-03 06:18:29 +0000606 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000607 MBB.SkipPHIsAndLabels(MBB.begin()));
Eric Christopher0f438112011-02-03 06:18:29 +0000608 RegAssign.insert(Start, VNI->def, OpenIdx);
609 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000610 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000611}
612
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000613void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
614 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000615 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +0000616 assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000617 "Parent changes value in extended range");
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000618 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
619 "Range cannot span basic blocks");
620
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000621 // The complement interval will be extended as needed by LRCalc.extend().
Jakob Stoklund Olesenb3dd8262011-04-05 23:43:14 +0000622 if (ParentVNI)
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000623 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000624 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
625 RegAssign.insert(Start, End, OpenIdx);
626 DEBUG(dump());
627}
628
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000629//===----------------------------------------------------------------------===//
630// Spill modes
631//===----------------------------------------------------------------------===//
632
633void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
Mark Lacey1feb5852013-08-14 23:50:04 +0000634 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000635 DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
636 RegAssignMap::iterator AssignI;
637 AssignI.setMap(RegAssign);
638
639 for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
640 VNInfo *VNI = Copies[i];
641 SlotIndex Def = VNI->def;
642 MachineInstr *MI = LIS.getInstructionFromIndex(Def);
643 assert(MI && "No instruction for back-copy");
644
645 MachineBasicBlock *MBB = MI->getParent();
646 MachineBasicBlock::iterator MBBI(MI);
647 bool AtBegin;
648 do AtBegin = MBBI == MBB->begin();
649 while (!AtBegin && (--MBBI)->isDebugValue());
650
651 DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
652 LI->removeValNo(VNI);
653 LIS.RemoveMachineInstrFromMaps(MI);
654 MI->eraseFromParent();
655
656 // Adjust RegAssign if a register assignment is killed at VNI->def. We
657 // want to avoid calculating the live range of the source register if
658 // possible.
Jakob Stoklund Olesen1599a642012-08-03 20:59:29 +0000659 AssignI.find(Def.getPrevSlot());
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000660 if (!AssignI.valid() || AssignI.start() >= Def)
661 continue;
662 // If MI doesn't kill the assigned register, just leave it.
663 if (AssignI.stop() != Def)
664 continue;
665 unsigned RegIdx = AssignI.value();
666 if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
667 DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n');
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000668 forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000669 } else {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000670 SlotIndex Kill = LIS.getInstructionIndex(MBBI).getRegSlot();
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000671 DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
672 AssignI.setStop(Kill);
673 }
674 }
675}
676
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000677MachineBasicBlock*
678SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
679 MachineBasicBlock *DefMBB) {
680 if (MBB == DefMBB)
681 return MBB;
682 assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
683
684 const MachineLoopInfo &Loops = SA.Loops;
685 const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
686 MachineDomTreeNode *DefDomNode = MDT[DefMBB];
687
688 // Best candidate so far.
689 MachineBasicBlock *BestMBB = MBB;
690 unsigned BestDepth = UINT_MAX;
691
692 for (;;) {
693 const MachineLoop *Loop = Loops.getLoopFor(MBB);
694
695 // MBB isn't in a loop, it doesn't get any better. All dominators have a
696 // higher frequency by definition.
697 if (!Loop) {
698 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
699 << MBB->getNumber() << " at depth 0\n");
700 return MBB;
701 }
702
703 // We'll never be able to exit the DefLoop.
704 if (Loop == DefLoop) {
705 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
706 << MBB->getNumber() << " in the same loop\n");
707 return MBB;
708 }
709
710 // Least busy dominator seen so far.
711 unsigned Depth = Loop->getLoopDepth();
712 if (Depth < BestDepth) {
713 BestMBB = MBB;
714 BestDepth = Depth;
715 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
716 << MBB->getNumber() << " at depth " << Depth << '\n');
717 }
718
719 // Leave loop by going to the immediate dominator of the loop header.
720 // This is a bigger stride than simply walking up the dominator tree.
721 MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
722
723 // Too far up the dominator tree?
724 if (!IDom || !MDT.dominates(DefDomNode, IDom))
725 return BestMBB;
726
727 MBB = IDom->getBlock();
728 }
729}
730
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000731void SplitEditor::hoistCopiesForSize() {
732 // Get the complement interval, always RegIdx 0.
Mark Lacey1feb5852013-08-14 23:50:04 +0000733 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000734 LiveInterval *Parent = &Edit->getParent();
735
736 // Track the nearest common dominator for all back-copies for each ParentVNI,
737 // indexed by ParentVNI->id.
738 typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
739 SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
740
741 // Find the nearest common dominator for parent values with multiple
742 // back-copies. If a single back-copy dominates, put it in DomPair.second.
743 for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
744 VI != VE; ++VI) {
745 VNInfo *VNI = *VI;
Jakob Stoklund Olesen1599a642012-08-03 20:59:29 +0000746 if (VNI->isUnused())
747 continue;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000748 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
749 assert(ParentVNI && "Parent not live at complement def");
750
751 // Don't hoist remats. The complement is probably going to disappear
752 // completely anyway.
753 if (Edit->didRematerialize(ParentVNI))
754 continue;
755
756 MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
757 DomPair &Dom = NearestDom[ParentVNI->id];
758
759 // Keep directly defined parent values. This is either a PHI or an
760 // instruction in the complement range. All other copies of ParentVNI
761 // should be eliminated.
762 if (VNI->def == ParentVNI->def) {
763 DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
764 Dom = DomPair(ValMBB, VNI->def);
765 continue;
766 }
767 // Skip the singly mapped values. There is nothing to gain from hoisting a
768 // single back-copy.
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000769 if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000770 DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
771 continue;
772 }
773
774 if (!Dom.first) {
775 // First time we see ParentVNI. VNI dominates itself.
776 Dom = DomPair(ValMBB, VNI->def);
777 } else if (Dom.first == ValMBB) {
778 // Two defs in the same block. Pick the earlier def.
779 if (!Dom.second.isValid() || VNI->def < Dom.second)
780 Dom.second = VNI->def;
781 } else {
782 // Different basic blocks. Check if one dominates.
783 MachineBasicBlock *Near =
784 MDT.findNearestCommonDominator(Dom.first, ValMBB);
785 if (Near == ValMBB)
786 // Def ValMBB dominates.
787 Dom = DomPair(ValMBB, VNI->def);
788 else if (Near != Dom.first)
789 // None dominate. Hoist to common dominator, need new def.
790 Dom = DomPair(Near, SlotIndex());
791 }
792
793 DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
794 << " for parent " << ParentVNI->id << '@' << ParentVNI->def
795 << " hoist to BB#" << Dom.first->getNumber() << ' '
796 << Dom.second << '\n');
797 }
798
799 // Insert the hoisted copies.
800 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
801 DomPair &Dom = NearestDom[i];
802 if (!Dom.first || Dom.second.isValid())
803 continue;
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000804 // This value needs a hoisted copy inserted at the end of Dom.first.
805 VNInfo *ParentVNI = Parent->getValNumInfo(i);
806 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
807 // Get a less loopy dominator than Dom.first.
808 Dom.first = findShallowDominator(Dom.first, DefMBB);
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000809 SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
810 Dom.second =
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000811 defFromParent(0, ParentVNI, Last, *Dom.first,
Jakob Stoklund Olesen74c4f972012-01-11 02:07:00 +0000812 SA.getLastSplitPointIter(Dom.first))->def;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000813 }
814
815 // Remove redundant back-copies that are now known to be dominated by another
816 // def with the same value.
817 SmallVector<VNInfo*, 8> BackCopies;
818 for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
819 VI != VE; ++VI) {
820 VNInfo *VNI = *VI;
Jakob Stoklund Olesen1599a642012-08-03 20:59:29 +0000821 if (VNI->isUnused())
822 continue;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000823 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
824 const DomPair &Dom = NearestDom[ParentVNI->id];
825 if (!Dom.first || Dom.second == VNI->def)
826 continue;
827 BackCopies.push_back(VNI);
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000828 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000829 }
830 removeBackCopies(BackCopies);
831}
832
833
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000834/// transferValues - Transfer all possible values to the new live ranges.
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000835/// Values that were rematerialized are left alone, they need LRCalc.extend().
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000836bool SplitEditor::transferValues() {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000837 bool Skipped = false;
838 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000839 for (LiveInterval::const_iterator ParentI = Edit->getParent().begin(),
840 ParentE = Edit->getParent().end(); ParentI != ParentE; ++ParentI) {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000841 DEBUG(dbgs() << " blit " << *ParentI << ':');
842 VNInfo *ParentVNI = ParentI->valno;
843 // RegAssign has holes where RegIdx 0 should be used.
844 SlotIndex Start = ParentI->start;
845 AssignI.advanceTo(Start);
846 do {
847 unsigned RegIdx;
848 SlotIndex End = ParentI->end;
849 if (!AssignI.valid()) {
850 RegIdx = 0;
851 } else if (AssignI.start() <= Start) {
852 RegIdx = AssignI.value();
853 if (AssignI.stop() < End) {
854 End = AssignI.stop();
855 ++AssignI;
856 }
857 } else {
858 RegIdx = 0;
859 End = std::min(End, AssignI.start());
860 }
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000861
862 // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000863 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
Matthias Braune25dde52013-10-10 21:28:57 +0000864 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000865
866 // Check for a simply defined value that can be blitted directly.
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000867 ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
868 if (VNInfo *VNI = VFP.getPointer()) {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000869 DEBUG(dbgs() << ':' << VNI->id);
Matthias Braune25dde52013-10-10 21:28:57 +0000870 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000871 Start = End;
872 continue;
873 }
874
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000875 // Skip values with forced recomputation.
876 if (VFP.getInt()) {
877 DEBUG(dbgs() << "(recalc)");
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000878 Skipped = true;
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000879 Start = End;
880 continue;
881 }
882
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000883 LiveRangeCalc &LRC = getLRCalc(RegIdx);
884
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000885 // This value has multiple defs in RegIdx, but it wasn't rematerialized,
886 // so the live range is accurate. Add live-in blocks in [Start;End) to the
887 // LiveInBlocks.
888 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
889 SlotIndex BlockStart, BlockEnd;
Stephen Hines36b56882014-04-23 16:57:46 -0700890 std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(MBB);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000891
892 // The first block may be live-in, or it may have its own def.
893 if (Start != BlockStart) {
Matthias Braune25dde52013-10-10 21:28:57 +0000894 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000895 assert(VNI && "Missing def for complex mapped value");
896 DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
897 // MBB has its own def. Is it also live-out?
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000898 if (BlockEnd <= End)
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000899 LRC.setLiveOutValue(MBB, VNI);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000900
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000901 // Skip to the next block for live-in.
902 ++MBB;
903 BlockStart = BlockEnd;
904 }
905
906 // Handle the live-in blocks covered by [Start;End).
907 assert(Start <= BlockStart && "Expected live-in block");
908 while (BlockStart < End) {
909 DEBUG(dbgs() << ">BB#" << MBB->getNumber());
910 BlockEnd = LIS.getMBBEndIdx(MBB);
911 if (BlockStart == ParentVNI->def) {
912 // This block has the def of a parent PHI, so it isn't live-in.
913 assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
Matthias Braune25dde52013-10-10 21:28:57 +0000914 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000915 assert(VNI && "Missing def for complex mapped parent PHI");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000916 if (End >= BlockEnd)
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000917 LRC.setLiveOutValue(MBB, VNI); // Live-out as well.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000918 } else {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000919 // This block needs a live-in value. The last block covered may not
920 // be live-out.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000921 if (End < BlockEnd)
Matthias Braune25dde52013-10-10 21:28:57 +0000922 LRC.addLiveInBlock(LR, MDT[MBB], End);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000923 else {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000924 // Live-through, and we don't know the value.
Matthias Braune25dde52013-10-10 21:28:57 +0000925 LRC.addLiveInBlock(LR, MDT[MBB]);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700926 LRC.setLiveOutValue(MBB, nullptr);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000927 }
928 }
929 BlockStart = BlockEnd;
930 ++MBB;
931 }
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000932 Start = End;
933 } while (Start != ParentI->end);
934 DEBUG(dbgs() << '\n');
935 }
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000936
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000937 LRCalc[0].calculateValues();
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000938 if (SpillMode)
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000939 LRCalc[1].calculateValues();
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000940
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000941 return Skipped;
942}
943
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000944void SplitEditor::extendPHIKillRanges() {
945 // Extend live ranges to be live-out for successor PHI values.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000946 for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
947 E = Edit->getParent().vni_end(); I != E; ++I) {
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000948 const VNInfo *PHIVNI = *I;
949 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
950 continue;
951 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
Matthias Braune25dde52013-10-10 21:28:57 +0000952 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000953 LiveRangeCalc &LRC = getLRCalc(RegIdx);
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000954 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
955 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
956 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000957 SlotIndex End = LIS.getMBBEndIdx(*PI);
958 SlotIndex LastUse = End.getPrevSlot();
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000959 // The predecessor may not have a live-out value. That is OK, like an
960 // undef PHI operand.
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000961 if (Edit->getParent().liveAt(LastUse)) {
962 assert(RegAssign.lookup(LastUse) == RegIdx &&
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000963 "Different register assignment in phi predecessor");
Matthias Braune25dde52013-10-10 21:28:57 +0000964 LRC.extend(LR, End);
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000965 }
966 }
967 }
968}
969
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000970/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000971void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000972 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000973 RE = MRI.reg_end(); RI != RE;) {
Stephen Hines36b56882014-04-23 16:57:46 -0700974 MachineOperand &MO = *RI;
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000975 MachineInstr *MI = MO.getParent();
976 ++RI;
Eric Christopher0f438112011-02-03 06:18:29 +0000977 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000978 if (MI->isDebugValue()) {
979 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000980 MO.setReg(0);
981 continue;
982 }
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +0000983
Jakob Stoklund Olesenb09701d2011-07-24 20:23:50 +0000984 // <undef> operands don't really read the register, so it doesn't matter
985 // which register we choose. When the use operand is tied to a def, we must
986 // use the same register as the def, so just do that always.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000987 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesenb09701d2011-07-24 20:23:50 +0000988 if (MO.isDef() || MO.isUndef())
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000989 Idx = Idx.getRegSlot(MO.isEarlyClobber());
Eric Christopher0f438112011-02-03 06:18:29 +0000990
991 // Rewrite to the mapped register at Idx.
992 unsigned RegIdx = RegAssign.lookup(Idx);
Mark Lacey1feb5852013-08-14 23:50:04 +0000993 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000994 MO.setReg(LI->reg);
Eric Christopher0f438112011-02-03 06:18:29 +0000995 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
996 << Idx << ':' << RegIdx << '\t' << *MI);
997
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +0000998 // Extend liveness to Idx if the instruction reads reg.
Jakob Stoklund Olesen81d686e2011-07-24 20:33:23 +0000999 if (!ExtendRanges || MO.isUndef())
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +00001000 continue;
1001
1002 // Skip instructions that don't read Reg.
1003 if (MO.isDef()) {
1004 if (!MO.getSubReg() && !MO.isEarlyClobber())
1005 continue;
1006 // We may wan't to extend a live range for a partial redef, or for a use
1007 // tied to an early clobber.
1008 Idx = Idx.getPrevSlot();
1009 if (!Edit->getParent().liveAt(Idx))
1010 continue;
1011 } else
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001012 Idx = Idx.getRegSlot(true);
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +00001013
Matthias Braune25dde52013-10-10 21:28:57 +00001014 getLRCalc(RegIdx).extend(*LI, Idx.getNextSlot());
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001015 }
1016}
1017
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001018void SplitEditor::deleteRematVictims() {
1019 SmallVector<MachineInstr*, 8> Dead;
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001020 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
Mark Lacey1feb5852013-08-14 23:50:04 +00001021 LiveInterval *LI = &LIS.getInterval(*I);
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001022 for (LiveInterval::const_iterator LII = LI->begin(), LIE = LI->end();
1023 LII != LIE; ++LII) {
Jakob Stoklund Olesen1f81e312011-11-13 22:42:13 +00001024 // Dead defs end at the dead slot.
1025 if (LII->end != LII->valno->def.getDeadSlot())
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001026 continue;
1027 MachineInstr *MI = LIS.getInstructionFromIndex(LII->valno->def);
1028 assert(MI && "Missing instruction for dead def");
1029 MI->addRegisterDead(LI->reg, &TRI);
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001030
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001031 if (!MI->allDefsAreDead())
1032 continue;
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001033
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001034 DEBUG(dbgs() << "All defs dead: " << *MI);
1035 Dead.push_back(MI);
1036 }
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001037 }
1038
1039 if (Dead.empty())
1040 return;
1041
Pete Cooper8a06af92012-04-02 22:22:53 +00001042 Edit->eliminateDeadDefs(Dead);
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001043}
1044
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001045void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001046 ++NumFinished;
Eric Christopher463a2972011-02-03 05:40:54 +00001047
Eric Christopher0f438112011-02-03 06:18:29 +00001048 // At this point, the live intervals in Edit contain VNInfos corresponding to
1049 // the inserted copies.
1050
1051 // Add the original defs from the parent interval.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001052 for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
1053 E = Edit->getParent().vni_end(); I != E; ++I) {
Eric Christopher0f438112011-02-03 06:18:29 +00001054 const VNInfo *ParentVNI = *I;
Jakob Stoklund Olesen9ecd1e72011-02-04 00:59:23 +00001055 if (ParentVNI->isUnused())
1056 continue;
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +00001057 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Jakob Stoklund Olesenb18d7792012-07-27 21:11:14 +00001058 defValue(RegIdx, ParentVNI, ParentVNI->def);
Jakob Stoklund Olesen29ef8752011-03-15 21:13:22 +00001059
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +00001060 // Force rematted values to be recomputed everywhere.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001061 // The new live ranges may be truncated.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001062 if (Edit->didRematerialize(ParentVNI))
1063 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +00001064 forceRecompute(i, ParentVNI);
Eric Christopher0f438112011-02-03 06:18:29 +00001065 }
1066
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001067 // Hoist back-copies to the complement interval when in spill mode.
1068 switch (SpillMode) {
1069 case SM_Partition:
1070 // Leave all back-copies as is.
1071 break;
1072 case SM_Size:
1073 hoistCopiesForSize();
1074 break;
1075 case SM_Speed:
1076 llvm_unreachable("Spill mode 'speed' not implemented yet");
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001077 }
1078
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001079 // Transfer the simply mapped values, check if any are skipped.
1080 bool Skipped = transferValues();
1081 if (Skipped)
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001082 extendPHIKillRanges();
1083 else
1084 ++NumSimple;
Eric Christopher0f438112011-02-03 06:18:29 +00001085
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001086 // Rewrite virtual registers, possibly extending ranges.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001087 rewriteAssigned(Skipped);
Eric Christopher0f438112011-02-03 06:18:29 +00001088
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001089 // Delete defs that were rematted everywhere.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001090 if (Skipped)
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001091 deleteRematVictims();
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001092
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001093 // Get rid of unused values and set phi-kill flags.
Mark Lacey1feb5852013-08-14 23:50:04 +00001094 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) {
1095 LiveInterval &LI = LIS.getInterval(*I);
1096 LI.RenumberValues();
1097 }
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001098
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001099 // Provide a reverse mapping from original indices to Edit ranges.
1100 if (LRMap) {
1101 LRMap->clear();
1102 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1103 LRMap->push_back(i);
1104 }
1105
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001106 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001107 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001108 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001109 // Don't use iterators, they are invalidated by create() below.
Mark Lacey1feb5852013-08-14 23:50:04 +00001110 LiveInterval *li = &LIS.getInterval(Edit->get(i));
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001111 unsigned NumComp = ConEQ.Classify(li);
1112 if (NumComp <= 1)
1113 continue;
1114 DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n');
1115 SmallVector<LiveInterval*, 8> dups;
1116 dups.push_back(li);
Matt Beaumont-Gayae5fbee2011-04-21 19:46:23 +00001117 for (unsigned j = 1; j != NumComp; ++j)
Mark Laceye742d682013-08-14 23:50:16 +00001118 dups.push_back(&Edit->createEmptyInterval());
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +00001119 ConEQ.Distribute(&dups[0], MRI);
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001120 // The new intervals all map back to i.
1121 if (LRMap)
1122 LRMap->resize(Edit->size(), i);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001123 }
1124
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +00001125 // Calculate spill weight and allocation hints for new intervals.
Benjamin Kramer4eed7562013-06-17 19:00:36 +00001126 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001127
1128 assert(!LRMap || LRMap->size() == Edit->size());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001129}
1130
1131
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +00001132//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001133// Single Block Splitting
1134//===----------------------------------------------------------------------===//
1135
Jakob Stoklund Olesen2d6d86b2011-08-05 22:20:45 +00001136bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
1137 bool SingleInstrs) const {
1138 // Always split for multiple instructions.
1139 if (!BI.isOneInstr())
1140 return true;
1141 // Don't split for single instructions unless explicitly requested.
1142 if (!SingleInstrs)
1143 return false;
1144 // Splitting a live-through range always makes progress.
1145 if (BI.LiveIn && BI.LiveOut)
1146 return true;
1147 // No point in isolating a copy. It has no register class constraints.
1148 if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
1149 return false;
1150 // Finally, don't isolate an end point that was created by earlier splits.
1151 return isOriginalEndpoint(BI.FirstInstr);
1152}
1153
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001154void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1155 openIntv();
1156 SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001157 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001158 LastSplitPoint));
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001159 if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
1160 useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001161 } else {
1162 // The last use is after the last valid split point.
1163 SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1164 useIntv(SegStart, SegStop);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001165 overlapIntv(SegStop, BI.LastInstr);
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001166 }
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001167}
1168
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001169
1170//===----------------------------------------------------------------------===//
1171// Global Live Range Splitting Support
1172//===----------------------------------------------------------------------===//
1173
1174// These methods support a method of global live range splitting that uses a
1175// global algorithm to decide intervals for CFG edges. They will insert split
1176// points and color intervals in basic blocks while avoiding interference.
1177//
1178// Note that splitSingleBlock is also useful for blocks where both CFG edges
1179// are on the stack.
1180
1181void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1182 unsigned IntvIn, SlotIndex LeaveBefore,
1183 unsigned IntvOut, SlotIndex EnterAfter){
1184 SlotIndex Start, Stop;
Stephen Hines36b56882014-04-23 16:57:46 -07001185 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001186
1187 DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
1188 << ") intf " << LeaveBefore << '-' << EnterAfter
1189 << ", live-through " << IntvIn << " -> " << IntvOut);
1190
1191 assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1192
Jakob Stoklund Olesenfe9b2d12011-07-23 03:32:26 +00001193 assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
1194 assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
1195 assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
1196
1197 MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
1198
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001199 if (!IntvOut) {
1200 DEBUG(dbgs() << ", spill on entry.\n");
1201 //
1202 // <<<<<<<<< Possible LeaveBefore interference.
1203 // |-----------| Live through.
1204 // -____________ Spill on entry.
1205 //
1206 selectIntv(IntvIn);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001207 SlotIndex Idx = leaveIntvAtTop(*MBB);
1208 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1209 (void)Idx;
1210 return;
1211 }
1212
1213 if (!IntvIn) {
1214 DEBUG(dbgs() << ", reload on exit.\n");
1215 //
1216 // >>>>>>> Possible EnterAfter interference.
1217 // |-----------| Live through.
1218 // ___________-- Reload on exit.
1219 //
1220 selectIntv(IntvOut);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001221 SlotIndex Idx = enterIntvAtEnd(*MBB);
1222 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1223 (void)Idx;
1224 return;
1225 }
1226
1227 if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1228 DEBUG(dbgs() << ", straight through.\n");
1229 //
1230 // |-----------| Live through.
1231 // ------------- Straight through, same intv, no interference.
1232 //
1233 selectIntv(IntvOut);
1234 useIntv(Start, Stop);
1235 return;
1236 }
1237
1238 // We cannot legally insert splits after LSP.
1239 SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
Jakob Stoklund Olesenfe9b2d12011-07-23 03:32:26 +00001240 assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001241
1242 if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1243 LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1244 DEBUG(dbgs() << ", switch avoiding interference.\n");
1245 //
1246 // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
1247 // |-----------| Live through.
1248 // ------======= Switch intervals between interference.
1249 //
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001250 selectIntv(IntvOut);
Jakob Stoklund Olesenfe9b2d12011-07-23 03:32:26 +00001251 SlotIndex Idx;
1252 if (LeaveBefore && LeaveBefore < LSP) {
1253 Idx = enterIntvBefore(LeaveBefore);
1254 useIntv(Idx, Stop);
1255 } else {
1256 Idx = enterIntvAtEnd(*MBB);
1257 }
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001258 selectIntv(IntvIn);
1259 useIntv(Start, Idx);
1260 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1261 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1262 return;
1263 }
1264
1265 DEBUG(dbgs() << ", create local intv for interference.\n");
1266 //
1267 // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
1268 // |-----------| Live through.
1269 // ==---------== Switch intervals before/after interference.
1270 //
1271 assert(LeaveBefore <= EnterAfter && "Missed case");
1272
1273 selectIntv(IntvOut);
1274 SlotIndex Idx = enterIntvAfter(EnterAfter);
1275 useIntv(Idx, Stop);
1276 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1277
1278 selectIntv(IntvIn);
1279 Idx = leaveIntvBefore(LeaveBefore);
1280 useIntv(Start, Idx);
1281 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1282}
1283
1284
1285void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1286 unsigned IntvIn, SlotIndex LeaveBefore) {
1287 SlotIndex Start, Stop;
Stephen Hines36b56882014-04-23 16:57:46 -07001288 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001289
1290 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001291 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001292 << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
1293 << (BI.LiveOut ? ", stack-out" : ", killed in block"));
1294
1295 assert(IntvIn && "Must have register in");
1296 assert(BI.LiveIn && "Must be live-in");
1297 assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1298
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001299 if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001300 DEBUG(dbgs() << " before interference.\n");
1301 //
1302 // <<< Interference after kill.
1303 // |---o---x | Killed in block.
1304 // ========= Use IntvIn everywhere.
1305 //
1306 selectIntv(IntvIn);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001307 useIntv(Start, BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001308 return;
1309 }
1310
1311 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1312
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001313 if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001314 //
1315 // <<< Possible interference after last use.
1316 // |---o---o---| Live-out on stack.
1317 // =========____ Leave IntvIn after last use.
1318 //
1319 // < Interference after last use.
1320 // |---o---o--o| Live-out on stack, late last use.
1321 // ============ Copy to stack after LSP, overlap IntvIn.
1322 // \_____ Stack interval is live-out.
1323 //
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001324 if (BI.LastInstr < LSP) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001325 DEBUG(dbgs() << ", spill after last use before interference.\n");
1326 selectIntv(IntvIn);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001327 SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001328 useIntv(Start, Idx);
1329 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1330 } else {
1331 DEBUG(dbgs() << ", spill before last split point.\n");
1332 selectIntv(IntvIn);
Jakob Stoklund Olesenaf4e40c2011-07-16 00:13:30 +00001333 SlotIndex Idx = leaveIntvBefore(LSP);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001334 overlapIntv(Idx, BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001335 useIntv(Start, Idx);
1336 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1337 }
1338 return;
1339 }
1340
1341 // The interference is overlapping somewhere we wanted to use IntvIn. That
1342 // means we need to create a local interval that can be allocated a
1343 // different register.
1344 unsigned LocalIntv = openIntv();
Matt Beaumont-Gayf9d7fb62011-07-16 04:18:47 +00001345 (void)LocalIntv;
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001346 DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
1347
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001348 if (!BI.LiveOut || BI.LastInstr < LSP) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001349 //
1350 // <<<<<<< Interference overlapping uses.
1351 // |---o---o---| Live-out on stack.
1352 // =====----____ Leave IntvIn before interference, then spill.
1353 //
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001354 SlotIndex To = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001355 SlotIndex From = enterIntvBefore(LeaveBefore);
1356 useIntv(From, To);
1357 selectIntv(IntvIn);
1358 useIntv(Start, From);
1359 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1360 return;
1361 }
1362
1363 // <<<<<<< Interference overlapping uses.
1364 // |---o---o--o| Live-out on stack, late last use.
1365 // =====------- Copy to stack before LSP, overlap LocalIntv.
1366 // \_____ Stack interval is live-out.
1367 //
1368 SlotIndex To = leaveIntvBefore(LSP);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001369 overlapIntv(To, BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001370 SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1371 useIntv(From, To);
1372 selectIntv(IntvIn);
1373 useIntv(Start, From);
1374 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1375}
1376
1377void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1378 unsigned IntvOut, SlotIndex EnterAfter) {
1379 SlotIndex Start, Stop;
Stephen Hines36b56882014-04-23 16:57:46 -07001380 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001381
1382 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001383 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001384 << ", reg-out " << IntvOut << ", enter after " << EnterAfter
1385 << (BI.LiveIn ? ", stack-in" : ", defined in block"));
1386
1387 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1388
1389 assert(IntvOut && "Must have register out");
1390 assert(BI.LiveOut && "Must be live-out");
1391 assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1392
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001393 if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001394 DEBUG(dbgs() << " after interference.\n");
1395 //
1396 // >>>> Interference before def.
1397 // | o---o---| Defined in block.
1398 // ========= Use IntvOut everywhere.
1399 //
1400 selectIntv(IntvOut);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001401 useIntv(BI.FirstInstr, Stop);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001402 return;
1403 }
1404
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001405 if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001406 DEBUG(dbgs() << ", reload after interference.\n");
1407 //
1408 // >>>> Interference before def.
1409 // |---o---o---| Live-through, stack-in.
1410 // ____========= Enter IntvOut before first use.
1411 //
1412 selectIntv(IntvOut);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001413 SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001414 useIntv(Idx, Stop);
1415 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1416 return;
1417 }
1418
1419 // The interference is overlapping somewhere we wanted to use IntvOut. That
1420 // means we need to create a local interval that can be allocated a
1421 // different register.
1422 DEBUG(dbgs() << ", interference overlaps uses.\n");
1423 //
1424 // >>>>>>> Interference overlapping uses.
1425 // |---o---o---| Live-through, stack-in.
1426 // ____---====== Create local interval for interference range.
1427 //
1428 selectIntv(IntvOut);
1429 SlotIndex Idx = enterIntvAfter(EnterAfter);
1430 useIntv(Idx, Stop);
1431 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1432
1433 openIntv();
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001434 SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001435 useIntv(From, Idx);
1436}