LLVM API Documentation

InstCount.cpp

Go to the documentation of this file.
00001 //===-- InstCount.cpp - Collects the count of all instructions ------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This pass collects the count of all instructions and reports them
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #define DEBUG_TYPE "instcount"
00015 #include "llvm/Analysis/Passes.h"
00016 #include "llvm/Pass.h"
00017 #include "llvm/Function.h"
00018 #include "llvm/Support/Compiler.h"
00019 #include "llvm/Support/InstVisitor.h"
00020 #include "llvm/Support/Streams.h"
00021 #include "llvm/ADT/Statistic.h"
00022 #include <ostream>
00023 using namespace llvm;
00024 
00025 STATISTIC(TotalInsts , "Number of instructions (of all types)");
00026 STATISTIC(TotalBlocks, "Number of basic blocks");
00027 STATISTIC(TotalFuncs , "Number of non-external functions");
00028 STATISTIC(TotalMemInst, "Number of memory instructions");
00029 
00030 #define HANDLE_INST(N, OPCODE, CLASS) \
00031   STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts");
00032 
00033 #include "llvm/Instruction.def"
00034 
00035 
00036 namespace {
00037   class VISIBILITY_HIDDEN InstCount 
00038       : public FunctionPass, public InstVisitor<InstCount> {
00039     friend class InstVisitor<InstCount>;
00040 
00041     void visitFunction  (Function &F) { ++TotalFuncs; }
00042     void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
00043 
00044 #define HANDLE_INST(N, OPCODE, CLASS) \
00045     void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
00046 
00047 #include "llvm/Instruction.def"
00048 
00049     void visitInstruction(Instruction &I) {
00050       cerr << "Instruction Count does not know about " << I;
00051       abort();
00052     }
00053   public:
00054     static char ID; // Pass identification, replacement for typeid
00055     InstCount() : FunctionPass(&ID) {}
00056 
00057     virtual bool runOnFunction(Function &F);
00058 
00059     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00060       AU.setPreservesAll();
00061     }
00062     virtual void print(std::ostream &O, const Module *M) const {}
00063 
00064   };
00065 }
00066 
00067 char InstCount::ID = 0;
00068 static RegisterPass<InstCount>
00069 X("instcount", "Counts the various types of Instructions", false, true);
00070 
00071 FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
00072 
00073 // InstCount::run - This is the main Analysis entry point for a
00074 // function.
00075 //
00076 bool InstCount::runOnFunction(Function &F) {
00077   unsigned StartMemInsts =
00078     NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
00079     NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
00080   visit(F);
00081   unsigned EndMemInsts =
00082     NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
00083     NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
00084   TotalMemInst += EndMemInsts-StartMemInsts;
00085   return false;
00086 }