blob: 3645a4e3466bc112de0499cd0d8ed48d3784d27e [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"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080018#include "llvm/InitializePasses.h"
Bob Wilson33e51882012-05-30 00:17:12 +000019#include "llvm/Support/Debug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Support/raw_ostream.h"
David Greene9b063df2010-04-02 23:17:14 +000021
22using namespace llvm;
23
24namespace {
25/// MachineFunctionPrinterPass - This is a pass to dump the IR of a
26/// MachineFunction.
27///
28struct MachineFunctionPrinterPass : public MachineFunctionPass {
29 static char ID;
30
31 raw_ostream &OS;
32 const std::string Banner;
33
Bob Wilson33e51882012-05-30 00:17:12 +000034 MachineFunctionPrinterPass() : MachineFunctionPass(ID), OS(dbgs()) { }
Weiming Zhao0f1762c2016-01-06 22:55:03 +000035 MachineFunctionPrinterPass(raw_ostream &os, const std::string &banner)
Owen Andersona7aed182010-08-06 18:33:48 +000036 : MachineFunctionPass(ID), OS(os), Banner(banner) {}
David Greene9b063df2010-04-02 23:17:14 +000037
Mehdi Amini117296c2016-10-01 02:56:57 +000038 StringRef getPassName() const override { return "MachineFunction Printer"; }
David Greene9b063df2010-04-02 23:17:14 +000039
Craig Topper4584cd52014-03-07 09:26:03 +000040 void getAnalysisUsage(AnalysisUsage &AU) const override {
David Greene9b063df2010-04-02 23:17:14 +000041 AU.setPreservesAll();
Matthias Braun7d88f0e2018-10-08 23:47:34 +000042 AU.addUsedIfAvailable<SlotIndexes>();
David Greene9b063df2010-04-02 23:17:14 +000043 MachineFunctionPass::getAnalysisUsage(AU);
44 }
45
Craig Topper4584cd52014-03-07 09:26:03 +000046 bool runOnMachineFunction(MachineFunction &MF) override {
Weiming Zhao0f1762c2016-01-06 22:55:03 +000047 if (!llvm::isFunctionInPrintList(MF.getName()))
48 return false;
David Greene9b063df2010-04-02 23:17:14 +000049 OS << "# " << Banner << ":\n";
Jakob Stoklund Olesenadb50a72012-07-04 23:53:19 +000050 MF.print(OS, getAnalysisIfAvailable<SlotIndexes>());
David Greene9b063df2010-04-02 23:17:14 +000051 return false;
52 }
53};
54
55char MachineFunctionPrinterPass::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000056}
David Greene9b063df2010-04-02 23:17:14 +000057
Benjamin Kramera74129a2012-10-20 12:53:26 +000058char &llvm::MachineFunctionPrinterPassID = MachineFunctionPrinterPass::ID;
Akira Hatanaka7ba78302014-12-13 04:52:04 +000059INITIALIZE_PASS(MachineFunctionPrinterPass, "machineinstr-printer",
Bob Wilson33e51882012-05-30 00:17:12 +000060 "Machine Function Printer", false, false)
61
David Greene9b063df2010-04-02 23:17:14 +000062namespace llvm {
63/// Returns a newly-created MachineFunction Printer pass. The
64/// default banner is empty.
65///
66MachineFunctionPass *createMachineFunctionPrinterPass(raw_ostream &OS,
67 const std::string &Banner){
68 return new MachineFunctionPrinterPass(OS, Banner);
69}
70
71}