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