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/IRPrinter/IRPrintingPasses.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/PrintPasses.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
25 PrintModulePass::PrintModulePass() : OS(dbgs()) {}
26 PrintModulePass::PrintModulePass(raw_ostream
&OS
, const std::string
&Banner
,
27 bool ShouldPreserveUseListOrder
,
28 bool EmitSummaryIndex
)
29 : OS(OS
), Banner(Banner
),
30 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder
),
31 EmitSummaryIndex(EmitSummaryIndex
) {}
33 PreservedAnalyses
PrintModulePass::run(Module
&M
, ModuleAnalysisManager
&AM
) {
34 if (llvm::isFunctionInPrintList("*")) {
37 M
.print(OS
, nullptr, ShouldPreserveUseListOrder
);
39 bool BannerPrinted
= false;
40 for (const auto &F
: M
.functions()) {
41 if (llvm::isFunctionInPrintList(F
.getName())) {
42 if (!BannerPrinted
&& !Banner
.empty()) {
51 ModuleSummaryIndex
*Index
=
52 EmitSummaryIndex
? &(AM
.getResult
<ModuleSummaryIndexAnalysis
>(M
))
55 if (Index
->modulePaths().empty())
56 Index
->addModule("", 0);
60 return PreservedAnalyses::all();
63 PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
64 PrintFunctionPass::PrintFunctionPass(raw_ostream
&OS
, const std::string
&Banner
)
65 : OS(OS
), Banner(Banner
) {}
67 PreservedAnalyses
PrintFunctionPass::run(Function
&F
,
68 FunctionAnalysisManager
&) {
69 if (isFunctionInPrintList(F
.getName())) {
70 if (forcePrintModuleIR())
71 OS
<< Banner
<< " (function: " << F
.getName() << ")\n" << *F
.getParent();
73 OS
<< Banner
<< '\n' << static_cast<Value
&>(F
);
75 return PreservedAnalyses::all();