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 // RemoveDIs: there's no textual representation of the DPValue debug-info,
35 // convert to dbg.values before writing out.
36 bool ShouldConvert
= M
.IsNewDbgInfoFormat
;
38 M
.convertFromNewDbgValues();
40 if (llvm::isFunctionInPrintList("*")) {
43 M
.print(OS
, nullptr, ShouldPreserveUseListOrder
);
45 bool BannerPrinted
= false;
46 for (const auto &F
: M
.functions()) {
47 if (llvm::isFunctionInPrintList(F
.getName())) {
48 if (!BannerPrinted
&& !Banner
.empty()) {
57 ModuleSummaryIndex
*Index
=
58 EmitSummaryIndex
? &(AM
.getResult
<ModuleSummaryIndexAnalysis
>(M
))
61 if (Index
->modulePaths().empty())
67 M
.convertToNewDbgValues();
69 return PreservedAnalyses::all();
72 PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
73 PrintFunctionPass::PrintFunctionPass(raw_ostream
&OS
, const std::string
&Banner
)
74 : OS(OS
), Banner(Banner
) {}
76 PreservedAnalyses
PrintFunctionPass::run(Function
&F
,
77 FunctionAnalysisManager
&) {
78 // RemoveDIs: there's no textual representation of the DPValue debug-info,
79 // convert to dbg.values before writing out.
80 bool ShouldConvert
= F
.IsNewDbgInfoFormat
;
82 F
.convertFromNewDbgValues();
84 if (isFunctionInPrintList(F
.getName())) {
85 if (forcePrintModuleIR())
86 OS
<< Banner
<< " (function: " << F
.getName() << ")\n" << *F
.getParent();
88 OS
<< Banner
<< '\n' << static_cast<Value
&>(F
);
92 F
.convertToNewDbgValues();
94 return PreservedAnalyses::all();