blob: 9b007a4ac153f13754722929305ec0ac5491db37 [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 Olesen08e93b12010-08-10 17:07:22 +000020#include "llvm/CodeGen/CalcSpillWeights.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000021#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +000022#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000025#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000028#include "llvm/Target/TargetInstrInfo.h"
29#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000030
31using namespace llvm;
32
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000033static cl::opt<bool>
34AllowSplit("spiller-splits-edges",
35 cl::desc("Allow critical edge splitting during spilling"));
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000036
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000037STATISTIC(NumFinished, "Number of splits finished");
38STATISTIC(NumSimple, "Number of splits that were simple");
39
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000040//===----------------------------------------------------------------------===//
41// Split Analysis
42//===----------------------------------------------------------------------===//
43
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000044SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm,
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000045 const LiveIntervals &lis,
46 const MachineLoopInfo &mli)
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000047 : MF(vrm.getMachineFunction()),
48 VRM(vrm),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000049 LIS(lis),
50 Loops(mli),
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000051 TII(*MF.getTarget().getInstrInfo()),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000052 CurLI(0) {}
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000053
54void SplitAnalysis::clear() {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000055 UseSlots.clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000056 UsingInstrs.clear();
57 UsingBlocks.clear();
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +000058 LiveBlocks.clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000059 CurLI = 0;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000060}
61
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000062bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) {
63 MachineBasicBlock *T, *F;
64 SmallVector<MachineOperand, 4> Cond;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000065 return !TII.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond);
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000066}
67
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000068/// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +000069void SplitAnalysis::analyzeUses() {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000070 const MachineRegisterInfo &MRI = MF.getRegInfo();
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +000071 for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(CurLI->reg),
72 E = MRI.reg_end(); I != E; ++I) {
73 MachineOperand &MO = I.getOperand();
74 if (MO.isUse() && MO.isUndef())
75 continue;
76 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000077 if (MI->isDebugValue() || !UsingInstrs.insert(MI))
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000078 continue;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000079 UseSlots.push_back(LIS.getInstructionIndex(MI).getDefIndex());
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000080 MachineBasicBlock *MBB = MI->getParent();
Jakob Stoklund Olesen4f5c9d22011-02-09 23:56:18 +000081 UsingBlocks[MBB]++;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000082 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000083 array_pod_sort(UseSlots.begin(), UseSlots.end());
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +000084 calcLiveBlockInfo();
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +000085 DEBUG(dbgs() << " counted "
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000086 << UsingInstrs.size() << " instrs, "
Jakob Stoklund Olesen4f5c9d22011-02-09 23:56:18 +000087 << UsingBlocks.size() << " blocks.\n");
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000088}
89
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +000090/// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
91/// where CurLI is live.
92void SplitAnalysis::calcLiveBlockInfo() {
93 if (CurLI->empty())
94 return;
95
96 LiveInterval::const_iterator LVI = CurLI->begin();
97 LiveInterval::const_iterator LVE = CurLI->end();
98
99 SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
100 UseI = UseSlots.begin();
101 UseE = UseSlots.end();
102
103 // Loop over basic blocks where CurLI is live.
104 MachineFunction::iterator MFI = LIS.getMBBFromIndex(LVI->start);
105 for (;;) {
106 BlockInfo BI;
107 BI.MBB = MFI;
108 SlotIndex Start, Stop;
109 tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
110
111 // The last split point is the latest possible insertion point that dominates
112 // all successor blocks. If interference reaches LastSplitPoint, it is not
113 // possible to insert a split or reload that makes CurLI live in the
114 // outgoing bundle.
115 MachineBasicBlock::iterator LSP = LIS.getLastSplitPoint(*CurLI, BI.MBB);
116 if (LSP == BI.MBB->end())
117 BI.LastSplitPoint = Stop;
118 else
119 BI.LastSplitPoint = LIS.getInstructionIndex(LSP);
120
121 // LVI is the first live segment overlapping MBB.
122 BI.LiveIn = LVI->start <= Start;
123 if (!BI.LiveIn)
124 BI.Def = LVI->start;
125
126 // Find the first and last uses in the block.
127 BI.Uses = hasUses(MFI);
128 if (BI.Uses && UseI != UseE) {
129 BI.FirstUse = *UseI;
130 assert(BI.FirstUse >= Start);
131 do ++UseI;
132 while (UseI != UseE && *UseI < Stop);
133 BI.LastUse = UseI[-1];
134 assert(BI.LastUse < Stop);
135 }
136
137 // Look for gaps in the live range.
138 bool hasGap = false;
139 BI.LiveOut = true;
140 while (LVI->end < Stop) {
141 SlotIndex LastStop = LVI->end;
142 if (++LVI == LVE || LVI->start >= Stop) {
143 BI.Kill = LastStop;
144 BI.LiveOut = false;
145 break;
146 }
147 if (LastStop < LVI->start) {
148 hasGap = true;
149 BI.Kill = LastStop;
150 BI.Def = LVI->start;
151 }
152 }
153
154 // Don't set LiveThrough when the block has a gap.
155 BI.LiveThrough = !hasGap && BI.LiveIn && BI.LiveOut;
156 LiveBlocks.push_back(BI);
157
158 // LVI is now at LVE or LVI->end >= Stop.
159 if (LVI == LVE)
160 break;
161
162 // Live segment ends exactly at Stop. Move to the next segment.
163 if (LVI->end == Stop && ++LVI == LVE)
164 break;
165
166 // Pick the next basic block.
167 if (LVI->start < Stop)
168 ++MFI;
169 else
170 MFI = LIS.getMBBFromIndex(LVI->start);
171 }
172}
173
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000174bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
175 unsigned OrigReg = VRM.getOriginal(CurLI->reg);
176 const LiveInterval &Orig = LIS.getInterval(OrigReg);
177 assert(!Orig.empty() && "Splitting empty interval?");
178 LiveInterval::const_iterator I = Orig.find(Idx);
179
180 // Range containing Idx should begin at Idx.
181 if (I != Orig.end() && I->start <= Idx)
182 return I->start == Idx;
183
184 // Range does not contain Idx, previous must end at Idx.
185 return I != Orig.begin() && (--I)->end == Idx;
186}
187
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000188void SplitAnalysis::print(const BlockPtrSet &B, raw_ostream &OS) const {
189 for (BlockPtrSet::const_iterator I = B.begin(), E = B.end(); I != E; ++I) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000190 unsigned count = UsingBlocks.lookup(*I);
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +0000191 OS << " BB#" << (*I)->getNumber();
192 if (count)
193 OS << '(' << count << ')';
194 }
195}
196
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000197void SplitAnalysis::analyze(const LiveInterval *li) {
198 clear();
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000199 CurLI = li;
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000200 analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000201}
202
Jakob Stoklund Olesen697483a2010-12-15 17:49:52 +0000203
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000204//===----------------------------------------------------------------------===//
205// Split Editor
206//===----------------------------------------------------------------------===//
207
208/// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000209SplitEditor::SplitEditor(SplitAnalysis &sa,
210 LiveIntervals &lis,
211 VirtRegMap &vrm,
212 MachineDominatorTree &mdt,
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000213 LiveRangeEdit &edit)
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000214 : SA(sa), LIS(lis), VRM(vrm),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000215 MRI(vrm.getMachineFunction().getRegInfo()),
Eric Christopher0f438112011-02-03 06:18:29 +0000216 MDT(mdt),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000217 TII(*vrm.getMachineFunction().getTarget().getInstrInfo()),
218 TRI(*vrm.getMachineFunction().getTarget().getRegisterInfo()),
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000219 Edit(&edit),
Eric Christopher0f438112011-02-03 06:18:29 +0000220 OpenIdx(0),
221 RegAssign(Allocator)
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000222{
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000223 // We don't need an AliasAnalysis since we will only be performing
224 // cheap-as-a-copy remats anyway.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000225 Edit->anyRematerializable(LIS, TII, 0);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000226}
227
Eric Christopher0f438112011-02-03 06:18:29 +0000228void SplitEditor::dump() const {
229 if (RegAssign.empty()) {
230 dbgs() << " empty\n";
231 return;
232 }
233
234 for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
235 dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
236 dbgs() << '\n';
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000237}
238
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000239VNInfo *SplitEditor::defValue(unsigned RegIdx,
240 const VNInfo *ParentVNI,
241 SlotIndex Idx) {
242 assert(ParentVNI && "Mapping NULL value");
243 assert(Idx.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000244 assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
245 LiveInterval *LI = Edit->get(RegIdx);
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000246
247 // Create a new value.
248 VNInfo *VNI = LI->getNextValue(Idx, 0, LIS.getVNInfoAllocator());
249
250 // Preserve the PHIDef bit.
251 if (ParentVNI->isPHIDef() && Idx == ParentVNI->def)
252 VNI->setIsPHIDef(true);
253
254 // Use insert for lookup, so we can add missing values with a second lookup.
255 std::pair<ValueMap::iterator, bool> InsP =
256 Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), VNI));
257
258 // This was the first time (RegIdx, ParentVNI) was mapped.
259 // Keep it as a simple def without any liveness.
260 if (InsP.second)
261 return VNI;
262
263 // If the previous value was a simple mapping, add liveness for it now.
264 if (VNInfo *OldVNI = InsP.first->second) {
265 SlotIndex Def = OldVNI->def;
266 LI->addRange(LiveRange(Def, Def.getNextSlot(), OldVNI));
267 // No longer a simple mapping.
268 InsP.first->second = 0;
269 }
270
271 // This is a complex mapping, add liveness for VNI
272 SlotIndex Def = VNI->def;
273 LI->addRange(LiveRange(Def, Def.getNextSlot(), VNI));
274
275 return VNI;
276}
277
278void SplitEditor::markComplexMapped(unsigned RegIdx, const VNInfo *ParentVNI) {
279 assert(ParentVNI && "Mapping NULL value");
280 VNInfo *&VNI = Values[std::make_pair(RegIdx, ParentVNI->id)];
281
282 // ParentVNI was either unmapped or already complex mapped. Either way.
283 if (!VNI)
284 return;
285
286 // This was previously a single mapping. Make sure the old def is represented
287 // by a trivial live range.
288 SlotIndex Def = VNI->def;
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000289 Edit->get(RegIdx)->addRange(LiveRange(Def, Def.getNextSlot(), VNI));
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000290 VNI = 0;
291}
292
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000293// extendRange - Extend the live range to reach Idx.
294// Potentially create phi-def values.
295void SplitEditor::extendRange(unsigned RegIdx, SlotIndex Idx) {
296 assert(Idx.isValid() && "Invalid SlotIndex");
297 MachineBasicBlock *IdxMBB = LIS.getMBBFromIndex(Idx);
298 assert(IdxMBB && "No MBB at Idx");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000299 LiveInterval *LI = Edit->get(RegIdx);
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000300
301 // Is there a def in the same MBB we can extend?
302 if (LI->extendInBlock(LIS.getMBBStartIdx(IdxMBB), Idx))
303 return;
304
305 // Now for the fun part. We know that ParentVNI potentially has multiple defs,
306 // and we may need to create even more phi-defs to preserve VNInfo SSA form.
307 // Perform a search for all predecessor blocks where we know the dominating
308 // VNInfo. Insert phi-def VNInfos along the path back to IdxMBB.
309 DEBUG(dbgs() << "\n Reaching defs for BB#" << IdxMBB->getNumber()
310 << " at " << Idx << " in " << *LI << '\n');
311
312 // Blocks where LI should be live-in.
313 SmallVector<MachineDomTreeNode*, 16> LiveIn;
314 LiveIn.push_back(MDT[IdxMBB]);
315
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000316 // Remember if we have seen more than one value.
317 bool UniqueVNI = true;
318 VNInfo *IdxVNI = 0;
319
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000320 // Using LiveOutCache as a visited set, perform a BFS for all reaching defs.
321 for (unsigned i = 0; i != LiveIn.size(); ++i) {
322 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
323 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
324 PE = MBB->pred_end(); PI != PE; ++PI) {
325 MachineBasicBlock *Pred = *PI;
326 // Is this a known live-out block?
327 std::pair<LiveOutMap::iterator,bool> LOIP =
328 LiveOutCache.insert(std::make_pair(Pred, LiveOutPair()));
329 // Yes, we have been here before.
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000330 if (!LOIP.second) {
331 if (VNInfo *VNI = LOIP.first->second.first) {
332 if (IdxVNI && IdxVNI != VNI)
333 UniqueVNI = false;
334 IdxVNI = VNI;
335 }
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000336 continue;
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000337 }
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000338 // Does Pred provide a live-out value?
339 SlotIndex Start, Last;
340 tie(Start, Last) = LIS.getSlotIndexes()->getMBBRange(Pred);
341 Last = Last.getPrevSlot();
342 if (VNInfo *VNI = LI->extendInBlock(Start, Last)) {
343 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(VNI->def);
344 LiveOutPair &LOP = LOIP.first->second;
345 LOP.first = VNI;
346 LOP.second = MDT[DefMBB];
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000347 if (IdxVNI && IdxVNI != VNI)
348 UniqueVNI = false;
349 IdxVNI = VNI;
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000350 continue;
351 }
352 // No, we need a live-in value for Pred as well
353 if (Pred != IdxMBB)
354 LiveIn.push_back(MDT[Pred]);
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000355 else
356 UniqueVNI = false; // Loopback to IdxMBB, ask updateSSA() for help.
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000357 }
358 }
359
360 // We may need to add phi-def values to preserve the SSA form.
Jakob Stoklund Olesen87017682011-03-03 01:29:10 +0000361 if (UniqueVNI) {
362 LiveOutPair LOP(IdxVNI, MDT[LIS.getMBBFromIndex(IdxVNI->def)]);
363 // Update LiveOutCache, but skip IdxMBB at LiveIn[0].
364 for (unsigned i = 1, e = LiveIn.size(); i != e; ++i)
365 LiveOutCache[LiveIn[i]->getBlock()] = LOP;
366 } else
367 IdxVNI = updateSSA(RegIdx, LiveIn, Idx, IdxMBB);
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000368
369#ifndef NDEBUG
370 // Check the LiveOutCache invariants.
371 for (LiveOutMap::iterator I = LiveOutCache.begin(), E = LiveOutCache.end();
372 I != E; ++I) {
373 assert(I->first && "Null MBB entry in cache");
374 assert(I->second.first && "Null VNInfo in cache");
375 assert(I->second.second && "Null DomTreeNode in cache");
376 if (I->second.second->getBlock() == I->first)
377 continue;
378 for (MachineBasicBlock::pred_iterator PI = I->first->pred_begin(),
379 PE = I->first->pred_end(); PI != PE; ++PI)
380 assert(LiveOutCache.lookup(*PI) == I->second && "Bad invariant");
381 }
382#endif
383
384 // Since we went through the trouble of a full BFS visiting all reaching defs,
385 // the values in LiveIn are now accurate. No more phi-defs are needed
386 // for these blocks, so we can color the live ranges.
387 for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) {
388 MachineBasicBlock *MBB = LiveIn[i]->getBlock();
389 SlotIndex Start = LIS.getMBBStartIdx(MBB);
390 VNInfo *VNI = LiveOutCache.lookup(MBB).first;
391
392 // Anything in LiveIn other than IdxMBB is live-through.
393 // In IdxMBB, we should stop at Idx unless the same value is live-out.
394 if (MBB == IdxMBB && IdxVNI != VNI)
395 LI->addRange(LiveRange(Start, Idx.getNextSlot(), IdxVNI));
396 else
397 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
398 }
399}
400
401VNInfo *SplitEditor::updateSSA(unsigned RegIdx,
402 SmallVectorImpl<MachineDomTreeNode*> &LiveIn,
403 SlotIndex Idx,
404 const MachineBasicBlock *IdxMBB) {
405 // This is essentially the same iterative algorithm that SSAUpdater uses,
406 // except we already have a dominator tree, so we don't have to recompute it.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000407 LiveInterval *LI = Edit->get(RegIdx);
Jakob Stoklund Olesen1c38ba62011-03-02 01:59:34 +0000408 VNInfo *IdxVNI = 0;
409 unsigned Changes;
410 do {
411 Changes = 0;
412 DEBUG(dbgs() << " Iterating over " << LiveIn.size() << " blocks.\n");
413 // Propagate live-out values down the dominator tree, inserting phi-defs
414 // when necessary. Since LiveIn was created by a BFS, going backwards makes
415 // it more likely for us to visit immediate dominators before their
416 // children.
417 for (unsigned i = LiveIn.size(); i; --i) {
418 MachineDomTreeNode *Node = LiveIn[i-1];
419 MachineBasicBlock *MBB = Node->getBlock();
420 MachineDomTreeNode *IDom = Node->getIDom();
421 LiveOutPair IDomValue;
422 // We need a live-in value to a block with no immediate dominator?
423 // This is probably an unreachable block that has survived somehow.
424 bool needPHI = !IDom;
425
426 // Get the IDom live-out value.
427 if (!needPHI) {
428 LiveOutMap::iterator I = LiveOutCache.find(IDom->getBlock());
429 if (I != LiveOutCache.end())
430 IDomValue = I->second;
431 else
432 // If IDom is outside our set of live-out blocks, there must be new
433 // defs, and we need a phi-def here.
434 needPHI = true;
435 }
436
437 // IDom dominates all of our predecessors, but it may not be the immediate
438 // dominator. Check if any of them have live-out values that are properly
439 // dominated by IDom. If so, we need a phi-def here.
440 if (!needPHI) {
441 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
442 PE = MBB->pred_end(); PI != PE; ++PI) {
443 LiveOutPair Value = LiveOutCache[*PI];
444 if (!Value.first || Value.first == IDomValue.first)
445 continue;
446 // This predecessor is carrying something other than IDomValue.
447 // It could be because IDomValue hasn't propagated yet, or it could be
448 // because MBB is in the dominance frontier of that value.
449 if (MDT.dominates(IDom, Value.second)) {
450 needPHI = true;
451 break;
452 }
453 }
454 }
455
456 // Create a phi-def if required.
457 if (needPHI) {
458 ++Changes;
459 SlotIndex Start = LIS.getMBBStartIdx(MBB);
460 VNInfo *VNI = LI->getNextValue(Start, 0, LIS.getVNInfoAllocator());
461 VNI->setIsPHIDef(true);
462 DEBUG(dbgs() << " - BB#" << MBB->getNumber()
463 << " phi-def #" << VNI->id << " at " << Start << '\n');
464 // We no longer need LI to be live-in.
465 LiveIn.erase(LiveIn.begin()+(i-1));
466 // Blocks in LiveIn are either IdxMBB, or have a value live-through.
467 if (MBB == IdxMBB)
468 IdxVNI = VNI;
469 // Check if we need to update live-out info.
470 LiveOutMap::iterator I = LiveOutCache.find(MBB);
471 if (I == LiveOutCache.end() || I->second.second == Node) {
472 // We already have a live-out defined in MBB, so this must be IdxMBB.
473 assert(MBB == IdxMBB && "Adding phi-def to known live-out");
474 LI->addRange(LiveRange(Start, Idx.getNextSlot(), VNI));
475 } else {
476 // This phi-def is also live-out, so color the whole block.
477 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
478 I->second = LiveOutPair(VNI, Node);
479 }
480 } else if (IDomValue.first) {
481 // No phi-def here. Remember incoming value for IdxMBB.
482 if (MBB == IdxMBB)
483 IdxVNI = IDomValue.first;
484 // Propagate IDomValue if needed:
485 // MBB is live-out and doesn't define its own value.
486 LiveOutMap::iterator I = LiveOutCache.find(MBB);
487 if (I != LiveOutCache.end() && I->second.second != Node &&
488 I->second.first != IDomValue.first) {
489 ++Changes;
490 I->second = IDomValue;
491 DEBUG(dbgs() << " - BB#" << MBB->getNumber()
492 << " idom valno #" << IDomValue.first->id
493 << " from BB#" << IDom->getBlock()->getNumber() << '\n');
494 }
495 }
496 }
497 DEBUG(dbgs() << " - made " << Changes << " changes.\n");
498 } while (Changes);
499
500 assert(IdxVNI && "Didn't find value for Idx");
501 return IdxVNI;
502}
503
Eric Christopher0f438112011-02-03 06:18:29 +0000504VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000505 VNInfo *ParentVNI,
506 SlotIndex UseIdx,
507 MachineBasicBlock &MBB,
508 MachineBasicBlock::iterator I) {
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000509 MachineInstr *CopyMI = 0;
510 SlotIndex Def;
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000511 LiveInterval *LI = Edit->get(RegIdx);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000512
513 // Attempt cheap-as-a-copy rematerialization.
514 LiveRangeEdit::Remat RM(ParentVNI);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000515 if (Edit->canRematerializeAt(RM, UseIdx, true, LIS)) {
516 Def = Edit->rematerializeAt(MBB, I, LI->reg, RM, LIS, TII, TRI);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000517 } else {
518 // Can't remat, just insert a copy from parent.
Eric Christopher0f438112011-02-03 06:18:29 +0000519 CopyMI = BuildMI(MBB, I, DebugLoc(), TII.get(TargetOpcode::COPY), LI->reg)
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000520 .addReg(Edit->getReg());
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000521 Def = LIS.InsertMachineInstrInMaps(CopyMI).getDefIndex();
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000522 }
523
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000524 // Define the value in Reg.
525 VNInfo *VNI = defValue(RegIdx, ParentVNI, Def);
526 VNI->setCopy(CopyMI);
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000527 return VNI;
528}
529
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000530/// Create a new virtual register and live interval.
531void SplitEditor::openIntv() {
Eric Christopher0f438112011-02-03 06:18:29 +0000532 assert(!OpenIdx && "Previous LI not closed before openIntv");
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000533
Eric Christopher0f438112011-02-03 06:18:29 +0000534 // Create the complement as index 0.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000535 if (Edit->empty())
536 Edit->create(MRI, LIS, VRM);
Eric Christopher0f438112011-02-03 06:18:29 +0000537
538 // Create the open interval.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000539 OpenIdx = Edit->size();
540 Edit->create(MRI, LIS, VRM);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000541}
542
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000543SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000544 assert(OpenIdx && "openIntv not called before enterIntvBefore");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000545 DEBUG(dbgs() << " enterIntvBefore " << Idx);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000546 Idx = Idx.getBaseIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000547 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000548 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000549 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000550 return Idx;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000551 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000552 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000553 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000554 assert(MI && "enterIntvBefore called with invalid index");
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000555
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000556 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
557 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000558}
559
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000560SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000561 assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000562 SlotIndex End = LIS.getMBBEndIdx(&MBB);
563 SlotIndex Last = End.getPrevSlot();
564 DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000565 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000566 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000567 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000568 return End;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000569 }
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000570 DEBUG(dbgs() << ": valno " << ParentVNI->id);
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000571 VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000572 LIS.getLastSplitPoint(Edit->getParent(), &MBB));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000573 RegAssign.insert(VNI->def, End, OpenIdx);
Eric Christopher0f438112011-02-03 06:18:29 +0000574 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000575 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000576}
577
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000578/// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000579void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000580 useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000581}
582
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000583void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
Eric Christopher0f438112011-02-03 06:18:29 +0000584 assert(OpenIdx && "openIntv not called before useIntv");
585 DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
586 RegAssign.insert(Start, End, OpenIdx);
587 DEBUG(dump());
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000588}
589
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000590SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
Eric Christopher0f438112011-02-03 06:18:29 +0000591 assert(OpenIdx && "openIntv not called before leaveIntvAfter");
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000592 DEBUG(dbgs() << " leaveIntvAfter " << Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000593
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000594 // The interval must be live beyond the instruction at Idx.
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000595 Idx = Idx.getBoundaryIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000596 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000597 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000598 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000599 return Idx.getNextSlot();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000600 }
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000601 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000602
Jakob Stoklund Olesen01cb34b2011-02-08 18:50:18 +0000603 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
604 assert(MI && "No instruction at index");
605 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(),
606 llvm::next(MachineBasicBlock::iterator(MI)));
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000607 return VNI->def;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000608}
609
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000610SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
611 assert(OpenIdx && "openIntv not called before leaveIntvBefore");
612 DEBUG(dbgs() << " leaveIntvBefore " << Idx);
613
614 // The interval must be live into the instruction at Idx.
615 Idx = Idx.getBoundaryIndex();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000616 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000617 if (!ParentVNI) {
618 DEBUG(dbgs() << ": not live\n");
619 return Idx.getNextSlot();
620 }
621 DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
622
623 MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
624 assert(MI && "No instruction at index");
625 VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
626 return VNI->def;
627}
628
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000629SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
Eric Christopher0f438112011-02-03 06:18:29 +0000630 assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000631 SlotIndex Start = LIS.getMBBStartIdx(&MBB);
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000632 DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000633
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000634 VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000635 if (!ParentVNI) {
Jakob Stoklund Olesen9b24afe2010-10-07 17:56:35 +0000636 DEBUG(dbgs() << ": not live\n");
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000637 return Start;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000638 }
639
Eric Christopher0f438112011-02-03 06:18:29 +0000640 VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000641 MBB.SkipPHIsAndLabels(MBB.begin()));
Eric Christopher0f438112011-02-03 06:18:29 +0000642 RegAssign.insert(Start, VNI->def, OpenIdx);
643 DEBUG(dump());
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000644 return VNI->def;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000645}
646
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000647void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
648 assert(OpenIdx && "openIntv not called before overlapIntv");
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000649 const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
650 assert(ParentVNI == Edit->getParent().getVNInfoAt(End.getPrevSlot()) &&
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000651 "Parent changes value in extended range");
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000652 assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
653 "Range cannot span basic blocks");
654
Jakob Stoklund Olesend3fdaeb2011-03-02 00:49:28 +0000655 // The complement interval will be extended as needed by extendRange().
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000656 markComplexMapped(0, ParentVNI);
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000657 DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
658 RegAssign.insert(Start, End, OpenIdx);
659 DEBUG(dump());
660}
661
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000662/// closeIntv - Indicate that we are done editing the currently open
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000663/// LiveInterval, and ranges can be trimmed.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000664void SplitEditor::closeIntv() {
Eric Christopher0f438112011-02-03 06:18:29 +0000665 assert(OpenIdx && "openIntv not called before closeIntv");
666 OpenIdx = 0;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000667}
668
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000669/// transferSimpleValues - Transfer all simply defined values to the new live
670/// ranges.
671/// Values that were rematerialized or that have multiple defs are left alone.
672bool SplitEditor::transferSimpleValues() {
673 bool Skipped = false;
674 RegAssignMap::const_iterator AssignI = RegAssign.begin();
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000675 for (LiveInterval::const_iterator ParentI = Edit->getParent().begin(),
676 ParentE = Edit->getParent().end(); ParentI != ParentE; ++ParentI) {
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000677 DEBUG(dbgs() << " blit " << *ParentI << ':');
678 VNInfo *ParentVNI = ParentI->valno;
679 // RegAssign has holes where RegIdx 0 should be used.
680 SlotIndex Start = ParentI->start;
681 AssignI.advanceTo(Start);
682 do {
683 unsigned RegIdx;
684 SlotIndex End = ParentI->end;
685 if (!AssignI.valid()) {
686 RegIdx = 0;
687 } else if (AssignI.start() <= Start) {
688 RegIdx = AssignI.value();
689 if (AssignI.stop() < End) {
690 End = AssignI.stop();
691 ++AssignI;
692 }
693 } else {
694 RegIdx = 0;
695 End = std::min(End, AssignI.start());
696 }
697 DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx);
698 if (VNInfo *VNI = Values.lookup(std::make_pair(RegIdx, ParentVNI->id))) {
699 DEBUG(dbgs() << ':' << VNI->id);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000700 Edit->get(RegIdx)->addRange(LiveRange(Start, End, VNI));
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000701 } else
702 Skipped = true;
703 Start = End;
704 } while (Start != ParentI->end);
705 DEBUG(dbgs() << '\n');
706 }
707 return Skipped;
708}
709
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000710void SplitEditor::extendPHIKillRanges() {
711 // Extend live ranges to be live-out for successor PHI values.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000712 for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
713 E = Edit->getParent().vni_end(); I != E; ++I) {
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000714 const VNInfo *PHIVNI = *I;
715 if (PHIVNI->isUnused() || !PHIVNI->isPHIDef())
716 continue;
717 unsigned RegIdx = RegAssign.lookup(PHIVNI->def);
718 MachineBasicBlock *MBB = LIS.getMBBFromIndex(PHIVNI->def);
719 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
720 PE = MBB->pred_end(); PI != PE; ++PI) {
721 SlotIndex End = LIS.getMBBEndIdx(*PI).getPrevSlot();
722 // The predecessor may not have a live-out value. That is OK, like an
723 // undef PHI operand.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000724 if (Edit->getParent().liveAt(End)) {
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000725 assert(RegAssign.lookup(End) == RegIdx &&
726 "Different register assignment in phi predecessor");
727 extendRange(RegIdx, End);
728 }
729 }
730 }
731}
732
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000733/// rewriteAssigned - Rewrite all uses of Edit->getReg().
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000734void SplitEditor::rewriteAssigned(bool ExtendRanges) {
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000735 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000736 RE = MRI.reg_end(); RI != RE;) {
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000737 MachineOperand &MO = RI.getOperand();
738 MachineInstr *MI = MO.getParent();
739 ++RI;
Eric Christopher0f438112011-02-03 06:18:29 +0000740 // LiveDebugVariables should have handled all DBG_VALUE instructions.
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000741 if (MI->isDebugValue()) {
742 DEBUG(dbgs() << "Zapping " << *MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000743 MO.setReg(0);
744 continue;
745 }
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +0000746
747 // <undef> operands don't really read the register, so just assign them to
748 // the complement.
749 if (MO.isUse() && MO.isUndef()) {
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000750 MO.setReg(Edit->get(0)->reg);
Jakob Stoklund Olesena372d162011-02-09 21:52:09 +0000751 continue;
752 }
753
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000754 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000755 Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
Eric Christopher0f438112011-02-03 06:18:29 +0000756
757 // Rewrite to the mapped register at Idx.
758 unsigned RegIdx = RegAssign.lookup(Idx);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000759 MO.setReg(Edit->get(RegIdx)->reg);
Eric Christopher0f438112011-02-03 06:18:29 +0000760 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
761 << Idx << ':' << RegIdx << '\t' << *MI);
762
763 // Extend liveness to Idx.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000764 if (ExtendRanges)
765 extendRange(RegIdx, Idx);
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000766 }
767}
768
Eric Christopher0f438112011-02-03 06:18:29 +0000769/// rewriteSplit - Rewrite uses of Intvs[0] according to the ConEQ mapping.
770void SplitEditor::rewriteComponents(const SmallVectorImpl<LiveInterval*> &Intvs,
771 const ConnectedVNInfoEqClasses &ConEq) {
772 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Intvs[0]->reg),
773 RE = MRI.reg_end(); RI != RE;) {
774 MachineOperand &MO = RI.getOperand();
775 MachineInstr *MI = MO.getParent();
776 ++RI;
777 if (MO.isUse() && MO.isUndef())
Jakob Stoklund Olesen9d999772010-10-21 18:47:08 +0000778 continue;
Eric Christopher0f438112011-02-03 06:18:29 +0000779 // DBG_VALUE instructions should have been eliminated earlier.
780 SlotIndex Idx = LIS.getInstructionIndex(MI);
781 Idx = MO.isUse() ? Idx.getUseIndex() : Idx.getDefIndex();
782 DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
783 << Idx << ':');
784 const VNInfo *VNI = Intvs[0]->getVNInfoAt(Idx);
785 assert(VNI && "Interval not live at use.");
786 MO.setReg(Intvs[ConEq.getEqClass(VNI)]->reg);
787 DEBUG(dbgs() << VNI->id << '\t' << *MI);
Eric Christopher463a2972011-02-03 05:40:54 +0000788 }
Eric Christopher463a2972011-02-03 05:40:54 +0000789}
Jakob Stoklund Olesen2cd21112011-02-03 00:54:23 +0000790
Eric Christopher463a2972011-02-03 05:40:54 +0000791void SplitEditor::finish() {
Eric Christopher0f438112011-02-03 06:18:29 +0000792 assert(OpenIdx == 0 && "Previous LI not closed before rewrite");
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000793 ++NumFinished;
Eric Christopher463a2972011-02-03 05:40:54 +0000794
Eric Christopher0f438112011-02-03 06:18:29 +0000795 // At this point, the live intervals in Edit contain VNInfos corresponding to
796 // the inserted copies.
797
798 // Add the original defs from the parent interval.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000799 for (LiveInterval::const_vni_iterator I = Edit->getParent().vni_begin(),
800 E = Edit->getParent().vni_end(); I != E; ++I) {
Eric Christopher0f438112011-02-03 06:18:29 +0000801 const VNInfo *ParentVNI = *I;
Jakob Stoklund Olesen9ecd1e72011-02-04 00:59:23 +0000802 if (ParentVNI->isUnused())
803 continue;
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000804 unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000805 defValue(RegIdx, ParentVNI, ParentVNI->def);
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000806 // Mark rematted values as complex everywhere to force liveness computation.
807 // The new live ranges may be truncated.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000808 if (Edit->didRematerialize(ParentVNI))
809 for (unsigned i = 0, e = Edit->size(); i != e; ++i)
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000810 markComplexMapped(i, ParentVNI);
Eric Christopher0f438112011-02-03 06:18:29 +0000811 }
812
813#ifndef NDEBUG
814 // Every new interval must have a def by now, otherwise the split is bogus.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000815 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I)
Eric Christopher0f438112011-02-03 06:18:29 +0000816 assert((*I)->hasAtLeastOneValue() && "Split interval has no value");
817#endif
818
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000819 // Transfer the simply mapped values, check if any are complex.
820 bool Complex = transferSimpleValues();
821 if (Complex)
822 extendPHIKillRanges();
823 else
824 ++NumSimple;
Eric Christopher0f438112011-02-03 06:18:29 +0000825
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000826 // Rewrite virtual registers, possibly extending ranges.
827 rewriteAssigned(Complex);
Eric Christopher0f438112011-02-03 06:18:29 +0000828
829 // FIXME: Delete defs that were rematted everywhere.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000830
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +0000831 // Get rid of unused values and set phi-kill flags.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000832 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I)
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000833 (*I)->RenumberValues(LIS);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000834
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000835 // Now check if any registers were separated into multiple components.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000836 ConnectedVNInfoEqClasses ConEQ(LIS);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000837 for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000838 // Don't use iterators, they are invalidated by create() below.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000839 LiveInterval *li = Edit->get(i);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000840 unsigned NumComp = ConEQ.Classify(li);
841 if (NumComp <= 1)
842 continue;
843 DEBUG(dbgs() << " " << NumComp << " components: " << *li << '\n');
844 SmallVector<LiveInterval*, 8> dups;
845 dups.push_back(li);
846 for (unsigned i = 1; i != NumComp; ++i)
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000847 dups.push_back(&Edit->create(MRI, LIS, VRM));
Eric Christopher0f438112011-02-03 06:18:29 +0000848 rewriteComponents(dups, ConEQ);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000849 ConEQ.Distribute(&dups[0]);
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000850 }
851
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000852 // Calculate spill weight and allocation hints for new intervals.
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000853 VirtRegAuxInfo vrai(VRM.getMachineFunction(), LIS, SA.Loops);
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000854 for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000855 LiveInterval &li = **I;
Jakob Stoklund Olesen9db3ea42010-08-10 18:37:40 +0000856 vrai.CalculateRegClass(li.reg);
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000857 vrai.CalculateWeightAndHint(li);
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000858 DEBUG(dbgs() << " new interval " << MRI.getRegClass(li.reg)->getName()
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000859 << ":" << li << '\n');
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +0000860 }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000861}
862
863
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000864//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000865// Single Block Splitting
866//===----------------------------------------------------------------------===//
867
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000868/// getMultiUseBlocks - if CurLI has more than one use in a basic block, it
869/// may be an advantage to split CurLI for the duration of the block.
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000870bool SplitAnalysis::getMultiUseBlocks(BlockPtrSet &Blocks) {
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000871 // If CurLI is local to one block, there is no point to splitting it.
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000872 if (LiveBlocks.size() <= 1)
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000873 return false;
874 // Add blocks with multiple uses.
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000875 for (unsigned i = 0, e = LiveBlocks.size(); i != e; ++i) {
876 const BlockInfo &BI = LiveBlocks[i];
877 if (!BI.Uses)
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000878 continue;
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000879 unsigned Instrs = UsingBlocks.lookup(BI.MBB);
880 if (Instrs <= 1)
881 continue;
882 if (Instrs == 2 && BI.LiveIn && BI.LiveOut && !BI.LiveThrough)
883 continue;
884 Blocks.insert(BI.MBB);
885 }
Jakob Stoklund Olesen2bfb3242010-10-22 22:48:56 +0000886 return !Blocks.empty();
887}
888
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000889/// splitSingleBlocks - Split CurLI into a separate live interval inside each
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000890/// basic block in Blocks.
891void SplitEditor::splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks) {
Jakob Stoklund Olesene1f543f2010-08-12 18:50:55 +0000892 DEBUG(dbgs() << " splitSingleBlocks for " << Blocks.size() << " blocks.\n");
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000893
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000894 for (unsigned i = 0, e = SA.LiveBlocks.size(); i != e; ++i) {
895 const SplitAnalysis::BlockInfo &BI = SA.LiveBlocks[i];
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000896 if (!BI.Uses || !Blocks.count(BI.MBB))
897 continue;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000898
899 openIntv();
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000900 SlotIndex SegStart = enterIntvBefore(BI.FirstUse);
Jakob Stoklund Olesenc70f6872011-02-23 18:26:31 +0000901 if (!BI.LiveOut || BI.LastUse < BI.LastSplitPoint) {
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000902 useIntv(SegStart, leaveIntvAfter(BI.LastUse));
903 } else {
Jakob Stoklund Olesenc70f6872011-02-23 18:26:31 +0000904 // The last use is after the last valid split point.
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000905 SlotIndex SegStop = leaveIntvBefore(BI.LastSplitPoint);
906 useIntv(SegStart, SegStop);
907 overlapIntv(SegStop, BI.LastUse);
908 }
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000909 closeIntv();
910 }
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000911 finish();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000912}