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/MachineRegisterInfo.h"
24#include "llvm/CodeGen/TargetRegisterInfo.h"
25
26using namespace llvm;
27
28static bool eraseProxyRegs(MachineFunction &MF) {
29 SmallVector<MachineInstr *, 16> RemoveList;
30
31 // ProxyReg instructions forward a register as another: `%dst = mov.iN %src`.
32 // Bulk RAUW the `%dst` registers in two passes over the machine function.
33 DenseMap<Register, Register> RAUWBatch;
34
35 for (auto &BB : MF) {
36 for (auto &MI : BB) {
37 switch (MI.getOpcode()) {
38 case NVPTX::ProxyRegB1:
39 case NVPTX::ProxyRegB16:
40 case NVPTX::ProxyRegB32:
41 case NVPTX::ProxyRegB64: {
42 auto &InOp = *MI.uses().begin();
43 auto &OutOp = *MI.defs().begin();
44 assert(InOp.isReg() && "ProxyReg input should be a register.");
45 assert(OutOp.isReg() && "ProxyReg output should be a register.");
46 RemoveList.push_back(Elt: &MI);
47 Register replacement = InOp.getReg();
48 // Check if the replacement itself has been replaced.
49 if (auto it = RAUWBatch.find(Val: replacement); it != RAUWBatch.end())
50 replacement = it->second;
51 RAUWBatch.try_emplace(Key: OutOp.getReg(), Args&: replacement);
52 break;
53 }
54 }
55 }
56 }
57
58 // If there were no proxy instructions, exit early.
59 if (RemoveList.empty())
60 return false;
61
62 // Erase the proxy instructions first.
63 for (auto *MI : RemoveList) {
64 MI->eraseFromParent();
65 }
66
67 // Now go replace the registers.
68 MachineRegisterInfo &MRI = MF.getRegInfo();
69 for (auto [From, To] : RAUWBatch)
70 MRI.replaceRegWith(FromReg: From, ToReg: To);
71
72 return true;
73}
74
75namespace {
76
77struct NVPTXProxyRegErasureLegacyPass : public MachineFunctionPass {
78 static char ID;
79 NVPTXProxyRegErasureLegacyPass() : MachineFunctionPass(ID) {}
80
81 bool runOnMachineFunction(MachineFunction &MF) override {
82 return eraseProxyRegs(MF);
83 }
84
85 StringRef getPassName() const override {
86 return "NVPTX Proxy Register Instruction Erasure";
87 }
88
89 void getAnalysisUsage(AnalysisUsage &AU) const override {
90 AU.setPreservesCFG();
91 MachineFunctionPass::getAnalysisUsage(AU);
92 }
93};
94
95} // namespace
96
97char NVPTXProxyRegErasureLegacyPass::ID = 0;
98
99INITIALIZE_PASS(NVPTXProxyRegErasureLegacyPass, "nvptx-proxyreg-erasure",
100 "NVPTX ProxyReg Erasure", false, false)
101
102MachineFunctionPass *llvm::createNVPTXProxyRegErasureLegacyPass() {
103 return new NVPTXProxyRegErasureLegacyPass();
104}
105
106PreservedAnalyses
107NVPTXProxyRegErasurePass::run(MachineFunction &MF,
108 MachineFunctionAnalysisManager &MFAM) {
109 if (!eraseProxyRegs(MF))
110 return PreservedAnalyses::all();
111 return getMachineFunctionPassPreservedAnalyses().preserveSet<CFGAnalyses>();
112}
113