LLVM API Documentation

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