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-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file implements a pass that prints out the LLVM module using the MIR |
10 | // 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 | |
21 | using namespace llvm; |
22 | |
23 | PreservedAnalyses PrintMIRPreparePass::run(Module &M, ModuleAnalysisManager &) { |
24 | printMIR(OS, M); |
25 | return PreservedAnalyses::all(); |
26 | } |
27 | |
28 | PreservedAnalyses PrintMIRPass::run(MachineFunction &MF, |
29 | MachineFunctionAnalysisManager &MFAM) { |
30 | auto &MAMP = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(IR&: MF); |
31 | Module *M = MF.getFunction().getParent(); |
32 | const MachineModuleInfo &MMI = |
33 | MAMP.getCachedResult<MachineModuleAnalysis>(IR&: *M)->getMMI(); |
34 | |
35 | printMIR(OS, MMI, MF); |
36 | return PreservedAnalyses::all(); |
37 | } |
38 | |
39 | namespace { |
40 | |
41 | /// This pass prints out the LLVM IR to an output stream using the MIR |
42 | /// serialization format. |
43 | struct 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(OS&: StrOS, MMI, MF); |
66 | MachineFunctions.append(str: 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 | |
77 | char MIRPrintingPass::ID = 0; |
78 | |
79 | } // end anonymous namespace |
80 | |
81 | char &llvm::MIRPrintingPassID = MIRPrintingPass::ID; |
82 | INITIALIZE_PASS(MIRPrintingPass, "mir-printer" , "MIR Printer" , false, false) |
83 | |
84 | MachineFunctionPass *llvm::createPrintMIRPass(raw_ostream &OS) { |
85 | return new MIRPrintingPass(OS); |
86 | } |
87 | |