1 //===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
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 // PrintModulePass and PrintFunctionPass implementations.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/IRPrintingPasses.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/IR/PassManager.h"
17 #include "llvm/InitializePasses.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
23 PrintModulePass::PrintModulePass() : OS(dbgs()) {}
24 PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,
25 bool ShouldPreserveUseListOrder)
26 : OS(OS), Banner(Banner),
27 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
29 PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &) {
30 if (llvm::isFunctionInPrintList("*")) {
33 M.print(OS, nullptr, ShouldPreserveUseListOrder);
36 bool BannerPrinted = false;
37 for(const auto &F : M.functions()) {
38 if (llvm::isFunctionInPrintList(F.getName())) {
39 if (!BannerPrinted && !Banner.empty()) {
47 return PreservedAnalyses::all();
50 PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
51 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
52 : OS(OS), Banner(Banner) {}
54 PreservedAnalyses PrintFunctionPass::run(Function &F,
55 FunctionAnalysisManager &) {
56 if (isFunctionInPrintList(F.getName())) {
57 if (forcePrintModuleIR())
58 OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();
60 OS << Banner << '\n' << static_cast<Value &>(F);
62 return PreservedAnalyses::all();
67 class PrintModulePassWrapper : public ModulePass {
72 PrintModulePassWrapper() : ModulePass(ID) {}
73 PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
74 bool ShouldPreserveUseListOrder)
75 : ModulePass(ID), P(OS, Banner, ShouldPreserveUseListOrder) {}
77 bool runOnModule(Module &M) override {
78 ModuleAnalysisManager DummyMAM;
83 void getAnalysisUsage(AnalysisUsage &AU) const override {
87 StringRef getPassName() const override { return "Print Module IR"; }
90 class PrintFunctionPassWrapper : public FunctionPass {
95 PrintFunctionPassWrapper() : FunctionPass(ID) {}
96 PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
97 : FunctionPass(ID), P(OS, Banner) {}
99 // This pass just prints a banner followed by the function as it's processed.
100 bool runOnFunction(Function &F) override {
101 FunctionAnalysisManager DummyFAM;
106 void getAnalysisUsage(AnalysisUsage &AU) const override {
107 AU.setPreservesAll();
110 StringRef getPassName() const override { return "Print Function IR"; }
115 char PrintModulePassWrapper::ID = 0;
116 INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
117 "Print module to stderr", false, true)
118 char PrintFunctionPassWrapper::ID = 0;
119 INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
120 "Print function to stderr", false, true)
122 ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS,
123 const std::string &Banner,
124 bool ShouldPreserveUseListOrder) {
125 return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
128 FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS,
129 const std::string &Banner) {
130 return new PrintFunctionPassWrapper(OS, Banner);
133 bool llvm::isIRPrintingPass(Pass *P) {
134 const char *PID = (const char*)P->getPassID();
136 return (PID == &PrintModulePassWrapper::ID) ||
137 (PID == &PrintFunctionPassWrapper::ID);