Path: blob/main/contrib/llvm-project/llvm/lib/IRPrinter/IRPrintingPasses.cpp
35233 views
//===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// PrintModulePass and PrintFunctionPass implementations.9//10//===----------------------------------------------------------------------===//1112#include "llvm/IRPrinter/IRPrintingPasses.h"13#include "llvm/ADT/StringRef.h"14#include "llvm/Analysis/ModuleSummaryAnalysis.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"2122using namespace llvm;2324extern cl::opt<bool> WriteNewDbgInfoFormat;2526PrintModulePass::PrintModulePass() : OS(dbgs()) {}27PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,28bool ShouldPreserveUseListOrder,29bool EmitSummaryIndex)30: OS(OS), Banner(Banner),31ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),32EmitSummaryIndex(EmitSummaryIndex) {}3334PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &AM) {35// RemoveDIs: Regardless of the format we've processed this module in, use36// `WriteNewDbgInfoFormat` to determine which format we use to write it.37ScopedDbgInfoFormatSetter FormatSetter(M, WriteNewDbgInfoFormat);38// Remove intrinsic declarations when printing in the new format.39// TODO: Move this into Module::setIsNewDbgInfoFormat when we're ready to40// update test output.41if (WriteNewDbgInfoFormat)42M.removeDebugIntrinsicDeclarations();4344if (llvm::isFunctionInPrintList("*")) {45if (!Banner.empty())46OS << Banner << "\n";47M.print(OS, nullptr, ShouldPreserveUseListOrder);48} else {49bool BannerPrinted = false;50for (const auto &F : M.functions()) {51if (llvm::isFunctionInPrintList(F.getName())) {52if (!BannerPrinted && !Banner.empty()) {53OS << Banner << "\n";54BannerPrinted = true;55}56F.print(OS);57}58}59}6061ModuleSummaryIndex *Index =62EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))63: nullptr;64if (Index) {65if (Index->modulePaths().empty())66Index->addModule("");67Index->print(OS);68}6970return PreservedAnalyses::all();71}7273PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}74PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)75: OS(OS), Banner(Banner) {}7677PreservedAnalyses PrintFunctionPass::run(Function &F,78FunctionAnalysisManager &) {79// RemoveDIs: Regardless of the format we've processed this function in, use80// `WriteNewDbgInfoFormat` to determine which format we use to write it.81ScopedDbgInfoFormatSetter FormatSetter(F, WriteNewDbgInfoFormat);8283if (isFunctionInPrintList(F.getName())) {84if (forcePrintModuleIR())85OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();86else87OS << Banner << '\n' << static_cast<Value &>(F);88}8990return PreservedAnalyses::all();91}929394