blob: 7d3b0cea1c150ccc237cc22db99d1f3d932d4a51 [file] [log] [blame]
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +00001//===-- llvm/CodeGen/VirtRegMap.cpp - Virtual Register Map ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Evlogimenos34d9bc92004-02-23 23:08:11 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner8c4d88d2004-09-30 01:54:45 +000010// This file implements the VirtRegMap class.
11//
Dan Gohmanf451cb82010-02-10 16:03:48 +000012// It also contains implementations of the Spiller interface, which, given a
Chris Lattner8c4d88d2004-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 Evlogimenos0d6c5b62004-02-24 08:58:30 +000015// code as necessary.
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000016//
17//===----------------------------------------------------------------------===//
18
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000019#include "llvm/CodeGen/VirtRegMap.h"
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +000020#include "LiveDebugVariables.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/ADT/STLExtras.h"
Stephen Hines36b56882014-04-23 16:57:46 -070022#include "llvm/ADT/SparseSet.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +000024#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Evan Chengbb36a432012-09-21 20:04:28 +000025#include "llvm/CodeGen/LiveStackAnalysis.h"
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner8c4d88d2004-09-30 01:54:45 +000027#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng4cce6b42008-04-11 17:53:36 +000028#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +000030#include "llvm/CodeGen/Passes.h"
Quentin Colombetce734f12013-09-25 00:26:17 +000031#include "llvm/IR/Function.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000033#include "llvm/Support/Compiler.h"
Evan Cheng752272a2009-02-11 08:24:21 +000034#include "llvm/Support/Debug.h"
Daniel Dunbar1cd1d982009-07-24 10:36:58 +000035#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000036#include "llvm/Target/TargetInstrInfo.h"
37#include "llvm/Target/TargetMachine.h"
38#include "llvm/Target/TargetRegisterInfo.h"
Stephen Hines37ed9c12014-12-01 14:51:49 -080039#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner27f29162004-10-26 15:35:58 +000040#include <algorithm>
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000041using namespace llvm;
42
Stephen Hinesdce4a402014-05-29 02:49:00 -070043#define DEBUG_TYPE "regalloc"
44
Jakob Stoklund Olesen01afdb32011-09-15 18:31:13 +000045STATISTIC(NumSpillSlots, "Number of spill slots allocated");
46STATISTIC(NumIdCopies, "Number of identity moves eliminated after rewriting");
Dan Gohman844731a2008-05-13 00:00:25 +000047
Chris Lattner8c4d88d2004-09-30 01:54:45 +000048//===----------------------------------------------------------------------===//
49// VirtRegMap implementation
50//===----------------------------------------------------------------------===//
51
Owen Anderson49c8aa02009-03-13 05:55:11 +000052char VirtRegMap::ID = 0;
53
Owen Andersonce665bd2010-10-07 22:25:06 +000054INITIALIZE_PASS(VirtRegMap, "virtregmap", "Virtual Register Map", false, false)
Owen Anderson49c8aa02009-03-13 05:55:11 +000055
56bool VirtRegMap::runOnMachineFunction(MachineFunction &mf) {
Evan Cheng90f95f82009-06-14 20:22:55 +000057 MRI = &mf.getRegInfo();
Stephen Hines37ed9c12014-12-01 14:51:49 -080058 TII = mf.getSubtarget().getInstrInfo();
59 TRI = mf.getSubtarget().getRegisterInfo();
Owen Anderson49c8aa02009-03-13 05:55:11 +000060 MF = &mf;
Lang Hames233a60e2009-11-03 23:52:08 +000061
Owen Anderson49c8aa02009-03-13 05:55:11 +000062 Virt2PhysMap.clear();
63 Virt2StackSlotMap.clear();
Owen Anderson49c8aa02009-03-13 05:55:11 +000064 Virt2SplitMap.clear();
Mike Stumpfe095f32009-05-04 18:40:41 +000065
Chris Lattner29268692006-09-05 02:12:02 +000066 grow();
Owen Anderson49c8aa02009-03-13 05:55:11 +000067 return false;
Chris Lattner29268692006-09-05 02:12:02 +000068}
69
Chris Lattner8c4d88d2004-09-30 01:54:45 +000070void VirtRegMap::grow() {
Jakob Stoklund Olesen42e9c962011-01-09 21:58:20 +000071 unsigned NumRegs = MF->getRegInfo().getNumVirtRegs();
72 Virt2PhysMap.resize(NumRegs);
73 Virt2StackSlotMap.resize(NumRegs);
Jakob Stoklund Olesen42e9c962011-01-09 21:58:20 +000074 Virt2SplitMap.resize(NumRegs);
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000075}
76
Jakob Stoklund Olesenb55e91e2010-11-16 00:41:01 +000077unsigned VirtRegMap::createSpillSlot(const TargetRegisterClass *RC) {
78 int SS = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
79 RC->getAlignment());
Jakob Stoklund Olesen01afdb32011-09-15 18:31:13 +000080 ++NumSpillSlots;
Jakob Stoklund Olesenb55e91e2010-11-16 00:41:01 +000081 return SS;
82}
83
Jakob Stoklund Olesen980bddf2012-12-04 00:30:22 +000084bool VirtRegMap::hasPreferredPhys(unsigned VirtReg) {
85 unsigned Hint = MRI->getSimpleHint(VirtReg);
86 if (!Hint)
87 return 0;
88 if (TargetRegisterInfo::isVirtualRegister(Hint))
89 Hint = getPhys(Hint);
90 return getPhys(VirtReg) == Hint;
91}
92
Jakob Stoklund Olesenfc637442012-12-03 23:23:50 +000093bool VirtRegMap::hasKnownPreference(unsigned VirtReg) {
94 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(VirtReg);
95 if (TargetRegisterInfo::isPhysicalRegister(Hint.second))
96 return true;
97 if (TargetRegisterInfo::isVirtualRegister(Hint.second))
98 return hasPhys(Hint.second);
99 return false;
100}
101
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000102int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000103 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000104 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000105 "attempt to assign stack slot to already spilled register");
Owen Anderson49c8aa02009-03-13 05:55:11 +0000106 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg);
Jakob Stoklund Olesenb55e91e2010-11-16 00:41:01 +0000107 return Virt2StackSlotMap[virtReg] = createSpillSlot(RC);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000108}
109
Evan Chengd3653122008-02-27 03:04:06 +0000110void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000111 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000112 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000113 "attempt to assign stack slot to already spilled register");
Evan Chengd3653122008-02-27 03:04:06 +0000114 assert((SS >= 0 ||
Owen Anderson49c8aa02009-03-13 05:55:11 +0000115 (SS >= MF->getFrameInfo()->getObjectIndexBegin())) &&
Evan Cheng91935142007-04-04 07:40:01 +0000116 "illegal fixed frame index");
Evan Chengd3653122008-02-27 03:04:06 +0000117 Virt2StackSlotMap[virtReg] = SS;
Alkis Evlogimenos38af59a2004-05-29 20:38:05 +0000118}
119
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000120void VirtRegMap::print(raw_ostream &OS, const Module*) const {
121 OS << "********** REGISTER MAP **********\n";
122 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
123 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
124 if (Virt2PhysMap[Reg] != (unsigned)VirtRegMap::NO_PHYS_REG) {
125 OS << '[' << PrintReg(Reg, TRI) << " -> "
126 << PrintReg(Virt2PhysMap[Reg], TRI) << "] "
Stephen Hines37ed9c12014-12-01 14:51:49 -0800127 << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n";
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000128 }
129 }
130
131 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
132 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
133 if (Virt2StackSlotMap[Reg] != VirtRegMap::NO_STACK_SLOT) {
134 OS << '[' << PrintReg(Reg, TRI) << " -> fi#" << Virt2StackSlotMap[Reg]
Stephen Hines37ed9c12014-12-01 14:51:49 -0800135 << "] " << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n";
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000136 }
137 }
138 OS << '\n';
139}
140
Manman Renb720be62012-09-11 22:23:19 +0000141#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000142void VirtRegMap::dump() const {
143 print(dbgs());
144}
Manman Ren77e300e2012-09-06 19:06:06 +0000145#endif
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000146
147//===----------------------------------------------------------------------===//
148// VirtRegRewriter
149//===----------------------------------------------------------------------===//
150//
151// The VirtRegRewriter is the last of the register allocator passes.
152// It rewrites virtual registers to physical registers as specified in the
153// VirtRegMap analysis. It also updates live-in information on basic blocks
154// according to LiveIntervals.
155//
156namespace {
157class VirtRegRewriter : public MachineFunctionPass {
158 MachineFunction *MF;
159 const TargetMachine *TM;
160 const TargetRegisterInfo *TRI;
161 const TargetInstrInfo *TII;
162 MachineRegisterInfo *MRI;
163 SlotIndexes *Indexes;
164 LiveIntervals *LIS;
165 VirtRegMap *VRM;
Stephen Hines36b56882014-04-23 16:57:46 -0700166 SparseSet<unsigned> PhysRegs;
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000167
168 void rewrite();
169 void addMBBLiveIns();
170public:
171 static char ID;
172 VirtRegRewriter() : MachineFunctionPass(ID) {}
173
Stephen Hines36b56882014-04-23 16:57:46 -0700174 void getAnalysisUsage(AnalysisUsage &AU) const override;
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000175
Stephen Hines36b56882014-04-23 16:57:46 -0700176 bool runOnMachineFunction(MachineFunction&) override;
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000177};
178} // end anonymous namespace
179
180char &llvm::VirtRegRewriterID = VirtRegRewriter::ID;
181
182INITIALIZE_PASS_BEGIN(VirtRegRewriter, "virtregrewriter",
183 "Virtual Register Rewriter", false, false)
184INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
185INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
186INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
Evan Chengbb36a432012-09-21 20:04:28 +0000187INITIALIZE_PASS_DEPENDENCY(LiveStacks)
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000188INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
189INITIALIZE_PASS_END(VirtRegRewriter, "virtregrewriter",
190 "Virtual Register Rewriter", false, false)
191
192char VirtRegRewriter::ID = 0;
193
194void VirtRegRewriter::getAnalysisUsage(AnalysisUsage &AU) const {
195 AU.setPreservesCFG();
196 AU.addRequired<LiveIntervals>();
197 AU.addRequired<SlotIndexes>();
198 AU.addPreserved<SlotIndexes>();
199 AU.addRequired<LiveDebugVariables>();
Evan Chengbb36a432012-09-21 20:04:28 +0000200 AU.addRequired<LiveStacks>();
201 AU.addPreserved<LiveStacks>();
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000202 AU.addRequired<VirtRegMap>();
203 MachineFunctionPass::getAnalysisUsage(AU);
204}
205
206bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) {
207 MF = &fn;
208 TM = &MF->getTarget();
Stephen Hines37ed9c12014-12-01 14:51:49 -0800209 TRI = MF->getSubtarget().getRegisterInfo();
210 TII = MF->getSubtarget().getInstrInfo();
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000211 MRI = &MF->getRegInfo();
212 Indexes = &getAnalysis<SlotIndexes>();
213 LIS = &getAnalysis<LiveIntervals>();
214 VRM = &getAnalysis<VirtRegMap>();
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +0000215 DEBUG(dbgs() << "********** REWRITE VIRTUAL REGISTERS **********\n"
216 << "********** Function: "
Craig Topper96601ca2012-08-22 06:07:19 +0000217 << MF->getName() << '\n');
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000218 DEBUG(VRM->dump());
219
220 // Add kill flags while we still have virtual registers.
Jakob Stoklund Olesene617ccb2012-09-06 18:15:18 +0000221 LIS->addKillFlags(VRM);
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000222
Jakob Stoklund Olesenfe17bdb2012-06-09 00:14:47 +0000223 // Live-in lists on basic blocks are required for physregs.
224 addMBBLiveIns();
225
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000226 // Rewrite virtual registers.
227 rewrite();
228
229 // Write out new DBG_VALUE instructions.
230 getAnalysis<LiveDebugVariables>().emitDebugValues(VRM);
231
232 // All machine operands and other references to virtual registers have been
233 // replaced. Remove the virtual registers and release all the transient data.
234 VRM->clearAllVirt();
235 MRI->clearVirtRegs();
236 return true;
237}
238
Jakob Stoklund Olesenfe17bdb2012-06-09 00:14:47 +0000239// Compute MBB live-in lists from virtual register live ranges and their
240// assignments.
241void VirtRegRewriter::addMBBLiveIns() {
242 SmallVector<MachineBasicBlock*, 16> LiveIn;
243 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
244 unsigned VirtReg = TargetRegisterInfo::index2VirtReg(Idx);
245 if (MRI->reg_nodbg_empty(VirtReg))
246 continue;
247 LiveInterval &LI = LIS->getInterval(VirtReg);
248 if (LI.empty() || LIS->intervalIsInOneMBB(LI))
249 continue;
250 // This is a virtual register that is live across basic blocks. Its
251 // assigned PhysReg must be marked as live-in to those blocks.
252 unsigned PhysReg = VRM->getPhys(VirtReg);
253 assert(PhysReg != VirtRegMap::NO_PHYS_REG && "Unmapped virtual register.");
254
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700255 if (LI.hasSubRanges()) {
256 for (LiveInterval::SubRange &S : LI.subranges()) {
257 for (const auto &Seg : S.segments) {
258 if (!Indexes->findLiveInMBBs(Seg.start, Seg.end, LiveIn))
259 continue;
260 for (MCSubRegIndexIterator SR(PhysReg, TRI); SR.isValid(); ++SR) {
261 unsigned SubReg = SR.getSubReg();
262 unsigned SubRegIndex = SR.getSubRegIndex();
263 unsigned SubRegLaneMask = TRI->getSubRegIndexLaneMask(SubRegIndex);
264 if ((SubRegLaneMask & S.LaneMask) == 0)
265 continue;
266 for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) {
267 if (!LiveIn[i]->isLiveIn(SubReg))
268 LiveIn[i]->addLiveIn(SubReg);
269 }
270 }
271 LiveIn.clear();
272 }
273 }
274 } else {
275 // Scan the segments of LI.
276 for (const auto &Seg : LI.segments) {
277 if (!Indexes->findLiveInMBBs(Seg.start, Seg.end, LiveIn))
278 continue;
279 for (unsigned i = 0, e = LiveIn.size(); i != e; ++i)
280 if (!LiveIn[i]->isLiveIn(PhysReg))
281 LiveIn[i]->addLiveIn(PhysReg);
282 LiveIn.clear();
283 }
Jakob Stoklund Olesenfe17bdb2012-06-09 00:14:47 +0000284 }
285 }
286}
287
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000288void VirtRegRewriter::rewrite() {
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700289 bool NoSubRegLiveness = !MRI->tracksSubRegLiveness();
Jakob Stoklund Olesen93e110b2011-04-27 17:42:31 +0000290 SmallVector<unsigned, 8> SuperDeads;
291 SmallVector<unsigned, 8> SuperDefs;
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +0000292 SmallVector<unsigned, 8> SuperKills;
Quentin Colombetce734f12013-09-25 00:26:17 +0000293 SmallPtrSet<const MachineInstr *, 4> NoReturnInsts;
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +0000294
Stephen Hines36b56882014-04-23 16:57:46 -0700295 // Here we have a SparseSet to hold which PhysRegs are actually encountered
296 // in the MF we are about to iterate over so that later when we call
297 // setPhysRegUsed, we are only doing it for physRegs that were actually found
298 // in the program and not for all of the possible physRegs for the given
299 // target architecture. If the target has a lot of physRegs, then for a small
300 // program there will be a significant compile time reduction here.
301 PhysRegs.clear();
302 PhysRegs.setUniverse(TRI->getNumRegs());
303
304 // The function with uwtable should guarantee that the stack unwinder
305 // can unwind the stack to the previous frame. Thus, we can't apply the
306 // noreturn optimization if the caller function has uwtable attribute.
307 bool HasUWTable = MF->getFunction()->hasFnAttribute(Attribute::UWTable);
308
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +0000309 for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
310 MBBI != MBBE; ++MBBI) {
311 DEBUG(MBBI->print(dbgs(), Indexes));
Quentin Colombetce734f12013-09-25 00:26:17 +0000312 bool IsExitBB = MBBI->succ_empty();
Evan Cheng3f9c2512012-01-19 07:46:36 +0000313 for (MachineBasicBlock::instr_iterator
314 MII = MBBI->instr_begin(), MIE = MBBI->instr_end(); MII != MIE;) {
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +0000315 MachineInstr *MI = MII;
316 ++MII;
317
Stephen Hines36b56882014-04-23 16:57:46 -0700318 // Check if this instruction is a call to a noreturn function. If this
319 // is a call to noreturn function and we don't need the stack unwinding
320 // functionality (i.e. this function does not have uwtable attribute and
321 // the callee function has the nounwind attribute), then we can ignore
322 // the definitions set by this instruction.
323 if (!HasUWTable && IsExitBB && MI->isCall()) {
Quentin Colombetce734f12013-09-25 00:26:17 +0000324 for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
325 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
326 MachineOperand &MO = *MOI;
327 if (!MO.isGlobal())
328 continue;
329 const Function *Func = dyn_cast<Function>(MO.getGlobal());
Quentin Colombetf0c6ab62013-11-08 18:14:17 +0000330 if (!Func || !Func->hasFnAttribute(Attribute::NoReturn) ||
331 // We need to keep correct unwind information
332 // even if the function will not return, since the
333 // runtime may need it.
334 !Func->hasFnAttribute(Attribute::NoUnwind))
Quentin Colombetce734f12013-09-25 00:26:17 +0000335 continue;
336 NoReturnInsts.insert(MI);
337 break;
338 }
Stephen Hines36b56882014-04-23 16:57:46 -0700339 }
Quentin Colombetce734f12013-09-25 00:26:17 +0000340
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +0000341 for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
342 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
343 MachineOperand &MO = *MOI;
Jakob Stoklund Olesend9f0ff52012-02-17 19:07:56 +0000344
345 // Make sure MRI knows about registers clobbered by regmasks.
346 if (MO.isRegMask())
347 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
348
Stephen Hines36b56882014-04-23 16:57:46 -0700349 // If we encounter a VirtReg or PhysReg then get at the PhysReg and add
350 // it to the physreg bitset. Later we use only the PhysRegs that were
351 // actually encountered in the MF to populate the MRI's used physregs.
352 if (MO.isReg() && MO.getReg())
353 PhysRegs.insert(
354 TargetRegisterInfo::isVirtualRegister(MO.getReg()) ?
355 VRM->getPhys(MO.getReg()) :
356 MO.getReg());
357
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +0000358 if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
359 continue;
360 unsigned VirtReg = MO.getReg();
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000361 unsigned PhysReg = VRM->getPhys(VirtReg);
362 assert(PhysReg != VirtRegMap::NO_PHYS_REG &&
363 "Instruction uses unmapped VirtReg");
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000364 assert(!MRI->isReserved(PhysReg) && "Reserved register assignment");
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +0000365
366 // Preserve semantics of sub-register operands.
367 if (MO.getSubReg()) {
368 // A virtual register kill refers to the whole register, so we may
Jakob Stoklund Olesen200a8ce2011-10-05 00:01:48 +0000369 // have to add <imp-use,kill> operands for the super-register. A
370 // partial redef always kills and redefines the super-register.
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700371 if (NoSubRegLiveness && MO.readsReg()
372 && (MO.isDef() || MO.isKill()))
Jakob Stoklund Olesen200a8ce2011-10-05 00:01:48 +0000373 SuperKills.push_back(PhysReg);
374
375 if (MO.isDef()) {
376 // The <def,undef> flag only makes sense for sub-register defs, and
377 // we are substituting a full physreg. An <imp-use,kill> operand
378 // from the SuperKills list will represent the partial read of the
379 // super-register.
380 MO.setIsUndef(false);
381
382 // Also add implicit defs for the super-register.
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700383 if (NoSubRegLiveness) {
384 if (MO.isDead())
385 SuperDeads.push_back(PhysReg);
386 else
387 SuperDefs.push_back(PhysReg);
388 }
Jakob Stoklund Olesen200a8ce2011-10-05 00:01:48 +0000389 }
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +0000390
391 // PhysReg operands cannot have subregister indexes.
392 PhysReg = TRI->getSubReg(PhysReg, MO.getSubReg());
393 assert(PhysReg && "Invalid SubReg for physical register");
394 MO.setSubReg(0);
395 }
396 // Rewrite. Note we could have used MachineOperand::substPhysReg(), but
397 // we need the inlining here.
398 MO.setReg(PhysReg);
399 }
400
401 // Add any missing super-register kills after rewriting the whole
402 // instruction.
403 while (!SuperKills.empty())
404 MI->addRegisterKilled(SuperKills.pop_back_val(), TRI, true);
405
Jakob Stoklund Olesen93e110b2011-04-27 17:42:31 +0000406 while (!SuperDeads.empty())
407 MI->addRegisterDead(SuperDeads.pop_back_val(), TRI, true);
408
409 while (!SuperDefs.empty())
410 MI->addRegisterDefined(SuperDefs.pop_back_val(), TRI);
411
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +0000412 DEBUG(dbgs() << "> " << *MI);
413
414 // Finally, remove any identity copies.
415 if (MI->isIdentityCopy()) {
Jakob Stoklund Olesencf5e5f32011-05-06 17:59:57 +0000416 ++NumIdCopies;
Jakob Stoklund Olesen280ea1a2011-03-31 17:55:25 +0000417 if (MI->getNumOperands() == 2) {
418 DEBUG(dbgs() << "Deleting identity copy.\n");
Jakob Stoklund Olesen280ea1a2011-03-31 17:55:25 +0000419 if (Indexes)
420 Indexes->removeMachineInstrFromMaps(MI);
421 // It's safe to erase MI because MII has already been incremented.
422 MI->eraseFromParent();
423 } else {
424 // Transform identity copy to a KILL to deal with subregisters.
425 MI->setDesc(TII->get(TargetOpcode::KILL));
426 DEBUG(dbgs() << "Identity copy: " << *MI);
427 }
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +0000428 }
429 }
430 }
431
432 // Tell MRI about physical registers in use.
Quentin Colombetce734f12013-09-25 00:26:17 +0000433 if (NoReturnInsts.empty()) {
Stephen Hines36b56882014-04-23 16:57:46 -0700434 for (SparseSet<unsigned>::iterator
435 RegI = PhysRegs.begin(), E = PhysRegs.end(); RegI != E; ++RegI)
436 if (!MRI->reg_nodbg_empty(*RegI))
437 MRI->setPhysRegUsed(*RegI);
Quentin Colombetce734f12013-09-25 00:26:17 +0000438 } else {
Stephen Hines36b56882014-04-23 16:57:46 -0700439 for (SparseSet<unsigned>::iterator
440 I = PhysRegs.begin(), E = PhysRegs.end(); I != E; ++I) {
441 unsigned Reg = *I;
Quentin Colombetce734f12013-09-25 00:26:17 +0000442 if (MRI->reg_nodbg_empty(Reg))
443 continue;
444 // Check if this register has a use that will impact the rest of the
445 // code. Uses in debug and noreturn instructions do not impact the
446 // generated code.
Stephen Hines36b56882014-04-23 16:57:46 -0700447 for (MachineInstr &It : MRI->reg_nodbg_instructions(Reg)) {
448 if (!NoReturnInsts.count(&It)) {
Quentin Colombetce734f12013-09-25 00:26:17 +0000449 MRI->setPhysRegUsed(Reg);
450 break;
451 }
452 }
453 }
454 }
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +0000455}
Stephen Hines36b56882014-04-23 16:57:46 -0700456