LLVM  6.0.0svn
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/ADT/Statistic.h"
15 #include "llvm/Analysis/Passes.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 
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 namespace {
36  class InstCount : public FunctionPass, public InstVisitor<InstCount> {
37  friend class InstVisitor<InstCount>;
38 
39  void visitFunction (Function &F) { ++TotalFuncs; }
40  void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
41 
42 #define HANDLE_INST(N, OPCODE, CLASS) \
43  void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
44 
45 #include "llvm/IR/Instruction.def"
46 
48  errs() << "Instruction Count does not know about " << I;
49  llvm_unreachable(nullptr);
50  }
51  public:
52  static char ID; // Pass identification, replacement for typeid
53  InstCount() : FunctionPass(ID) {
55  }
56 
57  bool runOnFunction(Function &F) override;
58 
59  void getAnalysisUsage(AnalysisUsage &AU) const override {
60  AU.setPreservesAll();
61  }
62  void print(raw_ostream &O, const Module *M) const override {}
63 
64  };
65 }
66 
67 char InstCount::ID = 0;
68 INITIALIZE_PASS(InstCount, "instcount",
69  "Counts the various types of Instructions", false, true)
70 
71 FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
72 
73 // InstCount::run - This is the main Analysis entry point for a
74 // function.
75 //
77  visit(F);
78  return false;
79 }
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...
INITIALIZE_PASS(InstCount, "instcount", "Counts the various types of Instructions", false, true) FunctionPass *llvm
Definition: InstCount.cpp:68
Base class for instruction visitors.
Definition: InstVisitor.h:81
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
STATISTIC(NumFunctions, "Total number of functions")
F(f)
void visitFunction(Function &F)
Definition: InstVisitor.h:145
void initializeInstCountPass(PassRegistry &)
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:90
static bool runOnFunction(Function &F, bool PostInlining)
void visitBasicBlock(BasicBlock &BB)
Definition: InstVisitor.h:146
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setPreservesAll()
Set by analyses that do not transform their input at all.
#define I(x, y, z)
Definition: MD5.cpp:58
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
void visitInstruction(Instruction &I)
Definition: InstVisitor.h:265