blob: d10ca1a7ff91812383588049fbc57228a1c1a735 [file] [log] [blame]
Alkis Evlogimenosc794a902004-02-23 23:08:11 +00001//===-- llvm/CodeGen/VirtRegMap.cpp - Virtual Register Map ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosc794a902004-02-23 23:08:11 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnere2b77d52004-09-30 01:54:45 +000010// This file implements the VirtRegMap class.
11//
Dan Gohman4a618822010-02-10 16:03:48 +000012// It also contains implementations of the Spiller interface, which, given a
Chris Lattnere2b77d52004-09-30 01:54:45 +000013// virtual register map and a machine function, eliminates all virtual
14// references by replacing them with physical register references - adding spill
Alkis Evlogimenos1dd872c2004-02-24 08:58:30 +000015// code as necessary.
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000016//
17//===----------------------------------------------------------------------===//
18
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000019#include "llvm/CodeGen/VirtRegMap.h"
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +000020#include "LiveDebugVariables.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ADT/STLExtras.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000022#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +000023#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Evan Chengb53825b2012-09-21 20:04:28 +000024#include "llvm/CodeGen/LiveStackAnalysis.h"
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnere2b77d52004-09-30 01:54:45 +000026#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng499ffa92008-04-11 17:53:36 +000027#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +000029#include "llvm/CodeGen/Passes.h"
Quentin Colombetfa403ab2013-09-25 00:26:17 +000030#include "llvm/IR/Function.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000031#include "llvm/Support/Compiler.h"
Evan Chenga1968b02009-02-11 08:24:21 +000032#include "llvm/Support/Debug.h"
Daniel Dunbar796e43e2009-07-24 10:36:58 +000033#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Target/TargetInstrInfo.h"
35#include "llvm/Target/TargetMachine.h"
36#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000037#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattnerc8b07dd2004-10-26 15:35:58 +000038#include <algorithm>
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000039using namespace llvm;
40
Chandler Carruth1b9dde02014-04-22 02:02:50 +000041#define DEBUG_TYPE "regalloc"
42
Jakob Stoklund Olesen53e2e482011-09-15 18:31:13 +000043STATISTIC(NumSpillSlots, "Number of spill slots allocated");
44STATISTIC(NumIdCopies, "Number of identity moves eliminated after rewriting");
Dan Gohmand78c4002008-05-13 00:00:25 +000045
Chris Lattnere2b77d52004-09-30 01:54:45 +000046//===----------------------------------------------------------------------===//
47// VirtRegMap implementation
48//===----------------------------------------------------------------------===//
49
Owen Andersond37ddf52009-03-13 05:55:11 +000050char VirtRegMap::ID = 0;
51
Owen Andersondf7a4f22010-10-07 22:25:06 +000052INITIALIZE_PASS(VirtRegMap, "virtregmap", "Virtual Register Map", false, false)
Owen Andersond37ddf52009-03-13 05:55:11 +000053
54bool VirtRegMap::runOnMachineFunction(MachineFunction &mf) {
Evan Cheng085caf12009-06-14 20:22:55 +000055 MRI = &mf.getRegInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +000056 TII = mf.getSubtarget().getInstrInfo();
57 TRI = mf.getSubtarget().getRegisterInfo();
Owen Andersond37ddf52009-03-13 05:55:11 +000058 MF = &mf;
Lang Hames05fb9632009-11-03 23:52:08 +000059
Owen Andersond37ddf52009-03-13 05:55:11 +000060 Virt2PhysMap.clear();
61 Virt2StackSlotMap.clear();
Owen Andersond37ddf52009-03-13 05:55:11 +000062 Virt2SplitMap.clear();
Evan Cheng3f778052009-05-04 03:30:11 +000063
Chris Lattner13a5dcd2006-09-05 02:12:02 +000064 grow();
Owen Andersond37ddf52009-03-13 05:55:11 +000065 return false;
Chris Lattner13a5dcd2006-09-05 02:12:02 +000066}
67
Chris Lattnere2b77d52004-09-30 01:54:45 +000068void VirtRegMap::grow() {
Jakob Stoklund Olesend82ac372011-01-09 21:58:20 +000069 unsigned NumRegs = MF->getRegInfo().getNumVirtRegs();
70 Virt2PhysMap.resize(NumRegs);
71 Virt2StackSlotMap.resize(NumRegs);
Jakob Stoklund Olesend82ac372011-01-09 21:58:20 +000072 Virt2SplitMap.resize(NumRegs);
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000073}
74
Jakob Stoklund Olesen39aed732010-11-16 00:41:01 +000075unsigned VirtRegMap::createSpillSlot(const TargetRegisterClass *RC) {
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +000076 unsigned Size = TRI->getSpillSize(*RC);
77 unsigned Align = TRI->getSpillAlignment(*RC);
78 int SS = MF->getFrameInfo().CreateSpillStackObject(Size, Align);
Jakob Stoklund Olesen53e2e482011-09-15 18:31:13 +000079 ++NumSpillSlots;
Jakob Stoklund Olesen39aed732010-11-16 00:41:01 +000080 return SS;
81}
82
Jakob Stoklund Olesen1dd82dd2012-12-04 00:30:22 +000083bool VirtRegMap::hasPreferredPhys(unsigned VirtReg) {
84 unsigned Hint = MRI->getSimpleHint(VirtReg);
85 if (!Hint)
Matt Arsenault50451d42016-06-02 18:37:21 +000086 return false;
Jakob Stoklund Olesen1dd82dd2012-12-04 00:30:22 +000087 if (TargetRegisterInfo::isVirtualRegister(Hint))
88 Hint = getPhys(Hint);
89 return getPhys(VirtReg) == Hint;
90}
91
Jakob Stoklund Olesen74052b02012-12-03 23:23:50 +000092bool VirtRegMap::hasKnownPreference(unsigned VirtReg) {
93 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(VirtReg);
94 if (TargetRegisterInfo::isPhysicalRegister(Hint.second))
95 return true;
96 if (TargetRegisterInfo::isVirtualRegister(Hint.second))
97 return hasPhys(Hint.second);
98 return false;
99}
100
Chris Lattnere2b77d52004-09-30 01:54:45 +0000101int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000102 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +0000103 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattnere2b77d52004-09-30 01:54:45 +0000104 "attempt to assign stack slot to already spilled register");
Owen Andersond37ddf52009-03-13 05:55:11 +0000105 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg);
Jakob Stoklund Olesen39aed732010-11-16 00:41:01 +0000106 return Virt2StackSlotMap[virtReg] = createSpillSlot(RC);
Chris Lattnere2b77d52004-09-30 01:54:45 +0000107}
108
Evan Cheng6d563682008-02-27 03:04:06 +0000109void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) {
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000110 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +0000111 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattnere2b77d52004-09-30 01:54:45 +0000112 "attempt to assign stack slot to already spilled register");
Evan Cheng6d563682008-02-27 03:04:06 +0000113 assert((SS >= 0 ||
Matthias Braun941a7052016-07-28 18:40:00 +0000114 (SS >= MF->getFrameInfo().getObjectIndexBegin())) &&
Evan Cheng8be98c12007-04-04 07:40:01 +0000115 "illegal fixed frame index");
Evan Cheng6d563682008-02-27 03:04:06 +0000116 Virt2StackSlotMap[virtReg] = SS;
Alkis Evlogimenosfd735bc2004-05-29 20:38:05 +0000117}
118
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000119void VirtRegMap::print(raw_ostream &OS, const Module*) const {
120 OS << "********** REGISTER MAP **********\n";
121 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
122 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
123 if (Virt2PhysMap[Reg] != (unsigned)VirtRegMap::NO_PHYS_REG) {
124 OS << '[' << PrintReg(Reg, TRI) << " -> "
125 << PrintReg(Virt2PhysMap[Reg], TRI) << "] "
Craig Toppercf0444b2014-11-17 05:50:14 +0000126 << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n";
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000127 }
128 }
129
130 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
131 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
132 if (Virt2StackSlotMap[Reg] != VirtRegMap::NO_STACK_SLOT) {
133 OS << '[' << PrintReg(Reg, TRI) << " -> fi#" << Virt2StackSlotMap[Reg]
Craig Toppercf0444b2014-11-17 05:50:14 +0000134 << "] " << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n";
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000135 }
136 }
137 OS << '\n';
138}
139
Manman Ren19f49ac2012-09-11 22:23:19 +0000140#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000141LLVM_DUMP_METHOD void VirtRegMap::dump() const {
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000142 print(dbgs());
143}
Manman Ren742534c2012-09-06 19:06:06 +0000144#endif
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000145
146//===----------------------------------------------------------------------===//
147// VirtRegRewriter
148//===----------------------------------------------------------------------===//
149//
150// The VirtRegRewriter is the last of the register allocator passes.
151// It rewrites virtual registers to physical registers as specified in the
152// VirtRegMap analysis. It also updates live-in information on basic blocks
153// according to LiveIntervals.
154//
155namespace {
156class VirtRegRewriter : public MachineFunctionPass {
157 MachineFunction *MF;
158 const TargetMachine *TM;
159 const TargetRegisterInfo *TRI;
160 const TargetInstrInfo *TII;
161 MachineRegisterInfo *MRI;
162 SlotIndexes *Indexes;
163 LiveIntervals *LIS;
164 VirtRegMap *VRM;
165
166 void rewrite();
167 void addMBBLiveIns();
Matthias Braunca4e8422015-06-16 18:22:28 +0000168 bool readsUndefSubreg(const MachineOperand &MO) const;
Matthias Brauncc580052015-09-09 18:07:54 +0000169 void addLiveInsForSubRanges(const LiveInterval &LI, unsigned PhysReg) const;
Matthias Braun152e7c82016-07-09 00:19:07 +0000170 void handleIdentityCopy(MachineInstr &MI) const;
Matthias Braunf0b68d32017-03-17 00:41:39 +0000171 void expandCopyBundle(MachineInstr &MI) const;
Matthias Brauncc580052015-09-09 18:07:54 +0000172
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000173public:
174 static char ID;
175 VirtRegRewriter() : MachineFunctionPass(ID) {}
176
Craig Topper4584cd52014-03-07 09:26:03 +0000177 void getAnalysisUsage(AnalysisUsage &AU) const override;
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000178
Craig Topper4584cd52014-03-07 09:26:03 +0000179 bool runOnMachineFunction(MachineFunction&) override;
Derek Schuff42666ee2016-03-29 17:40:22 +0000180 MachineFunctionProperties getSetProperties() const override {
181 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000182 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff42666ee2016-03-29 17:40:22 +0000183 }
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000184};
185} // end anonymous namespace
186
187char &llvm::VirtRegRewriterID = VirtRegRewriter::ID;
188
189INITIALIZE_PASS_BEGIN(VirtRegRewriter, "virtregrewriter",
190 "Virtual Register Rewriter", false, false)
191INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
192INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
193INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
Evan Chengb53825b2012-09-21 20:04:28 +0000194INITIALIZE_PASS_DEPENDENCY(LiveStacks)
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000195INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
196INITIALIZE_PASS_END(VirtRegRewriter, "virtregrewriter",
197 "Virtual Register Rewriter", false, false)
198
199char VirtRegRewriter::ID = 0;
200
201void VirtRegRewriter::getAnalysisUsage(AnalysisUsage &AU) const {
202 AU.setPreservesCFG();
203 AU.addRequired<LiveIntervals>();
204 AU.addRequired<SlotIndexes>();
205 AU.addPreserved<SlotIndexes>();
206 AU.addRequired<LiveDebugVariables>();
Evan Chengb53825b2012-09-21 20:04:28 +0000207 AU.addRequired<LiveStacks>();
208 AU.addPreserved<LiveStacks>();
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000209 AU.addRequired<VirtRegMap>();
210 MachineFunctionPass::getAnalysisUsage(AU);
211}
212
213bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) {
214 MF = &fn;
215 TM = &MF->getTarget();
Eric Christopher1c5fce02014-10-13 21:57:44 +0000216 TRI = MF->getSubtarget().getRegisterInfo();
217 TII = MF->getSubtarget().getInstrInfo();
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000218 MRI = &MF->getRegInfo();
219 Indexes = &getAnalysis<SlotIndexes>();
220 LIS = &getAnalysis<LiveIntervals>();
221 VRM = &getAnalysis<VirtRegMap>();
Jakob Stoklund Olesen5bfec692011-02-18 22:03:18 +0000222 DEBUG(dbgs() << "********** REWRITE VIRTUAL REGISTERS **********\n"
223 << "********** Function: "
Craig Toppera538d832012-08-22 06:07:19 +0000224 << MF->getName() << '\n');
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000225 DEBUG(VRM->dump());
226
227 // Add kill flags while we still have virtual registers.
Jakob Stoklund Olesenbb4bdd82012-09-06 18:15:18 +0000228 LIS->addKillFlags(VRM);
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000229
Jakob Stoklund Olesenbe336292012-06-09 00:14:47 +0000230 // Live-in lists on basic blocks are required for physregs.
231 addMBBLiveIns();
232
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000233 // Rewrite virtual registers.
234 rewrite();
235
236 // Write out new DBG_VALUE instructions.
237 getAnalysis<LiveDebugVariables>().emitDebugValues(VRM);
238
239 // All machine operands and other references to virtual registers have been
240 // replaced. Remove the virtual registers and release all the transient data.
241 VRM->clearAllVirt();
242 MRI->clearVirtRegs();
243 return true;
244}
245
Matthias Brauncc580052015-09-09 18:07:54 +0000246void VirtRegRewriter::addLiveInsForSubRanges(const LiveInterval &LI,
247 unsigned PhysReg) const {
248 assert(!LI.empty());
249 assert(LI.hasSubRanges());
250
251 typedef std::pair<const LiveInterval::SubRange *,
252 LiveInterval::const_iterator> SubRangeIteratorPair;
253 SmallVector<SubRangeIteratorPair, 4> SubRanges;
254 SlotIndex First;
255 SlotIndex Last;
256 for (const LiveInterval::SubRange &SR : LI.subranges()) {
257 SubRanges.push_back(std::make_pair(&SR, SR.begin()));
258 if (!First.isValid() || SR.segments.front().start < First)
259 First = SR.segments.front().start;
260 if (!Last.isValid() || SR.segments.back().end > Last)
261 Last = SR.segments.back().end;
262 }
263
264 // Check all mbb start positions between First and Last while
265 // simulatenously advancing an iterator for each subrange.
266 for (SlotIndexes::MBBIndexIterator MBBI = Indexes->findMBBIndex(First);
267 MBBI != Indexes->MBBIndexEnd() && MBBI->first <= Last; ++MBBI) {
268 SlotIndex MBBBegin = MBBI->first;
269 // Advance all subrange iterators so that their end position is just
270 // behind MBBBegin (or the iterator is at the end).
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000271 LaneBitmask LaneMask;
Matthias Brauncc580052015-09-09 18:07:54 +0000272 for (auto &RangeIterPair : SubRanges) {
273 const LiveInterval::SubRange *SR = RangeIterPair.first;
274 LiveInterval::const_iterator &SRI = RangeIterPair.second;
275 while (SRI != SR->end() && SRI->end <= MBBBegin)
276 ++SRI;
277 if (SRI == SR->end())
278 continue;
279 if (SRI->start <= MBBBegin)
280 LaneMask |= SR->LaneMask;
281 }
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000282 if (LaneMask.none())
Matthias Brauncc580052015-09-09 18:07:54 +0000283 continue;
284 MachineBasicBlock *MBB = MBBI->second;
Matthias Braund9da1622015-09-09 18:08:03 +0000285 MBB->addLiveIn(PhysReg, LaneMask);
Matthias Brauncc580052015-09-09 18:07:54 +0000286 }
287}
288
Jakob Stoklund Olesenbe336292012-06-09 00:14:47 +0000289// Compute MBB live-in lists from virtual register live ranges and their
290// assignments.
291void VirtRegRewriter::addMBBLiveIns() {
Jakob Stoklund Olesenbe336292012-06-09 00:14:47 +0000292 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
293 unsigned VirtReg = TargetRegisterInfo::index2VirtReg(Idx);
294 if (MRI->reg_nodbg_empty(VirtReg))
295 continue;
296 LiveInterval &LI = LIS->getInterval(VirtReg);
297 if (LI.empty() || LIS->intervalIsInOneMBB(LI))
298 continue;
299 // This is a virtual register that is live across basic blocks. Its
300 // assigned PhysReg must be marked as live-in to those blocks.
301 unsigned PhysReg = VRM->getPhys(VirtReg);
302 assert(PhysReg != VirtRegMap::NO_PHYS_REG && "Unmapped virtual register.");
303
Matthias Braun279f8362014-12-10 01:13:08 +0000304 if (LI.hasSubRanges()) {
Matthias Brauncc580052015-09-09 18:07:54 +0000305 addLiveInsForSubRanges(LI, PhysReg);
Matthias Braun279f8362014-12-10 01:13:08 +0000306 } else {
Matthias Brauncc580052015-09-09 18:07:54 +0000307 // Go over MBB begin positions and see if we have segments covering them.
308 // The following works because segments and the MBBIndex list are both
309 // sorted by slot indexes.
310 SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin();
311 for (const auto &Seg : LI) {
312 I = Indexes->advanceMBBIndex(I, Seg.start);
313 for (; I != Indexes->MBBIndexEnd() && I->first < Seg.end; ++I) {
314 MachineBasicBlock *MBB = I->second;
315 MBB->addLiveIn(PhysReg);
316 }
Matthias Braun279f8362014-12-10 01:13:08 +0000317 }
Jakob Stoklund Olesenbe336292012-06-09 00:14:47 +0000318 }
319 }
Puyan Lotfibb457b92015-05-22 08:11:26 +0000320
321 // Sort and unique MBB LiveIns as we've not checked if SubReg/PhysReg were in
322 // each MBB's LiveIns set before calling addLiveIn on them.
323 for (MachineBasicBlock &MBB : *MF)
324 MBB.sortUniqueLiveIns();
Jakob Stoklund Olesenbe336292012-06-09 00:14:47 +0000325}
326
Matthias Braunca4e8422015-06-16 18:22:28 +0000327/// Returns true if the given machine operand \p MO only reads undefined lanes.
328/// The function only works for use operands with a subregister set.
329bool VirtRegRewriter::readsUndefSubreg(const MachineOperand &MO) const {
330 // Shortcut if the operand is already marked undef.
331 if (MO.isUndef())
332 return true;
333
334 unsigned Reg = MO.getReg();
335 const LiveInterval &LI = LIS->getInterval(Reg);
336 const MachineInstr &MI = *MO.getParent();
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000337 SlotIndex BaseIndex = LIS->getInstructionIndex(MI);
Matthias Braunca4e8422015-06-16 18:22:28 +0000338 // This code is only meant to handle reading undefined subregisters which
339 // we couldn't properly detect before.
340 assert(LI.liveAt(BaseIndex) &&
341 "Reads of completely dead register should be marked undef already");
342 unsigned SubRegIdx = MO.getSubReg();
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000343 assert(SubRegIdx != 0 && LI.hasSubRanges());
Matthias Braune6a24852015-09-25 21:51:14 +0000344 LaneBitmask UseMask = TRI->getSubRegIndexLaneMask(SubRegIdx);
Matthias Braunca4e8422015-06-16 18:22:28 +0000345 // See if any of the relevant subregister liveranges is defined at this point.
346 for (const LiveInterval::SubRange &SR : LI.subranges()) {
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000347 if ((SR.LaneMask & UseMask).any() && SR.liveAt(BaseIndex))
Matthias Braunca4e8422015-06-16 18:22:28 +0000348 return false;
349 }
350 return true;
351}
352
Matthias Braun152e7c82016-07-09 00:19:07 +0000353void VirtRegRewriter::handleIdentityCopy(MachineInstr &MI) const {
354 if (!MI.isIdentityCopy())
355 return;
356 DEBUG(dbgs() << "Identity copy: " << MI);
357 ++NumIdCopies;
358
359 // Copies like:
360 // %R0 = COPY %R0<undef>
361 // %AL = COPY %AL, %EAX<imp-def>
362 // give us additional liveness information: The target (super-)register
363 // must not be valid before this point. Replace the COPY with a KILL
364 // instruction to maintain this information.
365 if (MI.getOperand(0).isUndef() || MI.getNumOperands() > 2) {
366 MI.setDesc(TII->get(TargetOpcode::KILL));
367 DEBUG(dbgs() << " replace by: " << MI);
368 return;
369 }
370
371 if (Indexes)
Matthias Braunfa289ec2017-03-17 00:41:33 +0000372 Indexes->removeSingleMachineInstrFromMaps(MI);
373 MI.eraseFromBundle();
Matthias Braun152e7c82016-07-09 00:19:07 +0000374 DEBUG(dbgs() << " deleted.\n");
375}
376
Matthias Braunf0b68d32017-03-17 00:41:39 +0000377/// The liverange splitting logic sometimes produces bundles of copies when
378/// subregisters are involved. Expand these into a sequence of copy instructions
379/// after processing the last in the bundle. Does not update LiveIntervals
380/// which we shouldn't need for this instruction anymore.
381void VirtRegRewriter::expandCopyBundle(MachineInstr &MI) const {
382 if (!MI.isCopy())
383 return;
384
385 if (MI.isBundledWithPred() && !MI.isBundledWithSucc()) {
386 // Only do this when the complete bundle is made out of COPYs.
Matthias Braun8445cbd2017-03-21 21:58:08 +0000387 MachineBasicBlock &MBB = *MI.getParent();
Matthias Braunf0b68d32017-03-17 00:41:39 +0000388 for (MachineBasicBlock::reverse_instr_iterator I =
Matthias Braun8445cbd2017-03-21 21:58:08 +0000389 std::next(MI.getReverseIterator()), E = MBB.instr_rend();
390 I != E && I->isBundledWithSucc(); ++I) {
Matthias Braunf0b68d32017-03-17 00:41:39 +0000391 if (!I->isCopy())
392 return;
393 }
394
395 for (MachineBasicBlock::reverse_instr_iterator I = MI.getReverseIterator();
396 I->isBundledWithPred(); ) {
397 MachineInstr &MI = *I;
398 ++I;
399
400 MI.unbundleFromPred();
401 if (Indexes)
402 Indexes->insertMachineInstrInMaps(MI);
403 }
404 }
405}
406
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000407void VirtRegRewriter::rewrite() {
Matthias Brauna25e13a2015-03-19 00:21:58 +0000408 bool NoSubRegLiveness = !MRI->subRegLivenessEnabled();
Jakob Stoklund Olesen71d3b892011-04-27 17:42:31 +0000409 SmallVector<unsigned, 8> SuperDeads;
410 SmallVector<unsigned, 8> SuperDefs;
Jakob Stoklund Olesen5bfec692011-02-18 22:03:18 +0000411 SmallVector<unsigned, 8> SuperKills;
Logan Chien18583d72014-02-25 16:57:28 +0000412
Jakob Stoklund Olesen5bfec692011-02-18 22:03:18 +0000413 for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
414 MBBI != MBBE; ++MBBI) {
415 DEBUG(MBBI->print(dbgs(), Indexes));
Evan Chengd42aba52012-01-19 07:46:36 +0000416 for (MachineBasicBlock::instr_iterator
417 MII = MBBI->instr_begin(), MIE = MBBI->instr_end(); MII != MIE;) {
Duncan P. N. Exon Smithf1ff53e2015-10-09 22:56:24 +0000418 MachineInstr *MI = &*MII;
Jakob Stoklund Olesen5bfec692011-02-18 22:03:18 +0000419 ++MII;
420
421 for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
422 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
423 MachineOperand &MO = *MOI;
Jakob Stoklund Olesena0cf42f2012-02-17 19:07:56 +0000424
425 // Make sure MRI knows about registers clobbered by regmasks.
426 if (MO.isRegMask())
427 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
428
Jakob Stoklund Olesen5bfec692011-02-18 22:03:18 +0000429 if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
430 continue;
431 unsigned VirtReg = MO.getReg();
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000432 unsigned PhysReg = VRM->getPhys(VirtReg);
433 assert(PhysReg != VirtRegMap::NO_PHYS_REG &&
434 "Instruction uses unmapped VirtReg");
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000435 assert(!MRI->isReserved(PhysReg) && "Reserved register assignment");
Jakob Stoklund Olesen5bfec692011-02-18 22:03:18 +0000436
437 // Preserve semantics of sub-register operands.
Matthias Braunca4e8422015-06-16 18:22:28 +0000438 unsigned SubReg = MO.getSubReg();
439 if (SubReg != 0) {
440 if (NoSubRegLiveness) {
441 // A virtual register kill refers to the whole register, so we may
442 // have to add <imp-use,kill> operands for the super-register. A
443 // partial redef always kills and redefines the super-register.
444 if (MO.readsReg() && (MO.isDef() || MO.isKill()))
445 SuperKills.push_back(PhysReg);
Jakob Stoklund Olesend5d39bb2011-10-05 00:01:48 +0000446
Matthias Braunca4e8422015-06-16 18:22:28 +0000447 if (MO.isDef()) {
448 // Also add implicit defs for the super-register.
Matthias Braund70caaf2014-12-10 01:13:04 +0000449 if (MO.isDead())
450 SuperDeads.push_back(PhysReg);
451 else
452 SuperDefs.push_back(PhysReg);
453 }
Matthias Braunca4e8422015-06-16 18:22:28 +0000454 } else {
455 if (MO.isUse()) {
456 if (readsUndefSubreg(MO))
457 // We need to add an <undef> flag if the subregister is
458 // completely undefined (and we are not adding super-register
459 // defs).
460 MO.setIsUndef(true);
461 } else if (!MO.isDead()) {
462 assert(MO.isDef());
Matthias Braunca4e8422015-06-16 18:22:28 +0000463 }
Jakob Stoklund Olesend5d39bb2011-10-05 00:01:48 +0000464 }
Jakob Stoklund Olesen5bfec692011-02-18 22:03:18 +0000465
Matthias Braunfa289ec2017-03-17 00:41:33 +0000466 // The <def,undef> and <def,internal> flags only make sense for
467 // sub-register defs, and we are substituting a full physreg. An
468 // <imp-use,kill> operand from the SuperKills list will represent the
469 // partial read of the super-register.
470 if (MO.isDef()) {
Matthias Braunca4e8422015-06-16 18:22:28 +0000471 MO.setIsUndef(false);
Matthias Braunfa289ec2017-03-17 00:41:33 +0000472 MO.setIsInternalRead(false);
473 }
Matthias Braunca4e8422015-06-16 18:22:28 +0000474
Jakob Stoklund Olesen5bfec692011-02-18 22:03:18 +0000475 // PhysReg operands cannot have subregister indexes.
Matthias Braunca4e8422015-06-16 18:22:28 +0000476 PhysReg = TRI->getSubReg(PhysReg, SubReg);
Jakob Stoklund Olesen5bfec692011-02-18 22:03:18 +0000477 assert(PhysReg && "Invalid SubReg for physical register");
478 MO.setSubReg(0);
479 }
480 // Rewrite. Note we could have used MachineOperand::substPhysReg(), but
481 // we need the inlining here.
482 MO.setReg(PhysReg);
483 }
484
485 // Add any missing super-register kills after rewriting the whole
486 // instruction.
487 while (!SuperKills.empty())
488 MI->addRegisterKilled(SuperKills.pop_back_val(), TRI, true);
489
Jakob Stoklund Olesen71d3b892011-04-27 17:42:31 +0000490 while (!SuperDeads.empty())
491 MI->addRegisterDead(SuperDeads.pop_back_val(), TRI, true);
492
493 while (!SuperDefs.empty())
494 MI->addRegisterDefined(SuperDefs.pop_back_val(), TRI);
495
Jakob Stoklund Olesen5bfec692011-02-18 22:03:18 +0000496 DEBUG(dbgs() << "> " << *MI);
497
Matthias Braunf0b68d32017-03-17 00:41:39 +0000498 expandCopyBundle(*MI);
499
Matthias Braun152e7c82016-07-09 00:19:07 +0000500 // We can remove identity copies right now.
501 handleIdentityCopy(*MI);
Jakob Stoklund Olesen5bfec692011-02-18 22:03:18 +0000502 }
503 }
Jakob Stoklund Olesen5bfec692011-02-18 22:03:18 +0000504}