Alex Lorenz | 61aecc8 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 1 | //===- 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 | |
Matthias Braun | 0cb25a2 | 2017-05-05 21:09:30 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MIRPrinter.h" |
| 16 | |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 18 | #include "llvm/CodeGen/Passes.h" |
Alex Lorenz | 61aecc8 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
Alex Lorenz | 61aecc8 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
| 23 | |
Alex Lorenz | 61aecc8 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | /// This pass prints out the LLVM IR to an output stream using the MIR |
| 27 | /// serialization format. |
| 28 | struct MIRPrintingPass : public MachineFunctionPass { |
| 29 | static char ID; |
| 30 | raw_ostream &OS; |
Alex Lorenz | 3682046 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 31 | std::string MachineFunctions; |
Alex Lorenz | 61aecc8 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 32 | |
| 33 | MIRPrintingPass() : MachineFunctionPass(ID), OS(dbgs()) {} |
| 34 | MIRPrintingPass(raw_ostream &OS) : MachineFunctionPass(ID), OS(OS) {} |
| 35 | |
Mehdi Amini | 67f335d | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 36 | StringRef getPassName() const override { return "MIR Printing Pass"; } |
Alex Lorenz | 61aecc8 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 37 | |
| 38 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 39 | AU.setPreservesAll(); |
| 40 | MachineFunctionPass::getAnalysisUsage(AU); |
| 41 | } |
| 42 | |
Hans Wennborg | 07a3b97 | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 43 | bool runOnMachineFunction(MachineFunction &MF) override { |
Alex Lorenz | 3682046 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 44 | std::string Str; |
| 45 | raw_string_ostream StrOS(Str); |
Alex Lorenz | b3607cd | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 46 | printMIR(StrOS, MF); |
Alex Lorenz | 3682046 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 47 | MachineFunctions.append(StrOS.str()); |
Alex Lorenz | 61aecc8 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 48 | return false; |
| 49 | } |
| 50 | |
Hans Wennborg | 07a3b97 | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 51 | bool doFinalization(Module &M) override { |
Alex Lorenz | b3607cd | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 52 | printMIR(OS, M); |
Alex Lorenz | 3682046 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 53 | OS << MachineFunctions; |
Alex Lorenz | 61aecc8 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 54 | return false; |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | char MIRPrintingPass::ID = 0; |
| 59 | |
| 60 | } // end anonymous namespace |
| 61 | |
| 62 | char &llvm::MIRPrintingPassID = MIRPrintingPass::ID; |
| 63 | INITIALIZE_PASS(MIRPrintingPass, "mir-printer", "MIR Printer", false, false) |
| 64 | |
| 65 | namespace llvm { |
| 66 | |
| 67 | MachineFunctionPass *createPrintMIRPass(raw_ostream &OS) { |
| 68 | return new MIRPrintingPass(OS); |
| 69 | } |
| 70 | |
| 71 | } // end namespace llvm |