LLVM 22.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"
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 {
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) {
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.
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(&MI);
70 Register replacement = InOp.getReg();
71 // Check if the replacement itself has been replaced.
72 if (auto it = RAUWBatch.find(replacement); it != RAUWBatch.end())
73 replacement = it->second;
74 RAUWBatch.try_emplace(OutOp.getReg(), 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(Op.getReg());
97 if (it != RAUWBatch.end())
98 Op.setReg(it->second);
99 }
100 }
101 }
102
103 return true;
104}
105
107 return new NVPTXProxyRegErasure();
108}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
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:177
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition: DenseMap.h:245
iterator end()
Definition: DenseMap.h:87
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...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:85
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
bool empty() const
Definition: SmallVector.h:82
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
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()