2014-01-09 10:39:45 +08:00
|
|
|
//===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
|
2008-10-22 07:33:38 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08: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-22 07:33:38 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// PrintModulePass and PrintFunctionPass implementations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-01-12 19:10:32 +08:00
|
|
|
#include "llvm/IR/IRPrintingPasses.h"
|
2020-07-25 22:52:05 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2020-12-04 02: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-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2008-10-22 07:33:38 +08:00
|
|
|
#include "llvm/Pass.h"
|
2010-01-05 09:30:18 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2008-10-22 11:25:22 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2020-07-25 22:52:05 +08:00
|
|
|
|
2008-10-22 07:33:38 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-01-12 20:15:39 +08:00
|
|
|
PrintModulePass::PrintModulePass() : OS(dbgs()) {}
|
2015-04-15 10:38:06 +08:00
|
|
|
PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,
|
|
|
|
bool ShouldPreserveUseListOrder)
|
|
|
|
: OS(OS), Banner(Banner),
|
|
|
|
ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
|
2014-01-12 20:15:39 +08:00
|
|
|
|
2016-08-09 08:28:38 +08:00
|
|
|
PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &) {
|
2019-09-04 16:08:58 +08:00
|
|
|
if (llvm::isFunctionInPrintList("*")) {
|
|
|
|
if (!Banner.empty())
|
|
|
|
OS << Banner << "\n";
|
2016-01-07 06:55:03 +08:00
|
|
|
M.print(OS, nullptr, ShouldPreserveUseListOrder);
|
2019-09-04 16:08:58 +08:00
|
|
|
}
|
2016-01-07 06:55:03 +08:00
|
|
|
else {
|
2019-09-04 16:08:58 +08:00
|
|
|
bool BannerPrinted = false;
|
|
|
|
for(const auto &F : M.functions()) {
|
|
|
|
if (llvm::isFunctionInPrintList(F.getName())) {
|
|
|
|
if (!BannerPrinted && !Banner.empty()) {
|
|
|
|
OS << Banner << "\n";
|
|
|
|
BannerPrinted = true;
|
|
|
|
}
|
2016-01-07 06:55:03 +08:00
|
|
|
F.print(OS);
|
2019-09-04 16:08:58 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-07 06:55:03 +08:00
|
|
|
}
|
2014-01-12 20:15:39 +08:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
|
|
|
|
PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
|
|
|
|
: OS(OS), Banner(Banner) {}
|
|
|
|
|
2016-06-17 08:11:01 +08:00
|
|
|
PreservedAnalyses PrintFunctionPass::run(Function &F,
|
2016-08-09 08:28:15 +08:00
|
|
|
FunctionAnalysisManager &) {
|
2017-12-02 02:39:58 +08:00
|
|
|
if (isFunctionInPrintList(F.getName())) {
|
2017-12-02 01:42:46 +08:00
|
|
|
if (forcePrintModuleIR())
|
|
|
|
OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();
|
|
|
|
else
|
2019-11-29 08:42:27 +08:00
|
|
|
OS << Banner << '\n' << static_cast<Value &>(F);
|
2017-12-02 02:39:58 +08:00
|
|
|
}
|
2014-01-12 20:15:39 +08:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
2008-10-22 07:33:38 +08:00
|
|
|
namespace {
|
|
|
|
|
2014-01-12 20:15:39 +08:00
|
|
|
class PrintModulePassWrapper : public ModulePass {
|
|
|
|
PrintModulePass P;
|
2014-01-12 19:16:01 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
2014-01-12 20:15:39 +08:00
|
|
|
PrintModulePassWrapper() : ModulePass(ID) {}
|
2015-04-15 10:38:06 +08:00
|
|
|
PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
|
|
|
|
bool ShouldPreserveUseListOrder)
|
|
|
|
: ModulePass(ID), P(OS, Banner, ShouldPreserveUseListOrder) {}
|
2014-01-12 19:16:01 +08:00
|
|
|
|
2014-03-05 14:35:38 +08:00
|
|
|
bool runOnModule(Module &M) override {
|
2016-06-17 08:11:01 +08:00
|
|
|
ModuleAnalysisManager DummyMAM;
|
|
|
|
P.run(M, DummyMAM);
|
2014-01-12 19:16:01 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-05 14:35:38 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2014-01-12 19:16:01 +08:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2017-03-10 15:09:20 +08:00
|
|
|
|
2017-08-25 20:38:53 +08:00
|
|
|
StringRef getPassName() const override { return "Print Module IR"; }
|
2014-01-12 19:16:01 +08:00
|
|
|
};
|
|
|
|
|
2014-01-12 20:15:39 +08:00
|
|
|
class PrintFunctionPassWrapper : public FunctionPass {
|
|
|
|
PrintFunctionPass P;
|
2014-01-12 19:16:01 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
2014-01-12 20:15:39 +08:00
|
|
|
PrintFunctionPassWrapper() : FunctionPass(ID) {}
|
|
|
|
PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
|
|
|
|
: FunctionPass(ID), P(OS, Banner) {}
|
2014-01-12 19:16:01 +08:00
|
|
|
|
|
|
|
// This pass just prints a banner followed by the function as it's processed.
|
2014-03-05 14:35:38 +08:00
|
|
|
bool runOnFunction(Function &F) override {
|
2016-06-17 08:11:01 +08:00
|
|
|
FunctionAnalysisManager DummyFAM;
|
|
|
|
P.run(F, DummyFAM);
|
2014-01-12 19:16:01 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-05 14:35:38 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2014-01-12 19:16:01 +08:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2017-03-10 15:09:20 +08:00
|
|
|
|
2017-08-25 20:38:53 +08:00
|
|
|
StringRef getPassName() const override { return "Print Function IR"; }
|
2014-01-12 19:16:01 +08:00
|
|
|
};
|
|
|
|
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2008-10-22 07:33:38 +08:00
|
|
|
|
2014-01-12 20:15:39 +08:00
|
|
|
char PrintModulePassWrapper::ID = 0;
|
|
|
|
INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
|
2018-07-12 07:30:25 +08:00
|
|
|
"Print module to stderr", false, true)
|
2014-01-12 20:15:39 +08:00
|
|
|
char PrintFunctionPassWrapper::ID = 0;
|
|
|
|
INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
|
2018-07-12 07:30:25 +08:00
|
|
|
"Print function to stderr", false, true)
|
2008-10-22 07:33:38 +08:00
|
|
|
|
2014-01-12 19:30:46 +08:00
|
|
|
ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS,
|
2015-04-15 10:38:06 +08:00
|
|
|
const std::string &Banner,
|
|
|
|
bool ShouldPreserveUseListOrder) {
|
|
|
|
return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
|
2008-10-22 07:33:38 +08:00
|
|
|
}
|
|
|
|
|
2014-01-12 19:30:46 +08:00
|
|
|
FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS,
|
|
|
|
const std::string &Banner) {
|
2014-01-12 20:15:39 +08:00
|
|
|
return new PrintFunctionPassWrapper(OS, Banner);
|
2008-10-22 07:33:38 +08:00
|
|
|
}
|
|
|
|
|
2018-05-15 08:29:27 +08:00
|
|
|
bool llvm::isIRPrintingPass(Pass *P) {
|
|
|
|
const char *PID = (const char*)P->getPassID();
|
|
|
|
|
2019-10-31 02:19:06 +08:00
|
|
|
return (PID == &PrintModulePassWrapper::ID) ||
|
|
|
|
(PID == &PrintFunctionPassWrapper::ID);
|
2018-05-15 08:29:27 +08:00
|
|
|
}
|