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/Compiler.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
23
24 using namespace llvm;
25
PrintModulePass()26 PrintModulePass::PrintModulePass() : OS(dbgs()) {}
PrintModulePass(raw_ostream & OS,const std::string & Banner,bool ShouldPreserveUseListOrder,bool EmitSummaryIndex)27 PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,
28 bool ShouldPreserveUseListOrder,
29 bool EmitSummaryIndex)
30 : OS(OS), Banner(Banner),
31 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
32 EmitSummaryIndex(EmitSummaryIndex) {}
33
run(Module & M,ModuleAnalysisManager & AM)34 PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &AM) {
35 // Remove intrinsic declarations when printing in the new format.
36 // TODO: consider removing this now that debug intrinsics are gone.
37 M.removeDebugIntrinsicDeclarations();
38
39 if (llvm::isFunctionInPrintList("*")) {
40 if (!Banner.empty())
41 OS << Banner << "\n";
42 M.print(OS, nullptr, ShouldPreserveUseListOrder);
43 } else {
44 bool BannerPrinted = false;
45 for (const auto &F : M.functions()) {
46 if (llvm::isFunctionInPrintList(F.getName())) {
47 if (!BannerPrinted && !Banner.empty()) {
48 OS << Banner << "\n";
49 BannerPrinted = true;
50 }
51 F.print(OS);
52 }
53 }
54 }
55
56 ModuleSummaryIndex *Index =
57 EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
58 : nullptr;
59 if (Index) {
60 if (Index->modulePaths().empty())
61 Index->addModule("");
62 Index->print(OS);
63 }
64
65 return PreservedAnalyses::all();
66 }
67
PrintFunctionPass()68 PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
PrintFunctionPass(raw_ostream & OS,const std::string & Banner)69 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
70 : OS(OS), Banner(Banner) {}
71
run(Function & F,FunctionAnalysisManager &)72 PreservedAnalyses PrintFunctionPass::run(Function &F,
73 FunctionAnalysisManager &) {
74 if (isFunctionInPrintList(F.getName())) {
75 if (forcePrintModuleIR())
76 OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();
77 else
78 OS << Banner << '\n' << static_cast<Value &>(F);
79 }
80
81 return PreservedAnalyses::all();
82 }
83