LLVM  3.7.0
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 #include "llvm/Analysis/Passes.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/InstVisitor.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/Debug.h"
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "instcount"
25 
26 STATISTIC(TotalInsts , "Number of instructions (of all types)");
27 STATISTIC(TotalBlocks, "Number of basic blocks");
28 STATISTIC(TotalFuncs , "Number of non-external functions");
29 STATISTIC(TotalMemInst, "Number of memory instructions");
30 
31 #define HANDLE_INST(N, OPCODE, CLASS) \
32  STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts");
33 
34 #include "llvm/IR/Instruction.def"
35 
36 
37 namespace {
38  class InstCount : public FunctionPass, public InstVisitor<InstCount> {
39  friend class InstVisitor<InstCount>;
40 
41  void visitFunction (Function &F) { ++TotalFuncs; }
42  void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
43 
44 #define HANDLE_INST(N, OPCODE, CLASS) \
45  void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
46 
47 #include "llvm/IR/Instruction.def"
48 
49  void visitInstruction(Instruction &I) {
50  errs() << "Instruction Count does not know about " << I;
51  llvm_unreachable(nullptr);
52  }
53  public:
54  static char ID; // Pass identification, replacement for typeid
55  InstCount() : FunctionPass(ID) {
57  }
58 
59  bool runOnFunction(Function &F) override;
60 
61  void getAnalysisUsage(AnalysisUsage &AU) const override {
62  AU.setPreservesAll();
63  }
64  void print(raw_ostream &O, const Module *M) const override {}
65 
66  };
67 }
68 
69 char InstCount::ID = 0;
70 INITIALIZE_PASS(InstCount, "instcount",
71  "Counts the various types of Instructions", false, true)
72 
73 FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
74 
75 // InstCount::run - This is the main Analysis entry point for a
76 // function.
77 //
78 bool InstCount::runOnFunction(Function &F) {
79  unsigned StartMemInsts =
80  NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
81  NumInvokeInst + NumAllocaInst;
82  visit(F);
83  unsigned EndMemInsts =
84  NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
85  NumInvokeInst + NumAllocaInst;
86  TotalMemInst += EndMemInsts-StartMemInsts;
87  return false;
88 }
FunctionPass * createInstCountPass()
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Base class for instruction visitors.
Definition: InstVisitor.h:81
STATISTIC(NumFunctions,"Total number of functions")
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
F(f)
void initializeInstCountPass(PassRegistry &)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
INITIALIZE_PASS(InstCount,"instcount","Counts the various types of Instructions", false, true) FunctionPass *llvm
Definition: InstCount.cpp:70
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
void setPreservesAll()
Set by analyses that do not transform their input at all.
#define I(x, y, z)
Definition: MD5.cpp:54
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38