87 lines · cpp
1//===- MIRPrintingPass.cpp - Pass that prints out using the MIR format ----===//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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements a pass that prints out the LLVM module using the MIR10// serialization format.11//12//===----------------------------------------------------------------------===//13 14#include "llvm/CodeGen/MIRPrinter.h"15#include "llvm/CodeGen/MachineFunctionPass.h"16#include "llvm/CodeGen/MachineModuleInfo.h"17#include "llvm/CodeGen/Passes.h"18#include "llvm/IR/Function.h"19#include "llvm/InitializePasses.h"20 21using namespace llvm;22 23PreservedAnalyses PrintMIRPreparePass::run(Module &M, ModuleAnalysisManager &) {24 printMIR(OS, M);25 return PreservedAnalyses::all();26}27 28PreservedAnalyses PrintMIRPass::run(MachineFunction &MF,29 MachineFunctionAnalysisManager &MFAM) {30 auto &MAMP = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF);31 Module *M = MF.getFunction().getParent();32 const MachineModuleInfo &MMI =33 MAMP.getCachedResult<MachineModuleAnalysis>(*M)->getMMI();34 35 printMIR(OS, MMI, MF);36 return PreservedAnalyses::all();37}38 39namespace {40 41/// This pass prints out the LLVM IR to an output stream using the MIR42/// serialization format.43struct MIRPrintingPass : public MachineFunctionPass {44 static char ID;45 raw_ostream &OS;46 std::string MachineFunctions;47 48 MIRPrintingPass() : MachineFunctionPass(ID), OS(dbgs()) {}49 MIRPrintingPass(raw_ostream &OS) : MachineFunctionPass(ID), OS(OS) {}50 51 StringRef getPassName() const override { return "MIR Printing Pass"; }52 53 void getAnalysisUsage(AnalysisUsage &AU) const override {54 AU.setPreservesAll();55 MachineFunctionPass::getAnalysisUsage(AU);56 }57 58 bool runOnMachineFunction(MachineFunction &MF) override {59 std::string Str;60 raw_string_ostream StrOS(Str);61 62 const MachineModuleInfo &MMI =63 getAnalysis<MachineModuleInfoWrapperPass>().getMMI();64 65 printMIR(StrOS, MMI, MF);66 MachineFunctions.append(Str);67 return false;68 }69 70 bool doFinalization(Module &M) override {71 printMIR(OS, M);72 OS << MachineFunctions;73 return false;74 }75};76 77char MIRPrintingPass::ID = 0;78 79} // end anonymous namespace80 81char &llvm::MIRPrintingPassID = MIRPrintingPass::ID;82INITIALIZE_PASS(MIRPrintingPass, "mir-printer", "MIR Printer", false, false)83 84MachineFunctionPass *llvm::createPrintMIRPass(raw_ostream &OS) {85 return new MIRPrintingPass(OS);86}87