blob: 0ea8975cc74c894d6930dc408d71fce81fcbcf2f [file] [log] [blame]
David Greene9b063df2010-04-02 23:17:14 +00001//===-- MachineFunctionPrinterPass.cpp ------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
David Greene9b063df2010-04-02 23:17:14 +00006//
7//===----------------------------------------------------------------------===//
8//
9// MachineFunctionPrinterPass implementation.
10//
11//===----------------------------------------------------------------------===//
12
David Greene9b063df2010-04-02 23:17:14 +000013#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesenadb50a72012-07-04 23:53:19 +000016#include "llvm/CodeGen/SlotIndexes.h"
Fedor Sergeev662e5682018-09-24 16:08:15 +000017#include "llvm/IR/IRPrintingPasses.h"
Bob Wilson33e51882012-05-30 00:17:12 +000018#include "llvm/Support/Debug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Support/raw_ostream.h"
David Greene9b063df2010-04-02 23:17:14 +000020
21using namespace llvm;
22
23namespace {
24/// MachineFunctionPrinterPass - This is a pass to dump the IR of a
25/// MachineFunction.
26///
27struct MachineFunctionPrinterPass : public MachineFunctionPass {
28 static char ID;
29
30 raw_ostream &OS;
31 const std::string Banner;
32
Bob Wilson33e51882012-05-30 00:17:12 +000033 MachineFunctionPrinterPass() : MachineFunctionPass(ID), OS(dbgs()) { }
Weiming Zhao0f1762c2016-01-06 22:55:03 +000034 MachineFunctionPrinterPass(raw_ostream &os, const std::string &banner)
Owen Andersona7aed182010-08-06 18:33:48 +000035 : MachineFunctionPass(ID), OS(os), Banner(banner) {}
David Greene9b063df2010-04-02 23:17:14 +000036
Mehdi Amini117296c2016-10-01 02:56:57 +000037 StringRef getPassName() const override { return "MachineFunction Printer"; }
David Greene9b063df2010-04-02 23:17:14 +000038
Craig Topper4584cd52014-03-07 09:26:03 +000039 void getAnalysisUsage(AnalysisUsage &AU) const override {
David Greene9b063df2010-04-02 23:17:14 +000040 AU.setPreservesAll();
Matthias Braun7d88f0e2018-10-08 23:47:34 +000041 AU.addUsedIfAvailable<SlotIndexes>();
David Greene9b063df2010-04-02 23:17:14 +000042 MachineFunctionPass::getAnalysisUsage(AU);
43 }
44
Craig Topper4584cd52014-03-07 09:26:03 +000045 bool runOnMachineFunction(MachineFunction &MF) override {
Weiming Zhao0f1762c2016-01-06 22:55:03 +000046 if (!llvm::isFunctionInPrintList(MF.getName()))
47 return false;
David Greene9b063df2010-04-02 23:17:14 +000048 OS << "# " << Banner << ":\n";
Jakob Stoklund Olesenadb50a72012-07-04 23:53:19 +000049 MF.print(OS, getAnalysisIfAvailable<SlotIndexes>());
David Greene9b063df2010-04-02 23:17:14 +000050 return false;
51 }
52};
53
54char MachineFunctionPrinterPass::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000055}
David Greene9b063df2010-04-02 23:17:14 +000056
Benjamin Kramera74129a2012-10-20 12:53:26 +000057char &llvm::MachineFunctionPrinterPassID = MachineFunctionPrinterPass::ID;
Akira Hatanaka7ba78302014-12-13 04:52:04 +000058INITIALIZE_PASS(MachineFunctionPrinterPass, "machineinstr-printer",
Bob Wilson33e51882012-05-30 00:17:12 +000059 "Machine Function Printer", false, false)
60
David Greene9b063df2010-04-02 23:17:14 +000061namespace llvm {
62/// Returns a newly-created MachineFunction Printer pass. The
63/// default banner is empty.
64///
65MachineFunctionPass *createMachineFunctionPrinterPass(raw_ostream &OS,
66 const std::string &Banner){
67 return new MachineFunctionPrinterPass(OS, Banner);
68}
69
70}