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 
PrintModulePass()25 PrintModulePass::PrintModulePass() : OS(dbgs()) {}
PrintModulePass(raw_ostream & OS,const std::string & Banner,bool ShouldPreserveUseListOrder,bool EmitSummaryIndex)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) {}
32 
run(Module & M,ModuleAnalysisManager & AM)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;
37   if (ShouldConvert)
38     M.convertFromNewDbgValues();
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   if (ShouldConvert)
67     M.convertToNewDbgValues();
68 
69   return PreservedAnalyses::all();
70 }
71 
PrintFunctionPass()72 PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
PrintFunctionPass(raw_ostream & OS,const std::string & Banner)73 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
74     : OS(OS), Banner(Banner) {}
75 
run(Function & F,FunctionAnalysisManager &)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;
81   if (ShouldConvert)
82     F.convertFromNewDbgValues();
83 
84   if (isFunctionInPrintList(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   if (ShouldConvert)
92     F.convertToNewDbgValues();
93 
94   return PreservedAnalyses::all();
95 }
96