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 #include "llvm/Analysis/Passes.h"
00015 #include "llvm/ADT/Statistic.h"
00016 #include "llvm/IR/Function.h"
00017 #include "llvm/IR/InstVisitor.h"
00018 #include "llvm/Pass.h"
00019 #include "llvm/Support/Debug.h"
00020 #include "llvm/Support/ErrorHandling.h"
00021 #include "llvm/Support/raw_ostream.h"
00022 using namespace llvm;
00023 
00024 #define DEBUG_TYPE "instcount"
00025 
00026 STATISTIC(TotalInsts , "Number of instructions (of all types)");
00027 STATISTIC(TotalBlocks, "Number of basic blocks");
00028 STATISTIC(TotalFuncs , "Number of non-external functions");
00029 STATISTIC(TotalMemInst, "Number of memory instructions");
00030 
00031 #define HANDLE_INST(N, OPCODE, CLASS) \
00032   STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts");
00033 
00034 #include "llvm/IR/Instruction.def"
00035 
00036 
00037 namespace {
00038   class InstCount : 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/IR/Instruction.def"
00048 
00049     void visitInstruction(Instruction &I) {
00050       errs() << "Instruction Count does not know about " << I;
00051       llvm_unreachable(nullptr);
00052     }
00053   public:
00054     static char ID; // Pass identification, replacement for typeid
00055     InstCount() : FunctionPass(ID) {
00056       initializeInstCountPass(*PassRegistry::getPassRegistry());
00057     }
00058 
00059     bool runOnFunction(Function &F) override;
00060 
00061     void getAnalysisUsage(AnalysisUsage &AU) const override {
00062       AU.setPreservesAll();
00063     }
00064     void print(raw_ostream &O, const Module *M) const override {}
00065 
00066   };
00067 }
00068 
00069 char InstCount::ID = 0;
00070 INITIALIZE_PASS(InstCount, "instcount",
00071                 "Counts the various types of Instructions", false, true)
00072 
00073 FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
00074 
00075 // InstCount::run - This is the main Analysis entry point for a
00076 // function.
00077 //
00078 bool InstCount::runOnFunction(Function &F) {
00079   unsigned StartMemInsts =
00080     NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
00081     NumInvokeInst + NumAllocaInst;
00082   visit(F);
00083   unsigned EndMemInsts =
00084     NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
00085     NumInvokeInst + NumAllocaInst;
00086   TotalMemInst += EndMemInsts-StartMemInsts;
00087   return false;
00088 }