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