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 extern cl::opt
<bool> WriteNewDbgInfoFormat
;
27 PrintModulePass::PrintModulePass() : OS(dbgs()) {}
28 PrintModulePass::PrintModulePass(raw_ostream
&OS
, const std::string
&Banner
,
29 bool ShouldPreserveUseListOrder
,
30 bool EmitSummaryIndex
)
31 : OS(OS
), Banner(Banner
),
32 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder
),
33 EmitSummaryIndex(EmitSummaryIndex
) {}
35 PreservedAnalyses
PrintModulePass::run(Module
&M
, ModuleAnalysisManager
&AM
) {
36 // RemoveDIs: Regardless of the format we've processed this module in, use
37 // `WriteNewDbgInfoFormat` to determine which format we use to write it.
38 ScopedDbgInfoFormatSetter
FormatSetter(M
, WriteNewDbgInfoFormat
);
39 // Remove intrinsic declarations when printing in the new format.
40 // TODO: Move this into Module::setIsNewDbgInfoFormat when we're ready to
41 // update test output.
42 if (WriteNewDbgInfoFormat
)
43 M
.removeDebugIntrinsicDeclarations();
45 if (llvm::isFunctionInPrintList("*")) {
48 M
.print(OS
, nullptr, ShouldPreserveUseListOrder
);
50 bool BannerPrinted
= false;
51 for (const auto &F
: M
.functions()) {
52 if (llvm::isFunctionInPrintList(F
.getName())) {
53 if (!BannerPrinted
&& !Banner
.empty()) {
62 ModuleSummaryIndex
*Index
=
63 EmitSummaryIndex
? &(AM
.getResult
<ModuleSummaryIndexAnalysis
>(M
))
66 if (Index
->modulePaths().empty())
71 return PreservedAnalyses::all();
74 PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
75 PrintFunctionPass::PrintFunctionPass(raw_ostream
&OS
, const std::string
&Banner
)
76 : OS(OS
), Banner(Banner
) {}
78 PreservedAnalyses
PrintFunctionPass::run(Function
&F
,
79 FunctionAnalysisManager
&) {
80 // RemoveDIs: Regardless of the format we've processed this function in, use
81 // `WriteNewDbgInfoFormat` to determine which format we use to write it.
82 ScopedDbgInfoFormatSetter
FormatSetter(F
, WriteNewDbgInfoFormat
);
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
);
91 return PreservedAnalyses::all();