1 //===-- InstCount.cpp - Collects the count of all instructions ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass collects the count of all instructions and reports them
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "instcount"
15 #include "llvm/Analysis/Passes.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Function.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/InstVisitor.h"
20 #include "llvm/Support/Streams.h"
21 #include "llvm/ADT/Statistic.h"
25 STATISTIC(TotalInsts
, "Number of instructions (of all types)");
26 STATISTIC(TotalBlocks
, "Number of basic blocks");
27 STATISTIC(TotalFuncs
, "Number of non-external functions");
28 STATISTIC(TotalMemInst
, "Number of memory instructions");
30 #define HANDLE_INST(N, OPCODE, CLASS) \
31 STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts");
33 #include "llvm/Instruction.def"
37 class VISIBILITY_HIDDEN InstCount
38 : public FunctionPass
, public InstVisitor
<InstCount
> {
39 friend class InstVisitor
<InstCount
>;
41 void visitFunction (Function
&F
) { ++TotalFuncs
; }
42 void visitBasicBlock(BasicBlock
&BB
) { ++TotalBlocks
; }
44 #define HANDLE_INST(N, OPCODE, CLASS) \
45 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
47 #include "llvm/Instruction.def"
49 void visitInstruction(Instruction
&I
) {
50 cerr
<< "Instruction Count does not know about " << I
;
54 static char ID
; // Pass identification, replacement for typeid
55 InstCount() : FunctionPass(&ID
) {}
57 virtual bool runOnFunction(Function
&F
);
59 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
62 virtual void print(std::ostream
&O
, const Module
*M
) const {}
67 char InstCount::ID
= 0;
68 static RegisterPass
<InstCount
>
69 X("instcount", "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
76 bool InstCount::runOnFunction(Function
&F
) {
77 unsigned StartMemInsts
=
78 NumGetElementPtrInst
+ NumLoadInst
+ NumStoreInst
+ NumCallInst
+
79 NumInvokeInst
+ NumAllocaInst
+ NumMallocInst
+ NumFreeInst
;
81 unsigned EndMemInsts
=
82 NumGetElementPtrInst
+ NumLoadInst
+ NumStoreInst
+ NumCallInst
+
83 NumInvokeInst
+ NumAllocaInst
+ NumMallocInst
+ NumFreeInst
;
84 TotalMemInst
+= EndMemInsts
-StartMemInsts
;