1//===- NVPTXProxyRegErasure.cpp - NVPTX Proxy Register Instruction Erasure -==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// The pass is needed to remove ProxyReg instructions and restore related
10// registers. The instructions were needed at instruction selection stage to
11// make sure that callseq_end nodes won't be removed as "dead nodes". This can
12// happen when we expand instructions into libcalls and the call site doesn't
13// care about the libcall chain. Call site cares about data flow only, and the
14// latest data flow node happens to be before callseq_end. Therefore the node
15// becomes dangling and "dead". The ProxyReg acts like an additional data flow
16// node *after* the callseq_end in the chain and ensures that everything will be
17// preserved.
18//
19//===----------------------------------------------------------------------===//
20
21#include "NVPTX.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/TargetRegisterInfo.h"
24
25using namespace llvm;
26
27namespace {
28
29struct NVPTXProxyRegErasure : public MachineFunctionPass {
30 static char ID;
31 NVPTXProxyRegErasure() : MachineFunctionPass(ID) {}
32
33 bool runOnMachineFunction(MachineFunction &MF) override;
34
35 StringRef getPassName() const override {
36 return "NVPTX Proxy Register Instruction Erasure";
37 }
38
39 void getAnalysisUsage(AnalysisUsage &AU) const override {
40 MachineFunctionPass::getAnalysisUsage(AU);
41 }
42};
43
44} // namespace
45
46char NVPTXProxyRegErasure::ID = 0;
47
48INITIALIZE_PASS(NVPTXProxyRegErasure, "nvptx-proxyreg-erasure",
49 "NVPTX ProxyReg Erasure", false, false)
50
51bool NVPTXProxyRegErasure::runOnMachineFunction(MachineFunction &MF) {
52 SmallVector<MachineInstr *, 16> RemoveList;
53
54 // ProxyReg instructions forward a register as another: `%dst = mov.iN %src`.
55 // Bulk RAUW the `%dst` registers in two passes over the machine function.
56 DenseMap<Register, Register> RAUWBatch;
57
58 for (auto &BB : MF) {
59 for (auto &MI : BB) {
60 switch (MI.getOpcode()) {
61 case NVPTX::ProxyRegB1:
62 case NVPTX::ProxyRegB16:
63 case NVPTX::ProxyRegB32:
64 case NVPTX::ProxyRegB64: {
65 auto &InOp = *MI.uses().begin();
66 auto &OutOp = *MI.defs().begin();
67 assert(InOp.isReg() && "ProxyReg input should be a register.");
68 assert(OutOp.isReg() && "ProxyReg output should be a register.");
69 RemoveList.push_back(Elt: &MI);
70 Register replacement = InOp.getReg();
71 // Check if the replacement itself has been replaced.
72 if (auto it = RAUWBatch.find(Val: replacement); it != RAUWBatch.end())
73 replacement = it->second;
74 RAUWBatch.try_emplace(Key: OutOp.getReg(), Args&: replacement);
75 break;
76 }
77 }
78 }
79 }
80
81 // If there were no proxy instructions, exit early.
82 if (RemoveList.empty())
83 return false;
84
85 // Erase the proxy instructions first.
86 for (auto *MI : RemoveList) {
87 MI->eraseFromParent();
88 }
89
90 // Now go replace the registers.
91 for (auto &BB : MF) {
92 for (auto &MI : BB) {
93 for (auto &Op : MI.uses()) {
94 if (!Op.isReg())
95 continue;
96 auto it = RAUWBatch.find(Val: Op.getReg());
97 if (it != RAUWBatch.end())
98 Op.setReg(it->second);
99 }
100 }
101 }
102
103 return true;
104}
105
106MachineFunctionPass *llvm::createNVPTXProxyRegErasurePass() {
107 return new NVPTXProxyRegErasure();
108}
109