MachineFunctionPrinterPass.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //===-- MachineFunctionPrinterPass.cpp ------------------------------------===//
  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. // MachineFunctionPrinterPass implementation.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/MachineFunction.h"
  13. #include "llvm/CodeGen/MachineFunctionPass.h"
  14. #include "llvm/CodeGen/Passes.h"
  15. #include "llvm/CodeGen/SlotIndexes.h"
  16. #include "llvm/IR/IRPrintingPasses.h"
  17. #include "llvm/Support/Debug.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace llvm;
  20. namespace {
  21. /// MachineFunctionPrinterPass - This is a pass to dump the IR of a
  22. /// MachineFunction.
  23. ///
  24. struct MachineFunctionPrinterPass : public MachineFunctionPass {
  25. static char ID;
  26. raw_ostream &OS;
  27. const std::string Banner;
  28. MachineFunctionPrinterPass() : MachineFunctionPass(ID), OS(dbgs()) { }
  29. MachineFunctionPrinterPass(raw_ostream &os, const std::string &banner)
  30. : MachineFunctionPass(ID), OS(os), Banner(banner) {}
  31. StringRef getPassName() const override { return "MachineFunction Printer"; }
  32. void getAnalysisUsage(AnalysisUsage &AU) const override {
  33. AU.setPreservesAll();
  34. AU.addUsedIfAvailable<SlotIndexes>();
  35. MachineFunctionPass::getAnalysisUsage(AU);
  36. }
  37. bool runOnMachineFunction(MachineFunction &MF) override {
  38. if (!llvm::isFunctionInPrintList(MF.getName()))
  39. return false;
  40. OS << "# " << Banner << ":\n";
  41. MF.print(OS, getAnalysisIfAvailable<SlotIndexes>());
  42. return false;
  43. }
  44. };
  45. char MachineFunctionPrinterPass::ID = 0;
  46. }
  47. char &llvm::MachineFunctionPrinterPassID = MachineFunctionPrinterPass::ID;
  48. INITIALIZE_PASS(MachineFunctionPrinterPass, "machineinstr-printer",
  49. "Machine Function Printer", false, false)
  50. namespace llvm {
  51. /// Returns a newly-created MachineFunction Printer pass. The
  52. /// default banner is empty.
  53. ///
  54. MachineFunctionPass *createMachineFunctionPrinterPass(raw_ostream &OS,
  55. const std::string &Banner){
  56. return new MachineFunctionPrinterPass(OS, Banner);
  57. }
  58. }