diff --git a/llvm/include/llvm/CodeGen/MIRPrinter.h b/llvm/include/llvm/CodeGen/MIRPrinter.h --- a/llvm/include/llvm/CodeGen/MIRPrinter.h +++ b/llvm/include/llvm/CodeGen/MIRPrinter.h @@ -14,6 +14,8 @@ #ifndef LLVM_LIB_CODEGEN_MIRPRINTER_H #define LLVM_LIB_CODEGEN_MIRPRINTER_H +#include "llvm/CodeGen/MachinePassManager.h" + namespace llvm { class MachineBasicBlock; @@ -22,6 +24,20 @@ class raw_ostream; template class SmallVectorImpl; +class PrintMIRPass : public PassInfoMixin { + raw_ostream &OS; + std::string MachineFunctions; + +public: + PrintMIRPass() : OS(dbgs()) {} + PrintMIRPass(raw_ostream &OS) : OS(OS) {} + + PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &); + Error doFinalization(Module &M, MachineFunctionAnalysisManager &); + + static AnalysisKey Key; +}; + /// Print LLVM IR using the MIR serialization format to the given output stream. void printMIR(raw_ostream &OS, const Module &M); diff --git a/llvm/lib/CodeGen/MIRPrintingPass.cpp b/llvm/lib/CodeGen/MIRPrintingPass.cpp --- a/llvm/lib/CodeGen/MIRPrintingPass.cpp +++ b/llvm/lib/CodeGen/MIRPrintingPass.cpp @@ -16,10 +16,29 @@ #include "llvm/CodeGen/Passes.h" #include "llvm/InitializePasses.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/Error.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; +PreservedAnalyses PrintMIRPass::run(MachineFunction &MF, + MachineFunctionAnalysisManager &) { + std::string Str; + raw_string_ostream StrOS(Str); + printMIR(StrOS, MF); + MachineFunctions.append(StrOS.str()); + return PreservedAnalyses::all(); +} + +Error PrintMIRPass::doFinalization(Module &M, + MachineFunctionAnalysisManager &) { + printMIR(OS, M); + OS << MachineFunctions; + return Error::success(); +} + +AnalysisKey PrintMIRPass::Key; + namespace { /// This pass prints out the LLVM IR to an output stream using the MIR