LLVM 24.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"
25
26using namespace llvm;
27
28namespace {
29
30struct NVPTXProxyRegErasure : public MachineFunctionPass {
31 static char ID;
32 NVPTXProxyRegErasure() : MachineFunctionPass(ID) {}
33
34 bool runOnMachineFunction(MachineFunction &MF) override;
35
36 StringRef getPassName() const override {
37 return "NVPTX Proxy Register Instruction Erasure";
38 }
39
40 void getAnalysisUsage(AnalysisUsage &AU) const override {
42 }
43};
44
45} // namespace
46
47char NVPTXProxyRegErasure::ID = 0;
48
49INITIALIZE_PASS(NVPTXProxyRegErasure, "nvptx-proxyreg-erasure",
50 "NVPTX ProxyReg Erasure", false, false)
51
52bool NVPTXProxyRegErasure::runOnMachineFunction(MachineFunction &MF) {
54
55 // ProxyReg instructions forward a register as another: `%dst = mov.iN %src`.
56 // Bulk RAUW the `%dst` registers in two passes over the machine function.
58
59 for (auto &BB : MF) {
60 for (auto &MI : BB) {
61 switch (MI.getOpcode()) {
62 case NVPTX::ProxyRegB1:
63 case NVPTX::ProxyRegB16:
64 case NVPTX::ProxyRegB32:
65 case NVPTX::ProxyRegB64: {
66 auto &InOp = *MI.uses().begin();
67 auto &OutOp = *MI.defs().begin();
68 assert(InOp.isReg() && "ProxyReg input should be a register.");
69 assert(OutOp.isReg() && "ProxyReg output should be a register.");
70 RemoveList.push_back(&MI);
71 Register replacement = InOp.getReg();
72 // Check if the replacement itself has been replaced.
73 if (auto it = RAUWBatch.find(replacement); it != RAUWBatch.end())
74 replacement = it->second;
75 RAUWBatch.try_emplace(OutOp.getReg(), replacement);
76 break;
77 }
78 }
79 }
80 }
81
82 // If there were no proxy instructions, exit early.
83 if (RemoveList.empty())
84 return false;
85
86 // Erase the proxy instructions first.
87 for (auto *MI : RemoveList) {
88 MI->eraseFromParent();
89 }
90
91 // Now go replace the registers and remove kill flags conservatively.
92 MachineRegisterInfo &MRI = MF.getRegInfo();
93 for (auto [From, To] : RAUWBatch) {
94 MRI.replaceRegWith(From, To);
95 MRI.clearKillFlags(To);
96 }
97
98 return true;
99}
100
102 return new NVPTXProxyRegErasure();
103}
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
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
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:223
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:299
iterator end()
Definition DenseMap.h:141
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.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI void clearKillFlags(Register Reg) const
clearKillFlags - Iterate over all the uses of the given register and clear the kill flag from the Mac...
LLVM_ABI void replaceRegWith(Register FromReg, Register ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This is an optimization pass for GlobalISel generic memory operations.
MachineFunctionPass * createNVPTXProxyRegErasurePass()