blob: bf27cc86574fee479332b2907658b23fbb7f0ab1 [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 Olesena17768f2010-10-14 23:49:52 +000017#include "LiveRangeEdit.h"
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000018#include "VirtRegMap.h"
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000019#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000020#include "llvm/CodeGen/LiveIntervalAnalysis.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 Olesen8ae02632010-07-20 15:41:07 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
24#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
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000031STATISTIC(NumFinished, "Number of splits finished");
32STATISTIC(NumSimple, "Number of splits that were simple");
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000033STATISTIC(NumCopies, "Number of copies inserted for splitting");
34STATISTIC(NumRemats, "Number of rematerialized defs for splitting");
Jakob Stoklund Olesenbdda37d2011-05-10 17:37:41 +000035STATISTIC(NumRepairs, "Number of invalid live ranges repaired");
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000036
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000037//===----------------------------------------------------------------------===//
38// Split Analysis
39//===----------------------------------------------------------------------===//
40
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000041SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm,
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000042 const LiveIntervals &lis,
43 const MachineLoopInfo &mli)
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000044 : MF(vrm.getMachineFunction()),
45 VRM(vrm),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000046 LIS(lis),
47 Loops(mli),
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000048 TII(*MF.getTarget().getInstrInfo()),
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000049 CurLI(0),
50 LastSplitPoint(MF.getNumBlockIDs()) {}
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000051
52void SplitAnalysis::clear() {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000053 UseSlots.clear();
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +000054 UseBlocks.clear();
55 ThroughBlocks.clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000056 CurLI = 0;
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +000057 DidRepairRange = false;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000058}
59
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000060SlotIndex SplitAnalysis::computeLastSplitPoint(unsigned Num) {
61 const MachineBasicBlock *MBB = MF.getBlockNumbered(Num);
62 const MachineBasicBlock *LPad = MBB->getLandingPadSuccessor();
63 std::pair<SlotIndex, SlotIndex> &LSP = LastSplitPoint[Num];
64
65 // Compute split points on the first call. The pair is independent of the
66 // current live interval.
67 if (!LSP.first.isValid()) {
68 MachineBasicBlock::const_iterator FirstTerm = MBB->getFirstTerminator();
69 if (FirstTerm == MBB->end())
70 LSP.first = LIS.getMBBEndIdx(MBB);
71 else
72 LSP.first = LIS.getInstructionIndex(FirstTerm);
73
74 // If there is a landing pad successor, also find the call instruction.
75 if (!LPad)
76 return LSP.first;
77 // There may not be a call instruction (?) in which case we ignore LPad.
78 LSP.second = LSP.first;
79 for (MachineBasicBlock::const_iterator I = FirstTerm, E = MBB->begin();
80 I != E; --I)
81 if (I->getDesc().isCall()) {
82 LSP.second = LIS.getInstructionIndex(I);
83 break;
84 }
85 }
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 Olesen71d9e652011-04-05 23:43:16 +000089 if (LPad && LSP.second.isValid() && LIS.isLiveInToMBB(*CurLI, LPad))
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000090 return LSP.second;
91 else
92 return LSP.first;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000093}
94
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000095/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +000096void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +000097 assert(UseSlots.empty() && "Call clear first");
98
99 // First get all the defs from the interval values. This provides the correct
100 // slots for early clobbers.
101 for (LiveInterval::const_vni_iterator I = CurLI->vni_begin(),
102 E = CurLI->vni_end(); I != E; ++I)
103 if (!(*I)->isPHIDef() && !(*I)->isUnused())
104 UseSlots.push_back((*I)->def);
105
106 // Get use slots form the use-def chain.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000107 const MachineRegisterInfo &MRI = MF.getRegInfo();
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000108 for (MachineRegisterInfo::use_nodbg_iterator
109 I = MRI.use_nodbg_begin(CurLI->reg), E = MRI.use_nodbg_end(); I != E;
110 ++I)
111 if (!I.getOperand().isUndef())
112 UseSlots.push_back(LIS.getInstructionIndex(&*I).getDefIndex());
113
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000114 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000115
Jakob Stoklund Olesena2948ef2011-04-05 15:18:18 +0000116 // Remove duplicates, keeping the smaller slot for each instruction.
117 // That is what we want for early clobbers.
118 UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
119 SlotIndex::isSameInstr),
120 UseSlots.end());
121
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000122 // Compute per-live block info.
123 if (!calcLiveBlockInfo()) {
124 // FIXME: calcLiveBlockInfo found inconsistencies in the live range.
125 // I am looking at you, SimpleRegisterCoalescing!
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +0000126 DidRepairRange = true;
Jakob Stoklund Olesenbdda37d2011-05-10 17:37:41 +0000127 ++NumRepairs;
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000128 DEBUG(dbgs() << "*** Fixing inconsistent live interval! ***\n");
129 const_cast<LiveIntervals&>(LIS)
130 .shrinkToUses(const_cast<LiveInterval*>(CurLI));
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000131 UseBlocks.clear();
132 ThroughBlocks.clear();
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000133 bool fixed = calcLiveBlockInfo();
134 (void)fixed;
135 assert(fixed && "Couldn't fix broken live interval");
136 }
137
Jakob Stoklund Olesenef1f5cc2011-03-27 22:49:23 +0000138 DEBUG(dbgs() << "Analyze counted "
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000139 << UseSlots.size() << " instrs in "
140 << UseBlocks.size() << " blocks, through "
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000141 << NumThroughBlocks << " blocks.\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000142}
143
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000144/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
145/// where CurLI is live.
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000146bool SplitAnalysis::calcLiveBlockInfo() {
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000147 ThroughBlocks.resize(MF.getNumBlockIDs());
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000148 NumThroughBlocks = NumGapBlocks = 0;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000149 if (CurLI->empty())
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000150 return true;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000151
152 LiveInterval::const_iterator LVI = CurLI->begin();
153 LiveInterval::const_iterator LVE = CurLI->end();
154
155 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
156 UseI = UseSlots.begin();
157 UseE = UseSlots.end();
158
159 // Loop over basic blocks where CurLI is live.
160 MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start);
161 for (;;) {
162 BlockInfo BI;
163 BI.MBB = MFI;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000164 SlotIndex Start, Stop;
165 tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000166
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000167 // If the block contains no uses, the range must be live through. At one
168 // point, SimpleRegisterCoalescing could create dangling ranges that ended
169 // mid-block.
170 if (UseI == UseE || *UseI >= Stop) {
171 ++NumThroughBlocks;
172 ThroughBlocks.set(BI.MBB->getNumber());
173 // The range shouldn't end mid-block if there are no uses. This shouldn't
174 // happen.
175 if (LVI->end < Stop)
176 return false;
177 } else {
178 // This block has uses. Find the first and last uses in the block.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000179 BI.FirstUse = *UseI;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000180 assert(BI.FirstUse >= Start);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000181 do ++UseI;
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000182 while (UseI != UseE && *UseI < Stop);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000183 BI.LastUse = UseI[-1];
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000184 assert(BI.LastUse < Stop);
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000185
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000186 // LVI is the first live segment overlapping MBB.
187 BI.LiveIn = LVI->start <= Start;
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000188
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000189 // Look for gaps in the live range.
190 BI.LiveOut = true;
191 while (LVI->end < Stop) {
192 SlotIndex LastStop = LVI->end;
193 if (++LVI == LVE || LVI->start >= Stop) {
194 BI.LiveOut = false;
195 BI.LastUse = LastStop;
196 break;
197 }
198 if (LastStop < LVI->start) {
199 // There is a gap in the live range. Create duplicate entries for the
200 // live-in snippet and the live-out snippet.
201 ++NumGapBlocks;
202
203 // Push the Live-in part.
204 BI.LiveThrough = false;
205 BI.LiveOut = false;
206 UseBlocks.push_back(BI);
207 UseBlocks.back().LastUse = LastStop;
208
209 // Set up BI for the live-out part.
210 BI.LiveIn = false;
211 BI.LiveOut = true;
212 BI.FirstUse = LVI->start;
213 }
214 }
215
216 // Don't set LiveThrough when the block has a gap.
217 BI.LiveThrough = BI.LiveIn && BI.LiveOut;
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000218 UseBlocks.push_back(BI);
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000219
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000220 // LVI is now at LVE or LVI->end >= Stop.
221 if (LVI == LVE)
222 break;
223 }
Jakob Stoklund Olesen626d6fb2011-05-29 21:24:39 +0000224
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000225 // Live segment ends exactly at Stop. Move to the next segment.
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000226 if (LVI->end == Stop && ++LVI == LVE)
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000227 break;
228
229 // Pick the next basic block.
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000230 if (LVI->start < Stop)
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000231 ++MFI;
232 else
233 MFI = LIS.getMBBFromIndex(LVI->start);
234 }
Jakob Stoklund Olesenb2abfa02011-05-28 02:32:57 +0000235
236 assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000237 return true;
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000238}
239
Jakob Stoklund Olesen9f4b8932011-04-26 22:33:12 +0000240unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
241 if (cli->empty())
242 return 0;
243 LiveInterval *li = const_cast<LiveInterval*>(cli);
244 LiveInterval::iterator LVI = li->begin();
245 LiveInterval::iterator LVE = li->end();
246 unsigned Count = 0;
247
248 // Loop over basic blocks where li is live.
249 MachineFunction::const_iterator MFI = LIS.getMBBFromIndex(LVI->start);
250 SlotIndex Stop = LIS.getMBBEndIdx(MFI);
251 for (;;) {
252 ++Count;
253 LVI = li->advanceTo(LVI, Stop);
254 if (LVI == LVE)
255 return Count;
256 do {
257 ++MFI;
258 Stop = LIS.getMBBEndIdx(MFI);
259 } while (Stop <= LVI->start);
260 }
261}
262
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000263bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
264 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
265 const LiveInterval &Orig = LIS.getInterval(OrigReg);
266 assert(!Orig.empty() && "Splitting empty interval?");
267 LiveInterval::const_iterator I = Orig.find(Idx);
268
269 // Range containing Idx should begin at Idx.
270 if (I != Orig.end() && I->start <= Idx)
271 return I->start == Idx;
272
273 // Range does not contain Idx, previous must end at Idx.
274 return I != Orig.begin() && (--I)->end == Idx;
275}
276
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000277void SplitAnalysis::analyze(const LiveInterval *li) {
278 clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000279 CurLI = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000280 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000281}
282
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000283
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000284//===----------------------------------------------------------------------===//
285// Split Editor
286//===----------------------------------------------------------------------===//
287
288/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000289SplitEditor::SplitEditor(SplitAnalysis &sa,
290 LiveIntervals &lis,
291 VirtRegMap &vrm,
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000292 MachineDominatorTree &mdt)
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000293 : SA(sa), LIS(lis), VRM(vrm),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000294 MRI(vrm.getMachineFunction().getRegInfo()),
Eric Christopher0f438112011-02-03 06:18:29 +0000295 MDT(mdt),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000296 TII(*vrm.getMachineFunction().getTarget().getInstrInfo()),
297 TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()),
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000298 Edit(0),
Eric Christopher0f438112011-02-03 06:18:29 +0000299 OpenIdx(0),
300 RegAssign(Allocator)
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000301{}
302
303void SplitEditor::reset(LiveRangeEdit &lre) {
304 Edit = &lre;
305 OpenIdx = 0;
306 RegAssign.clear();
307 Values.clear();
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000308
309 // We don't need to clear LiveOutCache, only LiveOutSeen entries are read.
310 LiveOutSeen.clear();
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000311
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000312 // We don't need an AliasAnalysis since we will only be performing
313 // cheap-as-a-copy remats anyway.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000314 Edit->anyRematerializable(LIS, TII, 0);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000315}
316
Eric Christopher0f438112011-02-03 06:18:29 +0000317void SplitEditor::dump() const {
318 if (RegAssign.empty()) {
319 dbgs() << " empty\n";
320 return;
321 }
322
323 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
324 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
325 dbgs() << '\n';
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000326}
327
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000328VNInfo *SplitEditor::defValue(unsigned RegIdx,
329 const VNInfo *ParentVNI,
330 SlotIndex Idx) {
331 assert(ParentVNI && "Mapping NULL value");
332 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000333 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
334 LiveInterval *LI = Edit->get(RegIdx);
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000335
336 // Create a new value.
337 VNInfo *VNI = LI->getNextValue(Idx, 0, LIS.getVNInfoAllocator());
338
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000339 // Use insert for lookup, so we can add missing values with a second lookup.
340 std::pair<ValueMap::iterator, bool> InsP =
341 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), VNI));
342
343 // This was the first time (RegIdx, ParentVNI) was mapped.
344 // Keep it as a simple def without any liveness.
345 if (InsP.second)
346 return VNI;
347
348 // If the previous value was a simple mapping, add liveness for it now.
349 if (VNInfo *OldVNI = InsP.first->second) {
350 SlotIndex Def = OldVNI->def;
351 LI->addRange(LiveRange(Def, Def.getNextSlot(), OldVNI));
352 // No longer a simple mapping.
353 InsP.first->second = 0;
354 }
355
356 // This is a complex mapping, add liveness for VNI
357 SlotIndex Def = VNI->def;
358 LI->addRange(LiveRange(Def, Def.getNextSlot(), VNI));
359
360 return VNI;
361}
362
363void SplitEditor::markComplexMapped(unsigned RegIdx, const VNInfo *ParentVNI) {
364 assert(ParentVNI && "Mapping NULL value");
365 VNInfo *&VNI = Values[std::make_pair(RegIdx, ParentVNI->id)];
366
367 // ParentVNI was either unmapped or already complex mapped. Either way.
368 if (!VNI)
369 return;
370
371 // This was previously a single mapping. Make sure the old def is represented
372 // by a trivial live range.
373 SlotIndex Def = VNI->def;
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000374 Edit->get(RegIdx)->addRange(LiveRange(Def, Def.getNextSlot(), VNI));
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000375 VNI = 0;
376}
377
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000378// extendRange - Extend the live range to reach Idx.
379// Potentially create phi-def values.
380void SplitEditor::extendRange(unsigned RegIdx, SlotIndex Idx) {
381 assert(Idx.isValid() && "Invalid SlotIndex");
382 MachineBasicBlock *IdxMBB = LIS.getMBBFromIndex(Idx);
383 assert(IdxMBB && "No MBB at Idx");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000384 LiveInterval *LI = Edit->get(RegIdx);
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000385
386 // Is there a def in the same MBB we can extend?
387 if (LI->extendInBlock(LIS.getMBBStartIdx(IdxMBB), Idx))
388 return;
389
390 // Now for the fun part. We know that ParentVNI potentially has multiple defs,
391 // and we may need to create even more phi-defs to preserve VNInfo SSA form.
392 // Perform a search for all predecessor blocks where we know the dominating
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000393 // VNInfo.
394 VNInfo *VNI = findReachingDefs(LI, IdxMBB, Idx.getNextSlot());
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000395
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000396 // When there were multiple different values, we may need new PHIs.
397 if (!VNI)
398 return updateSSA();
399
400 // Poor man's SSA update for the single-value case.
401 LiveOutPair LOP(VNI, MDT[LIS.getMBBFromIndex(VNI->def)]);
402 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveInBlocks.begin(),
403 E = LiveInBlocks.end(); I != E; ++I) {
404 MachineBasicBlock *MBB = I->DomNode->getBlock();
405 SlotIndex Start = LIS.getMBBStartIdx(MBB);
406 if (I->Kill.isValid())
407 LI->addRange(LiveRange(Start, I->Kill, VNI));
408 else {
409 LiveOutCache[MBB] = LOP;
410 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
411 }
412 }
413}
414
415/// findReachingDefs - Search the CFG for known live-out values.
416/// Add required live-in blocks to LiveInBlocks.
417VNInfo *SplitEditor::findReachingDefs(LiveInterval *LI,
418 MachineBasicBlock *KillMBB,
419 SlotIndex Kill) {
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000420 // Initialize the live-out cache the first time it is needed.
421 if (LiveOutSeen.empty()) {
422 unsigned N = VRM.getMachineFunction().getNumBlockIDs();
423 LiveOutSeen.resize(N);
424 LiveOutCache.resize(N);
425 }
426
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000427 // Blocks where LI should be live-in.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000428 SmallVector<MachineBasicBlock*, 16> WorkList(1, KillMBB);
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000429
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000430 // Remember if we have seen more than one value.
431 bool UniqueVNI = true;
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000432 VNInfo *TheVNI = 0;
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000433
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000434 // Using LiveOutCache as a visited set, perform a BFS for all reaching defs.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000435 for (unsigned i = 0; i != WorkList.size(); ++i) {
436 MachineBasicBlock *MBB = WorkList[i];
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +0000437 assert(!MBB->pred_empty() && "Value live-in to entry block?");
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000438 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
439 PE = MBB->pred_end(); PI != PE; ++PI) {
440 MachineBasicBlock *Pred = *PI;
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000441 LiveOutPair &LOP = LiveOutCache[Pred];
442
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000443 // Is this a known live-out block?
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000444 if (LiveOutSeen.test(Pred->getNumber())) {
445 if (VNInfo *VNI = LOP.first) {
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000446 if (TheVNI && TheVNI != VNI)
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000447 UniqueVNI = false;
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000448 TheVNI = VNI;
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000449 }
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000450 continue;
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000451 }
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000452
453 // First time. LOP is garbage and must be cleared below.
454 LiveOutSeen.set(Pred->getNumber());
455
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000456 // Does Pred provide a live-out value?
457 SlotIndex Start, Last;
458 tie(Start, Last) = LIS.getSlotIndexes()->getMBBRange(Pred);
459 Last = Last.getPrevSlot();
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000460 VNInfo *VNI = LI->extendInBlock(Start, Last);
461 LOP.first = VNI;
462 if (VNI) {
463 LOP.second = MDT[LIS.getMBBFromIndex(VNI->def)];
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000464 if (TheVNI && TheVNI != VNI)
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000465 UniqueVNI = false;
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000466 TheVNI = VNI;
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000467 continue;
468 }
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000469 LOP.second = 0;
470
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000471 // No, we need a live-in value for Pred as well
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000472 if (Pred != KillMBB)
473 WorkList.push_back(Pred);
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000474 else
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000475 // Loopback to KillMBB, so value is really live through.
476 Kill = SlotIndex();
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000477 }
478 }
479
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000480 // Transfer WorkList to LiveInBlocks in reverse order.
481 // This ordering works best with updateSSA().
482 LiveInBlocks.clear();
483 LiveInBlocks.reserve(WorkList.size());
484 while(!WorkList.empty())
485 LiveInBlocks.push_back(MDT[WorkList.pop_back_val()]);
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000486
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000487 // The kill block may not be live-through.
488 assert(LiveInBlocks.back().DomNode->getBlock() == KillMBB);
489 LiveInBlocks.back().Kill = Kill;
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000490
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000491 return UniqueVNI ? TheVNI : 0;
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000492}
493
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000494void SplitEditor::updateSSA() {
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000495 // This is essentially the same iterative algorithm that SSAUpdater uses,
496 // except we already have a dominator tree, so we don't have to recompute it.
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000497 unsigned Changes;
498 do {
499 Changes = 0;
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000500 // Propagate live-out values down the dominator tree, inserting phi-defs
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000501 // when necessary.
502 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveInBlocks.begin(),
503 E = LiveInBlocks.end(); I != E; ++I) {
504 MachineDomTreeNode *Node = I->DomNode;
505 // Skip block if the live-in value has already been determined.
506 if (!Node)
507 continue;
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000508 MachineBasicBlock *MBB = Node->getBlock();
509 MachineDomTreeNode *IDom = Node->getIDom();
510 LiveOutPair IDomValue;
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000511
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000512 // We need a live-in value to a block with no immediate dominator?
513 // This is probably an unreachable block that has survived somehow.
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000514 bool needPHI = !IDom || !LiveOutSeen.test(IDom->getBlock()->getNumber());
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000515
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000516 // IDom dominates all of our predecessors, but it may not be their
517 // immediate dominator. Check if any of them have live-out values that are
518 // properly dominated by IDom. If so, we need a phi-def here.
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000519 if (!needPHI) {
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000520 IDomValue = LiveOutCache[IDom->getBlock()];
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000521 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
522 PE = MBB->pred_end(); PI != PE; ++PI) {
523 LiveOutPair Value = LiveOutCache[*PI];
524 if (!Value.first || Value.first == IDomValue.first)
525 continue;
526 // This predecessor is carrying something other than IDomValue.
527 // It could be because IDomValue hasn't propagated yet, or it could be
528 // because MBB is in the dominance frontier of that value.
529 if (MDT.dominates(IDom, Value.second)) {
530 needPHI = true;
531 break;
532 }
533 }
534 }
535
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000536 // The value may be live-through even if Kill is set, as can happen when
537 // we are called from extendRange. In that case LiveOutSeen is true, and
538 // LiveOutCache indicates a foreign or missing value.
539 LiveOutPair &LOP = LiveOutCache[MBB];
540
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000541 // Create a phi-def if required.
542 if (needPHI) {
543 ++Changes;
544 SlotIndex Start = LIS.getMBBStartIdx(MBB);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000545 unsigned RegIdx = RegAssign.lookup(Start);
546 LiveInterval *LI = Edit->get(RegIdx);
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000547 VNInfo *VNI = LI->getNextValue(Start, 0, LIS.getVNInfoAllocator());
548 VNI->setIsPHIDef(true);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000549 I->Value = VNI;
550 // This block is done, we know the final value.
551 I->DomNode = 0;
552 if (I->Kill.isValid())
553 LI->addRange(LiveRange(Start, I->Kill, VNI));
554 else {
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000555 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000556 LOP = LiveOutPair(VNI, Node);
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000557 }
558 } else if (IDomValue.first) {
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000559 // No phi-def here. Remember incoming value.
560 I->Value = IDomValue.first;
561 if (I->Kill.isValid())
562 continue;
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000563 // Propagate IDomValue if needed:
564 // MBB is live-out and doesn't define its own value.
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000565 if (LOP.second != Node && LOP.first != IDomValue.first) {
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000566 ++Changes;
Jakob Stoklund Olesen13ba2da2011-03-04 00:15:36 +0000567 LOP = IDomValue;
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000568 }
569 }
570 }
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000571 } while (Changes);
572
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000573 // The values in LiveInBlocks are now accurate. No more phi-defs are needed
574 // for these blocks, so we can color the live ranges.
575 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveInBlocks.begin(),
576 E = LiveInBlocks.end(); I != E; ++I) {
577 if (!I->DomNode)
578 continue;
579 assert(I->Value && "No live-in value found");
580 MachineBasicBlock *MBB = I->DomNode->getBlock();
581 SlotIndex Start = LIS.getMBBStartIdx(MBB);
582 unsigned RegIdx = RegAssign.lookup(Start);
583 LiveInterval *LI = Edit->get(RegIdx);
584 LI->addRange(LiveRange(Start, I->Kill.isValid() ?
585 I->Kill : LIS.getMBBEndIdx(MBB), I->Value));
586 }
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000587}
588
Eric Christopher0f438112011-02-03 06:18:29 +0000589VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000590 VNInfo *ParentVNI,
591 SlotIndex UseIdx,
592 MachineBasicBlock &MBB,
593 MachineBasicBlock::iterator I) {
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000594 MachineInstr *CopyMI = 0;
595 SlotIndex Def;
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000596 LiveInterval *LI = Edit->get(RegIdx);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000597
Jakob Stoklund Olesenbb30dd42011-05-02 05:29:58 +0000598 // We may be trying to avoid interference that ends at a deleted instruction,
599 // so always begin RegIdx 0 early and all others late.
600 bool Late = RegIdx != 0;
601
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000602 // Attempt cheap-as-a-copy rematerialization.
603 LiveRangeEdit::Remat RM(ParentVNI);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000604 if (Edit->canRematerializeAt(RM, UseIdx, true, LIS)) {
Jakob Stoklund Olesenbb30dd42011-05-02 05:29:58 +0000605 Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, LIS, TII, TRI, Late);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000606 ++NumRemats;
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000607 } else {
608 // Can't remat, just insert a copy from parent.
Eric Christopher0f438112011-02-03 06:18:29 +0000609 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000610 .addReg(Edit->getReg());
Jakob Stoklund Olesenbb30dd42011-05-02 05:29:58 +0000611 Def = LIS.getSlotIndexes()->insertMachineInstrInMaps(CopyMI, Late)
612 .getDefIndex();
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000613 ++NumCopies;
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000614 }
615
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000616 // Define the value in Reg.
617 VNInfo *VNI = defValue(RegIdx, ParentVNI, Def);
618 VNI->setCopy(CopyMI);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000619 return VNI;
620}
621
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000622/// Create a new virtual register and live interval.
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000623unsigned SplitEditor::openIntv() {
Eric Christopher0f438112011-02-03 06:18:29 +0000624 // Create the complement as index 0.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000625 if (Edit->empty())
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000626 Edit->create(LIS, VRM);
Eric Christopher0f438112011-02-03 06:18:29 +0000627
628 // Create the open interval.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000629 OpenIdx = Edit->size();
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000630 Edit->create(LIS, VRM);
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000631 return OpenIdx;
632}
633
634void SplitEditor::selectIntv(unsigned Idx) {
635 assert(Idx != 0 && "Cannot select the complement interval");
636 assert(Idx < Edit->size() && "Can only select previously opened interval");
637 OpenIdx = Idx;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000638}
639
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000640SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000641 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000642 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000643 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000644 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000645 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000646 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000647 return Idx;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000648 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000649 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000650 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000651 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000652
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000653 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
654 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000655}
656
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000657SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000658 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000659 SlotIndex End = LIS.getMBBEndIdx(&MBB);
660 SlotIndex Last = End.getPrevSlot();
661 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000662 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000663 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000664 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000665 return End;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000666 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000667 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000668 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000669 LIS.getLastSplitPoint(Edit->getParent(), &MBB));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000670 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopher0f438112011-02-03 06:18:29 +0000671 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000672 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000673}
674
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000675/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000676void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000677 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000678}
679
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000680void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopher0f438112011-02-03 06:18:29 +0000681 assert(OpenIdx && "openIntv not called before useIntv");
682 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
683 RegAssign.insert(Start, End, OpenIdx);
684 DEBUG(dump());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000685}
686
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000687SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000688 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000689 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000690
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000691 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000692 Idx = Idx.getBoundaryIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000693 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000694 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000695 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000696 return Idx.getNextSlot();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000697 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000698 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000699
Jakob Stoklund Olesen01cb34b2011-02-08 18:50:18 +0000700 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
701 assert(MI && "No instruction at index");
702 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(),
703 llvm::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000704 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000705}
706
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000707SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
708 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
709 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
710
711 // The interval must be live into the instruction at Idx.
712 Idx = Idx.getBoundaryIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000713 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000714 if (!ParentVNI) {
715 DEBUG(dbgs() << ": not live\n");
716 return Idx.getNextSlot();
717 }
718 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
719
720 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
721 assert(MI && "No instruction at index");
722 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
723 return VNI->def;
724}
725
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000726SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000727 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000728 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000729 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000730
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000731 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000732 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000733 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000734 return Start;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000735 }
736
Eric Christopher0f438112011-02-03 06:18:29 +0000737 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000738 MBB.SkipPHIsAndLabels(MBB.begin()));
Eric Christopher0f438112011-02-03 06:18:29 +0000739 RegAssign.insert(Start, VNI->def, OpenIdx);
740 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000741 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000742}
743
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000744void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
745 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000746 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
747 assert(ParentVNI == Edit->getParent().getVNInfoAt(End.getPrevSlot()) &&
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000748 "Parent changes value in extended range");
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000749 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
750 "Range cannot span basic blocks");
751
Jakob Stoklund Olesend3fdaeb2011-03-02 00:49:28 +0000752 // The complement interval will be extended as needed by extendRange().
Jakob Stoklund Olesenb3dd8262011-04-05 23:43:14 +0000753 if (ParentVNI)
754 markComplexMapped(0, ParentVNI);
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000755 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
756 RegAssign.insert(Start, End, OpenIdx);
757 DEBUG(dump());
758}
759
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000760/// transferValues - Transfer all possible values to the new live ranges.
761/// Values that were rematerialized are left alone, they need extendRange().
762bool SplitEditor::transferValues() {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000763 bool Skipped = false;
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000764 LiveInBlocks.clear();
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000765 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000766 for (LiveInterval::const_iterator ParentI = Edit->getParent().begin(),
767 ParentE = Edit->getParent().end(); ParentI != ParentE; ++ParentI) {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000768 DEBUG(dbgs() << " blit " << *ParentI << ':');
769 VNInfo *ParentVNI = ParentI->valno;
770 // RegAssign has holes where RegIdx 0 should be used.
771 SlotIndex Start = ParentI->start;
772 AssignI.advanceTo(Start);
773 do {
774 unsigned RegIdx;
775 SlotIndex End = ParentI->end;
776 if (!AssignI.valid()) {
777 RegIdx = 0;
778 } else if (AssignI.start() <= Start) {
779 RegIdx = AssignI.value();
780 if (AssignI.stop() < End) {
781 End = AssignI.stop();
782 ++AssignI;
783 }
784 } else {
785 RegIdx = 0;
786 End = std::min(End, AssignI.start());
787 }
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000788
789 // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000790 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000791 LiveInterval *LI = Edit->get(RegIdx);
792
793 // Check for a simply defined value that can be blitted directly.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000794 if (VNInfo *VNI = Values.lookup(std::make_pair(RegIdx, ParentVNI->id))) {
795 DEBUG(dbgs() << ':' << VNI->id);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000796 LI->addRange(LiveRange(Start, End, VNI));
797 Start = End;
798 continue;
799 }
800
801 // Skip rematerialized values, we need to use extendRange() and
802 // extendPHIKillRanges() to completely recompute the live ranges.
803 if (Edit->didRematerialize(ParentVNI)) {
804 DEBUG(dbgs() << "(remat)");
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000805 Skipped = true;
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000806 Start = End;
807 continue;
808 }
809
810 // Initialize the live-out cache the first time it is needed.
811 if (LiveOutSeen.empty()) {
812 unsigned N = VRM.getMachineFunction().getNumBlockIDs();
813 LiveOutSeen.resize(N);
814 LiveOutCache.resize(N);
815 }
816
817 // This value has multiple defs in RegIdx, but it wasn't rematerialized,
818 // so the live range is accurate. Add live-in blocks in [Start;End) to the
819 // LiveInBlocks.
820 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start);
821 SlotIndex BlockStart, BlockEnd;
822 tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(MBB);
823
824 // The first block may be live-in, or it may have its own def.
825 if (Start != BlockStart) {
826 VNInfo *VNI = LI->extendInBlock(BlockStart,
827 std::min(BlockEnd, End).getPrevSlot());
828 assert(VNI && "Missing def for complex mapped value");
829 DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
830 // MBB has its own def. Is it also live-out?
831 if (BlockEnd <= End) {
832 LiveOutSeen.set(MBB->getNumber());
833 LiveOutCache[MBB] = LiveOutPair(VNI, MDT[MBB]);
834 }
835 // Skip to the next block for live-in.
836 ++MBB;
837 BlockStart = BlockEnd;
838 }
839
840 // Handle the live-in blocks covered by [Start;End).
841 assert(Start <= BlockStart && "Expected live-in block");
842 while (BlockStart < End) {
843 DEBUG(dbgs() << ">BB#" << MBB->getNumber());
844 BlockEnd = LIS.getMBBEndIdx(MBB);
845 if (BlockStart == ParentVNI->def) {
846 // This block has the def of a parent PHI, so it isn't live-in.
847 assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
848 VNInfo *VNI = LI->extendInBlock(BlockStart,
849 std::min(BlockEnd, End).getPrevSlot());
850 assert(VNI && "Missing def for complex mapped parent PHI");
851 if (End >= BlockEnd) {
852 // Live-out as well.
853 LiveOutSeen.set(MBB->getNumber());
854 LiveOutCache[MBB] = LiveOutPair(VNI, MDT[MBB]);
855 }
856 } else {
857 // This block needs a live-in value.
858 LiveInBlocks.push_back(MDT[MBB]);
859 // The last block covered may not be live-out.
860 if (End < BlockEnd)
861 LiveInBlocks.back().Kill = End;
862 else {
863 // Live-out, but we need updateSSA to tell us the value.
864 LiveOutSeen.set(MBB->getNumber());
Francois Pichetcbc5f402011-04-16 14:20:39 +0000865 LiveOutCache[MBB] = LiveOutPair((VNInfo*)0,
866 (MachineDomTreeNode*)0);
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000867 }
868 }
869 BlockStart = BlockEnd;
870 ++MBB;
871 }
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000872 Start = End;
873 } while (Start != ParentI->end);
874 DEBUG(dbgs() << '\n');
875 }
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000876
877 if (!LiveInBlocks.empty())
878 updateSSA();
879
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000880 return Skipped;
881}
882
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000883void SplitEditor::extendPHIKillRanges() {
884 // Extend live ranges to be live-out for successor PHI values.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000885 for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
886 E = Edit->getParent().vni_end(); I != E; ++I) {
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000887 const VNInfo *PHIVNI = *I;
888 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
889 continue;
890 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
891 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
892 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
893 PE = MBB->pred_end(); PI != PE; ++PI) {
894 SlotIndex End = LIS.getMBBEndIdx(*PI).getPrevSlot();
895 // The predecessor may not have a live-out value. That is OK, like an
896 // undef PHI operand.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000897 if (Edit->getParent().liveAt(End)) {
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000898 assert(RegAssign.lookup(End) == RegIdx &&
899 "Different register assignment in phi predecessor");
900 extendRange(RegIdx, End);
901 }
902 }
903 }
904}
905
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000906/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000907void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000908 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000909 RE = MRI.reg_end(); RI != RE;) {
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000910 MachineOperand &MO = RI.getOperand();
911 MachineInstr *MI = MO.getParent();
912 ++RI;
Eric Christopher0f438112011-02-03 06:18:29 +0000913 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000914 if (MI->isDebugValue()) {
915 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000916 MO.setReg(0);
917 continue;
918 }
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +0000919
920 // <undef> operands don't really read the register, so just assign them to
921 // the complement.
922 if (MO.isUse() && MO.isUndef()) {
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000923 MO.setReg(Edit->get(0)->reg);
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +0000924 continue;
925 }
926
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000927 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +0000928 if (MO.isDef())
929 Idx = MO.isEarlyClobber() ? Idx.getUseIndex() : Idx.getDefIndex();
Eric Christopher0f438112011-02-03 06:18:29 +0000930
931 // Rewrite to the mapped register at Idx.
932 unsigned RegIdx = RegAssign.lookup(Idx);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000933 MO.setReg(Edit->get(RegIdx)->reg);
Eric Christopher0f438112011-02-03 06:18:29 +0000934 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
935 << Idx << ':' << RegIdx << '\t' << *MI);
936
Jakob Stoklund Olesen7cec1792011-03-18 03:06:02 +0000937 // Extend liveness to Idx if the instruction reads reg.
938 if (!ExtendRanges)
939 continue;
940
941 // Skip instructions that don't read Reg.
942 if (MO.isDef()) {
943 if (!MO.getSubReg() && !MO.isEarlyClobber())
944 continue;
945 // We may wan't to extend a live range for a partial redef, or for a use
946 // tied to an early clobber.
947 Idx = Idx.getPrevSlot();
948 if (!Edit->getParent().liveAt(Idx))
949 continue;
950 } else
951 Idx = Idx.getUseIndex();
952
953 extendRange(RegIdx, Idx);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000954 }
955}
956
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000957void SplitEditor::deleteRematVictims() {
958 SmallVector<MachineInstr*, 8> Dead;
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +0000959 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
960 LiveInterval *LI = *I;
961 for (LiveInterval::const_iterator LII = LI->begin(), LIE = LI->end();
962 LII != LIE; ++LII) {
963 // Dead defs end at the store slot.
964 if (LII->end != LII->valno->def.getNextSlot())
965 continue;
966 MachineInstr *MI = LIS.getInstructionFromIndex(LII->valno->def);
967 assert(MI && "Missing instruction for dead def");
968 MI->addRegisterDead(LI->reg, &TRI);
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000969
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +0000970 if (!MI->allDefsAreDead())
971 continue;
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000972
Jakob Stoklund Olesen2dc455a2011-03-20 19:46:23 +0000973 DEBUG(dbgs() << "All defs dead: " << *MI);
974 Dead.push_back(MI);
975 }
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000976 }
977
978 if (Dead.empty())
979 return;
980
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000981 Edit->eliminateDeadDefs(Dead, LIS, VRM, TII);
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000982}
983
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +0000984void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000985 ++NumFinished;
Eric Christopher463a2972011-02-03 05:40:54 +0000986
Eric Christopher0f438112011-02-03 06:18:29 +0000987 // At this point, the live intervals in Edit contain VNInfos corresponding to
988 // the inserted copies.
989
990 // Add the original defs from the parent interval.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000991 for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
992 E = Edit->getParent().vni_end(); I != E; ++I) {
Eric Christopher0f438112011-02-03 06:18:29 +0000993 const VNInfo *ParentVNI = *I;
Jakob Stoklund Olesen9ecd1e72011-02-04 00:59:23 +0000994 if (ParentVNI->isUnused())
995 continue;
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000996 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Jakob Stoklund Olesen29ef8752011-03-15 21:13:22 +0000997 VNInfo *VNI = defValue(RegIdx, ParentVNI, ParentVNI->def);
998 VNI->setIsPHIDef(ParentVNI->isPHIDef());
999 VNI->setCopy(ParentVNI->getCopy());
1000
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001001 // Mark rematted values as complex everywhere to force liveness computation.
1002 // The new live ranges may be truncated.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001003 if (Edit->didRematerialize(ParentVNI))
1004 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001005 markComplexMapped(i, ParentVNI);
Eric Christopher0f438112011-02-03 06:18:29 +00001006 }
1007
1008#ifndef NDEBUG
1009 // Every new interval must have a def by now, otherwise the split is bogus.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001010 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I)
Eric Christopher0f438112011-02-03 06:18:29 +00001011 assert((*I)->hasAtLeastOneValue() && "Split interval has no value");
1012#endif
1013
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001014 // Transfer the simply mapped values, check if any are skipped.
1015 bool Skipped = transferValues();
1016 if (Skipped)
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001017 extendPHIKillRanges();
1018 else
1019 ++NumSimple;
Eric Christopher0f438112011-02-03 06:18:29 +00001020
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +00001021 // Rewrite virtual registers, possibly extending ranges.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001022 rewriteAssigned(Skipped);
Eric Christopher0f438112011-02-03 06:18:29 +00001023
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001024 // Delete defs that were rematted everywhere.
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +00001025 if (Skipped)
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +00001026 deleteRematVictims();
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001027
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001028 // Get rid of unused values and set phi-kill flags.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001029 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001030 (*I)->RenumberValues(LIS);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +00001031
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001032 // Provide a reverse mapping from original indices to Edit ranges.
1033 if (LRMap) {
1034 LRMap->clear();
1035 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
1036 LRMap->push_back(i);
1037 }
1038
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001039 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001040 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001041 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001042 // Don't use iterators, they are invalidated by create() below.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +00001043 LiveInterval *li = Edit->get(i);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001044 unsigned NumComp = ConEQ.Classify(li);
1045 if (NumComp <= 1)
1046 continue;
1047 DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n');
1048 SmallVector<LiveInterval*, 8> dups;
1049 dups.push_back(li);
Matt Beaumont-Gayae5fbee2011-04-21 19:46:23 +00001050 for (unsigned j = 1; j != NumComp; ++j)
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +00001051 dups.push_back(&Edit->create(LIS, VRM));
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +00001052 ConEQ.Distribute(&dups[0], MRI);
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001053 // The new intervals all map back to i.
1054 if (LRMap)
1055 LRMap->resize(Edit->size(), i);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +00001056 }
1057
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +00001058 // Calculate spill weight and allocation hints for new intervals.
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001059 Edit->calculateRegClassAndHint(VRM.getMachineFunction(), LIS, SA.Loops);
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001060
1061 assert(!LRMap || LRMap->size() == Edit->size());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +00001062}
1063
1064
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +00001065//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001066// Single Block Splitting
1067//===----------------------------------------------------------------------===//
1068
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001069/// getMultiUseBlocks - if CurLI has more than one use in a basic block, it
1070/// may be an advantage to split CurLI for the duration of the block.
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +00001071bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001072 // If CurLI is local to one block, there is no point to splitting it.
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +00001073 if (UseBlocks.size() <= 1)
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +00001074 return false;
1075 // Add blocks with multiple uses.
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +00001076 for (unsigned i = 0, e = UseBlocks.size(); i != e; ++i) {
1077 const BlockInfo &BI = UseBlocks[i];
1078 if (BI.FirstUse == BI.LastUse)
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +00001079 continue;
1080 Blocks.insert(BI.MBB);
1081 }
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +00001082 return !Blocks.empty();
1083}
1084
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001085void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
1086 openIntv();
1087 SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
1088 SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstUse,
1089 LastSplitPoint));
1090 if (!BI.LiveOut || BI.LastUse < LastSplitPoint) {
1091 useIntv(SegStart, leaveIntvAfter(BI.LastUse));
1092 } else {
1093 // The last use is after the last valid split point.
1094 SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
1095 useIntv(SegStart, SegStop);
1096 overlapIntv(SegStop, BI.LastUse);
1097 }
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001098}
1099
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +00001100/// splitSingleBlocks - Split CurLI into a separate live interval inside each
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +00001101/// basic block in Blocks.
1102void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) {
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +00001103 DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n");
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +00001104 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA.getUseBlocks();
1105 for (unsigned i = 0; i != UseBlocks.size(); ++i) {
1106 const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001107 if (Blocks.count(BI.MBB))
1108 splitSingleBlock(BI);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001109 }
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +00001110 finish();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +00001111}