1 //===-- InstCount.cpp - Collects the count of all instructions ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass collects the count of all instructions and reports them
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/Passes.h"
15 #include "llvm/Pass.h"
16 #include "llvm/Function.h"
17 #include "llvm/Support/InstVisitor.h"
18 #include "llvm/ADT/Statistic.h"
23 Statistic
<> TotalInsts ("instcount", "Number of instructions (of all types)");
24 Statistic
<> TotalBlocks("instcount", "Number of basic blocks");
25 Statistic
<> TotalFuncs ("instcount", "Number of non-external functions");
26 Statistic
<> TotalMemInst("instcount", "Number of memory instructions");
28 #define HANDLE_INST(N, OPCODE, CLASS) \
29 Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
31 #include "llvm/Instruction.def"
33 class InstCount
: public FunctionPass
, public InstVisitor
<InstCount
> {
34 friend class InstVisitor
<InstCount
>;
36 void visitFunction (Function
&F
) { ++TotalFuncs
; }
37 void visitBasicBlock(BasicBlock
&BB
) { ++TotalBlocks
; }
39 #define HANDLE_INST(N, OPCODE, CLASS) \
40 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
42 #include "llvm/Instruction.def"
44 void visitInstruction(Instruction
&I
) {
45 std::cerr
<< "Instruction Count does not know about " << I
;
49 virtual bool runOnFunction(Function
&F
);
51 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
54 virtual void print(std::ostream
&O
, const Module
*M
) const {}
58 RegisterAnalysis
<InstCount
> X("instcount",
59 "Counts the various types of Instructions");
62 FunctionPass
*llvm::createInstCountPass() { return new InstCount(); }
64 // InstCount::run - This is the main Analysis entry point for a
67 bool InstCount::runOnFunction(Function
&F
) {
68 unsigned StartMemInsts
=
69 NumGetElementPtrInst
+ NumLoadInst
+ NumStoreInst
+ NumCallInst
+
70 NumInvokeInst
+ NumAllocaInst
+ NumMallocInst
+ NumFreeInst
;
72 unsigned EndMemInsts
=
73 NumGetElementPtrInst
+ NumLoadInst
+ NumStoreInst
+ NumCallInst
+
74 NumInvokeInst
+ NumAllocaInst
+ NumMallocInst
+ NumFreeInst
;
75 TotalMemInst
+= EndMemInsts
-StartMemInsts
;