| 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" |
| 22 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 24 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | struct 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 { |
| 41 | MachineFunctionPass::getAnalysisUsage(AU); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | } // namespace |
| 46 | |
| 47 | char NVPTXProxyRegErasure::ID = 0; |
| 48 | |
| 49 | INITIALIZE_PASS(NVPTXProxyRegErasure, "nvptx-proxyreg-erasure" , |
| 50 | "NVPTX ProxyReg Erasure" , false, false) |
| 51 | |
| 52 | bool NVPTXProxyRegErasure::runOnMachineFunction(MachineFunction &MF) { |
| 53 | SmallVector<MachineInstr *, 16> RemoveList; |
| 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. |
| 57 | DenseMap<Register, Register> RAUWBatch; |
| 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(Elt: &MI); |
| 71 | Register replacement = InOp.getReg(); |
| 72 | // Check if the replacement itself has been replaced. |
| 73 | if (auto it = RAUWBatch.find(Val: replacement); it != RAUWBatch.end()) |
| 74 | replacement = it->second; |
| 75 | RAUWBatch.try_emplace(Key: OutOp.getReg(), Args&: 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(FromReg: From, ToReg: To); |
| 95 | MRI.clearKillFlags(Reg: To); |
| 96 | } |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | MachineFunctionPass *llvm::createNVPTXProxyRegErasurePass() { |
| 102 | return new NVPTXProxyRegErasure(); |
| 103 | } |
| 104 | |