LLVM  9.0.0svn
InstCount.cpp
Go to the documentation of this file.
1 //===-- InstCount.cpp - Collects the count of all instructions ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass collects the count of all instructions and reports them
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/Statistic.h"
14 #include "llvm/Analysis/Passes.h"
15 #include "llvm/IR/Function.h"
16 #include "llvm/IR/InstVisitor.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Support/Debug.h"
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "instcount"
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 
29 #define HANDLE_INST(N, OPCODE, CLASS) \
30  STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts");
31 
32 #include "llvm/IR/Instruction.def"
33 
34 namespace {
35  class InstCount : public FunctionPass, public InstVisitor<InstCount> {
36  friend class InstVisitor<InstCount>;
37 
38  void visitFunction (Function &F) { ++TotalFuncs; }
39  void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
40 
41 #define HANDLE_INST(N, OPCODE, CLASS) \
42  void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
43 
44 #include "llvm/IR/Instruction.def"
45 
47  errs() << "Instruction Count does not know about " << I;
48  llvm_unreachable(nullptr);
49  }
50  public:
51  static char ID; // Pass identification, replacement for typeid
52  InstCount() : FunctionPass(ID) {
54  }
55 
56  bool runOnFunction(Function &F) override;
57 
58  void getAnalysisUsage(AnalysisUsage &AU) const override {
59  AU.setPreservesAll();
60  }
61  void print(raw_ostream &O, const Module *M) const override {}
62 
63  };
64 }
65 
66 char InstCount::ID = 0;
67 INITIALIZE_PASS(InstCount, "instcount",
68  "Counts the various types of Instructions", false, true)
69 
70 FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
71 
72 // InstCount::run - This is the main Analysis entry point for a
73 // function.
74 //
76  visit(F);
77  return false;
78 }
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:67
Base class for instruction visitors.
Definition: InstVisitor.h:80
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
STATISTIC(NumFunctions, "Total number of functions")
F(f)
void visitFunction(Function &F)
Definition: InstVisitor.h:144
void initializeInstCountPass(PassRegistry &)
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:89
static bool runOnFunction(Function &F, bool PostInlining)
void visitBasicBlock(BasicBlock &BB)
Definition: InstVisitor.h:145
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
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:45
void visitInstruction(Instruction &I)
Definition: InstVisitor.h:298