MachineFunctionPrinterPass.cpp 2.3 KB

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