1*0b57cec5SDimitry Andric //===- NVPTXProxyRegErasure.cpp - NVPTX Proxy Register Instruction Erasure -==//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // The pass is needed to remove ProxyReg instructions and restore related
10*0b57cec5SDimitry Andric // registers. The instructions were needed at instruction selection stage to
11*0b57cec5SDimitry Andric // make sure that callseq_end nodes won't be removed as "dead nodes". This can
12*0b57cec5SDimitry Andric // happen when we expand instructions into libcalls and the call site doesn't
13*0b57cec5SDimitry Andric // care about the libcall chain. Call site cares about data flow only, and the
14*0b57cec5SDimitry Andric // latest data flow node happens to be before callseq_end. Therefore the node
15*0b57cec5SDimitry Andric // becomes dangling and "dead". The ProxyReg acts like an additional data flow
16*0b57cec5SDimitry Andric // node *after* the callseq_end in the chain and ensures that everything will be
17*0b57cec5SDimitry Andric // preserved.
18*0b57cec5SDimitry Andric //
19*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
20*0b57cec5SDimitry Andric
21*0b57cec5SDimitry Andric #include "NVPTX.h"
22*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
23*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
24*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
25*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
26*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
27*0b57cec5SDimitry Andric
28*0b57cec5SDimitry Andric using namespace llvm;
29*0b57cec5SDimitry Andric
30*0b57cec5SDimitry Andric namespace llvm {
31*0b57cec5SDimitry Andric void initializeNVPTXProxyRegErasurePass(PassRegistry &);
32*0b57cec5SDimitry Andric }
33*0b57cec5SDimitry Andric
34*0b57cec5SDimitry Andric namespace {
35*0b57cec5SDimitry Andric
36*0b57cec5SDimitry Andric struct NVPTXProxyRegErasure : public MachineFunctionPass {
37*0b57cec5SDimitry Andric public:
38*0b57cec5SDimitry Andric static char ID;
NVPTXProxyRegErasure__anona50ce6fe0111::NVPTXProxyRegErasure39*0b57cec5SDimitry Andric NVPTXProxyRegErasure() : MachineFunctionPass(ID) {
40*0b57cec5SDimitry Andric initializeNVPTXProxyRegErasurePass(*PassRegistry::getPassRegistry());
41*0b57cec5SDimitry Andric }
42*0b57cec5SDimitry Andric
43*0b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
44*0b57cec5SDimitry Andric
getPassName__anona50ce6fe0111::NVPTXProxyRegErasure45*0b57cec5SDimitry Andric StringRef getPassName() const override {
46*0b57cec5SDimitry Andric return "NVPTX Proxy Register Instruction Erasure";
47*0b57cec5SDimitry Andric }
48*0b57cec5SDimitry Andric
getAnalysisUsage__anona50ce6fe0111::NVPTXProxyRegErasure49*0b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
50*0b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
51*0b57cec5SDimitry Andric }
52*0b57cec5SDimitry Andric
53*0b57cec5SDimitry Andric private:
54*0b57cec5SDimitry Andric void replaceMachineInstructionUsage(MachineFunction &MF, MachineInstr &MI);
55*0b57cec5SDimitry Andric
56*0b57cec5SDimitry Andric void replaceRegisterUsage(MachineInstr &Instr, MachineOperand &From,
57*0b57cec5SDimitry Andric MachineOperand &To);
58*0b57cec5SDimitry Andric };
59*0b57cec5SDimitry Andric
60*0b57cec5SDimitry Andric } // namespace
61*0b57cec5SDimitry Andric
62*0b57cec5SDimitry Andric char NVPTXProxyRegErasure::ID = 0;
63*0b57cec5SDimitry Andric
64*0b57cec5SDimitry Andric INITIALIZE_PASS(NVPTXProxyRegErasure, "nvptx-proxyreg-erasure", "NVPTX ProxyReg Erasure", false, false)
65*0b57cec5SDimitry Andric
runOnMachineFunction(MachineFunction & MF)66*0b57cec5SDimitry Andric bool NVPTXProxyRegErasure::runOnMachineFunction(MachineFunction &MF) {
67*0b57cec5SDimitry Andric SmallVector<MachineInstr *, 16> RemoveList;
68*0b57cec5SDimitry Andric
69*0b57cec5SDimitry Andric for (auto &BB : MF) {
70*0b57cec5SDimitry Andric for (auto &MI : BB) {
71*0b57cec5SDimitry Andric switch (MI.getOpcode()) {
72*0b57cec5SDimitry Andric case NVPTX::ProxyRegI1:
73*0b57cec5SDimitry Andric case NVPTX::ProxyRegI16:
74*0b57cec5SDimitry Andric case NVPTX::ProxyRegI32:
75*0b57cec5SDimitry Andric case NVPTX::ProxyRegI64:
76*0b57cec5SDimitry Andric case NVPTX::ProxyRegF32:
77*0b57cec5SDimitry Andric case NVPTX::ProxyRegF64:
78*0b57cec5SDimitry Andric replaceMachineInstructionUsage(MF, MI);
79*0b57cec5SDimitry Andric RemoveList.push_back(&MI);
80*0b57cec5SDimitry Andric break;
81*0b57cec5SDimitry Andric }
82*0b57cec5SDimitry Andric }
83*0b57cec5SDimitry Andric }
84*0b57cec5SDimitry Andric
85*0b57cec5SDimitry Andric for (auto *MI : RemoveList) {
86*0b57cec5SDimitry Andric MI->eraseFromParent();
87*0b57cec5SDimitry Andric }
88*0b57cec5SDimitry Andric
89*0b57cec5SDimitry Andric return !RemoveList.empty();
90*0b57cec5SDimitry Andric }
91*0b57cec5SDimitry Andric
replaceMachineInstructionUsage(MachineFunction & MF,MachineInstr & MI)92*0b57cec5SDimitry Andric void NVPTXProxyRegErasure::replaceMachineInstructionUsage(MachineFunction &MF,
93*0b57cec5SDimitry Andric MachineInstr &MI) {
94*0b57cec5SDimitry Andric auto &InOp = *MI.uses().begin();
95*0b57cec5SDimitry Andric auto &OutOp = *MI.defs().begin();
96*0b57cec5SDimitry Andric
97*0b57cec5SDimitry Andric assert(InOp.isReg() && "ProxyReg input operand should be a register.");
98*0b57cec5SDimitry Andric assert(OutOp.isReg() && "ProxyReg output operand should be a register.");
99*0b57cec5SDimitry Andric
100*0b57cec5SDimitry Andric for (auto &BB : MF) {
101*0b57cec5SDimitry Andric for (auto &I : BB) {
102*0b57cec5SDimitry Andric replaceRegisterUsage(I, OutOp, InOp);
103*0b57cec5SDimitry Andric }
104*0b57cec5SDimitry Andric }
105*0b57cec5SDimitry Andric }
106*0b57cec5SDimitry Andric
replaceRegisterUsage(MachineInstr & Instr,MachineOperand & From,MachineOperand & To)107*0b57cec5SDimitry Andric void NVPTXProxyRegErasure::replaceRegisterUsage(MachineInstr &Instr,
108*0b57cec5SDimitry Andric MachineOperand &From,
109*0b57cec5SDimitry Andric MachineOperand &To) {
110*0b57cec5SDimitry Andric for (auto &Op : Instr.uses()) {
111*0b57cec5SDimitry Andric if (Op.isReg() && Op.getReg() == From.getReg()) {
112*0b57cec5SDimitry Andric Op.setReg(To.getReg());
113*0b57cec5SDimitry Andric }
114*0b57cec5SDimitry Andric }
115*0b57cec5SDimitry Andric }
116*0b57cec5SDimitry Andric
createNVPTXProxyRegErasurePass()117*0b57cec5SDimitry Andric MachineFunctionPass *llvm::createNVPTXProxyRegErasurePass() {
118*0b57cec5SDimitry Andric return new NVPTXProxyRegErasure();
119*0b57cec5SDimitry Andric }
120