Path: blob/main/contrib/llvm-project/llvm/lib/IR/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 for the legacy pass9// manager.10//11//===----------------------------------------------------------------------===//1213#include "llvm/IR/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/InitializePasses.h"19#include "llvm/Pass.h"20#include "llvm/Support/Debug.h"21#include "llvm/Support/raw_ostream.h"2223using namespace llvm;2425cl::opt<bool> WriteNewDbgInfoFormat(26"write-experimental-debuginfo",27cl::desc("Write debug info in the new non-intrinsic format. Has no effect "28"if --preserve-input-debuginfo-format=true."),29cl::init(true));3031namespace {3233class PrintModulePassWrapper : public ModulePass {34raw_ostream &OS;35std::string Banner;36bool ShouldPreserveUseListOrder;3738public:39static char ID;40PrintModulePassWrapper() : ModulePass(ID), OS(dbgs()) {}41PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,42bool ShouldPreserveUseListOrder)43: ModulePass(ID), OS(OS), Banner(Banner),44ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}4546bool runOnModule(Module &M) override {47// RemoveDIs: Regardless of the format we've processed this module in, use48// `WriteNewDbgInfoFormat` to determine which format we use to write it.49ScopedDbgInfoFormatSetter FormatSetter(M, WriteNewDbgInfoFormat);50// Remove intrinsic declarations when printing in the new format.51// TODO: Move this into Module::setIsNewDbgInfoFormat when we're ready to52// update test output.53if (WriteNewDbgInfoFormat)54M.removeDebugIntrinsicDeclarations();5556if (llvm::isFunctionInPrintList("*")) {57if (!Banner.empty())58OS << Banner << "\n";59M.print(OS, nullptr, ShouldPreserveUseListOrder);60} else {61bool BannerPrinted = false;62for (const auto &F : M.functions()) {63if (llvm::isFunctionInPrintList(F.getName())) {64if (!BannerPrinted && !Banner.empty()) {65OS << Banner << "\n";66BannerPrinted = true;67}68F.print(OS);69}70}71}7273return false;74}7576void getAnalysisUsage(AnalysisUsage &AU) const override {77AU.setPreservesAll();78}7980StringRef getPassName() const override { return "Print Module IR"; }81};8283class PrintFunctionPassWrapper : public FunctionPass {84raw_ostream &OS;85std::string Banner;8687public:88static char ID;89PrintFunctionPassWrapper() : FunctionPass(ID), OS(dbgs()) {}90PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)91: FunctionPass(ID), OS(OS), Banner(Banner) {}9293// This pass just prints a banner followed by the function as it's processed.94bool runOnFunction(Function &F) override {95// RemoveDIs: Regardless of the format we've processed this function in, use96// `WriteNewDbgInfoFormat` to determine which format we use to write it.97ScopedDbgInfoFormatSetter FormatSetter(F, WriteNewDbgInfoFormat);9899if (isFunctionInPrintList(F.getName())) {100if (forcePrintModuleIR())101OS << Banner << " (function: " << F.getName() << ")\n"102<< *F.getParent();103else104OS << Banner << '\n' << static_cast<Value &>(F);105}106107return false;108}109110void getAnalysisUsage(AnalysisUsage &AU) const override {111AU.setPreservesAll();112}113114StringRef getPassName() const override { return "Print Function IR"; }115};116117} // namespace118119char PrintModulePassWrapper::ID = 0;120INITIALIZE_PASS(PrintModulePassWrapper, "print-module",121"Print module to stderr", false, true)122char PrintFunctionPassWrapper::ID = 0;123INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",124"Print function to stderr", false, true)125126ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS,127const std::string &Banner,128bool ShouldPreserveUseListOrder) {129return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);130}131132FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS,133const std::string &Banner) {134return new PrintFunctionPassWrapper(OS, Banner);135}136137bool llvm::isIRPrintingPass(Pass *P) {138const char *PID = (const char *)P->getPassID();139140return (PID == &PrintModulePassWrapper::ID) ||141(PID == &PrintFunctionPassWrapper::ID);142}143144145