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
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.
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(&MI);
47 Register replacement = InOp.getReg();
48 // Check if the replacement itself has been replaced.
49 if (auto it = RAUWBatch.find(replacement); it != RAUWBatch.end())
50 replacement = it->second;
51 RAUWBatch.try_emplace(OutOp.getReg(), 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.
69 for (auto [From, To] : RAUWBatch) {
70 MRI.replaceRegWith(From, To);
71 MRI.clearKillFlags(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();
94 }
95};
96
97} // namespace
98
99char NVPTXProxyRegErasureLegacyPass::ID = 0;
100
101INITIALIZE_PASS(NVPTXProxyRegErasureLegacyPass, "nvptx-proxyreg-erasure",
102 "NVPTX ProxyReg Erasure", false, false)
103
105 return new NVPTXProxyRegErasureLegacyPass();
106}
107
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
IRTranslator LLVM IR MI
static bool eraseProxyRegs(MachineFunction &MF)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
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 & getRegInfo()
getRegInfo - Return information about the registers currently in use.
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.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
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.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
MachineFunctionPass * createNVPTXProxyRegErasureLegacyPass()