xref: /llvm-project/llvm/lib/IRPrinter/IRPrintingPasses.cpp (revision aa436493ab7ad4cf323b0189c15c59ac9dc293c7)
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 
40   if (llvm::isFunctionInPrintList("*")) {
41     if (!Banner.empty())
42       OS << Banner << "\n";
43     M.print(OS, nullptr, ShouldPreserveUseListOrder);
44   } else {
45     bool BannerPrinted = false;
46     for (const auto &F : M.functions()) {
47       if (llvm::isFunctionInPrintList(F.getName())) {
48         if (!BannerPrinted && !Banner.empty()) {
49           OS << Banner << "\n";
50           BannerPrinted = true;
51         }
52         F.print(OS);
53       }
54     }
55   }
56 
57   ModuleSummaryIndex *Index =
58       EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
59                        : nullptr;
60   if (Index) {
61     if (Index->modulePaths().empty())
62       Index->addModule("");
63     Index->print(OS);
64   }
65 
66   return PreservedAnalyses::all();
67 }
68 
69 PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
70 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
71     : OS(OS), Banner(Banner) {}
72 
73 PreservedAnalyses PrintFunctionPass::run(Function &F,
74                                          FunctionAnalysisManager &) {
75   // RemoveDIs: Regardless of the format we've processed this function in, use
76   // `WriteNewDbgInfoFormat` to determine which format we use to write it.
77   ScopedDbgInfoFormatSetter FormatSetter(F, WriteNewDbgInfoFormat);
78 
79   if (isFunctionInPrintList(F.getName())) {
80     if (forcePrintModuleIR())
81       OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();
82     else
83       OS << Banner << '\n' << static_cast<Value &>(F);
84   }
85 
86   return PreservedAnalyses::all();
87 }
88