blob: ea7b914c2542cdd0b9f8b332a74f50bcf1b2587a [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.
123 for (LiveInterval::const_vni_iterator I = CurLI->vni_begin(),
124 E = CurLI->vni_end(); I != E; ++I)
125 if (!(*I)->isPHIDef() && !(*I)->isUnused())
126 UseSlots.push_back((*I)->def);
127
128 // Get use slots form the use-def chain.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000129 const MachineRegisterInfo &MRI = MF.getRegInfo();
Stephen Hines36b56882014-04-23 16:57:46 -0700130 for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg))
131 if (!MO.isUndef())
132 UseSlots.push_back(LIS.getInstructionIndex(MO.getParent()).getRegSlot());
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000133
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000134 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000135
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000136 // Remove duplicates, keeping the smaller slot for each instruction.
137 // That is what we want for early clobbers.
138 UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
139 SlotIndex::isSameInstr),
140 UseSlots.end());
141
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000142 // Compute per-live block info.
143 if (!calcLiveBlockInfo()) {
144 // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
Rafael Espindola5b220212011-06-26 22:34:10 +0000145 // I am looking at you, RegisterCoalescer!
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +0000146 DidRepairRange = true;
Jakob Stoklund Olesenbdda37d2011-05-10 17:37:41 +0000147 ++NumRepairs;
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000148 DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
149 const_cast<LiveIntervals&>(LIS)
150 .shrinkToUses(const_cast<LiveInterval*>(CurLI));
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000151 UseBlocks.clear();
152 ThroughBlocks.clear();
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000153 bool fixed = calcLiveBlockInfo();
154 (void)fixed;
155 assert(fixed && "Couldn't fix broken live interval");
156 }
157
Jakob Stoklund Olesenef1f5cc2011-03-27 22:49:23 +0000158 DEBUG(dbgs() << "Analyze counted "
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000159 << UseSlots.size() << " instrs in "
160 << UseBlocks.size() << " blocks, through "
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000161 << NumThroughBlocks << " blocks.\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000162}
163
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000164/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
165/// where CurLI is live.
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000166bool SplitAnalysis::calcLiveBlockInfo() {
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000167 ThroughBlocks.resize(MF.getNumBlockIDs());
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000168 NumThroughBlocks = NumGapBlocks = 0;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000169 if (CurLI->empty())
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000170 return true;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000171
172 LiveInterval::const_iterator LVI = CurLI->begin();
173 LiveInterval::const_iterator LVE = CurLI->end();
174
175 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
176 UseI = UseSlots.begin();
177 UseE = UseSlots.end();
178
179 // Loop over basic blocks where CurLI is live.
180 MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start);
181 for (;;) {
182 BlockInfo BI;
183 BI.MBB = MFI;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000184 SlotIndex Start, Stop;
Stephen Hines36b56882014-04-23 16:57:46 -0700185 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000186
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000187 // If the block contains no uses, the range must be live through. At one
Rafael Espindola5b220212011-06-26 22:34:10 +0000188 // point, RegisterCoalescer could create dangling ranges that ended
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000189 // mid-block.
190 if (UseI == UseE || *UseI >= Stop) {
191 ++NumThroughBlocks;
192 ThroughBlocks.set(BI.MBB->getNumber());
193 // The range shouldn't end mid-block if there are no uses. This shouldn't
194 // happen.
195 if (LVI->end < Stop)
196 return false;
197 } else {
198 // This block has uses. Find the first and last uses in the block.
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000199 BI.FirstInstr = *UseI;
200 assert(BI.FirstInstr >= Start);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000201 do ++UseI;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000202 while (UseI != UseE && *UseI < Stop);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000203 BI.LastInstr = UseI[-1];
204 assert(BI.LastInstr < Stop);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000205
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000206 // LVI is the first live segment overlapping MBB.
207 BI.LiveIn = LVI->start <= Start;
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000208
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000209 // When not live in, the first use should be a def.
210 if (!BI.LiveIn) {
Matthias Braun331de112013-10-10 21:28:43 +0000211 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000212 assert(LVI->start == BI.FirstInstr && "First instr should be a def");
213 BI.FirstDef = BI.FirstInstr;
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000214 }
215
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000216 // Look for gaps in the live range.
217 BI.LiveOut = true;
218 while (LVI->end < Stop) {
219 SlotIndex LastStop = LVI->end;
220 if (++LVI == LVE || LVI->start >= Stop) {
221 BI.LiveOut = false;
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000222 BI.LastInstr = LastStop;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000223 break;
224 }
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000225
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000226 if (LastStop < LVI->start) {
227 // There is a gap in the live range. Create duplicate entries for the
228 // live-in snippet and the live-out snippet.
229 ++NumGapBlocks;
230
231 // Push the Live-in part.
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000232 BI.LiveOut = false;
233 UseBlocks.push_back(BI);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000234 UseBlocks.back().LastInstr = LastStop;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000235
236 // Set up BI for the live-out part.
237 BI.LiveIn = false;
238 BI.LiveOut = true;
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000239 BI.FirstInstr = BI.FirstDef = LVI->start;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000240 }
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000241
Matthias Braun331de112013-10-10 21:28:43 +0000242 // A Segment that starts in the middle of the block must be a def.
243 assert(LVI->start == LVI->valno->def && "Dangling Segment start");
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000244 if (!BI.FirstDef)
245 BI.FirstDef = LVI->start;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000246 }
247
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000248 UseBlocks.push_back(BI);
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000249
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000250 // LVI is now at LVE or LVI->end >= Stop.
251 if (LVI == LVE)
252 break;
253 }
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000254
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000255 // Live segment ends exactly at Stop. Move to the next segment.
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000256 if (LVI->end == Stop && ++LVI == LVE)
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000257 break;
258
259 // Pick the next basic block.
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000260 if (LVI->start < Stop)
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000261 ++MFI;
262 else
263 MFI = LIS.getMBBFromIndex(LVI->start);
264 }
Jakob Stoklund Olesenb2abfa02011-05-28 02:32:57 +0000265
266 assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000267 return true;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000268}
269
Jakob Stoklund Olesen9f4b8932011-04-26 22:33:12 +0000270unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
271 if (cli->empty())
272 return 0;
273 LiveInterval *li = const_cast<LiveInterval*>(cli);
274 LiveInterval::iterator LVI = li->begin();
275 LiveInterval::iterator LVE = li->end();
276 unsigned Count = 0;
277
278 // Loop over basic blocks where li is live.
279 MachineFunction::const_iterator MFI = LIS.getMBBFromIndex(LVI->start);
280 SlotIndex Stop = LIS.getMBBEndIdx(MFI);
281 for (;;) {
282 ++Count;
283 LVI = li->advanceTo(LVI, Stop);
284 if (LVI == LVE)
285 return Count;
286 do {
287 ++MFI;
288 Stop = LIS.getMBBEndIdx(MFI);
289 } while (Stop <= LVI->start);
290 }
291}
292
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000293bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
294 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
295 const LiveInterval &Orig = LIS.getInterval(OrigReg);
296 assert(!Orig.empty() && "Splitting empty interval?");
297 LiveInterval::const_iterator I = Orig.find(Idx);
298
299 // Range containing Idx should begin at Idx.
300 if (I != Orig.end() && I->start <= Idx)
301 return I->start == Idx;
302
303 // Range does not contain Idx, previous must end at Idx.
304 return I != Orig.begin() && (--I)->end == Idx;
305}
306
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000307void SplitAnalysis::analyze(const LiveInterval *li) {
308 clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000309 CurLI = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000310 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000311}
312
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000313
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000314//===----------------------------------------------------------------------===//
315// Split Editor
316//===----------------------------------------------------------------------===//
317
318/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800319SplitEditor::SplitEditor(SplitAnalysis &sa, LiveIntervals &lis, VirtRegMap &vrm,
Benjamin Kramer4eed7562013-06-17 19:00:36 +0000320 MachineDominatorTree &mdt,
321 MachineBlockFrequencyInfo &mbfi)
Stephen Hines37ed9c12014-12-01 14:51:49 -0800322 : SA(sa), LIS(lis), VRM(vrm), MRI(vrm.getMachineFunction().getRegInfo()),
323 MDT(mdt), TII(*vrm.getMachineFunction().getSubtarget().getInstrInfo()),
324 TRI(*vrm.getMachineFunction().getSubtarget().getRegisterInfo()),
325 MBFI(mbfi), Edit(nullptr), OpenIdx(0), SpillMode(SM_Partition),
326 RegAssign(Allocator) {}
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000327
Jakob Stoklund Olesen708d06f2011-09-12 16:49:21 +0000328void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
329 Edit = &LRE;
330 SpillMode = SM;
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000331 OpenIdx = 0;
332 RegAssign.clear();
333 Values.clear();
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000334
335 // Reset the LiveRangeCalc instances needed for this spill mode.
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000336 LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
337 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000338 if (SpillMode)
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000339 LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
340 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000341
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000342 // We don't need an AliasAnalysis since we will only be performing
343 // cheap-as-a-copy remats anyway.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700344 Edit->anyRematerializable(nullptr);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000345}
346
Manman Renb720be62012-09-11 22:23:19 +0000347#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Eric Christopher0f438112011-02-03 06:18:29 +0000348void SplitEditor::dump() const {
349 if (RegAssign.empty()) {
350 dbgs() << " empty\n";
351 return;
352 }
353
354 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
355 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
356 dbgs() << '\n';
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000357}
Manman Ren77e300e2012-09-06 19:06:06 +0000358#endif
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000359
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000360VNInfo *SplitEditor::defValue(unsigned RegIdx,
361 const VNInfo *ParentVNI,
362 SlotIndex Idx) {
363 assert(ParentVNI && "Mapping NULL value");
364 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000365 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
Mark Lacey1feb5852013-08-14 23:50:04 +0000366 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000367
368 // Create a new value.
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000369 VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000370
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000371 // Use insert for lookup, so we can add missing values with a second lookup.
372 std::pair<ValueMap::iterator, bool> InsP =
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000373 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id),
374 ValueForcePair(VNI, false)));
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000375
376 // This was the first time (RegIdx, ParentVNI) was mapped.
377 // Keep it as a simple def without any liveness.
378 if (InsP.second)
379 return VNI;
380
381 // If the previous value was a simple mapping, add liveness for it now.
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000382 if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000383 SlotIndex Def = OldVNI->def;
Matthias Braun331de112013-10-10 21:28:43 +0000384 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), OldVNI));
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000385 // No longer a simple mapping. Switch to a complex, non-forced mapping.
386 InsP.first->second = ValueForcePair();
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000387 }
388
389 // This is a complex mapping, add liveness for VNI
390 SlotIndex Def = VNI->def;
Matthias Braun331de112013-10-10 21:28:43 +0000391 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000392
393 return VNI;
394}
395
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000396void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000397 assert(ParentVNI && "Mapping NULL value");
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000398 ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
399 VNInfo *VNI = VFP.getPointer();
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000400
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000401 // ParentVNI was either unmapped or already complex mapped. Either way, just
402 // set the force bit.
403 if (!VNI) {
404 VFP.setInt(true);
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000405 return;
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000406 }
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000407
408 // This was previously a single mapping. Make sure the old def is represented
409 // by a trivial live range.
410 SlotIndex Def = VNI->def;
Mark Lacey1feb5852013-08-14 23:50:04 +0000411 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Matthias Braun331de112013-10-10 21:28:43 +0000412 LI->addSegment(LiveInterval::Segment(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000413 // Mark as complex mapped, forced.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700414 VFP = ValueForcePair(nullptr, true);
Jakob Stoklund Olesene5a2e362011-09-13 18:05:29 +0000415}
416
Eric Christopher0f438112011-02-03 06:18:29 +0000417VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000418 VNInfo *ParentVNI,
419 SlotIndex UseIdx,
420 MachineBasicBlock &MBB,
421 MachineBasicBlock::iterator I) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700422 MachineInstr *CopyMI = nullptr;
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000423 SlotIndex Def;
Mark Lacey1feb5852013-08-14 23:50:04 +0000424 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000425
Jakob Stoklund Olesenbb30dd42011-05-02 05:29:58 +0000426 // We may be trying to avoid interference that ends at a deleted instruction,
427 // so always begin RegIdx 0 early and all others late.
428 bool Late = RegIdx != 0;
429
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000430 // Attempt cheap-as-a-copy rematerialization.
431 LiveRangeEdit::Remat RM(ParentVNI);
Pete Cooper8a06af92012-04-02 22:22:53 +0000432 if (Edit->canRematerializeAt(RM, UseIdx, true)) {
433 Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000434 ++NumRemats;
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000435 } else {
436 // Can't remat, just insert a copy from parent.
Eric Christopher0f438112011-02-03 06:18:29 +0000437 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000438 .addReg(Edit->getReg());
Jakob Stoklund Olesenbb30dd42011-05-02 05:29:58 +0000439 Def = LIS.getSlotIndexes()->insertMachineInstrInMaps(CopyMI, Late)
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000440 .getRegSlot();
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000441 ++NumCopies;
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000442 }
443
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000444 // Define the value in Reg.
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000445 return defValue(RegIdx, ParentVNI, Def);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000446}
447
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000448/// Create a new virtual register and live interval.
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000449unsigned SplitEditor::openIntv() {
Eric Christopher0f438112011-02-03 06:18:29 +0000450 // Create the complement as index 0.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000451 if (Edit->empty())
Mark Laceye742d682013-08-14 23:50:16 +0000452 Edit->createEmptyInterval();
Eric Christopher0f438112011-02-03 06:18:29 +0000453
454 // Create the open interval.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000455 OpenIdx = Edit->size();
Mark Laceye742d682013-08-14 23:50:16 +0000456 Edit->createEmptyInterval();
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000457 return OpenIdx;
458}
459
460void SplitEditor::selectIntv(unsigned Idx) {
461 assert(Idx != 0 && "Cannot select the complement interval");
462 assert(Idx < Edit->size() && "Can only select previously opened interval");
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000463 DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000464 OpenIdx = Idx;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000465}
466
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000467SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000468 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000469 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000470 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000471 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000472 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000473 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000474 return Idx;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000475 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000476 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000477 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000478 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000479
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000480 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
481 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000482}
483
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000484SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
485 assert(OpenIdx && "openIntv not called before enterIntvAfter");
486 DEBUG(dbgs() << " enterIntvAfter " << Idx);
487 Idx = Idx.getBoundaryIndex();
488 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
489 if (!ParentVNI) {
490 DEBUG(dbgs() << ": not live\n");
491 return Idx;
492 }
493 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
494 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
495 assert(MI && "enterIntvAfter called with invalid index");
496
497 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
Stephen Hines36b56882014-04-23 16:57:46 -0700498 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000499 return VNI->def;
500}
501
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000502SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000503 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000504 SlotIndex End = LIS.getMBBEndIdx(&MBB);
505 SlotIndex Last = End.getPrevSlot();
506 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000507 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000508 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000509 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000510 return End;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000511 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000512 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000513 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesen74c4f972012-01-11 02:07:00 +0000514 SA.getLastSplitPointIter(&MBB));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000515 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopher0f438112011-02-03 06:18:29 +0000516 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000517 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000518}
519
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000520/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000521void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000522 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000523}
524
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000525void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopher0f438112011-02-03 06:18:29 +0000526 assert(OpenIdx && "openIntv not called before useIntv");
527 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
528 RegAssign.insert(Start, End, OpenIdx);
529 DEBUG(dump());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000530}
531
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000532SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000533 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000534 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000535
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000536 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000537 SlotIndex Boundary = Idx.getBoundaryIndex();
538 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000539 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000540 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000541 return Boundary.getNextSlot();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000542 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000543 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000544 MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
Jakob Stoklund Olesen01cb34b2011-02-08 18:50:18 +0000545 assert(MI && "No instruction at index");
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000546
547 // In spill mode, make live ranges as short as possible by inserting the copy
548 // before MI. This is only possible if that instruction doesn't redefine the
549 // value. The inserted COPY is not a kill, and we don't need to recompute
550 // the source live range. The spiller also won't try to hoist this copy.
551 if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
552 MI->readsVirtualRegister(Edit->getReg())) {
553 forceRecompute(0, ParentVNI);
554 defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
555 return Idx;
556 }
557
558 VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
Stephen Hines36b56882014-04-23 16:57:46 -0700559 std::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000560 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000561}
562
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000563SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
564 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
565 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
566
567 // The interval must be live into the instruction at Idx.
Jakob Stoklund Olesenfc479332011-07-18 18:47:13 +0000568 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000569 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000570 if (!ParentVNI) {
571 DEBUG(dbgs() << ": not live\n");
572 return Idx.getNextSlot();
573 }
574 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
575
576 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
577 assert(MI && "No instruction at index");
578 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
579 return VNI->def;
580}
581
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000582SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000583 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000584 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000585 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000586
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000587 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000588 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000589 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000590 return Start;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000591 }
592
Eric Christopher0f438112011-02-03 06:18:29 +0000593 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000594 MBB.SkipPHIsAndLabels(MBB.begin()));
Eric Christopher0f438112011-02-03 06:18:29 +0000595 RegAssign.insert(Start, VNI->def, OpenIdx);
596 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000597 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000598}
599
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000600void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
601 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000602 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +0000603 assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000604 "Parent changes value in extended range");
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000605 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
606 "Range cannot span basic blocks");
607
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000608 // The complement interval will be extended as needed by LRCalc.extend().
Jakob Stoklund Olesenb3dd8262011-04-05 23:43:14 +0000609 if (ParentVNI)
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000610 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000611 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
612 RegAssign.insert(Start, End, OpenIdx);
613 DEBUG(dump());
614}
615
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000616//===----------------------------------------------------------------------===//
617// Spill modes
618//===----------------------------------------------------------------------===//
619
620void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
Mark Lacey1feb5852013-08-14 23:50:04 +0000621 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000622 DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
623 RegAssignMap::iterator AssignI;
624 AssignI.setMap(RegAssign);
625
626 for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
627 VNInfo *VNI = Copies[i];
628 SlotIndex Def = VNI->def;
629 MachineInstr *MI = LIS.getInstructionFromIndex(Def);
630 assert(MI && "No instruction for back-copy");
631
632 MachineBasicBlock *MBB = MI->getParent();
633 MachineBasicBlock::iterator MBBI(MI);
634 bool AtBegin;
635 do AtBegin = MBBI == MBB->begin();
636 while (!AtBegin && (--MBBI)->isDebugValue());
637
638 DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
639 LI->removeValNo(VNI);
640 LIS.RemoveMachineInstrFromMaps(MI);
641 MI->eraseFromParent();
642
643 // Adjust RegAssign if a register assignment is killed at VNI->def. We
644 // want to avoid calculating the live range of the source register if
645 // possible.
Jakob Stoklund Olesen1599a642012-08-03 20:59:29 +0000646 AssignI.find(Def.getPrevSlot());
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000647 if (!AssignI.valid() || AssignI.start() >= Def)
648 continue;
649 // If MI doesn't kill the assigned register, just leave it.
650 if (AssignI.stop() != Def)
651 continue;
652 unsigned RegIdx = AssignI.value();
653 if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
654 DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n');
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000655 forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000656 } else {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000657 SlotIndex Kill = LIS.getInstructionIndex(MBBI).getRegSlot();
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000658 DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
659 AssignI.setStop(Kill);
660 }
661 }
662}
663
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000664MachineBasicBlock*
665SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
666 MachineBasicBlock *DefMBB) {
667 if (MBB == DefMBB)
668 return MBB;
669 assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
670
671 const MachineLoopInfo &Loops = SA.Loops;
672 const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
673 MachineDomTreeNode *DefDomNode = MDT[DefMBB];
674
675 // Best candidate so far.
676 MachineBasicBlock *BestMBB = MBB;
677 unsigned BestDepth = UINT_MAX;
678
679 for (;;) {
680 const MachineLoop *Loop = Loops.getLoopFor(MBB);
681
682 // MBB isn't in a loop, it doesn't get any better. All dominators have a
683 // higher frequency by definition.
684 if (!Loop) {
685 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
686 << MBB->getNumber() << " at depth 0\n");
687 return MBB;
688 }
689
690 // We'll never be able to exit the DefLoop.
691 if (Loop == DefLoop) {
692 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
693 << MBB->getNumber() << " in the same loop\n");
694 return MBB;
695 }
696
697 // Least busy dominator seen so far.
698 unsigned Depth = Loop->getLoopDepth();
699 if (Depth < BestDepth) {
700 BestMBB = MBB;
701 BestDepth = Depth;
702 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
703 << MBB->getNumber() << " at depth " << Depth << '\n');
704 }
705
706 // Leave loop by going to the immediate dominator of the loop header.
707 // This is a bigger stride than simply walking up the dominator tree.
708 MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
709
710 // Too far up the dominator tree?
711 if (!IDom || !MDT.dominates(DefDomNode, IDom))
712 return BestMBB;
713
714 MBB = IDom->getBlock();
715 }
716}
717
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000718void SplitEditor::hoistCopiesForSize() {
719 // Get the complement interval, always RegIdx 0.
Mark Lacey1feb5852013-08-14 23:50:04 +0000720 LiveInterval *LI = &LIS.getInterval(Edit->get(0));
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000721 LiveInterval *Parent = &Edit->getParent();
722
723 // Track the nearest common dominator for all back-copies for each ParentVNI,
724 // indexed by ParentVNI->id.
725 typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
726 SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
727
728 // Find the nearest common dominator for parent values with multiple
729 // back-copies. If a single back-copy dominates, put it in DomPair.second.
730 for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
731 VI != VE; ++VI) {
732 VNInfo *VNI = *VI;
Jakob Stoklund Olesen1599a642012-08-03 20:59:29 +0000733 if (VNI->isUnused())
734 continue;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000735 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
736 assert(ParentVNI && "Parent not live at complement def");
737
738 // Don't hoist remats. The complement is probably going to disappear
739 // completely anyway.
740 if (Edit->didRematerialize(ParentVNI))
741 continue;
742
743 MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
744 DomPair &Dom = NearestDom[ParentVNI->id];
745
746 // Keep directly defined parent values. This is either a PHI or an
747 // instruction in the complement range. All other copies of ParentVNI
748 // should be eliminated.
749 if (VNI->def == ParentVNI->def) {
750 DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
751 Dom = DomPair(ValMBB, VNI->def);
752 continue;
753 }
754 // Skip the singly mapped values. There is nothing to gain from hoisting a
755 // single back-copy.
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000756 if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000757 DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
758 continue;
759 }
760
761 if (!Dom.first) {
762 // First time we see ParentVNI. VNI dominates itself.
763 Dom = DomPair(ValMBB, VNI->def);
764 } else if (Dom.first == ValMBB) {
765 // Two defs in the same block. Pick the earlier def.
766 if (!Dom.second.isValid() || VNI->def < Dom.second)
767 Dom.second = VNI->def;
768 } else {
769 // Different basic blocks. Check if one dominates.
770 MachineBasicBlock *Near =
771 MDT.findNearestCommonDominator(Dom.first, ValMBB);
772 if (Near == ValMBB)
773 // Def ValMBB dominates.
774 Dom = DomPair(ValMBB, VNI->def);
775 else if (Near != Dom.first)
776 // None dominate. Hoist to common dominator, need new def.
777 Dom = DomPair(Near, SlotIndex());
778 }
779
780 DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
781 << " for parent " << ParentVNI->id << '@' << ParentVNI->def
782 << " hoist to BB#" << Dom.first->getNumber() << ' '
783 << Dom.second << '\n');
784 }
785
786 // Insert the hoisted copies.
787 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
788 DomPair &Dom = NearestDom[i];
789 if (!Dom.first || Dom.second.isValid())
790 continue;
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000791 // This value needs a hoisted copy inserted at the end of Dom.first.
792 VNInfo *ParentVNI = Parent->getValNumInfo(i);
793 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
794 // Get a less loopy dominator than Dom.first.
795 Dom.first = findShallowDominator(Dom.first, DefMBB);
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000796 SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
797 Dom.second =
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000798 defFromParent(0, ParentVNI, Last, *Dom.first,
Jakob Stoklund Olesen74c4f972012-01-11 02:07:00 +0000799 SA.getLastSplitPointIter(Dom.first))->def;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000800 }
801
802 // Remove redundant back-copies that are now known to be dominated by another
803 // def with the same value.
804 SmallVector<VNInfo*, 8> BackCopies;
805 for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
806 VI != VE; ++VI) {
807 VNInfo *VNI = *VI;
Jakob Stoklund Olesen1599a642012-08-03 20:59:29 +0000808 if (VNI->isUnused())
809 continue;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000810 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
811 const DomPair &Dom = NearestDom[ParentVNI->id];
812 if (!Dom.first || Dom.second == VNI->def)
813 continue;
814 BackCopies.push_back(VNI);
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000815 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000816 }
817 removeBackCopies(BackCopies);
818}
819
820
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000821/// transferValues - Transfer all possible values to the new live ranges.
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000822/// Values that were rematerialized are left alone, they need LRCalc.extend().
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000823bool SplitEditor::transferValues() {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000824 bool Skipped = false;
825 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000826 for (LiveInterval::const_iterator ParentI = Edit->getParent().begin(),
827 ParentE = Edit->getParent().end(); ParentI != ParentE; ++ParentI) {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000828 DEBUG(dbgs() << " blit " << *ParentI << ':');
829 VNInfo *ParentVNI = ParentI->valno;
830 // RegAssign has holes where RegIdx 0 should be used.
831 SlotIndex Start = ParentI->start;
832 AssignI.advanceTo(Start);
833 do {
834 unsigned RegIdx;
835 SlotIndex End = ParentI->end;
836 if (!AssignI.valid()) {
837 RegIdx = 0;
838 } else if (AssignI.start() <= Start) {
839 RegIdx = AssignI.value();
840 if (AssignI.stop() < End) {
841 End = AssignI.stop();
842 ++AssignI;
843 }
844 } else {
845 RegIdx = 0;
846 End = std::min(End, AssignI.start());
847 }
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000848
849 // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000850 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
Matthias Braune25dde52013-10-10 21:28:57 +0000851 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000852
853 // Check for a simply defined value that can be blitted directly.
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000854 ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
855 if (VNInfo *VNI = VFP.getPointer()) {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000856 DEBUG(dbgs() << ':' << VNI->id);
Matthias Braune25dde52013-10-10 21:28:57 +0000857 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000858 Start = End;
859 continue;
860 }
861
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000862 // Skip values with forced recomputation.
863 if (VFP.getInt()) {
864 DEBUG(dbgs() << "(recalc)");
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000865 Skipped = true;
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000866 Start = End;
867 continue;
868 }
869
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000870 LiveRangeCalc &LRC = getLRCalc(RegIdx);
871
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000872 // This value has multiple defs in RegIdx, but it wasn't rematerialized,
873 // so the live range is accurate. Add live-in blocks in [Start;End) to the
874 // LiveInBlocks.
875 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
876 SlotIndex BlockStart, BlockEnd;
Stephen Hines36b56882014-04-23 16:57:46 -0700877 std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(MBB);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000878
879 // The first block may be live-in, or it may have its own def.
880 if (Start != BlockStart) {
Matthias Braune25dde52013-10-10 21:28:57 +0000881 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000882 assert(VNI && "Missing def for complex mapped value");
883 DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
884 // MBB has its own def. Is it also live-out?
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000885 if (BlockEnd <= End)
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000886 LRC.setLiveOutValue(MBB, VNI);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000887
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000888 // Skip to the next block for live-in.
889 ++MBB;
890 BlockStart = BlockEnd;
891 }
892
893 // Handle the live-in blocks covered by [Start;End).
894 assert(Start <= BlockStart && "Expected live-in block");
895 while (BlockStart < End) {
896 DEBUG(dbgs() << ">BB#" << MBB->getNumber());
897 BlockEnd = LIS.getMBBEndIdx(MBB);
898 if (BlockStart == ParentVNI->def) {
899 // This block has the def of a parent PHI, so it isn't live-in.
900 assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
Matthias Braune25dde52013-10-10 21:28:57 +0000901 VNInfo *VNI = LR.extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000902 assert(VNI && "Missing def for complex mapped parent PHI");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000903 if (End >= BlockEnd)
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000904 LRC.setLiveOutValue(MBB, VNI); // Live-out as well.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000905 } else {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000906 // This block needs a live-in value. The last block covered may not
907 // be live-out.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000908 if (End < BlockEnd)
Matthias Braune25dde52013-10-10 21:28:57 +0000909 LRC.addLiveInBlock(LR, MDT[MBB], End);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000910 else {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000911 // Live-through, and we don't know the value.
Matthias Braune25dde52013-10-10 21:28:57 +0000912 LRC.addLiveInBlock(LR, MDT[MBB]);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700913 LRC.setLiveOutValue(MBB, nullptr);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000914 }
915 }
916 BlockStart = BlockEnd;
917 ++MBB;
918 }
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000919 Start = End;
920 } while (Start != ParentI->end);
921 DEBUG(dbgs() << '\n');
922 }
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000923
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000924 LRCalc[0].calculateValues();
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000925 if (SpillMode)
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000926 LRCalc[1].calculateValues();
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000927
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000928 return Skipped;
929}
930
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000931void SplitEditor::extendPHIKillRanges() {
932 // Extend live ranges to be live-out for successor PHI values.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000933 for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
934 E = Edit->getParent().vni_end(); I != E; ++I) {
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000935 const VNInfo *PHIVNI = *I;
936 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
937 continue;
938 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
Matthias Braune25dde52013-10-10 21:28:57 +0000939 LiveRange &LR = LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000940 LiveRangeCalc &LRC = getLRCalc(RegIdx);
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000941 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
942 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
943 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000944 SlotIndex End = LIS.getMBBEndIdx(*PI);
945 SlotIndex LastUse = End.getPrevSlot();
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000946 // The predecessor may not have a live-out value. That is OK, like an
947 // undef PHI operand.
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000948 if (Edit->getParent().liveAt(LastUse)) {
949 assert(RegAssign.lookup(LastUse) == RegIdx &&
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000950 "Different register assignment in phi predecessor");
Matthias Braune25dde52013-10-10 21:28:57 +0000951 LRC.extend(LR, End);
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000952 }
953 }
954 }
955}
956
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000957/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000958void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000959 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000960 RE = MRI.reg_end(); RI != RE;) {
Stephen Hines36b56882014-04-23 16:57:46 -0700961 MachineOperand &MO = *RI;
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000962 MachineInstr *MI = MO.getParent();
963 ++RI;
Eric Christopher0f438112011-02-03 06:18:29 +0000964 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000965 if (MI->isDebugValue()) {
966 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000967 MO.setReg(0);
968 continue;
969 }
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +0000970
Jakob Stoklund Olesenb09701d2011-07-24 20:23:50 +0000971 // <undef> operands don't really read the register, so it doesn't matter
972 // which register we choose. When the use operand is tied to a def, we must
973 // use the same register as the def, so just do that always.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000974 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesenb09701d2011-07-24 20:23:50 +0000975 if (MO.isDef() || MO.isUndef())
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000976 Idx = Idx.getRegSlot(MO.isEarlyClobber());
Eric Christopher0f438112011-02-03 06:18:29 +0000977
978 // Rewrite to the mapped register at Idx.
979 unsigned RegIdx = RegAssign.lookup(Idx);
Mark Lacey1feb5852013-08-14 23:50:04 +0000980 LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000981 MO.setReg(LI->reg);
Eric Christopher0f438112011-02-03 06:18:29 +0000982 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
983 << Idx << ':' << RegIdx << '\t' << *MI);
984
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +0000985 // Extend liveness to Idx if the instruction reads reg.
Jakob Stoklund Olesen81d686e2011-07-24 20:33:23 +0000986 if (!ExtendRanges || MO.isUndef())
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +0000987 continue;
988
989 // Skip instructions that don't read Reg.
990 if (MO.isDef()) {
991 if (!MO.getSubReg() && !MO.isEarlyClobber())
992 continue;
993 // We may wan't to extend a live range for a partial redef, or for a use
994 // tied to an early clobber.
995 Idx = Idx.getPrevSlot();
996 if (!Edit->getParent().liveAt(Idx))
997 continue;
998 } else
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000999 Idx = Idx.getRegSlot(true);
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +00001000
Matthias Braune25dde52013-10-10 21:28:57 +00001001 getLRCalc(RegIdx).extend(*LI, Idx.getNextSlot());
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001002 }
1003}
1004
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001005void SplitEditor::deleteRematVictims() {
1006 SmallVector<MachineInstr*, 8> Dead;
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001007 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
Mark Lacey1feb5852013-08-14 23:50:04 +00001008 LiveInterval *LI = &LIS.getInterval(*I);
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001009 for (LiveInterval::const_iterator LII = LI->begin(), LIE = LI->end();
1010 LII != LIE; ++LII) {
Jakob Stoklund Olesen1f81e312011-11-13 22:42:13 +00001011 // Dead defs end at the dead slot.
1012 if (LII->end != LII->valno->def.getDeadSlot())
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001013 continue;
1014 MachineInstr *MI = LIS.getInstructionFromIndex(LII->valno->def);
1015 assert(MI && "Missing instruction for dead def");
1016 MI->addRegisterDead(LI->reg, &TRI);
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001017
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001018 if (!MI->allDefsAreDead())
1019 continue;
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001020
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001021 DEBUG(dbgs() << "All defs dead: " << *MI);
1022 Dead.push_back(MI);
1023 }
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001024 }
1025
1026 if (Dead.empty())
1027 return;
1028
Pete Cooper8a06af92012-04-02 22:22:53 +00001029 Edit->eliminateDeadDefs(Dead);
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001030}
1031
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001032void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001033 ++NumFinished;
Eric Christopher463a2972011-02-03 05:40:54 +00001034
Eric Christopher0f438112011-02-03 06:18:29 +00001035 // At this point, the live intervals in Edit contain VNInfos corresponding to
1036 // the inserted copies.
1037
1038 // Add the original defs from the parent interval.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001039 for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
1040 E = Edit->getParent().vni_end(); I != E; ++I) {
Eric Christopher0f438112011-02-03 06:18:29 +00001041 const VNInfo *ParentVNI = *I;
Jakob Stoklund Olesen9ecd1e72011-02-04 00:59:23 +00001042 if (ParentVNI->isUnused())
1043 continue;
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +00001044 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Jakob Stoklund Olesenb18d7792012-07-27 21:11:14 +00001045 defValue(RegIdx, ParentVNI, ParentVNI->def);
Jakob Stoklund Olesen29ef8752011-03-15 21:13:22 +00001046
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +00001047 // Force rematted values to be recomputed everywhere.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001048 // The new live ranges may be truncated.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001049 if (Edit->didRematerialize(ParentVNI))
1050 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +00001051 forceRecompute(i, ParentVNI);
Eric Christopher0f438112011-02-03 06:18:29 +00001052 }
1053
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001054 // Hoist back-copies to the complement interval when in spill mode.
1055 switch (SpillMode) {
1056 case SM_Partition:
1057 // Leave all back-copies as is.
1058 break;
1059 case SM_Size:
1060 hoistCopiesForSize();
1061 break;
1062 case SM_Speed:
1063 llvm_unreachable("Spill mode 'speed' not implemented yet");
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001064 }
1065
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001066 // Transfer the simply mapped values, check if any are skipped.
1067 bool Skipped = transferValues();
1068 if (Skipped)
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001069 extendPHIKillRanges();
1070 else
1071 ++NumSimple;
Eric Christopher0f438112011-02-03 06:18:29 +00001072
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001073 // Rewrite virtual registers, possibly extending ranges.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001074 rewriteAssigned(Skipped);
Eric Christopher0f438112011-02-03 06:18:29 +00001075
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001076 // Delete defs that were rematted everywhere.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001077 if (Skipped)
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001078 deleteRematVictims();
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001079
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001080 // Get rid of unused values and set phi-kill flags.
Mark Lacey1feb5852013-08-14 23:50:04 +00001081 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I) {
1082 LiveInterval &LI = LIS.getInterval(*I);
1083 LI.RenumberValues();
1084 }
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001085
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001086 // Provide a reverse mapping from original indices to Edit ranges.
1087 if (LRMap) {
1088 LRMap->clear();
1089 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1090 LRMap->push_back(i);
1091 }
1092
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001093 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001094 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001095 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001096 // Don't use iterators, they are invalidated by create() below.
Mark Lacey1feb5852013-08-14 23:50:04 +00001097 LiveInterval *li = &LIS.getInterval(Edit->get(i));
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001098 unsigned NumComp = ConEQ.Classify(li);
1099 if (NumComp <= 1)
1100 continue;
1101 DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n');
1102 SmallVector<LiveInterval*, 8> dups;
1103 dups.push_back(li);
Matt Beaumont-Gayae5fbee2011-04-21 19:46:23 +00001104 for (unsigned j = 1; j != NumComp; ++j)
Mark Laceye742d682013-08-14 23:50:16 +00001105 dups.push_back(&Edit->createEmptyInterval());
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +00001106 ConEQ.Distribute(&dups[0], MRI);
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001107 // The new intervals all map back to i.
1108 if (LRMap)
1109 LRMap->resize(Edit->size(), i);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001110 }
1111
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +00001112 // Calculate spill weight and allocation hints for new intervals.
Benjamin Kramer4eed7562013-06-17 19:00:36 +00001113 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001114
1115 assert(!LRMap || LRMap->size() == Edit->size());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001116}
1117
1118
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +00001119//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001120// Single Block Splitting
1121//===----------------------------------------------------------------------===//
1122
Jakob Stoklund Olesen2d6d86b2011-08-05 22:20:45 +00001123bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
1124 bool SingleInstrs) const {
1125 // Always split for multiple instructions.
1126 if (!BI.isOneInstr())
1127 return true;
1128 // Don't split for single instructions unless explicitly requested.
1129 if (!SingleInstrs)
1130 return false;
1131 // Splitting a live-through range always makes progress.
1132 if (BI.LiveIn && BI.LiveOut)
1133 return true;
1134 // No point in isolating a copy. It has no register class constraints.
1135 if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
1136 return false;
1137 // Finally, don't isolate an end point that was created by earlier splits.
1138 return isOriginalEndpoint(BI.FirstInstr);
1139}
1140
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001141void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1142 openIntv();
1143 SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001144 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001145 LastSplitPoint));
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001146 if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
1147 useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001148 } else {
1149 // The last use is after the last valid split point.
1150 SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1151 useIntv(SegStart, SegStop);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001152 overlapIntv(SegStop, BI.LastInstr);
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001153 }
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001154}
1155
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001156
1157//===----------------------------------------------------------------------===//
1158// Global Live Range Splitting Support
1159//===----------------------------------------------------------------------===//
1160
1161// These methods support a method of global live range splitting that uses a
1162// global algorithm to decide intervals for CFG edges. They will insert split
1163// points and color intervals in basic blocks while avoiding interference.
1164//
1165// Note that splitSingleBlock is also useful for blocks where both CFG edges
1166// are on the stack.
1167
1168void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1169 unsigned IntvIn, SlotIndex LeaveBefore,
1170 unsigned IntvOut, SlotIndex EnterAfter){
1171 SlotIndex Start, Stop;
Stephen Hines36b56882014-04-23 16:57:46 -07001172 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001173
1174 DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
1175 << ") intf " << LeaveBefore << '-' << EnterAfter
1176 << ", live-through " << IntvIn << " -> " << IntvOut);
1177
1178 assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1179
Jakob Stoklund Olesenfe9b2d12011-07-23 03:32:26 +00001180 assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
1181 assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
1182 assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
1183
1184 MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
1185
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001186 if (!IntvOut) {
1187 DEBUG(dbgs() << ", spill on entry.\n");
1188 //
1189 // <<<<<<<<< Possible LeaveBefore interference.
1190 // |-----------| Live through.
1191 // -____________ Spill on entry.
1192 //
1193 selectIntv(IntvIn);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001194 SlotIndex Idx = leaveIntvAtTop(*MBB);
1195 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1196 (void)Idx;
1197 return;
1198 }
1199
1200 if (!IntvIn) {
1201 DEBUG(dbgs() << ", reload on exit.\n");
1202 //
1203 // >>>>>>> Possible EnterAfter interference.
1204 // |-----------| Live through.
1205 // ___________-- Reload on exit.
1206 //
1207 selectIntv(IntvOut);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001208 SlotIndex Idx = enterIntvAtEnd(*MBB);
1209 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1210 (void)Idx;
1211 return;
1212 }
1213
1214 if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1215 DEBUG(dbgs() << ", straight through.\n");
1216 //
1217 // |-----------| Live through.
1218 // ------------- Straight through, same intv, no interference.
1219 //
1220 selectIntv(IntvOut);
1221 useIntv(Start, Stop);
1222 return;
1223 }
1224
1225 // We cannot legally insert splits after LSP.
1226 SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
Jakob Stoklund Olesenfe9b2d12011-07-23 03:32:26 +00001227 assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001228
1229 if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1230 LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1231 DEBUG(dbgs() << ", switch avoiding interference.\n");
1232 //
1233 // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
1234 // |-----------| Live through.
1235 // ------======= Switch intervals between interference.
1236 //
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001237 selectIntv(IntvOut);
Jakob Stoklund Olesenfe9b2d12011-07-23 03:32:26 +00001238 SlotIndex Idx;
1239 if (LeaveBefore && LeaveBefore < LSP) {
1240 Idx = enterIntvBefore(LeaveBefore);
1241 useIntv(Idx, Stop);
1242 } else {
1243 Idx = enterIntvAtEnd(*MBB);
1244 }
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001245 selectIntv(IntvIn);
1246 useIntv(Start, Idx);
1247 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1248 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1249 return;
1250 }
1251
1252 DEBUG(dbgs() << ", create local intv for interference.\n");
1253 //
1254 // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
1255 // |-----------| Live through.
1256 // ==---------== Switch intervals before/after interference.
1257 //
1258 assert(LeaveBefore <= EnterAfter && "Missed case");
1259
1260 selectIntv(IntvOut);
1261 SlotIndex Idx = enterIntvAfter(EnterAfter);
1262 useIntv(Idx, Stop);
1263 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1264
1265 selectIntv(IntvIn);
1266 Idx = leaveIntvBefore(LeaveBefore);
1267 useIntv(Start, Idx);
1268 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1269}
1270
1271
1272void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1273 unsigned IntvIn, SlotIndex LeaveBefore) {
1274 SlotIndex Start, Stop;
Stephen Hines36b56882014-04-23 16:57:46 -07001275 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001276
1277 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001278 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001279 << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
1280 << (BI.LiveOut ? ", stack-out" : ", killed in block"));
1281
1282 assert(IntvIn && "Must have register in");
1283 assert(BI.LiveIn && "Must be live-in");
1284 assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1285
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001286 if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001287 DEBUG(dbgs() << " before interference.\n");
1288 //
1289 // <<< Interference after kill.
1290 // |---o---x | Killed in block.
1291 // ========= Use IntvIn everywhere.
1292 //
1293 selectIntv(IntvIn);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001294 useIntv(Start, BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001295 return;
1296 }
1297
1298 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1299
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001300 if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001301 //
1302 // <<< Possible interference after last use.
1303 // |---o---o---| Live-out on stack.
1304 // =========____ Leave IntvIn after last use.
1305 //
1306 // < Interference after last use.
1307 // |---o---o--o| Live-out on stack, late last use.
1308 // ============ Copy to stack after LSP, overlap IntvIn.
1309 // \_____ Stack interval is live-out.
1310 //
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001311 if (BI.LastInstr < LSP) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001312 DEBUG(dbgs() << ", spill after last use before interference.\n");
1313 selectIntv(IntvIn);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001314 SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001315 useIntv(Start, Idx);
1316 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1317 } else {
1318 DEBUG(dbgs() << ", spill before last split point.\n");
1319 selectIntv(IntvIn);
Jakob Stoklund Olesenaf4e40c2011-07-16 00:13:30 +00001320 SlotIndex Idx = leaveIntvBefore(LSP);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001321 overlapIntv(Idx, BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001322 useIntv(Start, Idx);
1323 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1324 }
1325 return;
1326 }
1327
1328 // The interference is overlapping somewhere we wanted to use IntvIn. That
1329 // means we need to create a local interval that can be allocated a
1330 // different register.
1331 unsigned LocalIntv = openIntv();
Matt Beaumont-Gayf9d7fb62011-07-16 04:18:47 +00001332 (void)LocalIntv;
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001333 DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
1334
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001335 if (!BI.LiveOut || BI.LastInstr < LSP) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001336 //
1337 // <<<<<<< Interference overlapping uses.
1338 // |---o---o---| Live-out on stack.
1339 // =====----____ Leave IntvIn before interference, then spill.
1340 //
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001341 SlotIndex To = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001342 SlotIndex From = enterIntvBefore(LeaveBefore);
1343 useIntv(From, To);
1344 selectIntv(IntvIn);
1345 useIntv(Start, From);
1346 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1347 return;
1348 }
1349
1350 // <<<<<<< Interference overlapping uses.
1351 // |---o---o--o| Live-out on stack, late last use.
1352 // =====------- Copy to stack before LSP, overlap LocalIntv.
1353 // \_____ Stack interval is live-out.
1354 //
1355 SlotIndex To = leaveIntvBefore(LSP);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001356 overlapIntv(To, BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001357 SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1358 useIntv(From, To);
1359 selectIntv(IntvIn);
1360 useIntv(Start, From);
1361 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1362}
1363
1364void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1365 unsigned IntvOut, SlotIndex EnterAfter) {
1366 SlotIndex Start, Stop;
Stephen Hines36b56882014-04-23 16:57:46 -07001367 std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001368
1369 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001370 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001371 << ", reg-out " << IntvOut << ", enter after " << EnterAfter
1372 << (BI.LiveIn ? ", stack-in" : ", defined in block"));
1373
1374 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1375
1376 assert(IntvOut && "Must have register out");
1377 assert(BI.LiveOut && "Must be live-out");
1378 assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1379
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001380 if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001381 DEBUG(dbgs() << " after interference.\n");
1382 //
1383 // >>>> Interference before def.
1384 // | o---o---| Defined in block.
1385 // ========= Use IntvOut everywhere.
1386 //
1387 selectIntv(IntvOut);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001388 useIntv(BI.FirstInstr, Stop);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001389 return;
1390 }
1391
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001392 if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001393 DEBUG(dbgs() << ", reload after interference.\n");
1394 //
1395 // >>>> Interference before def.
1396 // |---o---o---| Live-through, stack-in.
1397 // ____========= Enter IntvOut before first use.
1398 //
1399 selectIntv(IntvOut);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001400 SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001401 useIntv(Idx, Stop);
1402 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1403 return;
1404 }
1405
1406 // The interference is overlapping somewhere we wanted to use IntvOut. That
1407 // means we need to create a local interval that can be allocated a
1408 // different register.
1409 DEBUG(dbgs() << ", interference overlaps uses.\n");
1410 //
1411 // >>>>>>> Interference overlapping uses.
1412 // |---o---o---| Live-through, stack-in.
1413 // ____---====== Create local interval for interference range.
1414 //
1415 selectIntv(IntvOut);
1416 SlotIndex Idx = enterIntvAfter(EnterAfter);
1417 useIntv(Idx, Stop);
1418 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1419
1420 openIntv();
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001421 SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001422 useIntv(From, Idx);
1423}