LLVM API Documentation
00001 //===-- InstCount.cpp - Collects the count of all instructions ------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source 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 #include "llvm/Analysis/Passes.h" 00015 #include "llvm/Pass.h" 00016 #include "llvm/Function.h" 00017 #include "llvm/Support/InstVisitor.h" 00018 #include "llvm/ADT/Statistic.h" 00019 #include <iostream> 00020 using namespace llvm; 00021 00022 namespace { 00023 Statistic<> TotalInsts ("instcount", "Number of instructions (of all types)"); 00024 Statistic<> TotalBlocks("instcount", "Number of basic blocks"); 00025 Statistic<> TotalFuncs ("instcount", "Number of non-external functions"); 00026 Statistic<> TotalMemInst("instcount", "Number of memory instructions"); 00027 00028 #define HANDLE_INST(N, OPCODE, CLASS) \ 00029 Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts"); 00030 00031 #include "llvm/Instruction.def" 00032 00033 class InstCount : public FunctionPass, public InstVisitor<InstCount> { 00034 friend class InstVisitor<InstCount>; 00035 00036 void visitFunction (Function &F) { ++TotalFuncs; } 00037 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; } 00038 00039 #define HANDLE_INST(N, OPCODE, CLASS) \ 00040 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; } 00041 00042 #include "llvm/Instruction.def" 00043 00044 void visitInstruction(Instruction &I) { 00045 std::cerr << "Instruction Count does not know about " << I; 00046 abort(); 00047 } 00048 public: 00049 virtual bool runOnFunction(Function &F); 00050 00051 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00052 AU.setPreservesAll(); 00053 } 00054 virtual void print(std::ostream &O, const Module *M) const {} 00055 00056 }; 00057 00058 RegisterAnalysis<InstCount> X("instcount", 00059 "Counts the various types of Instructions"); 00060 } 00061 00062 FunctionPass *llvm::createInstCountPass() { return new InstCount(); } 00063 00064 // InstCount::run - This is the main Analysis entry point for a 00065 // function. 00066 // 00067 bool InstCount::runOnFunction(Function &F) { 00068 unsigned StartMemInsts = 00069 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst + 00070 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst; 00071 visit(F); 00072 unsigned EndMemInsts = 00073 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst + 00074 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst; 00075 TotalMemInst += EndMemInsts-StartMemInsts; 00076 return false; 00077 }