xref: /llvm-project/llvm/lib/IRPrinter/IRPrintingPasses.cpp (revision 2e39b57837aa1790b3ee078fa532bb1748a609c7)
17059a6c3SAlexander Shaposhnikov //===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
27059a6c3SAlexander Shaposhnikov //
37059a6c3SAlexander Shaposhnikov // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47059a6c3SAlexander Shaposhnikov // See https://llvm.org/LICENSE.txt for license information.
57059a6c3SAlexander Shaposhnikov // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67059a6c3SAlexander Shaposhnikov //
77059a6c3SAlexander Shaposhnikov //===----------------------------------------------------------------------===//
87059a6c3SAlexander Shaposhnikov //
97059a6c3SAlexander Shaposhnikov // PrintModulePass and PrintFunctionPass implementations.
107059a6c3SAlexander Shaposhnikov //
117059a6c3SAlexander Shaposhnikov //===----------------------------------------------------------------------===//
127059a6c3SAlexander Shaposhnikov 
137059a6c3SAlexander Shaposhnikov #include "llvm/IRPrinter/IRPrintingPasses.h"
147059a6c3SAlexander Shaposhnikov #include "llvm/ADT/StringRef.h"
15f102fe73SAlexander Shaposhnikov #include "llvm/Analysis/ModuleSummaryAnalysis.h"
167059a6c3SAlexander Shaposhnikov #include "llvm/IR/Function.h"
177059a6c3SAlexander Shaposhnikov #include "llvm/IR/Module.h"
187059a6c3SAlexander Shaposhnikov #include "llvm/IR/PrintPasses.h"
197059a6c3SAlexander Shaposhnikov #include "llvm/Pass.h"
207059a6c3SAlexander Shaposhnikov #include "llvm/Support/Debug.h"
217059a6c3SAlexander Shaposhnikov #include "llvm/Support/raw_ostream.h"
227059a6c3SAlexander Shaposhnikov 
237059a6c3SAlexander Shaposhnikov using namespace llvm;
247059a6c3SAlexander Shaposhnikov 
25*2e39b578SStephen Tozer extern cl::opt<bool> WriteNewDbgInfoFormat;
26*2e39b578SStephen Tozer 
PrintModulePass()277059a6c3SAlexander Shaposhnikov PrintModulePass::PrintModulePass() : OS(dbgs()) {}
PrintModulePass(raw_ostream & OS,const std::string & Banner,bool ShouldPreserveUseListOrder,bool EmitSummaryIndex)287059a6c3SAlexander Shaposhnikov PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,
29f102fe73SAlexander Shaposhnikov                                  bool ShouldPreserveUseListOrder,
30f102fe73SAlexander Shaposhnikov                                  bool EmitSummaryIndex)
317059a6c3SAlexander Shaposhnikov     : OS(OS), Banner(Banner),
32f102fe73SAlexander Shaposhnikov       ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
33f102fe73SAlexander Shaposhnikov       EmitSummaryIndex(EmitSummaryIndex) {}
347059a6c3SAlexander Shaposhnikov 
run(Module & M,ModuleAnalysisManager & AM)35f102fe73SAlexander Shaposhnikov PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &AM) {
36*2e39b578SStephen Tozer   // RemoveDIs: Regardless of the format we've processed this module in, use
37*2e39b578SStephen Tozer   // `WriteNewDbgInfoFormat` to determine which format we use to write it.
38*2e39b578SStephen Tozer   ScopedDbgInfoFormatSetter FormatSetter(M, WriteNewDbgInfoFormat);
39*2e39b578SStephen Tozer   // Remove intrinsic declarations when printing in the new format.
40*2e39b578SStephen Tozer   // TODO: Move this into Module::setIsNewDbgInfoFormat when we're ready to
41*2e39b578SStephen Tozer   // update test output.
42*2e39b578SStephen Tozer   if (WriteNewDbgInfoFormat)
43*2e39b578SStephen Tozer     M.removeDebugIntrinsicDeclarations();
4410a9e744SJeremy Morse 
457059a6c3SAlexander Shaposhnikov   if (llvm::isFunctionInPrintList("*")) {
467059a6c3SAlexander Shaposhnikov     if (!Banner.empty())
477059a6c3SAlexander Shaposhnikov       OS << Banner << "\n";
487059a6c3SAlexander Shaposhnikov     M.print(OS, nullptr, ShouldPreserveUseListOrder);
497059a6c3SAlexander Shaposhnikov   } else {
507059a6c3SAlexander Shaposhnikov     bool BannerPrinted = false;
517059a6c3SAlexander Shaposhnikov     for (const auto &F : M.functions()) {
527059a6c3SAlexander Shaposhnikov       if (llvm::isFunctionInPrintList(F.getName())) {
537059a6c3SAlexander Shaposhnikov         if (!BannerPrinted && !Banner.empty()) {
547059a6c3SAlexander Shaposhnikov           OS << Banner << "\n";
557059a6c3SAlexander Shaposhnikov           BannerPrinted = true;
567059a6c3SAlexander Shaposhnikov         }
577059a6c3SAlexander Shaposhnikov         F.print(OS);
587059a6c3SAlexander Shaposhnikov       }
597059a6c3SAlexander Shaposhnikov     }
607059a6c3SAlexander Shaposhnikov   }
61f102fe73SAlexander Shaposhnikov 
62f102fe73SAlexander Shaposhnikov   ModuleSummaryIndex *Index =
63f102fe73SAlexander Shaposhnikov       EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
64f102fe73SAlexander Shaposhnikov                        : nullptr;
65f102fe73SAlexander Shaposhnikov   if (Index) {
66f102fe73SAlexander Shaposhnikov     if (Index->modulePaths().empty())
67bbe8cd13STeresa Johnson       Index->addModule("");
68f102fe73SAlexander Shaposhnikov     Index->print(OS);
69f102fe73SAlexander Shaposhnikov   }
70f102fe73SAlexander Shaposhnikov 
717059a6c3SAlexander Shaposhnikov   return PreservedAnalyses::all();
727059a6c3SAlexander Shaposhnikov }
737059a6c3SAlexander Shaposhnikov 
PrintFunctionPass()747059a6c3SAlexander Shaposhnikov PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
PrintFunctionPass(raw_ostream & OS,const std::string & Banner)757059a6c3SAlexander Shaposhnikov PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
767059a6c3SAlexander Shaposhnikov     : OS(OS), Banner(Banner) {}
777059a6c3SAlexander Shaposhnikov 
run(Function & F,FunctionAnalysisManager &)787059a6c3SAlexander Shaposhnikov PreservedAnalyses PrintFunctionPass::run(Function &F,
797059a6c3SAlexander Shaposhnikov                                          FunctionAnalysisManager &) {
80*2e39b578SStephen Tozer   // RemoveDIs: Regardless of the format we've processed this function in, use
81*2e39b578SStephen Tozer   // `WriteNewDbgInfoFormat` to determine which format we use to write it.
82*2e39b578SStephen Tozer   ScopedDbgInfoFormatSetter FormatSetter(F, WriteNewDbgInfoFormat);
8310a9e744SJeremy Morse 
847059a6c3SAlexander Shaposhnikov   if (isFunctionInPrintList(F.getName())) {
857059a6c3SAlexander Shaposhnikov     if (forcePrintModuleIR())
867059a6c3SAlexander Shaposhnikov       OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();
877059a6c3SAlexander Shaposhnikov     else
887059a6c3SAlexander Shaposhnikov       OS << Banner << '\n' << static_cast<Value &>(F);
897059a6c3SAlexander Shaposhnikov   }
9010a9e744SJeremy Morse 
917059a6c3SAlexander Shaposhnikov   return PreservedAnalyses::all();
927059a6c3SAlexander Shaposhnikov }
93