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/IR/Function.h" 16 #include "llvm/IR/Module.h" 17 #include "llvm/IR/PrintPasses.h" 18 #include "llvm/Pass.h" 19 #include "llvm/Support/Debug.h" 20 #include "llvm/Support/raw_ostream.h" 21 22 using namespace llvm; 23 24 PrintModulePass::PrintModulePass() : OS(dbgs()) {} 25 PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner, 26 bool ShouldPreserveUseListOrder) 27 : OS(OS), Banner(Banner), 28 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {} 29 30 PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &) { 31 if (llvm::isFunctionInPrintList("*")) { 32 if (!Banner.empty()) 33 OS << Banner << "\n"; 34 M.print(OS, nullptr, ShouldPreserveUseListOrder); 35 } else { 36 bool BannerPrinted = false; 37 for (const auto &F : M.functions()) { 38 if (llvm::isFunctionInPrintList(F.getName())) { 39 if (!BannerPrinted && !Banner.empty()) { 40 OS << Banner << "\n"; 41 BannerPrinted = true; 42 } 43 F.print(OS); 44 } 45 } 46 } 47 return PreservedAnalyses::all(); 48 } 49 50 PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {} 51 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner) 52 : OS(OS), Banner(Banner) {} 53 54 PreservedAnalyses PrintFunctionPass::run(Function &F, 55 FunctionAnalysisManager &) { 56 if (isFunctionInPrintList(F.getName())) { 57 if (forcePrintModuleIR()) 58 OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent(); 59 else 60 OS << Banner << '\n' << static_cast<Value &>(F); 61 } 62 return PreservedAnalyses::all(); 63 } 64