blob: dca15ee7580f1b84562b41e7afea19693a27d1ef [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 Olesen376dcbd2010-11-03 20:39:23 +000015#define DEBUG_TYPE "regalloc"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000016#include "SplitKit.h"
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000017#include "VirtRegMap.h"
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000018#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper789d5d82012-04-02 22:44:18 +000020#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +000021#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +000023#include "llvm/CodeGen/MachineLoopInfo.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000027#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000029
30using namespace llvm;
31
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000032STATISTIC(NumFinished, "Number of splits finished");
33STATISTIC(NumSimple, "Number of splits that were simple");
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000034STATISTIC(NumCopies, "Number of copies inserted for splitting");
35STATISTIC(NumRemats, "Number of rematerialized defs for splitting");
Jakob Stoklund Olesenbdda37d2011-05-10 17:37:41 +000036STATISTIC(NumRepairs, "Number of invalid live ranges repaired");
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000037
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000038//===----------------------------------------------------------------------===//
39// Split Analysis
40//===----------------------------------------------------------------------===//
41
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000042SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm,
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000043 const LiveIntervals &lis,
44 const MachineLoopInfo &mli)
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000045 : MF(vrm.getMachineFunction()),
46 VRM(vrm),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000047 LIS(lis),
48 Loops(mli),
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000049 TII(*MF.getTarget().getInstrInfo()),
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000050 CurLI(0),
51 LastSplitPoint(MF.getNumBlockIDs()) {}
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000052
53void SplitAnalysis::clear() {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000054 UseSlots.clear();
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +000055 UseBlocks.clear();
56 ThroughBlocks.clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000057 CurLI = 0;
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +000058 DidRepairRange = false;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000059}
60
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000061SlotIndex SplitAnalysis::computeLastSplitPoint(unsigned Num) {
62 const MachineBasicBlock *MBB = MF.getBlockNumbered(Num);
63 const MachineBasicBlock *LPad = MBB->getLandingPadSuccessor();
64 std::pair<SlotIndex, SlotIndex> &LSP = LastSplitPoint[Num];
Jakob Stoklund Olesen2aad2f62012-01-11 02:07:05 +000065 SlotIndex MBBEnd = LIS.getMBBEndIdx(MBB);
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000066
67 // Compute split points on the first call. The pair is independent of the
68 // current live interval.
69 if (!LSP.first.isValid()) {
70 MachineBasicBlock::const_iterator FirstTerm = MBB->getFirstTerminator();
71 if (FirstTerm == MBB->end())
Jakob Stoklund Olesen2aad2f62012-01-11 02:07:05 +000072 LSP.first = MBBEnd;
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000073 else
74 LSP.first = LIS.getInstructionIndex(FirstTerm);
75
76 // If there is a landing pad successor, also find the call instruction.
77 if (!LPad)
78 return LSP.first;
79 // There may not be a call instruction (?) in which case we ignore LPad.
80 LSP.second = LSP.first;
Jakob Stoklund Olesen1e0bd632011-06-28 01:18:58 +000081 for (MachineBasicBlock::const_iterator I = MBB->end(), E = MBB->begin();
82 I != E;) {
83 --I;
Evan Cheng5a96b3d2011-12-07 07:15:52 +000084 if (I->isCall()) {
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000085 LSP.second = LIS.getInstructionIndex(I);
86 break;
87 }
Jakob Stoklund Olesen1e0bd632011-06-28 01:18:58 +000088 }
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000089 }
90
91 // If CurLI is live into a landing pad successor, move the last split point
92 // back to the call that may throw.
Jakob Stoklund Olesen2aad2f62012-01-11 02:07:05 +000093 if (!LPad || !LSP.second || !LIS.isLiveInToMBB(*CurLI, LPad))
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000094 return LSP.first;
Jakob Stoklund Olesen2aad2f62012-01-11 02:07:05 +000095
96 // Find the value leaving MBB.
97 const VNInfo *VNI = CurLI->getVNInfoBefore(MBBEnd);
98 if (!VNI)
99 return LSP.first;
100
101 // If the value leaving MBB was defined after the call in MBB, it can't
102 // really be live-in to the landing pad. This can happen if the landing pad
103 // has a PHI, and this register is undef on the exceptional edge.
104 // <rdar://problem/10664933>
105 if (!SlotIndex::isEarlierInstr(VNI->def, LSP.second) && VNI->def < MBBEnd)
106 return LSP.first;
107
108 // Value is properly live-in to the landing pad.
109 // Only allow splits before the call.
110 return LSP.second;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000111}
112
Jakob Stoklund Olesen74c4f972012-01-11 02:07:00 +0000113MachineBasicBlock::iterator
114SplitAnalysis::getLastSplitPointIter(MachineBasicBlock *MBB) {
115 SlotIndex LSP = getLastSplitPoint(MBB->getNumber());
116 if (LSP == LIS.getMBBEndIdx(MBB))
117 return MBB->end();
118 return LIS.getInstructionFromIndex(LSP);
119}
120
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000121/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000122void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000123 assert(UseSlots.empty() && "Call clear first");
124
125 // First get all the defs from the interval values. This provides the correct
126 // slots for early clobbers.
127 for (LiveInterval::const_vni_iterator I = CurLI->vni_begin(),
128 E = CurLI->vni_end(); I != E; ++I)
129 if (!(*I)->isPHIDef() && !(*I)->isUnused())
130 UseSlots.push_back((*I)->def);
131
132 // Get use slots form the use-def chain.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000133 const MachineRegisterInfo &MRI = MF.getRegInfo();
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000134 for (MachineRegisterInfo::use_nodbg_iterator
135 I = MRI.use_nodbg_begin(CurLI->reg), E = MRI.use_nodbg_end(); I != E;
136 ++I)
137 if (!I.getOperand().isUndef())
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000138 UseSlots.push_back(LIS.getInstructionIndex(&*I).getRegSlot());
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000139
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000140 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000141
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000142 // Remove duplicates, keeping the smaller slot for each instruction.
143 // That is what we want for early clobbers.
144 UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
145 SlotIndex::isSameInstr),
146 UseSlots.end());
147
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000148 // Compute per-live block info.
149 if (!calcLiveBlockInfo()) {
150 // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
Rafael Espindola5b220212011-06-26 22:34:10 +0000151 // I am looking at you, RegisterCoalescer!
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +0000152 DidRepairRange = true;
Jakob Stoklund Olesenbdda37d2011-05-10 17:37:41 +0000153 ++NumRepairs;
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000154 DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
155 const_cast<LiveIntervals&>(LIS)
156 .shrinkToUses(const_cast<LiveInterval*>(CurLI));
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000157 UseBlocks.clear();
158 ThroughBlocks.clear();
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000159 bool fixed = calcLiveBlockInfo();
160 (void)fixed;
161 assert(fixed && "Couldn't fix broken live interval");
162 }
163
Jakob Stoklund Olesenef1f5cc2011-03-27 22:49:23 +0000164 DEBUG(dbgs() << "Analyze counted "
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000165 << UseSlots.size() << " instrs in "
166 << UseBlocks.size() << " blocks, through "
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000167 << NumThroughBlocks << " blocks.\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000168}
169
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000170/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
171/// where CurLI is live.
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000172bool SplitAnalysis::calcLiveBlockInfo() {
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000173 ThroughBlocks.resize(MF.getNumBlockIDs());
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000174 NumThroughBlocks = NumGapBlocks = 0;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000175 if (CurLI->empty())
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000176 return true;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000177
178 LiveInterval::const_iterator LVI = CurLI->begin();
179 LiveInterval::const_iterator LVE = CurLI->end();
180
181 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
182 UseI = UseSlots.begin();
183 UseE = UseSlots.end();
184
185 // Loop over basic blocks where CurLI is live.
186 MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start);
187 for (;;) {
188 BlockInfo BI;
189 BI.MBB = MFI;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000190 SlotIndex Start, Stop;
191 tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000192
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000193 // If the block contains no uses, the range must be live through. At one
Rafael Espindola5b220212011-06-26 22:34:10 +0000194 // point, RegisterCoalescer could create dangling ranges that ended
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000195 // mid-block.
196 if (UseI == UseE || *UseI >= Stop) {
197 ++NumThroughBlocks;
198 ThroughBlocks.set(BI.MBB->getNumber());
199 // The range shouldn't end mid-block if there are no uses. This shouldn't
200 // happen.
201 if (LVI->end < Stop)
202 return false;
203 } else {
204 // This block has uses. Find the first and last uses in the block.
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000205 BI.FirstInstr = *UseI;
206 assert(BI.FirstInstr >= Start);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000207 do ++UseI;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000208 while (UseI != UseE && *UseI < Stop);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000209 BI.LastInstr = UseI[-1];
210 assert(BI.LastInstr < Stop);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000211
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000212 // LVI is the first live segment overlapping MBB.
213 BI.LiveIn = LVI->start <= Start;
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000214
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000215 // When not live in, the first use should be a def.
216 if (!BI.LiveIn) {
217 assert(LVI->start == LVI->valno->def && "Dangling LiveRange start");
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000218 assert(LVI->start == BI.FirstInstr && "First instr should be a def");
219 BI.FirstDef = BI.FirstInstr;
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000220 }
221
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000222 // Look for gaps in the live range.
223 BI.LiveOut = true;
224 while (LVI->end < Stop) {
225 SlotIndex LastStop = LVI->end;
226 if (++LVI == LVE || LVI->start >= Stop) {
227 BI.LiveOut = false;
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000228 BI.LastInstr = LastStop;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000229 break;
230 }
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000231
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000232 if (LastStop < LVI->start) {
233 // There is a gap in the live range. Create duplicate entries for the
234 // live-in snippet and the live-out snippet.
235 ++NumGapBlocks;
236
237 // Push the Live-in part.
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000238 BI.LiveOut = false;
239 UseBlocks.push_back(BI);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000240 UseBlocks.back().LastInstr = LastStop;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000241
242 // Set up BI for the live-out part.
243 BI.LiveIn = false;
244 BI.LiveOut = true;
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000245 BI.FirstInstr = BI.FirstDef = LVI->start;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000246 }
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +0000247
248 // A LiveRange that starts in the middle of the block must be a def.
249 assert(LVI->start == LVI->valno->def && "Dangling LiveRange start");
250 if (!BI.FirstDef)
251 BI.FirstDef = LVI->start;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000252 }
253
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000254 UseBlocks.push_back(BI);
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000255
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000256 // LVI is now at LVE or LVI->end >= Stop.
257 if (LVI == LVE)
258 break;
259 }
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000260
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000261 // Live segment ends exactly at Stop. Move to the next segment.
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000262 if (LVI->end == Stop && ++LVI == LVE)
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000263 break;
264
265 // Pick the next basic block.
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000266 if (LVI->start < Stop)
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000267 ++MFI;
268 else
269 MFI = LIS.getMBBFromIndex(LVI->start);
270 }
Jakob Stoklund Olesenb2abfa02011-05-28 02:32:57 +0000271
272 assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000273 return true;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000274}
275
Jakob Stoklund Olesen9f4b8932011-04-26 22:33:12 +0000276unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
277 if (cli->empty())
278 return 0;
279 LiveInterval *li = const_cast<LiveInterval*>(cli);
280 LiveInterval::iterator LVI = li->begin();
281 LiveInterval::iterator LVE = li->end();
282 unsigned Count = 0;
283
284 // Loop over basic blocks where li is live.
285 MachineFunction::const_iterator MFI = LIS.getMBBFromIndex(LVI->start);
286 SlotIndex Stop = LIS.getMBBEndIdx(MFI);
287 for (;;) {
288 ++Count;
289 LVI = li->advanceTo(LVI, Stop);
290 if (LVI == LVE)
291 return Count;
292 do {
293 ++MFI;
294 Stop = LIS.getMBBEndIdx(MFI);
295 } while (Stop <= LVI->start);
296 }
297}
298
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000299bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
300 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
301 const LiveInterval &Orig = LIS.getInterval(OrigReg);
302 assert(!Orig.empty() && "Splitting empty interval?");
303 LiveInterval::const_iterator I = Orig.find(Idx);
304
305 // Range containing Idx should begin at Idx.
306 if (I != Orig.end() && I->start <= Idx)
307 return I->start == Idx;
308
309 // Range does not contain Idx, previous must end at Idx.
310 return I != Orig.begin() && (--I)->end == Idx;
311}
312
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000313void SplitAnalysis::analyze(const LiveInterval *li) {
314 clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000315 CurLI = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000316 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000317}
318
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000319
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000320//===----------------------------------------------------------------------===//
321// Split Editor
322//===----------------------------------------------------------------------===//
323
324/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000325SplitEditor::SplitEditor(SplitAnalysis &sa,
326 LiveIntervals &lis,
327 VirtRegMap &vrm,
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000328 MachineDominatorTree &mdt)
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000329 : SA(sa), LIS(lis), VRM(vrm),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000330 MRI(vrm.getMachineFunction().getRegInfo()),
Eric Christopher0f438112011-02-03 06:18:29 +0000331 MDT(mdt),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000332 TII(*vrm.getMachineFunction().getTarget().getInstrInfo()),
333 TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()),
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000334 Edit(0),
Eric Christopher0f438112011-02-03 06:18:29 +0000335 OpenIdx(0),
Jakob Stoklund Olesen708d06f2011-09-12 16:49:21 +0000336 SpillMode(SM_Partition),
Eric Christopher0f438112011-02-03 06:18:29 +0000337 RegAssign(Allocator)
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000338{}
339
Jakob Stoklund Olesen708d06f2011-09-12 16:49:21 +0000340void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
341 Edit = &LRE;
342 SpillMode = SM;
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000343 OpenIdx = 0;
344 RegAssign.clear();
345 Values.clear();
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000346
347 // Reset the LiveRangeCalc instances needed for this spill mode.
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000348 LRCalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
349 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000350 if (SpillMode)
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000351 LRCalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
352 &LIS.getVNInfoAllocator());
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000353
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000354 // We don't need an AliasAnalysis since we will only be performing
355 // cheap-as-a-copy remats anyway.
Pete Cooper8a06af92012-04-02 22:22:53 +0000356 Edit->anyRematerializable(0);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000357}
358
Manman Renb720be62012-09-11 22:23:19 +0000359#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Eric Christopher0f438112011-02-03 06:18:29 +0000360void SplitEditor::dump() const {
361 if (RegAssign.empty()) {
362 dbgs() << " empty\n";
363 return;
364 }
365
366 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
367 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
368 dbgs() << '\n';
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000369}
Manman Ren77e300e2012-09-06 19:06:06 +0000370#endif
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000371
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000372VNInfo *SplitEditor::defValue(unsigned RegIdx,
373 const VNInfo *ParentVNI,
374 SlotIndex Idx) {
375 assert(ParentVNI && "Mapping NULL value");
376 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000377 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
378 LiveInterval *LI = Edit->get(RegIdx);
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000379
380 // Create a new value.
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000381 VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000382
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000383 // Use insert for lookup, so we can add missing values with a second lookup.
384 std::pair<ValueMap::iterator, bool> InsP =
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000385 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id),
386 ValueForcePair(VNI, false)));
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000387
388 // This was the first time (RegIdx, ParentVNI) was mapped.
389 // Keep it as a simple def without any liveness.
390 if (InsP.second)
391 return VNI;
392
393 // If the previous value was a simple mapping, add liveness for it now.
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000394 if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000395 SlotIndex Def = OldVNI->def;
Jakob Stoklund Olesen1f81e312011-11-13 22:42:13 +0000396 LI->addRange(LiveRange(Def, Def.getDeadSlot(), OldVNI));
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000397 // No longer a simple mapping. Switch to a complex, non-forced mapping.
398 InsP.first->second = ValueForcePair();
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000399 }
400
401 // This is a complex mapping, add liveness for VNI
402 SlotIndex Def = VNI->def;
Jakob Stoklund Olesen1f81e312011-11-13 22:42:13 +0000403 LI->addRange(LiveRange(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000404
405 return VNI;
406}
407
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000408void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI) {
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000409 assert(ParentVNI && "Mapping NULL value");
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000410 ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI->id)];
411 VNInfo *VNI = VFP.getPointer();
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000412
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000413 // ParentVNI was either unmapped or already complex mapped. Either way, just
414 // set the force bit.
415 if (!VNI) {
416 VFP.setInt(true);
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000417 return;
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000418 }
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000419
420 // This was previously a single mapping. Make sure the old def is represented
421 // by a trivial live range.
422 SlotIndex Def = VNI->def;
Jakob Stoklund Olesen1f81e312011-11-13 22:42:13 +0000423 Edit->get(RegIdx)->addRange(LiveRange(Def, Def.getDeadSlot(), VNI));
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000424 // Mark as complex mapped, forced.
425 VFP = ValueForcePair(0, true);
Jakob Stoklund Olesene5a2e362011-09-13 18:05:29 +0000426}
427
Eric Christopher0f438112011-02-03 06:18:29 +0000428VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000429 VNInfo *ParentVNI,
430 SlotIndex UseIdx,
431 MachineBasicBlock &MBB,
432 MachineBasicBlock::iterator I) {
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000433 MachineInstr *CopyMI = 0;
434 SlotIndex Def;
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000435 LiveInterval *LI = Edit->get(RegIdx);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000436
Jakob Stoklund Olesenbb30dd42011-05-02 05:29:58 +0000437 // We may be trying to avoid interference that ends at a deleted instruction,
438 // so always begin RegIdx 0 early and all others late.
439 bool Late = RegIdx != 0;
440
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000441 // Attempt cheap-as-a-copy rematerialization.
442 LiveRangeEdit::Remat RM(ParentVNI);
Pete Cooper8a06af92012-04-02 22:22:53 +0000443 if (Edit->canRematerializeAt(RM, UseIdx, true)) {
444 Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, TRI, Late);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000445 ++NumRemats;
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000446 } else {
447 // Can't remat, just insert a copy from parent.
Eric Christopher0f438112011-02-03 06:18:29 +0000448 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000449 .addReg(Edit->getReg());
Jakob Stoklund Olesenbb30dd42011-05-02 05:29:58 +0000450 Def = LIS.getSlotIndexes()->insertMachineInstrInMaps(CopyMI, Late)
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000451 .getRegSlot();
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000452 ++NumCopies;
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000453 }
454
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000455 // Define the value in Reg.
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000456 return defValue(RegIdx, ParentVNI, Def);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000457}
458
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000459/// Create a new virtual register and live interval.
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000460unsigned SplitEditor::openIntv() {
Eric Christopher0f438112011-02-03 06:18:29 +0000461 // Create the complement as index 0.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000462 if (Edit->empty())
Pete Cooper8a06af92012-04-02 22:22:53 +0000463 Edit->create();
Eric Christopher0f438112011-02-03 06:18:29 +0000464
465 // Create the open interval.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000466 OpenIdx = Edit->size();
Pete Cooper8a06af92012-04-02 22:22:53 +0000467 Edit->create();
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000468 return OpenIdx;
469}
470
471void SplitEditor::selectIntv(unsigned Idx) {
472 assert(Idx != 0 && "Cannot select the complement interval");
473 assert(Idx < Edit->size() && "Can only select previously opened interval");
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000474 DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000475 OpenIdx = Idx;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000476}
477
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000478SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000479 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000480 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000481 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000482 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000483 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000484 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000485 return Idx;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000486 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000487 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000488 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000489 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000490
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000491 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
492 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000493}
494
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000495SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
496 assert(OpenIdx && "openIntv not called before enterIntvAfter");
497 DEBUG(dbgs() << " enterIntvAfter " << Idx);
498 Idx = Idx.getBoundaryIndex();
499 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
500 if (!ParentVNI) {
501 DEBUG(dbgs() << ": not live\n");
502 return Idx;
503 }
504 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
505 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
506 assert(MI && "enterIntvAfter called with invalid index");
507
508 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
509 llvm::next(MachineBasicBlock::iterator(MI)));
510 return VNI->def;
511}
512
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000513SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000514 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000515 SlotIndex End = LIS.getMBBEndIdx(&MBB);
516 SlotIndex Last = End.getPrevSlot();
517 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000518 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000519 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000520 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000521 return End;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000522 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000523 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000524 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesen74c4f972012-01-11 02:07:00 +0000525 SA.getLastSplitPointIter(&MBB));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000526 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopher0f438112011-02-03 06:18:29 +0000527 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000528 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000529}
530
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000531/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000532void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000533 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000534}
535
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000536void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopher0f438112011-02-03 06:18:29 +0000537 assert(OpenIdx && "openIntv not called before useIntv");
538 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
539 RegAssign.insert(Start, End, OpenIdx);
540 DEBUG(dump());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000541}
542
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000543SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000544 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000545 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000546
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000547 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000548 SlotIndex Boundary = Idx.getBoundaryIndex();
549 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000550 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000551 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000552 return Boundary.getNextSlot();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000553 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000554 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000555 MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
Jakob Stoklund Olesen01cb34b2011-02-08 18:50:18 +0000556 assert(MI && "No instruction at index");
Jakob Stoklund Olesenebac0c12011-09-16 00:03:35 +0000557
558 // In spill mode, make live ranges as short as possible by inserting the copy
559 // before MI. This is only possible if that instruction doesn't redefine the
560 // value. The inserted COPY is not a kill, and we don't need to recompute
561 // the source live range. The spiller also won't try to hoist this copy.
562 if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
563 MI->readsVirtualRegister(Edit->getReg())) {
564 forceRecompute(0, ParentVNI);
565 defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
566 return Idx;
567 }
568
569 VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
Jakob Stoklund Olesen01cb34b2011-02-08 18:50:18 +0000570 llvm::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000571 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000572}
573
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000574SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
575 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
576 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
577
578 // The interval must be live into the instruction at Idx.
Jakob Stoklund Olesenfc479332011-07-18 18:47:13 +0000579 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000580 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000581 if (!ParentVNI) {
582 DEBUG(dbgs() << ": not live\n");
583 return Idx.getNextSlot();
584 }
585 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
586
587 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
588 assert(MI && "No instruction at index");
589 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
590 return VNI->def;
591}
592
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000593SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000594 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000595 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000596 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000597
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000598 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000599 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000600 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000601 return Start;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000602 }
603
Eric Christopher0f438112011-02-03 06:18:29 +0000604 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000605 MBB.SkipPHIsAndLabels(MBB.begin()));
Eric Christopher0f438112011-02-03 06:18:29 +0000606 RegAssign.insert(Start, VNI->def, OpenIdx);
607 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000608 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000609}
610
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000611void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
612 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000613 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +0000614 assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000615 "Parent changes value in extended range");
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000616 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
617 "Range cannot span basic blocks");
618
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000619 // The complement interval will be extended as needed by LRCalc.extend().
Jakob Stoklund Olesenb3dd8262011-04-05 23:43:14 +0000620 if (ParentVNI)
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000621 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000622 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
623 RegAssign.insert(Start, End, OpenIdx);
624 DEBUG(dump());
625}
626
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000627//===----------------------------------------------------------------------===//
628// Spill modes
629//===----------------------------------------------------------------------===//
630
631void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
632 LiveInterval *LI = Edit->get(0);
633 DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
634 RegAssignMap::iterator AssignI;
635 AssignI.setMap(RegAssign);
636
637 for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
638 VNInfo *VNI = Copies[i];
639 SlotIndex Def = VNI->def;
640 MachineInstr *MI = LIS.getInstructionFromIndex(Def);
641 assert(MI && "No instruction for back-copy");
642
643 MachineBasicBlock *MBB = MI->getParent();
644 MachineBasicBlock::iterator MBBI(MI);
645 bool AtBegin;
646 do AtBegin = MBBI == MBB->begin();
647 while (!AtBegin && (--MBBI)->isDebugValue());
648
649 DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
650 LI->removeValNo(VNI);
651 LIS.RemoveMachineInstrFromMaps(MI);
652 MI->eraseFromParent();
653
654 // Adjust RegAssign if a register assignment is killed at VNI->def. We
655 // want to avoid calculating the live range of the source register if
656 // possible.
Jakob Stoklund Olesen1599a642012-08-03 20:59:29 +0000657 AssignI.find(Def.getPrevSlot());
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000658 if (!AssignI.valid() || AssignI.start() >= Def)
659 continue;
660 // If MI doesn't kill the assigned register, just leave it.
661 if (AssignI.stop() != Def)
662 continue;
663 unsigned RegIdx = AssignI.value();
664 if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
665 DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n');
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000666 forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000667 } else {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000668 SlotIndex Kill = LIS.getInstructionIndex(MBBI).getRegSlot();
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000669 DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
670 AssignI.setStop(Kill);
671 }
672 }
673}
674
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000675MachineBasicBlock*
676SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
677 MachineBasicBlock *DefMBB) {
678 if (MBB == DefMBB)
679 return MBB;
680 assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
681
682 const MachineLoopInfo &Loops = SA.Loops;
683 const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
684 MachineDomTreeNode *DefDomNode = MDT[DefMBB];
685
686 // Best candidate so far.
687 MachineBasicBlock *BestMBB = MBB;
688 unsigned BestDepth = UINT_MAX;
689
690 for (;;) {
691 const MachineLoop *Loop = Loops.getLoopFor(MBB);
692
693 // MBB isn't in a loop, it doesn't get any better. All dominators have a
694 // higher frequency by definition.
695 if (!Loop) {
696 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
697 << MBB->getNumber() << " at depth 0\n");
698 return MBB;
699 }
700
701 // We'll never be able to exit the DefLoop.
702 if (Loop == DefLoop) {
703 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
704 << MBB->getNumber() << " in the same loop\n");
705 return MBB;
706 }
707
708 // Least busy dominator seen so far.
709 unsigned Depth = Loop->getLoopDepth();
710 if (Depth < BestDepth) {
711 BestMBB = MBB;
712 BestDepth = Depth;
713 DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
714 << MBB->getNumber() << " at depth " << Depth << '\n');
715 }
716
717 // Leave loop by going to the immediate dominator of the loop header.
718 // This is a bigger stride than simply walking up the dominator tree.
719 MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
720
721 // Too far up the dominator tree?
722 if (!IDom || !MDT.dominates(DefDomNode, IDom))
723 return BestMBB;
724
725 MBB = IDom->getBlock();
726 }
727}
728
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000729void SplitEditor::hoistCopiesForSize() {
730 // Get the complement interval, always RegIdx 0.
731 LiveInterval *LI = Edit->get(0);
732 LiveInterval *Parent = &Edit->getParent();
733
734 // Track the nearest common dominator for all back-copies for each ParentVNI,
735 // indexed by ParentVNI->id.
736 typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
737 SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
738
739 // Find the nearest common dominator for parent values with multiple
740 // back-copies. If a single back-copy dominates, put it in DomPair.second.
741 for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
742 VI != VE; ++VI) {
743 VNInfo *VNI = *VI;
Jakob Stoklund Olesen1599a642012-08-03 20:59:29 +0000744 if (VNI->isUnused())
745 continue;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000746 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
747 assert(ParentVNI && "Parent not live at complement def");
748
749 // Don't hoist remats. The complement is probably going to disappear
750 // completely anyway.
751 if (Edit->didRematerialize(ParentVNI))
752 continue;
753
754 MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
755 DomPair &Dom = NearestDom[ParentVNI->id];
756
757 // Keep directly defined parent values. This is either a PHI or an
758 // instruction in the complement range. All other copies of ParentVNI
759 // should be eliminated.
760 if (VNI->def == ParentVNI->def) {
761 DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
762 Dom = DomPair(ValMBB, VNI->def);
763 continue;
764 }
765 // Skip the singly mapped values. There is nothing to gain from hoisting a
766 // single back-copy.
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000767 if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000768 DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
769 continue;
770 }
771
772 if (!Dom.first) {
773 // First time we see ParentVNI. VNI dominates itself.
774 Dom = DomPair(ValMBB, VNI->def);
775 } else if (Dom.first == ValMBB) {
776 // Two defs in the same block. Pick the earlier def.
777 if (!Dom.second.isValid() || VNI->def < Dom.second)
778 Dom.second = VNI->def;
779 } else {
780 // Different basic blocks. Check if one dominates.
781 MachineBasicBlock *Near =
782 MDT.findNearestCommonDominator(Dom.first, ValMBB);
783 if (Near == ValMBB)
784 // Def ValMBB dominates.
785 Dom = DomPair(ValMBB, VNI->def);
786 else if (Near != Dom.first)
787 // None dominate. Hoist to common dominator, need new def.
788 Dom = DomPair(Near, SlotIndex());
789 }
790
791 DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
792 << " for parent " << ParentVNI->id << '@' << ParentVNI->def
793 << " hoist to BB#" << Dom.first->getNumber() << ' '
794 << Dom.second << '\n');
795 }
796
797 // Insert the hoisted copies.
798 for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
799 DomPair &Dom = NearestDom[i];
800 if (!Dom.first || Dom.second.isValid())
801 continue;
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000802 // This value needs a hoisted copy inserted at the end of Dom.first.
803 VNInfo *ParentVNI = Parent->getValNumInfo(i);
804 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
805 // Get a less loopy dominator than Dom.first.
806 Dom.first = findShallowDominator(Dom.first, DefMBB);
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000807 SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
808 Dom.second =
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000809 defFromParent(0, ParentVNI, Last, *Dom.first,
Jakob Stoklund Olesen74c4f972012-01-11 02:07:00 +0000810 SA.getLastSplitPointIter(Dom.first))->def;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000811 }
812
813 // Remove redundant back-copies that are now known to be dominated by another
814 // def with the same value.
815 SmallVector<VNInfo*, 8> BackCopies;
816 for (LiveInterval::vni_iterator VI = LI->vni_begin(), VE = LI->vni_end();
817 VI != VE; ++VI) {
818 VNInfo *VNI = *VI;
Jakob Stoklund Olesen1599a642012-08-03 20:59:29 +0000819 if (VNI->isUnused())
820 continue;
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000821 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
822 const DomPair &Dom = NearestDom[ParentVNI->id];
823 if (!Dom.first || Dom.second == VNI->def)
824 continue;
825 BackCopies.push_back(VNI);
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000826 forceRecompute(0, ParentVNI);
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000827 }
828 removeBackCopies(BackCopies);
829}
830
831
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000832/// transferValues - Transfer all possible values to the new live ranges.
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000833/// Values that were rematerialized are left alone, they need LRCalc.extend().
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000834bool SplitEditor::transferValues() {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000835 bool Skipped = false;
836 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000837 for (LiveInterval::const_iterator ParentI = Edit->getParent().begin(),
838 ParentE = Edit->getParent().end(); ParentI != ParentE; ++ParentI) {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000839 DEBUG(dbgs() << " blit " << *ParentI << ':');
840 VNInfo *ParentVNI = ParentI->valno;
841 // RegAssign has holes where RegIdx 0 should be used.
842 SlotIndex Start = ParentI->start;
843 AssignI.advanceTo(Start);
844 do {
845 unsigned RegIdx;
846 SlotIndex End = ParentI->end;
847 if (!AssignI.valid()) {
848 RegIdx = 0;
849 } else if (AssignI.start() <= Start) {
850 RegIdx = AssignI.value();
851 if (AssignI.stop() < End) {
852 End = AssignI.stop();
853 ++AssignI;
854 }
855 } else {
856 RegIdx = 0;
857 End = std::min(End, AssignI.start());
858 }
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000859
860 // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000861 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000862 LiveInterval *LI = Edit->get(RegIdx);
863
864 // Check for a simply defined value that can be blitted directly.
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000865 ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
866 if (VNInfo *VNI = VFP.getPointer()) {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000867 DEBUG(dbgs() << ':' << VNI->id);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000868 LI->addRange(LiveRange(Start, End, VNI));
869 Start = End;
870 continue;
871 }
872
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000873 // Skip values with forced recomputation.
874 if (VFP.getInt()) {
875 DEBUG(dbgs() << "(recalc)");
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000876 Skipped = true;
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000877 Start = End;
878 continue;
879 }
880
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000881 LiveRangeCalc &LRC = getLRCalc(RegIdx);
882
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000883 // This value has multiple defs in RegIdx, but it wasn't rematerialized,
884 // so the live range is accurate. Add live-in blocks in [Start;End) to the
885 // LiveInBlocks.
886 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
887 SlotIndex BlockStart, BlockEnd;
888 tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(MBB);
889
890 // The first block may be live-in, or it may have its own def.
891 if (Start != BlockStart) {
Jakob Stoklund Olesenee5655d2011-09-13 16:47:56 +0000892 VNInfo *VNI = LI->extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000893 assert(VNI && "Missing def for complex mapped value");
894 DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
895 // MBB has its own def. Is it also live-out?
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000896 if (BlockEnd <= End)
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000897 LRC.setLiveOutValue(MBB, VNI);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000898
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000899 // Skip to the next block for live-in.
900 ++MBB;
901 BlockStart = BlockEnd;
902 }
903
904 // Handle the live-in blocks covered by [Start;End).
905 assert(Start <= BlockStart && "Expected live-in block");
906 while (BlockStart < End) {
907 DEBUG(dbgs() << ">BB#" << MBB->getNumber());
908 BlockEnd = LIS.getMBBEndIdx(MBB);
909 if (BlockStart == ParentVNI->def) {
910 // This block has the def of a parent PHI, so it isn't live-in.
911 assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
Jakob Stoklund Olesenee5655d2011-09-13 16:47:56 +0000912 VNInfo *VNI = LI->extendInBlock(BlockStart, std::min(BlockEnd, End));
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000913 assert(VNI && "Missing def for complex mapped parent PHI");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000914 if (End >= BlockEnd)
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000915 LRC.setLiveOutValue(MBB, VNI); // Live-out as well.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000916 } else {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000917 // This block needs a live-in value. The last block covered may not
918 // be live-out.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000919 if (End < BlockEnd)
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000920 LRC.addLiveInBlock(LI, MDT[MBB], End);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000921 else {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000922 // Live-through, and we don't know the value.
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000923 LRC.addLiveInBlock(LI, MDT[MBB]);
924 LRC.setLiveOutValue(MBB, 0);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000925 }
926 }
927 BlockStart = BlockEnd;
928 ++MBB;
929 }
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000930 Start = End;
931 } while (Start != ParentI->end);
932 DEBUG(dbgs() << '\n');
933 }
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000934
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000935 LRCalc[0].calculateValues();
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000936 if (SpillMode)
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000937 LRCalc[1].calculateValues();
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000938
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000939 return Skipped;
940}
941
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000942void SplitEditor::extendPHIKillRanges() {
943 // Extend live ranges to be live-out for successor PHI values.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000944 for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
945 E = Edit->getParent().vni_end(); I != E; ++I) {
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000946 const VNInfo *PHIVNI = *I;
947 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
948 continue;
949 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000950 LiveInterval *LI = Edit->get(RegIdx);
951 LiveRangeCalc &LRC = getLRCalc(RegIdx);
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000952 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
953 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
954 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000955 SlotIndex End = LIS.getMBBEndIdx(*PI);
956 SlotIndex LastUse = End.getPrevSlot();
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000957 // The predecessor may not have a live-out value. That is OK, like an
958 // undef PHI operand.
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000959 if (Edit->getParent().liveAt(LastUse)) {
960 assert(RegAssign.lookup(LastUse) == RegIdx &&
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000961 "Different register assignment in phi predecessor");
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000962 LRC.extend(LI, End);
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000963 }
964 }
965 }
966}
967
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000968/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000969void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000970 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000971 RE = MRI.reg_end(); RI != RE;) {
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000972 MachineOperand &MO = RI.getOperand();
973 MachineInstr *MI = MO.getParent();
974 ++RI;
Eric Christopher0f438112011-02-03 06:18:29 +0000975 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000976 if (MI->isDebugValue()) {
977 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000978 MO.setReg(0);
979 continue;
980 }
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +0000981
Jakob Stoklund Olesenb09701d2011-07-24 20:23:50 +0000982 // <undef> operands don't really read the register, so it doesn't matter
983 // which register we choose. When the use operand is tied to a def, we must
984 // use the same register as the def, so just do that always.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000985 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesenb09701d2011-07-24 20:23:50 +0000986 if (MO.isDef() || MO.isUndef())
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000987 Idx = Idx.getRegSlot(MO.isEarlyClobber());
Eric Christopher0f438112011-02-03 06:18:29 +0000988
989 // Rewrite to the mapped register at Idx.
990 unsigned RegIdx = RegAssign.lookup(Idx);
Jakob Stoklund Olesenabcc73e2011-09-13 17:38:57 +0000991 LiveInterval *LI = Edit->get(RegIdx);
992 MO.setReg(LI->reg);
Eric Christopher0f438112011-02-03 06:18:29 +0000993 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
994 << Idx << ':' << RegIdx << '\t' << *MI);
995
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +0000996 // Extend liveness to Idx if the instruction reads reg.
Jakob Stoklund Olesen81d686e2011-07-24 20:33:23 +0000997 if (!ExtendRanges || MO.isUndef())
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +0000998 continue;
999
1000 // Skip instructions that don't read Reg.
1001 if (MO.isDef()) {
1002 if (!MO.getSubReg() && !MO.isEarlyClobber())
1003 continue;
1004 // We may wan't to extend a live range for a partial redef, or for a use
1005 // tied to an early clobber.
1006 Idx = Idx.getPrevSlot();
1007 if (!Edit->getParent().liveAt(Idx))
1008 continue;
1009 } else
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001010 Idx = Idx.getRegSlot(true);
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +00001011
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +00001012 getLRCalc(RegIdx).extend(LI, Idx.getNextSlot());
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001013 }
1014}
1015
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001016void SplitEditor::deleteRematVictims() {
1017 SmallVector<MachineInstr*, 8> Dead;
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001018 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
1019 LiveInterval *LI = *I;
1020 for (LiveInterval::const_iterator LII = LI->begin(), LIE = LI->end();
1021 LII != LIE; ++LII) {
Jakob Stoklund Olesen1f81e312011-11-13 22:42:13 +00001022 // Dead defs end at the dead slot.
1023 if (LII->end != LII->valno->def.getDeadSlot())
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001024 continue;
1025 MachineInstr *MI = LIS.getInstructionFromIndex(LII->valno->def);
1026 assert(MI && "Missing instruction for dead def");
1027 MI->addRegisterDead(LI->reg, &TRI);
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001028
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001029 if (!MI->allDefsAreDead())
1030 continue;
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001031
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +00001032 DEBUG(dbgs() << "All defs dead: " << *MI);
1033 Dead.push_back(MI);
1034 }
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001035 }
1036
1037 if (Dead.empty())
1038 return;
1039
Pete Cooper8a06af92012-04-02 22:22:53 +00001040 Edit->eliminateDeadDefs(Dead);
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001041}
1042
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001043void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001044 ++NumFinished;
Eric Christopher463a2972011-02-03 05:40:54 +00001045
Eric Christopher0f438112011-02-03 06:18:29 +00001046 // At this point, the live intervals in Edit contain VNInfos corresponding to
1047 // the inserted copies.
1048
1049 // Add the original defs from the parent interval.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001050 for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
1051 E = Edit->getParent().vni_end(); I != E; ++I) {
Eric Christopher0f438112011-02-03 06:18:29 +00001052 const VNInfo *ParentVNI = *I;
Jakob Stoklund Olesen9ecd1e72011-02-04 00:59:23 +00001053 if (ParentVNI->isUnused())
1054 continue;
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +00001055 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Jakob Stoklund Olesenb18d7792012-07-27 21:11:14 +00001056 defValue(RegIdx, ParentVNI, ParentVNI->def);
Jakob Stoklund Olesen29ef8752011-03-15 21:13:22 +00001057
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +00001058 // Force rematted values to be recomputed everywhere.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001059 // The new live ranges may be truncated.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001060 if (Edit->didRematerialize(ParentVNI))
1061 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +00001062 forceRecompute(i, ParentVNI);
Eric Christopher0f438112011-02-03 06:18:29 +00001063 }
1064
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001065 // Hoist back-copies to the complement interval when in spill mode.
1066 switch (SpillMode) {
1067 case SM_Partition:
1068 // Leave all back-copies as is.
1069 break;
1070 case SM_Size:
1071 hoistCopiesForSize();
1072 break;
1073 case SM_Speed:
1074 llvm_unreachable("Spill mode 'speed' not implemented yet");
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +00001075 }
1076
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001077 // Transfer the simply mapped values, check if any are skipped.
1078 bool Skipped = transferValues();
1079 if (Skipped)
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001080 extendPHIKillRanges();
1081 else
1082 ++NumSimple;
Eric Christopher0f438112011-02-03 06:18:29 +00001083
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001084 // Rewrite virtual registers, possibly extending ranges.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001085 rewriteAssigned(Skipped);
Eric Christopher0f438112011-02-03 06:18:29 +00001086
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001087 // Delete defs that were rematted everywhere.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001088 if (Skipped)
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001089 deleteRematVictims();
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001090
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001091 // Get rid of unused values and set phi-kill flags.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001092 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001093 (*I)->RenumberValues(LIS);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001094
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001095 // Provide a reverse mapping from original indices to Edit ranges.
1096 if (LRMap) {
1097 LRMap->clear();
1098 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1099 LRMap->push_back(i);
1100 }
1101
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001102 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001103 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001104 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001105 // Don't use iterators, they are invalidated by create() below.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001106 LiveInterval *li = Edit->get(i);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001107 unsigned NumComp = ConEQ.Classify(li);
1108 if (NumComp <= 1)
1109 continue;
1110 DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n');
1111 SmallVector<LiveInterval*, 8> dups;
1112 dups.push_back(li);
Matt Beaumont-Gayae5fbee2011-04-21 19:46:23 +00001113 for (unsigned j = 1; j != NumComp; ++j)
Pete Cooper8a06af92012-04-02 22:22:53 +00001114 dups.push_back(&Edit->create());
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +00001115 ConEQ.Distribute(&dups[0], MRI);
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001116 // The new intervals all map back to i.
1117 if (LRMap)
1118 LRMap->resize(Edit->size(), i);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001119 }
1120
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +00001121 // Calculate spill weight and allocation hints for new intervals.
Pete Cooper8a06af92012-04-02 22:22:53 +00001122 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops);
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001123
1124 assert(!LRMap || LRMap->size() == Edit->size());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001125}
1126
1127
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +00001128//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001129// Single Block Splitting
1130//===----------------------------------------------------------------------===//
1131
Jakob Stoklund Olesen2d6d86b2011-08-05 22:20:45 +00001132bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
1133 bool SingleInstrs) const {
1134 // Always split for multiple instructions.
1135 if (!BI.isOneInstr())
1136 return true;
1137 // Don't split for single instructions unless explicitly requested.
1138 if (!SingleInstrs)
1139 return false;
1140 // Splitting a live-through range always makes progress.
1141 if (BI.LiveIn && BI.LiveOut)
1142 return true;
1143 // No point in isolating a copy. It has no register class constraints.
1144 if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
1145 return false;
1146 // Finally, don't isolate an end point that was created by earlier splits.
1147 return isOriginalEndpoint(BI.FirstInstr);
1148}
1149
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001150void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1151 openIntv();
1152 SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001153 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001154 LastSplitPoint));
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001155 if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
1156 useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001157 } else {
1158 // The last use is after the last valid split point.
1159 SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1160 useIntv(SegStart, SegStop);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001161 overlapIntv(SegStop, BI.LastInstr);
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001162 }
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001163}
1164
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001165
1166//===----------------------------------------------------------------------===//
1167// Global Live Range Splitting Support
1168//===----------------------------------------------------------------------===//
1169
1170// These methods support a method of global live range splitting that uses a
1171// global algorithm to decide intervals for CFG edges. They will insert split
1172// points and color intervals in basic blocks while avoiding interference.
1173//
1174// Note that splitSingleBlock is also useful for blocks where both CFG edges
1175// are on the stack.
1176
1177void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
1178 unsigned IntvIn, SlotIndex LeaveBefore,
1179 unsigned IntvOut, SlotIndex EnterAfter){
1180 SlotIndex Start, Stop;
1181 tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
1182
1183 DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
1184 << ") intf " << LeaveBefore << '-' << EnterAfter
1185 << ", live-through " << IntvIn << " -> " << IntvOut);
1186
1187 assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
1188
Jakob Stoklund Olesenfe9b2d12011-07-23 03:32:26 +00001189 assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
1190 assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
1191 assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
1192
1193 MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
1194
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001195 if (!IntvOut) {
1196 DEBUG(dbgs() << ", spill on entry.\n");
1197 //
1198 // <<<<<<<<< Possible LeaveBefore interference.
1199 // |-----------| Live through.
1200 // -____________ Spill on entry.
1201 //
1202 selectIntv(IntvIn);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001203 SlotIndex Idx = leaveIntvAtTop(*MBB);
1204 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1205 (void)Idx;
1206 return;
1207 }
1208
1209 if (!IntvIn) {
1210 DEBUG(dbgs() << ", reload on exit.\n");
1211 //
1212 // >>>>>>> Possible EnterAfter interference.
1213 // |-----------| Live through.
1214 // ___________-- Reload on exit.
1215 //
1216 selectIntv(IntvOut);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001217 SlotIndex Idx = enterIntvAtEnd(*MBB);
1218 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1219 (void)Idx;
1220 return;
1221 }
1222
1223 if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
1224 DEBUG(dbgs() << ", straight through.\n");
1225 //
1226 // |-----------| Live through.
1227 // ------------- Straight through, same intv, no interference.
1228 //
1229 selectIntv(IntvOut);
1230 useIntv(Start, Stop);
1231 return;
1232 }
1233
1234 // We cannot legally insert splits after LSP.
1235 SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
Jakob Stoklund Olesenfe9b2d12011-07-23 03:32:26 +00001236 assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001237
1238 if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
1239 LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
1240 DEBUG(dbgs() << ", switch avoiding interference.\n");
1241 //
1242 // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
1243 // |-----------| Live through.
1244 // ------======= Switch intervals between interference.
1245 //
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001246 selectIntv(IntvOut);
Jakob Stoklund Olesenfe9b2d12011-07-23 03:32:26 +00001247 SlotIndex Idx;
1248 if (LeaveBefore && LeaveBefore < LSP) {
1249 Idx = enterIntvBefore(LeaveBefore);
1250 useIntv(Idx, Stop);
1251 } else {
1252 Idx = enterIntvAtEnd(*MBB);
1253 }
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001254 selectIntv(IntvIn);
1255 useIntv(Start, Idx);
1256 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1257 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1258 return;
1259 }
1260
1261 DEBUG(dbgs() << ", create local intv for interference.\n");
1262 //
1263 // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
1264 // |-----------| Live through.
1265 // ==---------== Switch intervals before/after interference.
1266 //
1267 assert(LeaveBefore <= EnterAfter && "Missed case");
1268
1269 selectIntv(IntvOut);
1270 SlotIndex Idx = enterIntvAfter(EnterAfter);
1271 useIntv(Idx, Stop);
1272 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1273
1274 selectIntv(IntvIn);
1275 Idx = leaveIntvBefore(LeaveBefore);
1276 useIntv(Start, Idx);
1277 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1278}
1279
1280
1281void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
1282 unsigned IntvIn, SlotIndex LeaveBefore) {
1283 SlotIndex Start, Stop;
1284 tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
1285
1286 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001287 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001288 << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
1289 << (BI.LiveOut ? ", stack-out" : ", killed in block"));
1290
1291 assert(IntvIn && "Must have register in");
1292 assert(BI.LiveIn && "Must be live-in");
1293 assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
1294
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001295 if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001296 DEBUG(dbgs() << " before interference.\n");
1297 //
1298 // <<< Interference after kill.
1299 // |---o---x | Killed in block.
1300 // ========= Use IntvIn everywhere.
1301 //
1302 selectIntv(IntvIn);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001303 useIntv(Start, BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001304 return;
1305 }
1306
1307 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1308
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001309 if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001310 //
1311 // <<< Possible interference after last use.
1312 // |---o---o---| Live-out on stack.
1313 // =========____ Leave IntvIn after last use.
1314 //
1315 // < Interference after last use.
1316 // |---o---o--o| Live-out on stack, late last use.
1317 // ============ Copy to stack after LSP, overlap IntvIn.
1318 // \_____ Stack interval is live-out.
1319 //
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001320 if (BI.LastInstr < LSP) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001321 DEBUG(dbgs() << ", spill after last use before interference.\n");
1322 selectIntv(IntvIn);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001323 SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001324 useIntv(Start, Idx);
1325 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1326 } else {
1327 DEBUG(dbgs() << ", spill before last split point.\n");
1328 selectIntv(IntvIn);
Jakob Stoklund Olesenaf4e40c2011-07-16 00:13:30 +00001329 SlotIndex Idx = leaveIntvBefore(LSP);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001330 overlapIntv(Idx, BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001331 useIntv(Start, Idx);
1332 assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
1333 }
1334 return;
1335 }
1336
1337 // The interference is overlapping somewhere we wanted to use IntvIn. That
1338 // means we need to create a local interval that can be allocated a
1339 // different register.
1340 unsigned LocalIntv = openIntv();
Matt Beaumont-Gayf9d7fb62011-07-16 04:18:47 +00001341 (void)LocalIntv;
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001342 DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
1343
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001344 if (!BI.LiveOut || BI.LastInstr < LSP) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001345 //
1346 // <<<<<<< Interference overlapping uses.
1347 // |---o---o---| Live-out on stack.
1348 // =====----____ Leave IntvIn before interference, then spill.
1349 //
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001350 SlotIndex To = leaveIntvAfter(BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001351 SlotIndex From = enterIntvBefore(LeaveBefore);
1352 useIntv(From, To);
1353 selectIntv(IntvIn);
1354 useIntv(Start, From);
1355 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1356 return;
1357 }
1358
1359 // <<<<<<< Interference overlapping uses.
1360 // |---o---o--o| Live-out on stack, late last use.
1361 // =====------- Copy to stack before LSP, overlap LocalIntv.
1362 // \_____ Stack interval is live-out.
1363 //
1364 SlotIndex To = leaveIntvBefore(LSP);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001365 overlapIntv(To, BI.LastInstr);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001366 SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
1367 useIntv(From, To);
1368 selectIntv(IntvIn);
1369 useIntv(Start, From);
1370 assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
1371}
1372
1373void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
1374 unsigned IntvOut, SlotIndex EnterAfter) {
1375 SlotIndex Start, Stop;
1376 tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
1377
1378 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001379 << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001380 << ", reg-out " << IntvOut << ", enter after " << EnterAfter
1381 << (BI.LiveIn ? ", stack-in" : ", defined in block"));
1382
1383 SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
1384
1385 assert(IntvOut && "Must have register out");
1386 assert(BI.LiveOut && "Must be live-out");
1387 assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
1388
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001389 if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001390 DEBUG(dbgs() << " after interference.\n");
1391 //
1392 // >>>> Interference before def.
1393 // | o---o---| Defined in block.
1394 // ========= Use IntvOut everywhere.
1395 //
1396 selectIntv(IntvOut);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001397 useIntv(BI.FirstInstr, Stop);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001398 return;
1399 }
1400
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001401 if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001402 DEBUG(dbgs() << ", reload after interference.\n");
1403 //
1404 // >>>> Interference before def.
1405 // |---o---o---| Live-through, stack-in.
1406 // ____========= Enter IntvOut before first use.
1407 //
1408 selectIntv(IntvOut);
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001409 SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001410 useIntv(Idx, Stop);
1411 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1412 return;
1413 }
1414
1415 // The interference is overlapping somewhere we wanted to use IntvOut. That
1416 // means we need to create a local interval that can be allocated a
1417 // different register.
1418 DEBUG(dbgs() << ", interference overlaps uses.\n");
1419 //
1420 // >>>>>>> Interference overlapping uses.
1421 // |---o---o---| Live-through, stack-in.
1422 // ____---====== Create local interval for interference range.
1423 //
1424 selectIntv(IntvOut);
1425 SlotIndex Idx = enterIntvAfter(EnterAfter);
1426 useIntv(Idx, Stop);
1427 assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
1428
1429 openIntv();
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001430 SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001431 useIntv(From, Idx);
1432}