1 //===-- MachineFunctionPrinterPass.cpp ------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // MachineFunctionPrinterPass implementation.
11 //===----------------------------------------------------------------------===//
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/IRPrintingPasses.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
24 /// MachineFunctionPrinterPass - This is a pass to dump the IR of a
27 struct MachineFunctionPrinterPass
: public MachineFunctionPass
{
31 const std::string Banner
;
33 MachineFunctionPrinterPass() : MachineFunctionPass(ID
), OS(dbgs()) { }
34 MachineFunctionPrinterPass(raw_ostream
&os
, const std::string
&banner
)
35 : MachineFunctionPass(ID
), OS(os
), Banner(banner
) {}
37 StringRef
getPassName() const override
{ return "MachineFunction Printer"; }
39 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
41 AU
.addUsedIfAvailable
<SlotIndexes
>();
42 MachineFunctionPass::getAnalysisUsage(AU
);
45 bool runOnMachineFunction(MachineFunction
&MF
) override
{
46 if (!llvm::isFunctionInPrintList(MF
.getName()))
48 OS
<< "# " << Banner
<< ":\n";
49 MF
.print(OS
, getAnalysisIfAvailable
<SlotIndexes
>());
54 char MachineFunctionPrinterPass::ID
= 0;
57 char &llvm::MachineFunctionPrinterPassID
= MachineFunctionPrinterPass::ID
;
58 INITIALIZE_PASS(MachineFunctionPrinterPass
, "machineinstr-printer",
59 "Machine Function Printer", false, false)
62 /// Returns a newly-created MachineFunction Printer pass. The
63 /// default banner is empty.
65 MachineFunctionPass
*createMachineFunctionPrinterPass(raw_ostream
&OS
,
66 const std::string
&Banner
){
67 return new MachineFunctionPrinterPass(OS
, Banner
);