83 lines · cpp
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-exception6//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 // Remove intrinsic declarations when printing in the new format.36 // TODO: consider removing this now that debug intrinsics are gone.37 M.removeDebugIntrinsicDeclarations();38 39 if (llvm::isFunctionInPrintList("*")) {40 if (!Banner.empty())41 OS << Banner << "\n";42 M.print(OS, nullptr, ShouldPreserveUseListOrder);43 } else {44 bool BannerPrinted = false;45 for (const auto &F : M.functions()) {46 if (llvm::isFunctionInPrintList(F.getName())) {47 if (!BannerPrinted && !Banner.empty()) {48 OS << Banner << "\n";49 BannerPrinted = true;50 }51 F.print(OS);52 }53 }54 }55 56 ModuleSummaryIndex *Index =57 EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))58 : nullptr;59 if (Index) {60 if (Index->modulePaths().empty())61 Index->addModule("");62 Index->print(OS);63 }64 65 return PreservedAnalyses::all();66}67 68PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}69PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)70 : OS(OS), Banner(Banner) {}71 72PreservedAnalyses PrintFunctionPass::run(Function &F,73 FunctionAnalysisManager &) {74 if (isFunctionInPrintList(F.getName())) {75 if (forcePrintModuleIR())76 OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();77 else78 OS << Banner << '\n' << static_cast<Value &>(F);79 }80 81 return PreservedAnalyses::all();82}83