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 and remove kill flags conservatively.
68 MachineRegisterInfo &MRI = MF.getRegInfo();
69 for (auto [From, To] : RAUWBatch) {
70 MRI.replaceRegWith(FromReg: From, ToReg: To);
71 MRI.clearKillFlags(Reg: To);
72 }
73
74 return true;
75}
76
77namespace {
78
79struct NVPTXProxyRegErasureLegacyPass : public MachineFunctionPass {
80 static char ID;
81 NVPTXProxyRegErasureLegacyPass() : MachineFunctionPass(ID) {}
82
83 bool runOnMachineFunction(MachineFunction &MF) override {
84 return eraseProxyRegs(MF);
85 }
86
87 StringRef getPassName() const override {
88 return "NVPTX Proxy Register Instruction Erasure";
89 }
90
91 void getAnalysisUsage(AnalysisUsage &AU) const override {
92 AU.setPreservesCFG();
93 MachineFunctionPass::getAnalysisUsage(AU);
94 }
95};
96
97} // namespace
98
99char NVPTXProxyRegErasureLegacyPass::ID = 0;
100
101INITIALIZE_PASS(NVPTXProxyRegErasureLegacyPass, "nvptx-proxyreg-erasure",
102 "NVPTX ProxyReg Erasure", false, false)
103
104MachineFunctionPass *llvm::createNVPTXProxyRegErasureLegacyPass() {
105 return new NVPTXProxyRegErasureLegacyPass();
106}
107
108PreservedAnalyses
109NVPTXProxyRegErasurePass::run(MachineFunction &MF,
110 MachineFunctionAnalysisManager &MFAM) {
111 if (!eraseProxyRegs(MF))
112 return PreservedAnalyses::all();
113 return getMachineFunctionPassPreservedAnalyses().preserveSet<CFGAnalyses>();
114}
115