blob: 08a27a66b3287a19cf145a20c879153a91f170fd [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- InstCount.cpp - Collects the count of all instructions ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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/Pass.h"
17#include "llvm/Function.h"
18#include "llvm/Support/Compiler.h"
19#include "llvm/Support/InstVisitor.h"
20#include "llvm/Support/Streams.h"
21#include "llvm/ADT/Statistic.h"
22#include <ostream>
23using namespace llvm;
24
25STATISTIC(TotalInsts , "Number of instructions (of all types)");
26STATISTIC(TotalBlocks, "Number of basic blocks");
27STATISTIC(TotalFuncs , "Number of non-external functions");
28STATISTIC(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/Instruction.def"
34
35
36namespace {
37 class VISIBILITY_HIDDEN InstCount
38 : 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/Instruction.def"
48
49 void visitInstruction(Instruction &I) {
50 cerr << "Instruction Count does not know about " << I;
51 abort();
52 }
53 public:
Devang Patel2b4fa682008-03-18 00:39:19 +000054
55 /// isAnalysis - Return true if this pass is implementing an analysis pass.
56 virtual bool isAnalysis() const { return true; }
57
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 static char ID; // Pass identification, replacement for typeid
59 InstCount() : FunctionPass((intptr_t)&ID) {}
60
61 virtual bool runOnFunction(Function &F);
62
63 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
64 AU.setPreservesAll();
65 }
66 virtual void print(std::ostream &O, const Module *M) const {}
67
68 };
69
70 char InstCount::ID = 0;
71 RegisterPass<InstCount> X("instcount",
72 "Counts the various types of Instructions");
73}
74
75FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
76
77// InstCount::run - This is the main Analysis entry point for a
78// function.
79//
80bool InstCount::runOnFunction(Function &F) {
81 unsigned StartMemInsts =
82 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
83 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
84 visit(F);
85 unsigned EndMemInsts =
86 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
87 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
88 TotalMemInst += EndMemInsts-StartMemInsts;
89 return false;
90}