1 //===-- InstCount.cpp - Collects the count of all instructions ------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 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. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This pass collects the count of all instructions and reports them 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Pass.h" 15 #include "llvm/Function.h" 16 #include "llvm/Support/InstVisitor.h" 17 #include "llvm/ADT/Statistic.h" 18 using namespace llvm; 19 20 namespace { 21 Statistic<> TotalInsts ("instcount", "Number of instructions (of all types)"); 22 Statistic<> TotalBlocks("instcount", "Number of basic blocks"); 23 Statistic<> TotalFuncs ("instcount", "Number of non-external functions"); 24 Statistic<> TotalMemInst("instcount", "Number of memory instructions"); 25 26 #define HANDLE_INST(N, OPCODE, CLASS) \ 27 Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts"); 28 29 #include "llvm/Instruction.def" 30 31 class InstCount : public FunctionPass, public InstVisitor<InstCount> { 32 friend class InstVisitor<InstCount>; 33 34 void visitFunction (Function &F) { ++TotalFuncs; } 35 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; } 36 37 #define HANDLE_INST(N, OPCODE, CLASS) \ 38 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; } 39 40 #include "llvm/Instruction.def" 41 42 void visitInstruction(Instruction &I) { 43 std::cerr << "Instruction Count does not know about " << I; 44 abort(); 45 } 46 public: 47 virtual bool runOnFunction(Function &F); 48 49 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 50 AU.setPreservesAll(); 51 } 52 virtual void print(std::ostream &O, const Module *M) const {} 53 54 }; 55 56 RegisterAnalysis<InstCount> X("instcount", 57 "Counts the various types of Instructions"); 58 } 59 60 // InstCount::run - This is the main Analysis entry point for a 61 // function. 62 // 63 bool InstCount::runOnFunction(Function &F) { 64 unsigned StartMemInsts = 65 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst + 66 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst; 67 visit(F); 68 unsigned EndMemInsts = 69 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst + 70 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst; 71 TotalMemInst += EndMemInsts-StartMemInsts; 72 return false; 73 } 74