2002-11-14 02:22:13 +08:00
|
|
|
//===-- InstCount.cpp - Collects the count of all instructions ------------===//
|
2005-04-22 05:13:18 +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
|
2005-04-22 05:13:18 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-14 02:22:13 +08:00
|
|
|
//
|
2005-04-22 05:13:18 +08:00
|
|
|
// This pass collects the count of all instructions and reports them
|
2002-11-14 02:22:13 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-07-17 19:30:51 +08:00
|
|
|
#include "llvm/Analysis/InstCount.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
2014-03-06 11:23:41 +08:00
|
|
|
#include "llvm/IR/InstVisitor.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"
|
2002-11-14 02:22:13 +08:00
|
|
|
#include "llvm/Pass.h"
|
2009-12-24 04:34:27 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 21:10:19 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-08-23 12:37:46 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2005-03-22 11:55:10 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2014-04-22 10:48:03 +08:00
|
|
|
#define DEBUG_TYPE "instcount"
|
|
|
|
|
2020-07-17 19:30:51 +08:00
|
|
|
STATISTIC(TotalInsts, "Number of instructions (of all types)");
|
2006-12-20 06:30:33 +08:00
|
|
|
STATISTIC(TotalBlocks, "Number of basic blocks");
|
2020-07-17 19:30:51 +08:00
|
|
|
STATISTIC(TotalFuncs, "Number of non-external functions");
|
2002-12-08 07:24:24 +08:00
|
|
|
|
2020-07-17 19:30:51 +08:00
|
|
|
#define HANDLE_INST(N, OPCODE, CLASS) \
|
|
|
|
STATISTIC(Num##OPCODE##Inst, "Number of " #OPCODE " insts");
|
2002-12-04 03:40:16 +08:00
|
|
|
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instruction.def"
|
2002-11-14 02:22:13 +08:00
|
|
|
|
2006-12-20 06:30:33 +08:00
|
|
|
namespace {
|
2020-07-17 19:30:51 +08:00
|
|
|
class InstCount : public InstVisitor<InstCount> {
|
|
|
|
friend class InstVisitor<InstCount>;
|
2002-11-14 02:22:13 +08:00
|
|
|
|
2020-07-17 19:30:51 +08:00
|
|
|
void visitFunction(Function &F) { ++TotalFuncs; }
|
|
|
|
void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
|
2002-12-08 07:24:24 +08:00
|
|
|
|
2020-07-17 19:30:51 +08:00
|
|
|
#define HANDLE_INST(N, OPCODE, CLASS) \
|
|
|
|
void visit##OPCODE(CLASS &) { \
|
|
|
|
++Num##OPCODE##Inst; \
|
|
|
|
++TotalInsts; \
|
|
|
|
}
|
2002-11-14 02:22:13 +08:00
|
|
|
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instruction.def"
|
2002-11-14 02:22:13 +08:00
|
|
|
|
2020-07-17 19:30:51 +08:00
|
|
|
void visitInstruction(Instruction &I) {
|
|
|
|
errs() << "Instruction Count does not know about " << I;
|
|
|
|
llvm_unreachable(nullptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
2007-05-02 05:15:47 +08:00
|
|
|
|
2020-07-17 19:30:51 +08:00
|
|
|
PreservedAnalyses InstCountPass::run(Function &F,
|
|
|
|
FunctionAnalysisManager &FAM) {
|
|
|
|
LLVM_DEBUG(dbgs() << "INSTCOUNT: running on function " << F.getName()
|
|
|
|
<< "\n");
|
|
|
|
InstCount().visit(F);
|
2002-11-14 02:22:13 +08:00
|
|
|
|
2020-07-17 19:30:51 +08:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
2002-12-04 03:40:16 +08:00
|
|
|
|
2020-07-17 19:30:51 +08:00
|
|
|
namespace {
|
|
|
|
class InstCountLegacyPass : public FunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
InstCountLegacyPass() : FunctionPass(ID) {
|
|
|
|
initializeInstCountLegacyPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnFunction(Function &F) override {
|
|
|
|
LLVM_DEBUG(dbgs() << "INSTCOUNT: running on function " << F.getName()
|
|
|
|
<< "\n");
|
|
|
|
InstCount().visit(F);
|
|
|
|
return false;
|
2002-11-14 02:22:13 +08:00
|
|
|
};
|
|
|
|
|
2020-07-17 19:30:51 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2008-05-13 08:00:25 +08:00
|
|
|
|
2020-07-17 19:30:51 +08:00
|
|
|
void print(raw_ostream &O, const Module *M) const override {}
|
|
|
|
};
|
|
|
|
} // namespace
|
2005-10-24 09:00:45 +08:00
|
|
|
|
2020-07-17 19:30:51 +08:00
|
|
|
char InstCountLegacyPass::ID = 0;
|
|
|
|
INITIALIZE_PASS(InstCountLegacyPass, "instcount",
|
|
|
|
"Counts the various types of Instructions", false, true)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createInstCountPass() { return new InstCountLegacyPass(); }
|