[llvm-objcopy] [COFF] Test absolute symbols wrt --strip-unneeded and --discard-all...
[llvm-complete.git] / lib / Analysis / InstCount.cpp
blob95ab6ee3db5bd4714bc48afba87ebca9b01ca88a
1 //===-- InstCount.cpp - Collects the count of all instructions ------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass collects the count of all instructions and reports them
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/Statistic.h"
15 #include "llvm/Analysis/Passes.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/InstVisitor.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
22 using namespace llvm;
24 #define DEBUG_TYPE "instcount"
26 STATISTIC(TotalInsts , "Number of instructions (of all types)");
27 STATISTIC(TotalBlocks, "Number of basic blocks");
28 STATISTIC(TotalFuncs , "Number of non-external functions");
30 #define HANDLE_INST(N, OPCODE, CLASS) \
31 STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts");
33 #include "llvm/IR/Instruction.def"
35 namespace {
36 class InstCount : public FunctionPass, public InstVisitor<InstCount> {
37 friend class InstVisitor<InstCount>;
39 void visitFunction (Function &F) { ++TotalFuncs; }
40 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
42 #define HANDLE_INST(N, OPCODE, CLASS) \
43 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
45 #include "llvm/IR/Instruction.def"
47 void visitInstruction(Instruction &I) {
48 errs() << "Instruction Count does not know about " << I;
49 llvm_unreachable(nullptr);
51 public:
52 static char ID; // Pass identification, replacement for typeid
53 InstCount() : FunctionPass(ID) {
54 initializeInstCountPass(*PassRegistry::getPassRegistry());
57 bool runOnFunction(Function &F) override;
59 void getAnalysisUsage(AnalysisUsage &AU) const override {
60 AU.setPreservesAll();
62 void print(raw_ostream &O, const Module *M) const override {}
67 char InstCount::ID = 0;
68 INITIALIZE_PASS(InstCount, "instcount",
69 "Counts the various types of Instructions", false, true)
71 FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
73 // InstCount::run - This is the main Analysis entry point for a
74 // function.
76 bool InstCount::runOnFunction(Function &F) {
77 visit(F);
78 return false;