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
24using namespace llvm;
25
26PrintModulePass::PrintModulePass() : OS(dbgs()) {}
27PrintModulePass::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
34PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &AM) {
35 if (llvm::isFunctionInPrintList(FunctionName: "*")) {
36 if (!Banner.empty())
37 OS << Banner << "\n";
38 M.print(OS, AAW: nullptr, ShouldPreserveUseListOrder);
39 } else {
40 bool BannerPrinted = false;
41 for (const auto &F : M.functions()) {
42 if (llvm::isFunctionInPrintList(FunctionName: F.getName())) {
43 if (!BannerPrinted && !Banner.empty()) {
44 OS << Banner << "\n";
45 BannerPrinted = true;
46 }
47 F.print(OS);
48 }
49 }
50 }
51
52 ModuleSummaryIndex *Index =
53 EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(IR&: M))
54 : nullptr;
55 if (Index) {
56 if (Index->modulePaths().empty())
57 Index->addModule(ModPath: "");
58 Index->print(OS);
59 }
60
61 return PreservedAnalyses::all();
62}
63
64PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
65PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
66 : OS(OS), Banner(Banner) {}
67
68PreservedAnalyses PrintFunctionPass::run(Function &F,
69 FunctionAnalysisManager &) {
70 if (isFunctionInPrintList(FunctionName: F.getName())) {
71 if (forcePrintModuleIR())
72 OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();
73 else
74 OS << Banner << '\n' << static_cast<Value &>(F);
75 }
76
77 return PreservedAnalyses::all();
78}
79