1 | //===--- IRPrintingPasses.cpp - Module and Function printing passes -------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // PrintModulePass and PrintFunctionPass implementations. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
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" |
22 | |
23 | using namespace llvm; |
24 | |
25 | extern cl::opt<bool> WriteNewDbgInfoFormat; |
26 | |
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) {} |
34 | |
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(); |
44 | |
45 | if (llvm::isFunctionInPrintList(FunctionName: "*" )) { |
46 | if (!Banner.empty()) |
47 | OS << Banner << "\n" ; |
48 | M.print(OS, AAW: nullptr, ShouldPreserveUseListOrder); |
49 | } else { |
50 | bool BannerPrinted = false; |
51 | for (const auto &F : M.functions()) { |
52 | if (llvm::isFunctionInPrintList(FunctionName: F.getName())) { |
53 | if (!BannerPrinted && !Banner.empty()) { |
54 | OS << Banner << "\n" ; |
55 | BannerPrinted = true; |
56 | } |
57 | F.print(OS); |
58 | } |
59 | } |
60 | } |
61 | |
62 | ModuleSummaryIndex *Index = |
63 | EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(IR&: M)) |
64 | : nullptr; |
65 | if (Index) { |
66 | if (Index->modulePaths().empty()) |
67 | Index->addModule(ModPath: "" ); |
68 | Index->print(OS); |
69 | } |
70 | |
71 | return PreservedAnalyses::all(); |
72 | } |
73 | |
74 | PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {} |
75 | PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner) |
76 | : OS(OS), Banner(Banner) {} |
77 | |
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); |
83 | |
84 | if (isFunctionInPrintList(FunctionName: F.getName())) { |
85 | if (forcePrintModuleIR()) |
86 | OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent(); |
87 | else |
88 | OS << Banner << '\n' << static_cast<Value &>(F); |
89 | } |
90 | |
91 | return PreservedAnalyses::all(); |
92 | } |
93 | |