xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/MIRPrintingPass.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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/Passes.h"
17 #include "llvm/InitializePasses.h"
18 
19 using namespace llvm;
20 
run(Module & M,ModuleAnalysisManager &)21 PreservedAnalyses PrintMIRPreparePass::run(Module &M, ModuleAnalysisManager &) {
22   printMIR(OS, M);
23   return PreservedAnalyses::all();
24 }
25 
run(MachineFunction & MF,MachineFunctionAnalysisManager &)26 PreservedAnalyses PrintMIRPass::run(MachineFunction &MF,
27                                     MachineFunctionAnalysisManager &) {
28   printMIR(OS, MF);
29   return PreservedAnalyses::all();
30 }
31 
32 namespace {
33 
34 /// This pass prints out the LLVM IR to an output stream using the MIR
35 /// serialization format.
36 struct MIRPrintingPass : public MachineFunctionPass {
37   static char ID;
38   raw_ostream &OS;
39   std::string MachineFunctions;
40 
MIRPrintingPass__anonf75782be0111::MIRPrintingPass41   MIRPrintingPass() : MachineFunctionPass(ID), OS(dbgs()) {}
MIRPrintingPass__anonf75782be0111::MIRPrintingPass42   MIRPrintingPass(raw_ostream &OS) : MachineFunctionPass(ID), OS(OS) {}
43 
getPassName__anonf75782be0111::MIRPrintingPass44   StringRef getPassName() const override { return "MIR Printing Pass"; }
45 
getAnalysisUsage__anonf75782be0111::MIRPrintingPass46   void getAnalysisUsage(AnalysisUsage &AU) const override {
47     AU.setPreservesAll();
48     MachineFunctionPass::getAnalysisUsage(AU);
49   }
50 
runOnMachineFunction__anonf75782be0111::MIRPrintingPass51   bool runOnMachineFunction(MachineFunction &MF) override {
52     std::string Str;
53     raw_string_ostream StrOS(Str);
54     printMIR(StrOS, MF);
55     MachineFunctions.append(Str);
56     return false;
57   }
58 
doFinalization__anonf75782be0111::MIRPrintingPass59   bool doFinalization(Module &M) override {
60     printMIR(OS, M);
61     OS << MachineFunctions;
62     return false;
63   }
64 };
65 
66 char MIRPrintingPass::ID = 0;
67 
68 } // end anonymous namespace
69 
70 char &llvm::MIRPrintingPassID = MIRPrintingPass::ID;
71 INITIALIZE_PASS(MIRPrintingPass, "mir-printer", "MIR Printer", false, false)
72 
73 namespace llvm {
74 
createPrintMIRPass(raw_ostream & OS)75 MachineFunctionPass *createPrintMIRPass(raw_ostream &OS) {
76   return new MIRPrintingPass(OS);
77 }
78 
79 } // end namespace llvm
80