1 //===- MIRPrintingPass.cpp - Pass that prints out using the MIR format ----===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements a pass that prints out the LLVM module using the MIR
10 // serialization format.
12 //===----------------------------------------------------------------------===//
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"
23 PreservedAnalyses
PrintMIRPreparePass::run(Module
&M
, ModuleAnalysisManager
&) {
25 return PreservedAnalyses::all();
28 PreservedAnalyses
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();
35 printMIR(OS
, MMI
, MF
);
36 return PreservedAnalyses::all();
41 /// This pass prints out the LLVM IR to an output stream using the MIR
42 /// serialization format.
43 struct MIRPrintingPass
: public MachineFunctionPass
{
46 std::string MachineFunctions
;
48 MIRPrintingPass() : MachineFunctionPass(ID
), OS(dbgs()) {}
49 MIRPrintingPass(raw_ostream
&OS
) : MachineFunctionPass(ID
), OS(OS
) {}
51 StringRef
getPassName() const override
{ return "MIR Printing Pass"; }
53 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
55 MachineFunctionPass::getAnalysisUsage(AU
);
58 bool runOnMachineFunction(MachineFunction
&MF
) override
{
60 raw_string_ostream
StrOS(Str
);
62 const MachineModuleInfo
&MMI
=
63 getAnalysis
<MachineModuleInfoWrapperPass
>().getMMI();
65 printMIR(StrOS
, MMI
, MF
);
66 MachineFunctions
.append(Str
);
70 bool doFinalization(Module
&M
) override
{
72 OS
<< MachineFunctions
;
77 char MIRPrintingPass::ID
= 0;
79 } // end anonymous namespace
81 char &llvm::MIRPrintingPassID
= MIRPrintingPass::ID
;
82 INITIALIZE_PASS(MIRPrintingPass
, "mir-printer", "MIR Printer", false, false)
86 MachineFunctionPass
*createPrintMIRPass(raw_ostream
&OS
) {
87 return new MIRPrintingPass(OS
);
90 } // end namespace llvm