Path: blob/main/contrib/llvm-project/llvm/lib/Target/NVPTX/NVPTXProxyRegErasure.cpp
35271 views
//===- NVPTXProxyRegErasure.cpp - NVPTX Proxy Register Instruction Erasure -==//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// The pass is needed to remove ProxyReg instructions and restore related9// registers. The instructions were needed at instruction selection stage to10// make sure that callseq_end nodes won't be removed as "dead nodes". This can11// happen when we expand instructions into libcalls and the call site doesn't12// care about the libcall chain. Call site cares about data flow only, and the13// latest data flow node happens to be before callseq_end. Therefore the node14// becomes dangling and "dead". The ProxyReg acts like an additional data flow15// node *after* the callseq_end in the chain and ensures that everything will be16// preserved.17//18//===----------------------------------------------------------------------===//1920#include "NVPTX.h"21#include "llvm/CodeGen/MachineFunctionPass.h"22#include "llvm/CodeGen/MachineInstrBuilder.h"23#include "llvm/CodeGen/MachineRegisterInfo.h"24#include "llvm/CodeGen/TargetInstrInfo.h"25#include "llvm/CodeGen/TargetRegisterInfo.h"2627using namespace llvm;2829namespace llvm {30void initializeNVPTXProxyRegErasurePass(PassRegistry &);31}3233namespace {3435struct NVPTXProxyRegErasure : public MachineFunctionPass {36public:37static char ID;38NVPTXProxyRegErasure() : MachineFunctionPass(ID) {39initializeNVPTXProxyRegErasurePass(*PassRegistry::getPassRegistry());40}4142bool runOnMachineFunction(MachineFunction &MF) override;4344StringRef getPassName() const override {45return "NVPTX Proxy Register Instruction Erasure";46}4748void getAnalysisUsage(AnalysisUsage &AU) const override {49MachineFunctionPass::getAnalysisUsage(AU);50}5152private:53void replaceMachineInstructionUsage(MachineFunction &MF, MachineInstr &MI);5455void replaceRegisterUsage(MachineInstr &Instr, MachineOperand &From,56MachineOperand &To);57};5859} // namespace6061char NVPTXProxyRegErasure::ID = 0;6263INITIALIZE_PASS(NVPTXProxyRegErasure, "nvptx-proxyreg-erasure", "NVPTX ProxyReg Erasure", false, false)6465bool NVPTXProxyRegErasure::runOnMachineFunction(MachineFunction &MF) {66SmallVector<MachineInstr *, 16> RemoveList;6768for (auto &BB : MF) {69for (auto &MI : BB) {70switch (MI.getOpcode()) {71case NVPTX::ProxyRegI1:72case NVPTX::ProxyRegI16:73case NVPTX::ProxyRegI32:74case NVPTX::ProxyRegI64:75case NVPTX::ProxyRegF32:76case NVPTX::ProxyRegF64:77replaceMachineInstructionUsage(MF, MI);78RemoveList.push_back(&MI);79break;80}81}82}8384for (auto *MI : RemoveList) {85MI->eraseFromParent();86}8788return !RemoveList.empty();89}9091void NVPTXProxyRegErasure::replaceMachineInstructionUsage(MachineFunction &MF,92MachineInstr &MI) {93auto &InOp = *MI.uses().begin();94auto &OutOp = *MI.defs().begin();9596assert(InOp.isReg() && "ProxyReg input operand should be a register.");97assert(OutOp.isReg() && "ProxyReg output operand should be a register.");9899for (auto &BB : MF) {100for (auto &I : BB) {101replaceRegisterUsage(I, OutOp, InOp);102}103}104}105106void NVPTXProxyRegErasure::replaceRegisterUsage(MachineInstr &Instr,107MachineOperand &From,108MachineOperand &To) {109for (auto &Op : Instr.uses()) {110if (Op.isReg() && Op.getReg() == From.getReg()) {111Op.setReg(To.getReg());112}113}114}115116MachineFunctionPass *llvm::createNVPTXProxyRegErasurePass() {117return new NVPTXProxyRegErasure();118}119120121