blob: dab1dfe4f1f856bcafe05deb5db7097b16a954ee [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
Stephen Hines37ed9c12014-12-01 14:51:49 -080043SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000044 const MachineLoopInfo &mli)
Stephen Hines37ed9c12014-12-01 14:51:49 -080045 : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli),
46 TII(*MF.getSubtarget().getInstrInfo()), CurLI(nullptr),
47 LastSplitPoint(MF.getNumBlockIDs()) {}
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000048
49void SplitAnalysis::clear() {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000050 UseSlots.clear();
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +000051 UseBlocks.clear();
52 ThroughBlocks.clear();
Stephen Hinesdce4a402014-05-29 02:49:00 -070053 CurLI = nullptr;
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +000054 DidRepairRange = false;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000055}
56
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000057SlotIndex SplitAnalysis::computeLastSplitPoint(unsigned Num) {
58 const MachineBasicBlock *MBB = MF.getBlockNumbered(Num);
59 const MachineBasicBlock *LPad = MBB->getLandingPadSuccessor();
60 std::pair<SlotIndex, SlotIndex> &LSP = LastSplitPoint[Num];
Jakob Stoklund Olesen2aad2f62012-01-11 02:07:05 +000061 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000062
63 // Compute split points on the first call. The pair is independent of the
64 // current live interval.
65 if (!LSP.first.isValid()) {
66 MachineBasicBlock::const_iterator FirstTerm = MBB->getFirstTerminator();
67 if (FirstTerm == MBB->end())
Jakob Stoklund Olesen2aad2f62012-01-11 02:07:05 +000068 LSP.first = MBBEnd;
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000069 else
70 LSP.first = LIS.getInstructionIndex(FirstTerm);
71
72 // If there is a landing pad successor, also find the call instruction.
73 if (!LPad)
74 return LSP.first;
75 // There may not be a call instruction (?) in which case we ignore LPad.
76 LSP.second = LSP.first;
Jakob Stoklund Olesen1e0bd632011-06-28 01:18:58 +000077 for (MachineBasicBlock::const_iterator I = MBB->end(), E = MBB->begin();
78 I != E;) {
79 --I;
Evan Cheng5a96b3d2011-12-07 07:15:52 +000080 if (I->isCall()) {
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000081 LSP.second = LIS.getInstructionIndex(I);
82 break;
83 }
Jakob Stoklund Olesen1e0bd632011-06-28 01:18:58 +000084 }
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000085 }
86
87 // If CurLI is live into a landing pad successor, move the last split point
88 // back to the call that may throw.
Jakob Stoklund Olesen2aad2f62012-01-11 02:07:05 +000089 if (!LPad || !LSP.second || !LIS.isLiveInToMBB(*CurLI, LPad))
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000090 return LSP.first;
Jakob Stoklund Olesen2aad2f62012-01-11 02:07:05 +000091
92 // Find the value leaving MBB.
93 const VNInfo *VNI = CurLI->getVNInfoBefore(MBBEnd);
94 if (!VNI)
95 return LSP.first;
96
97 // If the value leaving MBB was defined after the call in MBB, it can't
98 // really be live-in to the landing pad. This can happen if the landing pad
99 // has a PHI, and this register is undef on the exceptional edge.
100 // <rdar://problem/10664933>
101 if (!SlotIndex::isEarlierInstr(VNI->def, LSP.second) && VNI->def < MBBEnd)
102 return LSP.first;
103
104 // Value is properly live-in to the landing pad.
105 // Only allow splits before the call.
106 return LSP.second;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000107}
108
Jakob Stoklund Olesen74c4f972012-01-11 02:07:00 +0000109MachineBasicBlock::iterator
110SplitAnalysis::getLastSplitPointIter(MachineBasicBlock *MBB) {
111 SlotIndex LSP = getLastSplitPoint(MBB->getNumber());
112 if (LSP == LIS.getMBBEndIdx(MBB))
113 return MBB->end();
114 return LIS.getInstructionFromIndex(LSP);
115}
116
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000117/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000118void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000119 assert(UseSlots.empty() && "Call clear first");
120
121 // First get all the defs from the interval values. This provides the correct
122 // slots for early clobbers.
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700123 for (const VNInfo *VNI : CurLI->valnos)
124 if (!VNI->isPHIDef() && !VNI->isUnused())
125 UseSlots.push_back(VNI->def);
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000126
127 // Get use slots form the use-def chain.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000128 const MachineRegisterInfo &MRI = MF.getRegInfo();
Stephen Hines36b56882014-04-23 16:57:46 -0700129 for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg))
130 if (!MO.isUndef())
131 UseSlots.push_back(LIS.getInstructionIndex(MO.getParent()).getRegSlot());
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000132
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000133 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000134
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000135 // Remove duplicates, keeping the smaller slot for each instruction.
136 // That is what we want for early clobbers.
137 UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
138 SlotIndex::isSameInstr),
139 UseSlots.end());
140
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000141 // Compute per-live block info.
142 if (!calcLiveBlockInfo()) {
143 // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
Rafael Espindola5b220212011-06-26 22:34:10 +0000144 // I am looking at you, RegisterCoalescer!
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +0000145 DidRepairRange = true;
Jakob Stoklund Olesenbdda37d2011-05-10 17:37:41 +0000146 ++NumRepairs;
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000147 DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
148 const_cast<LiveIntervals&>(LIS)
149 .shrinkToUses(const_cast<LiveInterval*>(CurLI));
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000150 UseBlocks.clear();
151 ThroughBlocks.clear();
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000152 bool fixed = calcLiveBlockInfo();
153 (void)fixed;
154 assert(fixed && "Couldn't fix broken live interval");
155 }
156
Jakob Stoklund Olesenef1f5cc2011-03-27 22:49:23 +0000157 DEBUG(dbgs() << "Analyze counted "
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000158 << UseSlots.size() << " instrs in "
159 << UseBlocks.size() << " blocks, through "
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000160 << NumThroughBlocks << " blocks.\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000161}
162
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000163/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
164/// where CurLI is live.
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000165bool SplitAnalysis::calcLiveBlockInfo() {
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000166 ThroughBlocks.resize(MF.getNumBlockIDs());
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000167 NumThroughBlocks = NumGapBlocks = 0;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000168 if (CurLI->empty())
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000169 return true;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000170
171 LiveInterval::const_iterator LVI = CurLI->begin();
172 LiveInterval::const_iterator LVE = CurLI->end();
173
174 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
175 UseI = UseSlots.begin();
176 UseE = UseSlots.end();
177
178 // Loop over basic blocks where CurLI is live.
179 MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start);
180 for (;;) {
181 BlockInfo BI;
182 BI.MBB = MFI;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000183 SlotIndex Start, Stop;
Stephen Hines36b56882014-04-23 16:57:46 -0700184 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000185
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000186 // If the block contains no uses, the range must be live through. At one
Rafael Espindola5b220212011-06-26 22:34:10 +0000187 // point, RegisterCoalescer could create dangling ranges that ended
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000188 // mid-block.
189 if (UseI == UseE || *UseI >= Stop) {
190 ++NumThroughBlocks;
191 ThroughBlocks.set(BI.MBB->getNumber());
192 // The range shouldn't end mid-block if there are no uses. This shouldn't
193 // happen.
194 if (LVI->end < Stop)
195 return false;
196 } else {
197 // This block has uses. Find the first and last uses in the block.
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000198 BI.FirstInstr = *UseI;
199 assert(BI.FirstInstr >= Start);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000200 do ++UseI;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000201 while (UseI != UseE && *UseI < Stop);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000202 BI.LastInstr = UseI[-1];
203 assert(BI.LastInstr < Stop);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000204
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000205 // LVI is the first live segment overlapping MBB.
206 BI.LiveIn = LVI->start <= Start;
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000207
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000208 // When not live in, the first use should be a def.
209 if (!BI.LiveIn) {
Matthias Braun331de112013-10-10 21:28:43 +0000210 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000211 assert(LVI->start == BI.FirstInstr && "First instr should be a def");
212 BI.FirstDef = BI.FirstInstr;
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000213 }
214
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000215 // Look for gaps in the live range.
216 BI.LiveOut = true;
217 while (LVI->end < Stop) {
218 SlotIndex LastStop = LVI->end;
219 if (++LVI == LVE || LVI->start >= Stop) {
220 BI.LiveOut = false;
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000221 BI.LastInstr = LastStop;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000222 break;
223 }
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000224
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000225 if (LastStop < LVI->start) {
226 // There is a gap in the live range. Create duplicate entries for the
227 // live-in snippet and the live-out snippet.
228 ++NumGapBlocks;
229
230 // Push the Live-in part.
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000231 BI.LiveOut = false;
232 UseBlocks.push_back(BI);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000233 UseBlocks.back().LastInstr = LastStop;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000234
235 // Set up BI for the live-out part.
236 BI.LiveIn = false;
237 BI.LiveOut = true;
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000238 BI.FirstInstr = BI.FirstDef = LVI->start;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000239 }
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000240
Matthias Braun331de112013-10-10 21:28:43 +0000241 // A Segment that starts in the middle of the block must be a def.
242 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000243 if (!BI.FirstDef)
244 BI.FirstDef = LVI->start;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000245 }
246
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000247 UseBlocks.push_back(BI);
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000248
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000249 // LVI is now at LVE or LVI->end >= Stop.
250 if (LVI == LVE)
251 break;
252 }
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000253
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000254 // Live segment ends exactly at Stop. Move to the next segment.
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000255 if (LVI->end == Stop && ++LVI == LVE)
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000256 break;
257
258 // Pick the next basic block.
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000259 if (LVI->start < Stop)
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000260 ++MFI;
261 else
262 MFI = LIS.getMBBFromIndex(LVI->start);
263 }
Jakob Stoklund Olesenb2abfa02011-05-28 02:32:57 +0000264
265 assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000266 return true;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000267}
268
Jakob Stoklund Olesen9f4b8932011-04-26 22:33:12 +0000269unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
270 if (cli->empty())
271 return 0;
272 LiveInterval *li = const_cast<LiveInterval*>(cli);
273 LiveInterval::iterator LVI = li->begin();
274 LiveInterval::iterator LVE = li->end();
275 unsigned Count = 0;
276
277 // Loop over basic blocks where li is live.
278 MachineFunction::const_iterator MFI = LIS.getMBBFromIndex(LVI->start);
279 SlotIndex Stop = LIS.getMBBEndIdx(MFI);
280 for (;;) {
281 ++Count;
282 LVI = li->advanceTo(LVI, Stop);
283 if (LVI == LVE)
284 return Count;
285 do {
286 ++MFI;
287 Stop = LIS.getMBBEndIdx(MFI);
288 } while (Stop <= LVI->start);
289 }
290}
291
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000292bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
293 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
294 const LiveInterval &Orig = LIS.getInterval(OrigReg);
295 assert(!Orig.empty() && "Splitting empty interval?");
296 LiveInterval::const_iterator I = Orig.find(Idx);
297
298 // Range containing Idx should begin at Idx.
299 if (I != Orig.end() && I->start <= Idx)
300 return I->start == Idx;
301
302 // Range does not contain Idx, previous must end at Idx.
303 return I != Orig.begin() && (--I)->end == Idx;
304}
305
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000306void SplitAnalysis::analyze(const LiveInterval *li) {
307 clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000308 CurLI = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000309 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000310}
311
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000312
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000313//===----------------------------------------------------------------------===//
314// Split Editor
315//===----------------------------------------------------------------------===//
316
317/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800318SplitEditor::SplitEditor(SplitAnalysis &sa, LiveIntervals &lis, VirtRegMap &vrm,
Benjamin Kramer4eed7562013-06-17 19:00:36 +0000319 MachineDominatorTree &mdt,
320 MachineBlockFrequencyInfo &mbfi)
Stephen Hines37ed9c12014-12-01 14:51:49 -0800321 : SA(sa), LIS(lis), VRM(vrm), MRI(vrm.getMachineFunction().getRegInfo()),
322 MDT(mdt), TII(*vrm.getMachineFunction().getSubtarget().getInstrInfo()),
323 TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()),
324 MBFI(mbfi), Edit(nullptr), OpenIdx(0), SpillMode(SM_Partition),
325 RegAssign(Allocator) {}
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000326
Jakob Stoklund Olesen708d06f2011-09-12 16:49:21 +0000327void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
328 Edit = &LRE;
329 SpillMode = SM;
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000330 OpenIdx = 0;
331 RegAssign.clear();
332 Values.clear();
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000333
334 // Reset the LiveRangeCalc instances needed for this spill mode.
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000335 LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
336 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000337 if (SpillMode)
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000338 LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
339 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000340
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000341 // We don't need an AliasAnalysis since we will only be performing
342 // cheap-as-a-copy remats anyway.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700343 Edit->anyRematerializable(nullptr);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000344}
345
Manman Renb720be62012-09-11 22:23:19 +0000346#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Eric Christopher0f438112011-02-03 06:18:29 +0000347void SplitEditor::dump() const {
348 if (RegAssign.empty()) {
349 dbgs() << " empty\n";
350 return;
351 }
352
353 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
354 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
355 dbgs() << '\n';
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000356}
Manman Ren77e300e2012-09-06 19:06:06 +0000357#endif
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000358
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000359VNInfo *SplitEditor::defValue(unsigned RegIdx,
360 const VNInfo *ParentVNI,
361 SlotIndex Idx) {
362 assert(ParentVNI && "Mapping NULL value");
363 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000364 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
Mark Lacey1feb5852013-08-14 23:50:04 +0000365 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000366
367 // Create a new value.
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000368 VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000369
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000370 // Use insert for lookup, so we can add missing values with a second lookup.
371 std::pair<ValueMap::iterator, bool> InsP =
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000372 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id),
373 ValueForcePair(VNI, false)));
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000374
375 // This was the first time (RegIdx, ParentVNI) was mapped.
376 // Keep it as a simple def without any liveness.
377 if (InsP.second)
378 return VNI;
379
380 // If the previous value was a simple mapping, add liveness for it now.
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000381 if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000382 SlotIndex Def = OldVNI->def;
Matthias Braun331de112013-10-10 21:28:43 +0000383 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), OldVNI));
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000384 // No longer a simple mapping. Switch to a complex, non-forced mapping.
385 InsP.first->second = ValueForcePair();
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000386 }
387
388 // This is a complex mapping, add liveness for VNI
389 SlotIndex Def = VNI->def;
Matthias Braun331de112013-10-10 21:28:43 +0000390 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000391
392 return VNI;
393}
394
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000395void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000396 assert(ParentVNI && "Mapping NULL value");
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000397 ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
398 VNInfo *VNI = VFP.getPointer();
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000399
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000400 // ParentVNI was either unmapped or already complex mapped. Either way, just
401 // set the force bit.
402 if (!VNI) {
403 VFP.setInt(true);
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000404 return;
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000405 }
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000406
407 // This was previously a single mapping. Make sure the old def is represented
408 // by a trivial live range.
409 SlotIndex Def = VNI->def;
Mark Lacey1feb5852013-08-14 23:50:04 +0000410 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Matthias Braun331de112013-10-10 21:28:43 +0000411 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000412 // Mark as complex mapped, forced.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700413 VFP = ValueForcePair(nullptr, true);
Jakob Stoklund Olesene5a2e362011-09-13 18:05:29 +0000414}
415
Eric Christopher0f438112011-02-03 06:18:29 +0000416VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000417 VNInfo *ParentVNI,
418 SlotIndex UseIdx,
419 MachineBasicBlock &MBB,
420 MachineBasicBlock::iterator I) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700421 MachineInstr *CopyMI = nullptr;
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000422 SlotIndex Def;
Mark Lacey1feb5852013-08-14 23:50:04 +0000423 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000424
Jakob Stoklund Olesenbb30dd42011-05-02 05:29:58 +0000425 // We may be trying to avoid interference that ends at a deleted instruction,
426 // so always begin RegIdx 0 early and all others late.
427 bool Late = RegIdx != 0;
428
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000429 // Attempt cheap-as-a-copy rematerialization.
430 LiveRangeEdit::Remat RM(ParentVNI);
Pete Cooper8a06af92012-04-02 22:22:53 +0000431 if (Edit->canRematerializeAt(RM, UseIdx, true)) {
432 Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000433 ++NumRemats;
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000434 } else {
435 // Can't remat, just insert a copy from parent.
Eric Christopher0f438112011-02-03 06:18:29 +0000436 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000437 .addReg(Edit->getReg());
Jakob Stoklund Olesenbb30dd42011-05-02 05:29:58 +0000438 Def = LIS.getSlotIndexes()->insertMachineInstrInMaps(CopyMI, Late)
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000439 .getRegSlot();
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000440 ++NumCopies;
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000441 }
442
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000443 // Define the value in Reg.
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000444 return defValue(RegIdx, ParentVNI, Def);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000445}
446
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000447/// Create a new virtual register and live interval.
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000448unsigned SplitEditor::openIntv() {
Eric Christopher0f438112011-02-03 06:18:29 +0000449 // Create the complement as index 0.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000450 if (Edit->empty())
Mark Laceye742d682013-08-14 23:50:16 +0000451 Edit->createEmptyInterval();
Eric Christopher0f438112011-02-03 06:18:29 +0000452
453 // Create the open interval.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000454 OpenIdx = Edit->size();
Mark Laceye742d682013-08-14 23:50:16 +0000455 Edit->createEmptyInterval();
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000456 return OpenIdx;
457}
458
459void SplitEditor::selectIntv(unsigned Idx) {
460 assert(Idx != 0 && "Cannot select the complement interval");
461 assert(Idx < Edit->size() && "Can only select previously opened interval");
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000462 DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000463 OpenIdx = Idx;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000464}
465
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000466SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000467 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000468 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000469 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000470 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000471 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000472 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000473 return Idx;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000474 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000475 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000476 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000477 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000478
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000479 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
480 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000481}
482
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000483SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
484 assert(OpenIdx && "openIntv not called before enterIntvAfter");
485 DEBUG(dbgs() << " enterIntvAfter " << Idx);
486 Idx = Idx.getBoundaryIndex();
487 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
488 if (!ParentVNI) {
489 DEBUG(dbgs() << ": not live\n");
490 return Idx;
491 }
492 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
493 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
494 assert(MI && "enterIntvAfter called with invalid index");
495
496 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
Stephen Hines36b56882014-04-23 16:57:46 -0700497 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000498 return VNI->def;
499}
500
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000501SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000502 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000503 SlotIndex End = LIS.getMBBEndIdx(&MBB);
504 SlotIndex Last = End.getPrevSlot();
505 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000506 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000507 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000508 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000509 return End;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000510 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000511 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000512 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesen74c4f972012-01-11 02:07:00 +0000513 SA.getLastSplitPointIter(&MBB));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000514 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopher0f438112011-02-03 06:18:29 +0000515 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000516 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000517}
518
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000519/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000520void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000521 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000522}
523
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000524void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopher0f438112011-02-03 06:18:29 +0000525 assert(OpenIdx && "openIntv not called before useIntv");
526 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
527 RegAssign.insert(Start, End, OpenIdx);
528 DEBUG(dump());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000529}
530
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000531SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000532 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000533 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000534
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000535 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000536 SlotIndex Boundary = Idx.getBoundaryIndex();
537 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000538 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000539 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000540 return Boundary.getNextSlot();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000541 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000542 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000543 MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
Jakob Stoklund Olesen01cb34b2011-02-08 18:50:18 +0000544 assert(MI && "No instruction at index");
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000545
546 // In spill mode, make live ranges as short as possible by inserting the copy
547 // before MI. This is only possible if that instruction doesn't redefine the
548 // value. The inserted COPY is not a kill, and we don't need to recompute
549 // the source live range. The spiller also won't try to hoist this copy.
550 if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
551 MI->readsVirtualRegister(Edit->getReg())) {
552 forceRecompute(0, ParentVNI);
553 defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
554 return Idx;
555 }
556
557 VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
Stephen Hines36b56882014-04-23 16:57:46 -0700558 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000559 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000560}
561
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000562SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
563 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
564 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
565
566 // The interval must be live into the instruction at Idx.
Jakob Stoklund Olesenfc479332011-07-18 18:47:13 +0000567 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000568 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000569 if (!ParentVNI) {
570 DEBUG(dbgs() << ": not live\n");
571 return Idx.getNextSlot();
572 }
573 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
574
575 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
576 assert(MI && "No instruction at index");
577 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
578 return VNI->def;
579}
580
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000581SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000582 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000583 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000584 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000585
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000586 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000587 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000588 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000589 return Start;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000590 }
591
Eric Christopher0f438112011-02-03 06:18:29 +0000592 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000593 MBB.SkipPHIsAndLabels(MBB.begin()));
Eric Christopher0f438112011-02-03 06:18:29 +0000594 RegAssign.insert(Start, VNI->def, OpenIdx);
595 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000596 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000597}
598
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000599void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
600 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000601 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +0000602 assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000603 "Parent changes value in extended range");
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000604 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
605 "Range cannot span basic blocks");
606
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000607 // The complement interval will be extended as needed by LRCalc.extend().
Jakob Stoklund Olesenb3dd8262011-04-05 23:43:14 +0000608 if (ParentVNI)
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000609 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000610 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
611 RegAssign.insert(Start, End, OpenIdx);
612 DEBUG(dump());
613}
614
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000615//===----------------------------------------------------------------------===//
616// Spill modes
617//===----------------------------------------------------------------------===//
618
619void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
Mark Lacey1feb5852013-08-14 23:50:04 +0000620 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000621 DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
622 RegAssignMap::iterator AssignI;
623 AssignI.setMap(RegAssign);
624
625 for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700626 SlotIndex Def = Copies[i]->def;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000627 MachineInstr *MI = LIS.getInstructionFromIndex(Def);
628 assert(MI && "No instruction for back-copy");
629
630 MachineBasicBlock *MBB = MI->getParent();
631 MachineBasicBlock::iterator MBBI(MI);
632 bool AtBegin;
633 do AtBegin = MBBI == MBB->begin();
634 while (!AtBegin && (--MBBI)->isDebugValue());
635
636 DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700637 LIS.removeVRegDefAt(*LI, Def);
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000638 LIS.RemoveMachineInstrFromMaps(MI);
639 MI->eraseFromParent();
640
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700641 // Adjust RegAssign if a register assignment is killed at Def. We want to
642 // avoid calculating the live range of the source register if possible.
Jakob Stoklund Olesen1599a642012-08-03 20:59:29 +0000643 AssignI.find(Def.getPrevSlot());
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000644 if (!AssignI.valid() || AssignI.start() >= Def)
645 continue;
646 // If MI doesn't kill the assigned register, just leave it.
647 if (AssignI.stop() != Def)
648 continue;
649 unsigned RegIdx = AssignI.value();
650 if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
651 DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n');
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000652 forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000653 } else {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000654 SlotIndex Kill = LIS.getInstructionIndex(MBBI).getRegSlot();
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000655 DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
656 AssignI.setStop(Kill);
657 }
658 }
659}
660
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000661MachineBasicBlock*
662SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
663 MachineBasicBlock *DefMBB) {
664 if (MBB == DefMBB)
665 return MBB;
666 assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
667
668 const MachineLoopInfo &Loops = SA.Loops;
669 const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
670 MachineDomTreeNode *DefDomNode = MDT[DefMBB];
671
672 // Best candidate so far.
673 MachineBasicBlock *BestMBB = MBB;
674 unsigned BestDepth = UINT_MAX;
675
676 for (;;) {
677 const MachineLoop *Loop = Loops.getLoopFor(MBB);
678
679 // MBB isn't in a loop, it doesn't get any better. All dominators have a
680 // higher frequency by definition.
681 if (!Loop) {
682 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
683 << MBB->getNumber() << " at depth 0\n");
684 return MBB;
685 }
686
687 // We'll never be able to exit the DefLoop.
688 if (Loop == DefLoop) {
689 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
690 << MBB->getNumber() << " in the same loop\n");
691 return MBB;
692 }
693
694 // Least busy dominator seen so far.
695 unsigned Depth = Loop->getLoopDepth();
696 if (Depth < BestDepth) {
697 BestMBB = MBB;
698 BestDepth = Depth;
699 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
700 << MBB->getNumber() << " at depth " << Depth << '\n');
701 }
702
703 // Leave loop by going to the immediate dominator of the loop header.
704 // This is a bigger stride than simply walking up the dominator tree.
705 MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
706
707 // Too far up the dominator tree?
708 if (!IDom || !MDT.dominates(DefDomNode, IDom))
709 return BestMBB;
710
711 MBB = IDom->getBlock();
712 }
713}
714
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000715void SplitEditor::hoistCopiesForSize() {
716 // Get the complement interval, always RegIdx 0.
Mark Lacey1feb5852013-08-14 23:50:04 +0000717 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000718 LiveInterval *Parent = &Edit->getParent();
719
720 // Track the nearest common dominator for all back-copies for each ParentVNI,
721 // indexed by ParentVNI->id.
722 typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
723 SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
724
725 // Find the nearest common dominator for parent values with multiple
726 // back-copies. If a single back-copy dominates, put it in DomPair.second.
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700727 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen1599a642012-08-03 20:59:29 +0000728 if (VNI->isUnused())
729 continue;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000730 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
731 assert(ParentVNI && "Parent not live at complement def");
732
733 // Don't hoist remats. The complement is probably going to disappear
734 // completely anyway.
735 if (Edit->didRematerialize(ParentVNI))
736 continue;
737
738 MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
739 DomPair &Dom = NearestDom[ParentVNI->id];
740
741 // Keep directly defined parent values. This is either a PHI or an
742 // instruction in the complement range. All other copies of ParentVNI
743 // should be eliminated.
744 if (VNI->def == ParentVNI->def) {
745 DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
746 Dom = DomPair(ValMBB, VNI->def);
747 continue;
748 }
749 // Skip the singly mapped values. There is nothing to gain from hoisting a
750 // single back-copy.
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000751 if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000752 DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
753 continue;
754 }
755
756 if (!Dom.first) {
757 // First time we see ParentVNI. VNI dominates itself.
758 Dom = DomPair(ValMBB, VNI->def);
759 } else if (Dom.first == ValMBB) {
760 // Two defs in the same block. Pick the earlier def.
761 if (!Dom.second.isValid() || VNI->def < Dom.second)
762 Dom.second = VNI->def;
763 } else {
764 // Different basic blocks. Check if one dominates.
765 MachineBasicBlock *Near =
766 MDT.findNearestCommonDominator(Dom.first, ValMBB);
767 if (Near == ValMBB)
768 // Def ValMBB dominates.
769 Dom = DomPair(ValMBB, VNI->def);
770 else if (Near != Dom.first)
771 // None dominate. Hoist to common dominator, need new def.
772 Dom = DomPair(Near, SlotIndex());
773 }
774
775 DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
776 << " for parent " << ParentVNI->id << '@' << ParentVNI->def
777 << " hoist to BB#" << Dom.first->getNumber() << ' '
778 << Dom.second << '\n');
779 }
780
781 // Insert the hoisted copies.
782 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
783 DomPair &Dom = NearestDom[i];
784 if (!Dom.first || Dom.second.isValid())
785 continue;
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000786 // This value needs a hoisted copy inserted at the end of Dom.first.
787 VNInfo *ParentVNI = Parent->getValNumInfo(i);
788 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
789 // Get a less loopy dominator than Dom.first.
790 Dom.first = findShallowDominator(Dom.first, DefMBB);
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000791 SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
792 Dom.second =
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000793 defFromParent(0, ParentVNI, Last, *Dom.first,
Jakob Stoklund Olesen74c4f972012-01-11 02:07:00 +0000794 SA.getLastSplitPointIter(Dom.first))->def;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000795 }
796
797 // Remove redundant back-copies that are now known to be dominated by another
798 // def with the same value.
799 SmallVector<VNInfo*, 8> BackCopies;
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700800 for (VNInfo *VNI : LI->valnos) {
Jakob Stoklund Olesen1599a642012-08-03 20:59:29 +0000801 if (VNI->isUnused())
802 continue;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000803 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
804 const DomPair &Dom = NearestDom[ParentVNI->id];
805 if (!Dom.first || Dom.second == VNI->def)
806 continue;
807 BackCopies.push_back(VNI);
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000808 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000809 }
810 removeBackCopies(BackCopies);
811}
812
813
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000814/// transferValues - Transfer all possible values to the new live ranges.
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000815/// Values that were rematerialized are left alone, they need LRCalc.extend().
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000816bool SplitEditor::transferValues() {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000817 bool Skipped = false;
818 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700819 for (const LiveRange::Segment &S : Edit->getParent()) {
820 DEBUG(dbgs() << " blit " << S << ':');
821 VNInfo *ParentVNI = S.valno;
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000822 // RegAssign has holes where RegIdx 0 should be used.
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700823 SlotIndex Start = S.start;
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000824 AssignI.advanceTo(Start);
825 do {
826 unsigned RegIdx;
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700827 SlotIndex End = S.end;
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000828 if (!AssignI.valid()) {
829 RegIdx = 0;
830 } else if (AssignI.start() <= Start) {
831 RegIdx = AssignI.value();
832 if (AssignI.stop() < End) {
833 End = AssignI.stop();
834 ++AssignI;
835 }
836 } else {
837 RegIdx = 0;
838 End = std::min(End, AssignI.start());
839 }
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000840
841 // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000842 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
Matthias Braune25dde52013-10-10 21:28:57 +0000843 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000844
845 // Check for a simply defined value that can be blitted directly.
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000846 ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
847 if (VNInfo *VNI = VFP.getPointer()) {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000848 DEBUG(dbgs() << ':' << VNI->id);
Matthias Braune25dde52013-10-10 21:28:57 +0000849 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000850 Start = End;
851 continue;
852 }
853
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000854 // Skip values with forced recomputation.
855 if (VFP.getInt()) {
856 DEBUG(dbgs() << "(recalc)");
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000857 Skipped = true;
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000858 Start = End;
859 continue;
860 }
861
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000862 LiveRangeCalc &LRC = getLRCalc(RegIdx);
863
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000864 // This value has multiple defs in RegIdx, but it wasn't rematerialized,
865 // so the live range is accurate. Add live-in blocks in [Start;End) to the
866 // LiveInBlocks.
867 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
868 SlotIndex BlockStart, BlockEnd;
Stephen Hines36b56882014-04-23 16:57:46 -0700869 std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(MBB);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000870
871 // The first block may be live-in, or it may have its own def.
872 if (Start != BlockStart) {
Matthias Braune25dde52013-10-10 21:28:57 +0000873 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000874 assert(VNI && "Missing def for complex mapped value");
875 DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
876 // MBB has its own def. Is it also live-out?
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000877 if (BlockEnd <= End)
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000878 LRC.setLiveOutValue(MBB, VNI);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000879
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000880 // Skip to the next block for live-in.
881 ++MBB;
882 BlockStart = BlockEnd;
883 }
884
885 // Handle the live-in blocks covered by [Start;End).
886 assert(Start <= BlockStart && "Expected live-in block");
887 while (BlockStart < End) {
888 DEBUG(dbgs() << ">BB#" << MBB->getNumber());
889 BlockEnd = LIS.getMBBEndIdx(MBB);
890 if (BlockStart == ParentVNI->def) {
891 // This block has the def of a parent PHI, so it isn't live-in.
892 assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
Matthias Braune25dde52013-10-10 21:28:57 +0000893 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000894 assert(VNI && "Missing def for complex mapped parent PHI");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000895 if (End >= BlockEnd)
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000896 LRC.setLiveOutValue(MBB, VNI); // Live-out as well.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000897 } else {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000898 // This block needs a live-in value. The last block covered may not
899 // be live-out.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000900 if (End < BlockEnd)
Matthias Braune25dde52013-10-10 21:28:57 +0000901 LRC.addLiveInBlock(LR, MDT[MBB], End);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000902 else {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000903 // Live-through, and we don't know the value.
Matthias Braune25dde52013-10-10 21:28:57 +0000904 LRC.addLiveInBlock(LR, MDT[MBB]);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700905 LRC.setLiveOutValue(MBB, nullptr);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000906 }
907 }
908 BlockStart = BlockEnd;
909 ++MBB;
910 }
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000911 Start = End;
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700912 } while (Start != S.end);
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000913 DEBUG(dbgs() << '\n');
914 }
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000915
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000916 LRCalc[0].calculateValues();
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000917 if (SpillMode)
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000918 LRCalc[1].calculateValues();
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000919
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000920 return Skipped;
921}
922
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000923void SplitEditor::extendPHIKillRanges() {
924 // Extend live ranges to be live-out for successor PHI values.
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700925 for (const VNInfo *PHIVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000926 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
927 continue;
928 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
Matthias Braune25dde52013-10-10 21:28:57 +0000929 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000930 LiveRangeCalc &LRC = getLRCalc(RegIdx);
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000931 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
932 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
933 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000934 SlotIndex End = LIS.getMBBEndIdx(*PI);
935 SlotIndex LastUse = End.getPrevSlot();
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000936 // The predecessor may not have a live-out value. That is OK, like an
937 // undef PHI operand.
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000938 if (Edit->getParent().liveAt(LastUse)) {
939 assert(RegAssign.lookup(LastUse) == RegIdx &&
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000940 "Different register assignment in phi predecessor");
Matthias Braune25dde52013-10-10 21:28:57 +0000941 LRC.extend(LR, End);
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000942 }
943 }
944 }
945}
946
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000947/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000948void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000949 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000950 RE = MRI.reg_end(); RI != RE;) {
Stephen Hines36b56882014-04-23 16:57:46 -0700951 MachineOperand &MO = *RI;
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000952 MachineInstr *MI = MO.getParent();
953 ++RI;
Eric Christopher0f438112011-02-03 06:18:29 +0000954 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000955 if (MI->isDebugValue()) {
956 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000957 MO.setReg(0);
958 continue;
959 }
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +0000960
Jakob Stoklund Olesenb09701d2011-07-24 20:23:50 +0000961 // <undef> operands don't really read the register, so it doesn't matter
962 // which register we choose. When the use operand is tied to a def, we must
963 // use the same register as the def, so just do that always.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000964 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesenb09701d2011-07-24 20:23:50 +0000965 if (MO.isDef() || MO.isUndef())
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000966 Idx = Idx.getRegSlot(MO.isEarlyClobber());
Eric Christopher0f438112011-02-03 06:18:29 +0000967
968 // Rewrite to the mapped register at Idx.
969 unsigned RegIdx = RegAssign.lookup(Idx);
Mark Lacey1feb5852013-08-14 23:50:04 +0000970 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000971 MO.setReg(LI->reg);
Eric Christopher0f438112011-02-03 06:18:29 +0000972 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
973 << Idx << ':' << RegIdx << '\t' << *MI);
974
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +0000975 // Extend liveness to Idx if the instruction reads reg.
Jakob Stoklund Olesen81d686e2011-07-24 20:33:23 +0000976 if (!ExtendRanges || MO.isUndef())
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +0000977 continue;
978
979 // Skip instructions that don't read Reg.
980 if (MO.isDef()) {
981 if (!MO.getSubReg() && !MO.isEarlyClobber())
982 continue;
983 // We may wan't to extend a live range for a partial redef, or for a use
984 // tied to an early clobber.
985 Idx = Idx.getPrevSlot();
986 if (!Edit->getParent().liveAt(Idx))
987 continue;
988 } else
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000989 Idx = Idx.getRegSlot(true);
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +0000990
Matthias Braune25dde52013-10-10 21:28:57 +0000991 getLRCalc(RegIdx).extend(*LI, Idx.getNextSlot());
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000992 }
993}
994
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000995void SplitEditor::deleteRematVictims() {
996 SmallVector<MachineInstr*, 8> Dead;
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +0000997 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
Mark Lacey1feb5852013-08-14 23:50:04 +0000998 LiveInterval *LI = &LIS.getInterval(*I);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700999 for (const LiveRange::Segment &S : LI->segments) {
Jakob Stoklund Olesen1f81e312011-11-13 22:42:13 +00001000 // Dead defs end at the dead slot.
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001001 if (S.end != S.valno->def.getDeadSlot())
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001002 continue;
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001003 MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001004 assert(MI && "Missing instruction for dead def");
1005 MI->addRegisterDead(LI->reg, &TRI);
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001006
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001007 if (!MI->allDefsAreDead())
1008 continue;
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001009
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001010 DEBUG(dbgs() << "All defs dead: " << *MI);
1011 Dead.push_back(MI);
1012 }
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001013 }
1014
1015 if (Dead.empty())
1016 return;
1017
Pete Cooper8a06af92012-04-02 22:22:53 +00001018 Edit->eliminateDeadDefs(Dead);
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001019}
1020
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001021void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001022 ++NumFinished;
Eric Christopher463a2972011-02-03 05:40:54 +00001023
Eric Christopher0f438112011-02-03 06:18:29 +00001024 // At this point, the live intervals in Edit contain VNInfos corresponding to
1025 // the inserted copies.
1026
1027 // Add the original defs from the parent interval.
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001028 for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
Jakob Stoklund Olesen9ecd1e72011-02-04 00:59:23 +00001029 if (ParentVNI->isUnused())
1030 continue;
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +00001031 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Jakob Stoklund Olesenb18d7792012-07-27 21:11:14 +00001032 defValue(RegIdx, ParentVNI, ParentVNI->def);
Jakob Stoklund Olesen29ef8752011-03-15 21:13:22 +00001033
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +00001034 // Force rematted values to be recomputed everywhere.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001035 // The new live ranges may be truncated.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001036 if (Edit->didRematerialize(ParentVNI))
1037 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +00001038 forceRecompute(i, ParentVNI);
Eric Christopher0f438112011-02-03 06:18:29 +00001039 }
1040
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001041 // Hoist back-copies to the complement interval when in spill mode.
1042 switch (SpillMode) {
1043 case SM_Partition:
1044 // Leave all back-copies as is.
1045 break;
1046 case SM_Size:
1047 hoistCopiesForSize();
1048 break;
1049 case SM_Speed:
1050 llvm_unreachable("Spill mode 'speed' not implemented yet");
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001051 }
1052
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001053 // Transfer the simply mapped values, check if any are skipped.
1054 bool Skipped = transferValues();
1055 if (Skipped)
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001056 extendPHIKillRanges();
1057 else
1058 ++NumSimple;
Eric Christopher0f438112011-02-03 06:18:29 +00001059
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001060 // Rewrite virtual registers, possibly extending ranges.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001061 rewriteAssigned(Skipped);
Eric Christopher0f438112011-02-03 06:18:29 +00001062
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001063 // Delete defs that were rematted everywhere.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001064 if (Skipped)
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001065 deleteRematVictims();
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001066
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001067 // Get rid of unused values and set phi-kill flags.
Mark Lacey1feb5852013-08-14 23:50:04 +00001068 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) {
1069 LiveInterval &LI = LIS.getInterval(*I);
1070 LI.RenumberValues();
1071 }
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001072
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001073 // Provide a reverse mapping from original indices to Edit ranges.
1074 if (LRMap) {
1075 LRMap->clear();
1076 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1077 LRMap->push_back(i);
1078 }
1079
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001080 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001081 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001082 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001083 // Don't use iterators, they are invalidated by create() below.
Mark Lacey1feb5852013-08-14 23:50:04 +00001084 LiveInterval *li = &LIS.getInterval(Edit->get(i));
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001085 unsigned NumComp = ConEQ.Classify(li);
1086 if (NumComp <= 1)
1087 continue;
1088 DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n');
1089 SmallVector<LiveInterval*, 8> dups;
1090 dups.push_back(li);
Matt Beaumont-Gayae5fbee2011-04-21 19:46:23 +00001091 for (unsigned j = 1; j != NumComp; ++j)
Mark Laceye742d682013-08-14 23:50:16 +00001092 dups.push_back(&Edit->createEmptyInterval());
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +00001093 ConEQ.Distribute(&dups[0], MRI);
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001094 // The new intervals all map back to i.
1095 if (LRMap)
1096 LRMap->resize(Edit->size(), i);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001097 }
1098
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +00001099 // Calculate spill weight and allocation hints for new intervals.
Benjamin Kramer4eed7562013-06-17 19:00:36 +00001100 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001101
1102 assert(!LRMap || LRMap->size() == Edit->size());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001103}
1104
1105
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +00001106//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001107// Single Block Splitting
1108//===----------------------------------------------------------------------===//
1109
Jakob Stoklund Olesen2d6d86b2011-08-05 22:20:45 +00001110bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
1111 bool SingleInstrs) const {
1112 // Always split for multiple instructions.
1113 if (!BI.isOneInstr())
1114 return true;
1115 // Don't split for single instructions unless explicitly requested.
1116 if (!SingleInstrs)
1117 return false;
1118 // Splitting a live-through range always makes progress.
1119 if (BI.LiveIn && BI.LiveOut)
1120 return true;
1121 // No point in isolating a copy. It has no register class constraints.
1122 if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
1123 return false;
1124 // Finally, don't isolate an end point that was created by earlier splits.
1125 return isOriginalEndpoint(BI.FirstInstr);
1126}
1127
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001128void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1129 openIntv();
1130 SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001131 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001132 LastSplitPoint));
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001133 if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
1134 useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001135 } else {
1136 // The last use is after the last valid split point.
1137 SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1138 useIntv(SegStart, SegStop);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001139 overlapIntv(SegStop, BI.LastInstr);
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001140 }
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001141}
1142
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001143
1144//===----------------------------------------------------------------------===//
1145// Global Live Range Splitting Support
1146//===----------------------------------------------------------------------===//
1147
1148// These methods support a method of global live range splitting that uses a
1149// global algorithm to decide intervals for CFG edges. They will insert split
1150// points and color intervals in basic blocks while avoiding interference.
1151//
1152// Note that splitSingleBlock is also useful for blocks where both CFG edges
1153// are on the stack.
1154
1155void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1156 unsigned IntvIn, SlotIndex LeaveBefore,
1157 unsigned IntvOut, SlotIndex EnterAfter){
1158 SlotIndex Start, Stop;
Stephen Hines36b56882014-04-23 16:57:46 -07001159 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001160
1161 DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
1162 << ") intf " << LeaveBefore << '-' << EnterAfter
1163 << ", live-through " << IntvIn << " -> " << IntvOut);
1164
1165 assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1166
Jakob Stoklund Olesenfe9b2d12011-07-23 03:32:26 +00001167 assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
1168 assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
1169 assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
1170
1171 MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
1172
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001173 if (!IntvOut) {
1174 DEBUG(dbgs() << ", spill on entry.\n");
1175 //
1176 // <<<<<<<<< Possible LeaveBefore interference.
1177 // |-----------| Live through.
1178 // -____________ Spill on entry.
1179 //
1180 selectIntv(IntvIn);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001181 SlotIndex Idx = leaveIntvAtTop(*MBB);
1182 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1183 (void)Idx;
1184 return;
1185 }
1186
1187 if (!IntvIn) {
1188 DEBUG(dbgs() << ", reload on exit.\n");
1189 //
1190 // >>>>>>> Possible EnterAfter interference.
1191 // |-----------| Live through.
1192 // ___________-- Reload on exit.
1193 //
1194 selectIntv(IntvOut);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001195 SlotIndex Idx = enterIntvAtEnd(*MBB);
1196 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1197 (void)Idx;
1198 return;
1199 }
1200
1201 if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1202 DEBUG(dbgs() << ", straight through.\n");
1203 //
1204 // |-----------| Live through.
1205 // ------------- Straight through, same intv, no interference.
1206 //
1207 selectIntv(IntvOut);
1208 useIntv(Start, Stop);
1209 return;
1210 }
1211
1212 // We cannot legally insert splits after LSP.
1213 SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
Jakob Stoklund Olesenfe9b2d12011-07-23 03:32:26 +00001214 assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001215
1216 if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1217 LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1218 DEBUG(dbgs() << ", switch avoiding interference.\n");
1219 //
1220 // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
1221 // |-----------| Live through.
1222 // ------======= Switch intervals between interference.
1223 //
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001224 selectIntv(IntvOut);
Jakob Stoklund Olesenfe9b2d12011-07-23 03:32:26 +00001225 SlotIndex Idx;
1226 if (LeaveBefore && LeaveBefore < LSP) {
1227 Idx = enterIntvBefore(LeaveBefore);
1228 useIntv(Idx, Stop);
1229 } else {
1230 Idx = enterIntvAtEnd(*MBB);
1231 }
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001232 selectIntv(IntvIn);
1233 useIntv(Start, Idx);
1234 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1235 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1236 return;
1237 }
1238
1239 DEBUG(dbgs() << ", create local intv for interference.\n");
1240 //
1241 // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
1242 // |-----------| Live through.
1243 // ==---------== Switch intervals before/after interference.
1244 //
1245 assert(LeaveBefore <= EnterAfter && "Missed case");
1246
1247 selectIntv(IntvOut);
1248 SlotIndex Idx = enterIntvAfter(EnterAfter);
1249 useIntv(Idx, Stop);
1250 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1251
1252 selectIntv(IntvIn);
1253 Idx = leaveIntvBefore(LeaveBefore);
1254 useIntv(Start, Idx);
1255 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1256}
1257
1258
1259void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1260 unsigned IntvIn, SlotIndex LeaveBefore) {
1261 SlotIndex Start, Stop;
Stephen Hines36b56882014-04-23 16:57:46 -07001262 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001263
1264 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001265 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001266 << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
1267 << (BI.LiveOut ? ", stack-out" : ", killed in block"));
1268
1269 assert(IntvIn && "Must have register in");
1270 assert(BI.LiveIn && "Must be live-in");
1271 assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1272
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001273 if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001274 DEBUG(dbgs() << " before interference.\n");
1275 //
1276 // <<< Interference after kill.
1277 // |---o---x | Killed in block.
1278 // ========= Use IntvIn everywhere.
1279 //
1280 selectIntv(IntvIn);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001281 useIntv(Start, BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001282 return;
1283 }
1284
1285 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1286
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001287 if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001288 //
1289 // <<< Possible interference after last use.
1290 // |---o---o---| Live-out on stack.
1291 // =========____ Leave IntvIn after last use.
1292 //
1293 // < Interference after last use.
1294 // |---o---o--o| Live-out on stack, late last use.
1295 // ============ Copy to stack after LSP, overlap IntvIn.
1296 // \_____ Stack interval is live-out.
1297 //
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001298 if (BI.LastInstr < LSP) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001299 DEBUG(dbgs() << ", spill after last use before interference.\n");
1300 selectIntv(IntvIn);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001301 SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001302 useIntv(Start, Idx);
1303 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1304 } else {
1305 DEBUG(dbgs() << ", spill before last split point.\n");
1306 selectIntv(IntvIn);
Jakob Stoklund Olesenaf4e40c2011-07-16 00:13:30 +00001307 SlotIndex Idx = leaveIntvBefore(LSP);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001308 overlapIntv(Idx, BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001309 useIntv(Start, Idx);
1310 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1311 }
1312 return;
1313 }
1314
1315 // The interference is overlapping somewhere we wanted to use IntvIn. That
1316 // means we need to create a local interval that can be allocated a
1317 // different register.
1318 unsigned LocalIntv = openIntv();
Matt Beaumont-Gayf9d7fb62011-07-16 04:18:47 +00001319 (void)LocalIntv;
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001320 DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
1321
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001322 if (!BI.LiveOut || BI.LastInstr < LSP) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001323 //
1324 // <<<<<<< Interference overlapping uses.
1325 // |---o---o---| Live-out on stack.
1326 // =====----____ Leave IntvIn before interference, then spill.
1327 //
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001328 SlotIndex To = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001329 SlotIndex From = enterIntvBefore(LeaveBefore);
1330 useIntv(From, To);
1331 selectIntv(IntvIn);
1332 useIntv(Start, From);
1333 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1334 return;
1335 }
1336
1337 // <<<<<<< Interference overlapping uses.
1338 // |---o---o--o| Live-out on stack, late last use.
1339 // =====------- Copy to stack before LSP, overlap LocalIntv.
1340 // \_____ Stack interval is live-out.
1341 //
1342 SlotIndex To = leaveIntvBefore(LSP);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001343 overlapIntv(To, BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001344 SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1345 useIntv(From, To);
1346 selectIntv(IntvIn);
1347 useIntv(Start, From);
1348 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1349}
1350
1351void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1352 unsigned IntvOut, SlotIndex EnterAfter) {
1353 SlotIndex Start, Stop;
Stephen Hines36b56882014-04-23 16:57:46 -07001354 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001355
1356 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001357 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001358 << ", reg-out " << IntvOut << ", enter after " << EnterAfter
1359 << (BI.LiveIn ? ", stack-in" : ", defined in block"));
1360
1361 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1362
1363 assert(IntvOut && "Must have register out");
1364 assert(BI.LiveOut && "Must be live-out");
1365 assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1366
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001367 if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001368 DEBUG(dbgs() << " after interference.\n");
1369 //
1370 // >>>> Interference before def.
1371 // | o---o---| Defined in block.
1372 // ========= Use IntvOut everywhere.
1373 //
1374 selectIntv(IntvOut);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001375 useIntv(BI.FirstInstr, Stop);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001376 return;
1377 }
1378
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001379 if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001380 DEBUG(dbgs() << ", reload after interference.\n");
1381 //
1382 // >>>> Interference before def.
1383 // |---o---o---| Live-through, stack-in.
1384 // ____========= Enter IntvOut before first use.
1385 //
1386 selectIntv(IntvOut);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001387 SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001388 useIntv(Idx, Stop);
1389 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1390 return;
1391 }
1392
1393 // The interference is overlapping somewhere we wanted to use IntvOut. That
1394 // means we need to create a local interval that can be allocated a
1395 // different register.
1396 DEBUG(dbgs() << ", interference overlaps uses.\n");
1397 //
1398 // >>>>>>> Interference overlapping uses.
1399 // |---o---o---| Live-through, stack-in.
1400 // ____---====== Create local interval for interference range.
1401 //
1402 selectIntv(IntvOut);
1403 SlotIndex Idx = enterIntvAfter(EnterAfter);
1404 useIntv(Idx, Stop);
1405 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1406
1407 openIntv();
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001408 SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001409 useIntv(From, Idx);
1410}