MIRPrintingPass.cpp 2.0 KB

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