blob: c4f3b682119b96cb13bfe7570fa0b8e6003f1509 [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"
Edwin Törökced9ff82009-07-11 13:10:19 +000018#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Support/InstVisitor.h"
Chris Lattner8a6411c2009-08-23 04:37:46 +000020#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/ADT/Statistic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022using namespace llvm;
23
24STATISTIC(TotalInsts , "Number of instructions (of all types)");
25STATISTIC(TotalBlocks, "Number of basic blocks");
26STATISTIC(TotalFuncs , "Number of non-external functions");
27STATISTIC(TotalMemInst, "Number of memory instructions");
28
29#define HANDLE_INST(N, OPCODE, CLASS) \
30 STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts");
31
32#include "llvm/Instruction.def"
33
34
35namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000036 class InstCount : public FunctionPass, public InstVisitor<InstCount> {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037 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/Instruction.def"
46
47 void visitInstruction(Instruction &I) {
Chris Lattner8a6411c2009-08-23 04:37:46 +000048 errs() << "Instruction Count does not know about " << I;
Edwin Törökbd448e32009-07-14 16:55:14 +000049 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 }
51 public:
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000053 InstCount() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054
55 virtual bool runOnFunction(Function &F);
56
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
58 AU.setPreservesAll();
59 }
Chris Lattner397f4562009-08-23 06:03:38 +000060 virtual void print(raw_ostream &O, const Module *M) const {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061
62 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063}
64
Dan Gohman089efff2008-05-13 00:00:25 +000065char InstCount::ID = 0;
66static RegisterPass<InstCount>
67X("instcount", "Counts the various types of Instructions", false, true);
68
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
70
71// InstCount::run - This is the main Analysis entry point for a
72// function.
73//
74bool InstCount::runOnFunction(Function &F) {
75 unsigned StartMemInsts =
76 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
Victor Hernandez37f513d2009-10-17 01:18:07 +000077 NumInvokeInst + NumAllocaInst + NumFreeInst;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 visit(F);
79 unsigned EndMemInsts =
80 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
Victor Hernandez37f513d2009-10-17 01:18:07 +000081 NumInvokeInst + NumAllocaInst + NumFreeInst;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 TotalMemInst += EndMemInsts-StartMemInsts;
83 return false;
84}