1 //===- MIRPrintingPass.cpp - Pass that prints out using the MIR format ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a pass that prints out the LLVM module using the MIR
11 // serialization format.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/Passes.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/CodeGen/MIRYamlMapping.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Support/YAMLTraits.h"
29 /// This struct serializes the LLVM IR module.
30 template <> struct BlockScalarTraits<Module> {
31 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
32 Mod.print(OS, nullptr);
34 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
35 llvm_unreachable("LLVM Module is supposed to be parsed separately");
40 } // end namespace yaml
41 } // end namespace llvm
45 /// This class prints out the machine functions using the MIR serialization
51 MIRPrinter(raw_ostream &OS) : OS(OS) {}
53 void print(const MachineFunction &MF);
56 void MIRPrinter::print(const MachineFunction &MF) {
57 yaml::MachineFunction YamlMF;
58 YamlMF.Name = MF.getName();
63 /// This pass prints out the LLVM IR to an output stream using the MIR
64 /// serialization format.
65 struct MIRPrintingPass : public MachineFunctionPass {
68 std::string MachineFunctions;
70 MIRPrintingPass() : MachineFunctionPass(ID), OS(dbgs()) {}
71 MIRPrintingPass(raw_ostream &OS) : MachineFunctionPass(ID), OS(OS) {}
73 const char *getPassName() const override { return "MIR Printing Pass"; }
75 void getAnalysisUsage(AnalysisUsage &AU) const override {
77 MachineFunctionPass::getAnalysisUsage(AU);
80 virtual bool runOnMachineFunction(MachineFunction &MF) override {
82 raw_string_ostream StrOS(Str);
83 MIRPrinter(StrOS).print(MF);
84 MachineFunctions.append(StrOS.str());
88 virtual bool doFinalization(Module &M) override {
91 OS << MachineFunctions;
96 char MIRPrintingPass::ID = 0;
98 } // end anonymous namespace
100 char &llvm::MIRPrintingPassID = MIRPrintingPass::ID;
101 INITIALIZE_PASS(MIRPrintingPass, "mir-printer", "MIR Printer", false, false)
105 MachineFunctionPass *createPrintMIRPass(raw_ostream &OS) {
106 return new MIRPrintingPass(OS);
109 } // end namespace llvm