LLVM 20.0.0git
NVPTXProxyRegErasure.cpp
Go to the documentation of this file.
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"
27
28using namespace llvm;
29
30namespace llvm {
32}
33
34namespace {
35
36struct NVPTXProxyRegErasure : public MachineFunctionPass {
37 static char ID;
38 NVPTXProxyRegErasure() : MachineFunctionPass(ID) {
40 }
41
42 bool runOnMachineFunction(MachineFunction &MF) override;
43
44 StringRef getPassName() const override {
45 return "NVPTX Proxy Register Instruction Erasure";
46 }
47
48 void getAnalysisUsage(AnalysisUsage &AU) const override {
50 }
51};
52
53} // namespace
54
55char NVPTXProxyRegErasure::ID = 0;
56
57INITIALIZE_PASS(NVPTXProxyRegErasure, "nvptx-proxyreg-erasure",
58 "NVPTX ProxyReg Erasure", false, false)
59
60bool NVPTXProxyRegErasure::runOnMachineFunction(MachineFunction &MF) {
62
63 // ProxyReg instructions forward a register as another: `%dst = mov.iN %src`.
64 // Bulk RAUW the `%dst` registers in two passes over the machine function.
66
67 for (auto &BB : MF) {
68 for (auto &MI : BB) {
69 switch (MI.getOpcode()) {
70 case NVPTX::ProxyRegI1:
71 case NVPTX::ProxyRegI16:
72 case NVPTX::ProxyRegI32:
73 case NVPTX::ProxyRegI64:
74 case NVPTX::ProxyRegF32:
75 case NVPTX::ProxyRegF64: {
76 auto &InOp = *MI.uses().begin();
77 auto &OutOp = *MI.defs().begin();
78 assert(InOp.isReg() && "ProxyReg input should be a register.");
79 assert(OutOp.isReg() && "ProxyReg output should be a register.");
80 RemoveList.push_back(&MI);
81 RAUWBatch.try_emplace(OutOp.getReg(), InOp.getReg());
82 break;
83 }
84 }
85 }
86 }
87
88 // If there were no proxy instructions, exit early.
89 if (RemoveList.empty())
90 return false;
91
92 // Erase the proxy instructions first.
93 for (auto *MI : RemoveList) {
94 MI->eraseFromParent();
95 }
96
97 // Now go replace the registers.
98 for (auto &BB : MF) {
99 for (auto &MI : BB) {
100 for (auto &Op : MI.uses()) {
101 if (!Op.isReg())
102 continue;
103 auto it = RAUWBatch.find(Op.getReg());
104 if (it != RAUWBatch.end())
105 Op.setReg(it->second);
106 }
107 }
108 }
109
110 return true;
111}
112
114 return new NVPTXProxyRegErasure();
115}
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Represent the analysis usage information of a pass.
This class represents an Operation in the Expression.
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
Definition: DenseMap.h:226
iterator end()
Definition: DenseMap.h:84
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:37
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
bool empty() const
Definition: SmallVector.h:94
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachineFunctionPass * createNVPTXProxyRegErasurePass()
void initializeNVPTXProxyRegErasurePass(PassRegistry &)