2014-01-09 02:39:45 +00:00
|
|
|
//===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
|
2008-10-21 23:33:38 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2008-10-21 23:33:38 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2022-11-18 01:47:56 +00:00
|
|
|
// PrintModulePass and PrintFunctionPass implementations for the legacy pass
|
|
|
|
// manager.
|
2008-10-21 23:33:38 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-01-12 11:10:32 +00:00
|
|
|
#include "llvm/IR/IRPrintingPasses.h"
|
2020-07-25 15:52:05 +01:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2020-12-03 10:59:10 -08:00
|
|
|
#include "llvm/IR/PrintPasses.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-13 13:15:01 -08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2008-10-21 23:33:38 +00:00
|
|
|
#include "llvm/Pass.h"
|
2010-01-05 01:30:18 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2008-10-22 03:25:22 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2020-07-25 15:52:05 +01:00
|
|
|
|
2008-10-21 23:33:38 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-01-12 12:15:39 +00:00
|
|
|
class PrintModulePassWrapper : public ModulePass {
|
2022-11-18 01:47:56 +00:00
|
|
|
raw_ostream &OS;
|
|
|
|
std::string Banner;
|
|
|
|
bool ShouldPreserveUseListOrder;
|
2014-01-12 11:16:01 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
2022-11-18 01:47:56 +00:00
|
|
|
PrintModulePassWrapper() : ModulePass(ID), OS(dbgs()) {}
|
2015-04-15 02:38:06 +00:00
|
|
|
PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
|
|
|
|
bool ShouldPreserveUseListOrder)
|
2022-11-18 01:47:56 +00:00
|
|
|
: ModulePass(ID), OS(OS), Banner(Banner),
|
|
|
|
ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
|
2014-01-12 11:16:01 +00:00
|
|
|
|
2014-03-05 06:35:38 +00:00
|
|
|
bool runOnModule(Module &M) override {
|
2022-11-18 01:47:56 +00:00
|
|
|
if (llvm::isFunctionInPrintList("*")) {
|
|
|
|
if (!Banner.empty())
|
|
|
|
OS << Banner << "\n";
|
|
|
|
M.print(OS, nullptr, ShouldPreserveUseListOrder);
|
|
|
|
} else {
|
|
|
|
bool BannerPrinted = false;
|
|
|
|
for (const auto &F : M.functions()) {
|
|
|
|
if (llvm::isFunctionInPrintList(F.getName())) {
|
|
|
|
if (!BannerPrinted && !Banner.empty()) {
|
|
|
|
OS << Banner << "\n";
|
|
|
|
BannerPrinted = true;
|
|
|
|
}
|
|
|
|
F.print(OS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-12 11:16:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-05 06:35:38 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2014-01-12 11:16:01 +00:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2017-03-10 07:09:20 +00:00
|
|
|
|
2017-08-25 12:38:53 +00:00
|
|
|
StringRef getPassName() const override { return "Print Module IR"; }
|
2014-01-12 11:16:01 +00:00
|
|
|
};
|
|
|
|
|
2014-01-12 12:15:39 +00:00
|
|
|
class PrintFunctionPassWrapper : public FunctionPass {
|
2022-11-18 01:47:56 +00:00
|
|
|
raw_ostream &OS;
|
|
|
|
std::string Banner;
|
2014-01-12 11:16:01 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
2022-11-18 01:47:56 +00:00
|
|
|
PrintFunctionPassWrapper() : FunctionPass(ID), OS(dbgs()) {}
|
2014-01-12 12:15:39 +00:00
|
|
|
PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
|
2022-11-18 01:47:56 +00:00
|
|
|
: FunctionPass(ID), OS(OS), Banner(Banner) {}
|
2014-01-12 11:16:01 +00:00
|
|
|
|
|
|
|
// This pass just prints a banner followed by the function as it's processed.
|
2014-03-05 06:35:38 +00:00
|
|
|
bool runOnFunction(Function &F) override {
|
2022-11-18 01:47:56 +00:00
|
|
|
if (isFunctionInPrintList(F.getName())) {
|
|
|
|
if (forcePrintModuleIR())
|
|
|
|
OS << Banner << " (function: " << F.getName() << ")\n"
|
|
|
|
<< *F.getParent();
|
|
|
|
else
|
|
|
|
OS << Banner << '\n' << static_cast<Value &>(F);
|
|
|
|
}
|
2014-01-12 11:16:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-05 06:35:38 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2014-01-12 11:16:01 +00:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2017-03-10 07:09:20 +00:00
|
|
|
|
2017-08-25 12:38:53 +00:00
|
|
|
StringRef getPassName() const override { return "Print Function IR"; }
|
2014-01-12 11:16:01 +00:00
|
|
|
};
|
|
|
|
|
2022-11-18 01:47:56 +00:00
|
|
|
} // namespace
|
2008-10-21 23:33:38 +00:00
|
|
|
|
2014-01-12 12:15:39 +00:00
|
|
|
char PrintModulePassWrapper::ID = 0;
|
|
|
|
INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
|
2018-07-11 23:30:25 +00:00
|
|
|
"Print module to stderr", false, true)
|
2014-01-12 12:15:39 +00:00
|
|
|
char PrintFunctionPassWrapper::ID = 0;
|
|
|
|
INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
|
2018-07-11 23:30:25 +00:00
|
|
|
"Print function to stderr", false, true)
|
2008-10-21 23:33:38 +00:00
|
|
|
|
2014-01-12 11:30:46 +00:00
|
|
|
ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS,
|
2015-04-15 02:38:06 +00:00
|
|
|
const std::string &Banner,
|
|
|
|
bool ShouldPreserveUseListOrder) {
|
|
|
|
return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
|
2008-10-21 23:33:38 +00:00
|
|
|
}
|
|
|
|
|
2014-01-12 11:30:46 +00:00
|
|
|
FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS,
|
|
|
|
const std::string &Banner) {
|
2014-01-12 12:15:39 +00:00
|
|
|
return new PrintFunctionPassWrapper(OS, Banner);
|
2008-10-21 23:33:38 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 00:29:27 +00:00
|
|
|
bool llvm::isIRPrintingPass(Pass *P) {
|
2022-11-18 01:47:56 +00:00
|
|
|
const char *PID = (const char *)P->getPassID();
|
2018-05-15 00:29:27 +00:00
|
|
|
|
2019-10-30 11:19:06 -07:00
|
|
|
return (PID == &PrintModulePassWrapper::ID) ||
|
|
|
|
(PID == &PrintFunctionPassWrapper::ID);
|
2018-05-15 00:29:27 +00:00
|
|
|
}
|