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