blob: dcbcac005a2fc1c1a0bdbc214cd1993ac082725d [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"
David Greene7176e082009-12-23 20:34:27 +000018#include "llvm/Support/Debug.h"
Edwin Törökced9ff82009-07-11 13:10:19 +000019#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/Support/InstVisitor.h"
Chris Lattner8a6411c2009-08-23 04:37:46 +000021#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/ADT/Statistic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023using 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 {
Nick Lewycky492d06e2009-10-25 06:33:48 +000037 class InstCount : public FunctionPass, public InstVisitor<InstCount> {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038 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/Instruction.def"
47
Argiris Kirtzidisfe9d1862010-08-15 10:27:23 +000048 void visitInstruction(Instruction &I) {
David Greene8377a152009-12-23 23:29:28 +000049 errs() << "Instruction Count does not know about " << I;
Edwin Törökbd448e32009-07-14 16:55:14 +000050 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 }
52 public:
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 static char ID; // Pass identification, replacement for typeid
Owen Anderson75693222010-08-06 18:33:48 +000054 InstCount() : FunctionPass(ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055
56 virtual bool runOnFunction(Function &F);
57
58 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.setPreservesAll();
60 }
Chris Lattner397f4562009-08-23 06:03:38 +000061 virtual void print(raw_ostream &O, const Module *M) const {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062
63 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064}
65
Dan Gohman089efff2008-05-13 00:00:25 +000066char InstCount::ID = 0;
Owen Anderson6374c3d2010-07-21 22:09:45 +000067INITIALIZE_PASS(InstCount, "instcount",
68 "Counts the various types of Instructions", false, true);
Dan Gohman089efff2008-05-13 00:00:25 +000069
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
71
72// InstCount::run - This is the main Analysis entry point for a
73// function.
74//
75bool InstCount::runOnFunction(Function &F) {
76 unsigned StartMemInsts =
77 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
Victor Hernandezf9a7a332009-10-26 23:43:48 +000078 NumInvokeInst + NumAllocaInst;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 visit(F);
80 unsigned EndMemInsts =
81 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
Victor Hernandezf9a7a332009-10-26 23:43:48 +000082 NumInvokeInst + NumAllocaInst;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 TotalMemInst += EndMemInsts-StartMemInsts;
84 return false;
85}