1 //===-- MachineFunctionPrinterPass.cpp ------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // MachineFunctionPrinterPass implementation. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/MachineFunction.h" 15 #include "llvm/CodeGen/MachineFunctionPass.h" 16 #include "llvm/CodeGen/Passes.h" 17 #include "llvm/CodeGen/SlotIndexes.h" 18 #include "llvm/IR/IRPrintingPasses.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 MachineFunctionPass::getAnalysisUsage(AU); 43 } 44 45 bool runOnMachineFunction(MachineFunction &MF) override { 46 if (!llvm::isFunctionInPrintList(MF.getName())) 47 return false; 48 OS << "# " << Banner << ":\n"; 49 MF.print(OS, getAnalysisIfAvailable<SlotIndexes>()); 50 return false; 51 } 52 }; 53 54 char MachineFunctionPrinterPass::ID = 0; 55 } 56 57 char &llvm::MachineFunctionPrinterPassID = MachineFunctionPrinterPass::ID; 58 INITIALIZE_PASS(MachineFunctionPrinterPass, "machineinstr-printer", 59 "Machine Function Printer", false, false) 60 61 namespace llvm { 62 /// Returns a newly-created MachineFunction Printer pass. The 63 /// default banner is empty. 64 /// 65 MachineFunctionPass *createMachineFunctionPrinterPass(raw_ostream &OS, 66 const std::string &Banner){ 67 return new MachineFunctionPrinterPass(OS, Banner); 68 } 69 70 } 71