xref: /freebsd-src/contrib/llvm-project/llvm/lib/IRPrinter/IRPrintingPasses.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1bdd1243dSDimitry Andric //===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
2bdd1243dSDimitry Andric //
3bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bdd1243dSDimitry Andric //
7bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
8bdd1243dSDimitry Andric //
9bdd1243dSDimitry Andric // PrintModulePass and PrintFunctionPass implementations.
10bdd1243dSDimitry Andric //
11bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
12bdd1243dSDimitry Andric 
13bdd1243dSDimitry Andric #include "llvm/IRPrinter/IRPrintingPasses.h"
14bdd1243dSDimitry Andric #include "llvm/ADT/StringRef.h"
15bdd1243dSDimitry Andric #include "llvm/Analysis/ModuleSummaryAnalysis.h"
16bdd1243dSDimitry Andric #include "llvm/IR/Function.h"
17bdd1243dSDimitry Andric #include "llvm/IR/Module.h"
18bdd1243dSDimitry Andric #include "llvm/IR/PrintPasses.h"
19bdd1243dSDimitry Andric #include "llvm/Pass.h"
20bdd1243dSDimitry Andric #include "llvm/Support/Debug.h"
21bdd1243dSDimitry Andric #include "llvm/Support/raw_ostream.h"
22bdd1243dSDimitry Andric 
23bdd1243dSDimitry Andric using namespace llvm;
24bdd1243dSDimitry Andric 
25*0fca6ea1SDimitry Andric extern cl::opt<bool> WriteNewDbgInfoFormat;
26*0fca6ea1SDimitry Andric 
27bdd1243dSDimitry Andric PrintModulePass::PrintModulePass() : OS(dbgs()) {}
28bdd1243dSDimitry Andric PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,
29bdd1243dSDimitry Andric                                  bool ShouldPreserveUseListOrder,
30bdd1243dSDimitry Andric                                  bool EmitSummaryIndex)
31bdd1243dSDimitry Andric     : OS(OS), Banner(Banner),
32bdd1243dSDimitry Andric       ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
33bdd1243dSDimitry Andric       EmitSummaryIndex(EmitSummaryIndex) {}
34bdd1243dSDimitry Andric 
35bdd1243dSDimitry Andric PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &AM) {
36*0fca6ea1SDimitry Andric   // RemoveDIs: Regardless of the format we've processed this module in, use
37*0fca6ea1SDimitry Andric   // `WriteNewDbgInfoFormat` to determine which format we use to write it.
38*0fca6ea1SDimitry Andric   ScopedDbgInfoFormatSetter FormatSetter(M, WriteNewDbgInfoFormat);
39*0fca6ea1SDimitry Andric   // Remove intrinsic declarations when printing in the new format.
40*0fca6ea1SDimitry Andric   // TODO: Move this into Module::setIsNewDbgInfoFormat when we're ready to
41*0fca6ea1SDimitry Andric   // update test output.
42*0fca6ea1SDimitry Andric   if (WriteNewDbgInfoFormat)
43*0fca6ea1SDimitry Andric     M.removeDebugIntrinsicDeclarations();
445f757f3fSDimitry Andric 
45bdd1243dSDimitry Andric   if (llvm::isFunctionInPrintList("*")) {
46bdd1243dSDimitry Andric     if (!Banner.empty())
47bdd1243dSDimitry Andric       OS << Banner << "\n";
48bdd1243dSDimitry Andric     M.print(OS, nullptr, ShouldPreserveUseListOrder);
49bdd1243dSDimitry Andric   } else {
50bdd1243dSDimitry Andric     bool BannerPrinted = false;
51bdd1243dSDimitry Andric     for (const auto &F : M.functions()) {
52bdd1243dSDimitry Andric       if (llvm::isFunctionInPrintList(F.getName())) {
53bdd1243dSDimitry Andric         if (!BannerPrinted && !Banner.empty()) {
54bdd1243dSDimitry Andric           OS << Banner << "\n";
55bdd1243dSDimitry Andric           BannerPrinted = true;
56bdd1243dSDimitry Andric         }
57bdd1243dSDimitry Andric         F.print(OS);
58bdd1243dSDimitry Andric       }
59bdd1243dSDimitry Andric     }
60bdd1243dSDimitry Andric   }
61bdd1243dSDimitry Andric 
62bdd1243dSDimitry Andric   ModuleSummaryIndex *Index =
63bdd1243dSDimitry Andric       EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
64bdd1243dSDimitry Andric                        : nullptr;
65bdd1243dSDimitry Andric   if (Index) {
66bdd1243dSDimitry Andric     if (Index->modulePaths().empty())
675f757f3fSDimitry Andric       Index->addModule("");
68bdd1243dSDimitry Andric     Index->print(OS);
69bdd1243dSDimitry Andric   }
70bdd1243dSDimitry Andric 
71bdd1243dSDimitry Andric   return PreservedAnalyses::all();
72bdd1243dSDimitry Andric }
73bdd1243dSDimitry Andric 
74bdd1243dSDimitry Andric PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
75bdd1243dSDimitry Andric PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
76bdd1243dSDimitry Andric     : OS(OS), Banner(Banner) {}
77bdd1243dSDimitry Andric 
78bdd1243dSDimitry Andric PreservedAnalyses PrintFunctionPass::run(Function &F,
79bdd1243dSDimitry Andric                                          FunctionAnalysisManager &) {
80*0fca6ea1SDimitry Andric   // RemoveDIs: Regardless of the format we've processed this function in, use
81*0fca6ea1SDimitry Andric   // `WriteNewDbgInfoFormat` to determine which format we use to write it.
82*0fca6ea1SDimitry Andric   ScopedDbgInfoFormatSetter FormatSetter(F, WriteNewDbgInfoFormat);
835f757f3fSDimitry Andric 
84bdd1243dSDimitry Andric   if (isFunctionInPrintList(F.getName())) {
85bdd1243dSDimitry Andric     if (forcePrintModuleIR())
86bdd1243dSDimitry Andric       OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();
87bdd1243dSDimitry Andric     else
88bdd1243dSDimitry Andric       OS << Banner << '\n' << static_cast<Value &>(F);
89bdd1243dSDimitry Andric   }
905f757f3fSDimitry Andric 
91bdd1243dSDimitry Andric   return PreservedAnalyses::all();
92bdd1243dSDimitry Andric }
93