blob: e032fffd658c843ce034568b7b23b8bd4c4bd6e5 [file] [log] [blame]
Alex Lorenz2bdb4e12015-05-27 18:02:19 +00001//===- MIRPrintingPass.cpp - Pass that prints out using the MIR format ----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Alex Lorenz2bdb4e12015-05-27 18:02:19 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a pass that prints out the LLVM module using the MIR
10// serialization format.
11//
12//===----------------------------------------------------------------------===//
13
Matthias Braun89401142017-05-05 21:09:30 +000014#include "llvm/CodeGen/MIRPrinter.h"
15
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/CodeGen/MachineFunctionPass.h"
17#include "llvm/CodeGen/Passes.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000018#include "llvm/Support/Debug.h"
19#include "llvm/Support/raw_ostream.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000020
21using namespace llvm;
22
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000023namespace {
24
25/// This pass prints out the LLVM IR to an output stream using the MIR
26/// serialization format.
27struct MIRPrintingPass : public MachineFunctionPass {
28 static char ID;
29 raw_ostream &OS;
Alex Lorenz78d78312015-05-28 22:41:12 +000030 std::string MachineFunctions;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000031
32 MIRPrintingPass() : MachineFunctionPass(ID), OS(dbgs()) {}
33 MIRPrintingPass(raw_ostream &OS) : MachineFunctionPass(ID), OS(OS) {}
34
Mehdi Amini117296c2016-10-01 02:56:57 +000035 StringRef getPassName() const override { return "MIR Printing Pass"; }
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000036
37 void getAnalysisUsage(AnalysisUsage &AU) const override {
38 AU.setPreservesAll();
39 MachineFunctionPass::getAnalysisUsage(AU);
40 }
41
Hans Wennborgaa15bff2015-09-10 16:49:58 +000042 bool runOnMachineFunction(MachineFunction &MF) override {
Alex Lorenz78d78312015-05-28 22:41:12 +000043 std::string Str;
44 raw_string_ostream StrOS(Str);
Alex Lorenz345c1442015-06-15 23:52:35 +000045 printMIR(StrOS, MF);
Alex Lorenz78d78312015-05-28 22:41:12 +000046 MachineFunctions.append(StrOS.str());
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000047 return false;
48 }
49
Hans Wennborgaa15bff2015-09-10 16:49:58 +000050 bool doFinalization(Module &M) override {
Alex Lorenz345c1442015-06-15 23:52:35 +000051 printMIR(OS, M);
Alex Lorenz78d78312015-05-28 22:41:12 +000052 OS << MachineFunctions;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000053 return false;
54 }
55};
56
57char MIRPrintingPass::ID = 0;
58
59} // end anonymous namespace
60
61char &llvm::MIRPrintingPassID = MIRPrintingPass::ID;
62INITIALIZE_PASS(MIRPrintingPass, "mir-printer", "MIR Printer", false, false)
63
64namespace llvm {
65
66MachineFunctionPass *createPrintMIRPass(raw_ostream &OS) {
67 return new MIRPrintingPass(OS);
68}
69
70} // end namespace llvm