| 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 &FAM = MFAM.getResult<FunctionAnalysisManagerMachineFunctionProxy>(IR&: MF) |
| 31 | .getManager(); |
| 32 | |
| 33 | printMIR(OS, FAM, MF); |
| 34 | return PreservedAnalyses::all(); |
| 35 | } |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | /// This pass prints out the LLVM IR to an output stream using the MIR |
| 40 | /// serialization format. |
| 41 | struct MIRPrintingPass : public MachineFunctionPass { |
| 42 | static char ID; |
| 43 | raw_ostream &OS; |
| 44 | std::string MachineFunctions; |
| 45 | |
| 46 | MIRPrintingPass() : MachineFunctionPass(ID), OS(dbgs()) {} |
| 47 | MIRPrintingPass(raw_ostream &OS) : MachineFunctionPass(ID), OS(OS) {} |
| 48 | |
| 49 | StringRef getPassName() const override { return "MIR Printing Pass" ; } |
| 50 | |
| 51 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 52 | AU.setPreservesAll(); |
| 53 | MachineFunctionPass::getAnalysisUsage(AU); |
| 54 | } |
| 55 | |
| 56 | bool runOnMachineFunction(MachineFunction &MF) override { |
| 57 | std::string Str; |
| 58 | raw_string_ostream StrOS(Str); |
| 59 | |
| 60 | MachineModuleInfo *MMI = |
| 61 | &getAnalysis<MachineModuleInfoWrapperPass>().getMMI(); |
| 62 | |
| 63 | printMIR(OS&: StrOS, MMI: *MMI, MF); |
| 64 | MachineFunctions.append(str: Str); |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | bool doFinalization(Module &M) override { |
| 69 | printMIR(OS, M); |
| 70 | OS << MachineFunctions; |
| 71 | return false; |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | char MIRPrintingPass::ID = 0; |
| 76 | |
| 77 | } // end anonymous namespace |
| 78 | |
| 79 | char &llvm::MIRPrintingPassID = MIRPrintingPass::ID; |
| 80 | INITIALIZE_PASS(MIRPrintingPass, "mir-printer" , "MIR Printer" , false, false) |
| 81 | |
| 82 | MachineFunctionPass *llvm::createPrintMIRPass(raw_ostream &OS) { |
| 83 | return new MIRPrintingPass(OS); |
| 84 | } |
| 85 | |