MIRPrintingPass.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include "MIRPrinter.h"
  15. #include "llvm/CodeGen/Passes.h"
  16. #include "llvm/CodeGen/MachineFunctionPass.h"
  17. #include "llvm/CodeGen/MIRYamlMapping.h"
  18. #include "llvm/Support/Debug.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. using namespace llvm;
  21. namespace {
  22. /// This pass prints out the LLVM IR to an output stream using the MIR
  23. /// serialization format.
  24. struct MIRPrintingPass : public MachineFunctionPass {
  25. static char ID;
  26. raw_ostream &OS;
  27. std::string MachineFunctions;
  28. MIRPrintingPass() : MachineFunctionPass(ID), OS(dbgs()) {}
  29. MIRPrintingPass(raw_ostream &OS) : MachineFunctionPass(ID), OS(OS) {}
  30. const char *getPassName() const override { return "MIR Printing Pass"; }
  31. void getAnalysisUsage(AnalysisUsage &AU) const override {
  32. AU.setPreservesAll();
  33. MachineFunctionPass::getAnalysisUsage(AU);
  34. }
  35. virtual bool runOnMachineFunction(MachineFunction &MF) override {
  36. std::string Str;
  37. raw_string_ostream StrOS(Str);
  38. printMIR(StrOS, MF);
  39. MachineFunctions.append(StrOS.str());
  40. return false;
  41. }
  42. virtual bool doFinalization(Module &M) override {
  43. printMIR(OS, M);
  44. OS << MachineFunctions;
  45. return false;
  46. }
  47. };
  48. char MIRPrintingPass::ID = 0;
  49. } // end anonymous namespace
  50. char &llvm::MIRPrintingPassID = MIRPrintingPass::ID;
  51. INITIALIZE_PASS(MIRPrintingPass, "mir-printer", "MIR Printer", false, false)
  52. namespace llvm {
  53. MachineFunctionPass *createPrintMIRPass(raw_ostream &OS) {
  54. return new MIRPrintingPass(OS);
  55. }
  56. } // end namespace llvm