1*f4a2713aSLionel Sambuc //===-- InstCount.cpp - Collects the count of all instructions ------------===// 2*f4a2713aSLionel Sambuc // 3*f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4*f4a2713aSLionel Sambuc // 5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7*f4a2713aSLionel Sambuc // 8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9*f4a2713aSLionel Sambuc // 10*f4a2713aSLionel Sambuc // This pass collects the count of all instructions and reports them 11*f4a2713aSLionel Sambuc // 12*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 13*f4a2713aSLionel Sambuc 14*f4a2713aSLionel Sambuc #define DEBUG_TYPE "instcount" 15*f4a2713aSLionel Sambuc #include "llvm/Analysis/Passes.h" 16*f4a2713aSLionel Sambuc #include "llvm/ADT/Statistic.h" 17*f4a2713aSLionel Sambuc #include "llvm/IR/Function.h" 18*f4a2713aSLionel Sambuc #include "llvm/InstVisitor.h" 19*f4a2713aSLionel Sambuc #include "llvm/Pass.h" 20*f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h" 21*f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h" 22*f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h" 23*f4a2713aSLionel Sambuc using namespace llvm; 24*f4a2713aSLionel Sambuc 25*f4a2713aSLionel Sambuc STATISTIC(TotalInsts , "Number of instructions (of all types)"); 26*f4a2713aSLionel Sambuc STATISTIC(TotalBlocks, "Number of basic blocks"); 27*f4a2713aSLionel Sambuc STATISTIC(TotalFuncs , "Number of non-external functions"); 28*f4a2713aSLionel Sambuc STATISTIC(TotalMemInst, "Number of memory instructions"); 29*f4a2713aSLionel Sambuc 30*f4a2713aSLionel Sambuc #define HANDLE_INST(N, OPCODE, CLASS) \ 31*f4a2713aSLionel Sambuc STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts"); 32*f4a2713aSLionel Sambuc 33*f4a2713aSLionel Sambuc #include "llvm/IR/Instruction.def" 34*f4a2713aSLionel Sambuc 35*f4a2713aSLionel Sambuc 36*f4a2713aSLionel Sambuc namespace { 37*f4a2713aSLionel Sambuc class InstCount : public FunctionPass, public InstVisitor<InstCount> { 38*f4a2713aSLionel Sambuc friend class InstVisitor<InstCount>; 39*f4a2713aSLionel Sambuc 40*f4a2713aSLionel Sambuc void visitFunction (Function &F) { ++TotalFuncs; } 41*f4a2713aSLionel Sambuc void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; } 42*f4a2713aSLionel Sambuc 43*f4a2713aSLionel Sambuc #define HANDLE_INST(N, OPCODE, CLASS) \ 44*f4a2713aSLionel Sambuc void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; } 45*f4a2713aSLionel Sambuc 46*f4a2713aSLionel Sambuc #include "llvm/IR/Instruction.def" 47*f4a2713aSLionel Sambuc 48*f4a2713aSLionel Sambuc void visitInstruction(Instruction &I) { 49*f4a2713aSLionel Sambuc errs() << "Instruction Count does not know about " << I; 50*f4a2713aSLionel Sambuc llvm_unreachable(0); 51*f4a2713aSLionel Sambuc } 52*f4a2713aSLionel Sambuc public: 53*f4a2713aSLionel Sambuc static char ID; // Pass identification, replacement for typeid 54*f4a2713aSLionel Sambuc InstCount() : FunctionPass(ID) { 55*f4a2713aSLionel Sambuc initializeInstCountPass(*PassRegistry::getPassRegistry()); 56*f4a2713aSLionel Sambuc } 57*f4a2713aSLionel Sambuc 58*f4a2713aSLionel Sambuc virtual bool runOnFunction(Function &F); 59*f4a2713aSLionel Sambuc 60*f4a2713aSLionel Sambuc virtual void getAnalysisUsage(AnalysisUsage &AU) const { 61*f4a2713aSLionel Sambuc AU.setPreservesAll(); 62*f4a2713aSLionel Sambuc } 63*f4a2713aSLionel Sambuc virtual void print(raw_ostream &O, const Module *M) const {} 64*f4a2713aSLionel Sambuc 65*f4a2713aSLionel Sambuc }; 66*f4a2713aSLionel Sambuc } 67*f4a2713aSLionel Sambuc 68*f4a2713aSLionel Sambuc char InstCount::ID = 0; 69*f4a2713aSLionel Sambuc INITIALIZE_PASS(InstCount, "instcount", 70*f4a2713aSLionel Sambuc "Counts the various types of Instructions", false, true) 71*f4a2713aSLionel Sambuc 72*f4a2713aSLionel Sambuc FunctionPass *llvm::createInstCountPass() { return new InstCount(); } 73*f4a2713aSLionel Sambuc 74*f4a2713aSLionel Sambuc // InstCount::run - This is the main Analysis entry point for a 75*f4a2713aSLionel Sambuc // function. 76*f4a2713aSLionel Sambuc // 77*f4a2713aSLionel Sambuc bool InstCount::runOnFunction(Function &F) { 78*f4a2713aSLionel Sambuc unsigned StartMemInsts = 79*f4a2713aSLionel Sambuc NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst + 80*f4a2713aSLionel Sambuc NumInvokeInst + NumAllocaInst; 81*f4a2713aSLionel Sambuc visit(F); 82*f4a2713aSLionel Sambuc unsigned EndMemInsts = 83*f4a2713aSLionel Sambuc NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst + 84*f4a2713aSLionel Sambuc NumInvokeInst + NumAllocaInst; 85*f4a2713aSLionel Sambuc TotalMemInst += EndMemInsts-StartMemInsts; 86*f4a2713aSLionel Sambuc return false; 87*f4a2713aSLionel Sambuc } 88