blob: 5e0f4cdcbfde093b1305fb73e0e512f0ae3fb533 [file] [log] [blame]
Alex Lorenz2bdb4e12015-05-27 18:02:19 +00001//===- MIRPrintingPass.cpp - Pass that prints out using the MIR format ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a pass that prints out the LLVM module using the MIR
11// serialization format.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/Passes.h"
16#include "llvm/CodeGen/MachineFunction.h"
17#include "llvm/CodeGen/MachineFunctionPass.h"
Alex Lorenz78d78312015-05-28 22:41:12 +000018#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000019#include "llvm/IR/Module.h"
20#include "llvm/Support/Debug.h"
21#include "llvm/Support/raw_ostream.h"
22#include "llvm/Support/YAMLTraits.h"
23
24using namespace llvm;
25
26namespace llvm {
27namespace yaml {
28
29/// This struct serializes the LLVM IR module.
30template <> struct BlockScalarTraits<Module> {
31 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
32 Mod.print(OS, nullptr);
33 }
34 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
35 llvm_unreachable("LLVM Module is supposed to be parsed separately");
36 return "";
37 }
38};
39
40} // end namespace yaml
41} // end namespace llvm
42
43namespace {
44
Alex Lorenz78d78312015-05-28 22:41:12 +000045/// This class prints out the machine functions using the MIR serialization
46/// format.
47class MIRPrinter {
48 raw_ostream &OS;
49
50public:
51 MIRPrinter(raw_ostream &OS) : OS(OS) {}
52
53 void print(const MachineFunction &MF);
54};
55
56void MIRPrinter::print(const MachineFunction &MF) {
57 yaml::MachineFunction YamlMF;
58 YamlMF.Name = MF.getName();
59 yaml::Output Out(OS);
60 Out << YamlMF;
61}
62
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000063/// This pass prints out the LLVM IR to an output stream using the MIR
64/// serialization format.
65struct MIRPrintingPass : public MachineFunctionPass {
66 static char ID;
67 raw_ostream &OS;
Alex Lorenz78d78312015-05-28 22:41:12 +000068 std::string MachineFunctions;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000069
70 MIRPrintingPass() : MachineFunctionPass(ID), OS(dbgs()) {}
71 MIRPrintingPass(raw_ostream &OS) : MachineFunctionPass(ID), OS(OS) {}
72
73 const char *getPassName() const override { return "MIR Printing Pass"; }
74
75 void getAnalysisUsage(AnalysisUsage &AU) const override {
76 AU.setPreservesAll();
77 MachineFunctionPass::getAnalysisUsage(AU);
78 }
79
80 virtual bool runOnMachineFunction(MachineFunction &MF) override {
Alex Lorenz78d78312015-05-28 22:41:12 +000081 std::string Str;
82 raw_string_ostream StrOS(Str);
83 MIRPrinter(StrOS).print(MF);
84 MachineFunctions.append(StrOS.str());
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000085 return false;
86 }
87
88 virtual bool doFinalization(Module &M) override {
89 yaml::Output Out(OS);
90 Out << M;
Alex Lorenz78d78312015-05-28 22:41:12 +000091 OS << MachineFunctions;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000092 return false;
93 }
94};
95
96char MIRPrintingPass::ID = 0;
97
98} // end anonymous namespace
99
100char &llvm::MIRPrintingPassID = MIRPrintingPass::ID;
101INITIALIZE_PASS(MIRPrintingPass, "mir-printer", "MIR Printer", false, false)
102
103namespace llvm {
104
105MachineFunctionPass *createPrintMIRPass(raw_ostream &OS) {
106 return new MIRPrintingPass(OS);
107}
108
109} // end namespace llvm