LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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 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/Pass.h"
00015 #include "llvm/Function.h"
00016 #include "llvm/Support/InstVisitor.h"
00017 #include "llvm/ADT/Statistic.h"
00018 
00019 namespace llvm {
00020 
00021 namespace {
00022   Statistic<> TotalInsts ("instcount", "Number of instructions (of all types)");
00023   Statistic<> TotalBlocks("instcount", "Number of basic blocks");
00024   Statistic<> TotalFuncs ("instcount", "Number of non-external functions");
00025 
00026 #define HANDLE_INST(N, OPCODE, CLASS) \
00027     Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
00028 
00029 #include "llvm/Instruction.def"
00030 
00031   class InstCount : public FunctionPass, public InstVisitor<InstCount> {
00032     friend class InstVisitor<InstCount>;
00033 
00034     void visitFunction  (Function &F) { ++TotalFuncs; }
00035     void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
00036 
00037 #define HANDLE_INST(N, OPCODE, CLASS) \
00038     void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
00039 
00040 #include "llvm/Instruction.def"
00041 
00042     void visitInstruction(Instruction &I) {
00043       std::cerr << "Instruction Count does not know about " << I;
00044       abort();
00045     }
00046   public:
00047     virtual bool runOnFunction(Function &F);
00048 
00049     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00050       AU.setPreservesAll();
00051     }
00052     virtual void print(std::ostream &O, const Module *M) const {}
00053 
00054   };
00055 
00056   RegisterAnalysis<InstCount> X("instcount",
00057                                 "Counts the various types of Instructions");
00058 }
00059 
00060 // InstCount::run - This is the main Analysis entry point for a
00061 // function.
00062 //
00063 bool InstCount::runOnFunction(Function &F) {
00064   visit(F);
00065   return false;
00066 }
00067 
00068 } // End llvm namespace