blob: dcbcac005a2fc1c1a0bdbc214cd1993ac082725d [file] [log] [blame]
Dinakar Dhurjatie5554602002-11-13 18:22:13 +00001//===-- InstCount.cpp - Collects the count of all instructions ------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Dinakar Dhurjatie5554602002-11-13 18:22:13 +00009//
Misha Brukman2b37d7c2005-04-21 21:13:18 +000010// This pass collects the count of all instructions and reports them
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000011//
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000012//===----------------------------------------------------------------------===//
13
Chris Lattner3b27d682006-12-19 22:30:33 +000014#define DEBUG_TYPE "instcount"
Chris Lattner4fb1b212005-10-24 01:00:45 +000015#include "llvm/Analysis/Passes.h"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000016#include "llvm/Pass.h"
Chris Lattner6666a042003-08-29 14:43:17 +000017#include "llvm/Function.h"
David Greene270862d2009-12-23 20:34:27 +000018#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000019#include "llvm/Support/ErrorHandling.h"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000020#include "llvm/Support/InstVisitor.h"
Chris Lattnerbdff5482009-08-23 04:37:46 +000021#include "llvm/Support/raw_ostream.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/ADT/Statistic.h"
Chris Lattner6ae7e982005-03-22 03:55:10 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattner3b27d682006-12-19 22:30:33 +000025STATISTIC(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");
Chris Lattnera1af8bd2002-12-07 23:24:24 +000029
Chris Lattner149a5202002-12-03 19:40:16 +000030#define HANDLE_INST(N, OPCODE, CLASS) \
Chris Lattner3b27d682006-12-19 22:30:33 +000031 STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts");
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000032
Chris Lattner149a5202002-12-03 19:40:16 +000033#include "llvm/Instruction.def"
34
Chris Lattner3b27d682006-12-19 22:30:33 +000035
36namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000037 class InstCount : public FunctionPass, public InstVisitor<InstCount> {
Reid Spencere26057a2004-11-16 06:58:55 +000038 friend class InstVisitor<InstCount>;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000039
Chris Lattnera1af8bd2002-12-07 23:24:24 +000040 void visitFunction (Function &F) { ++TotalFuncs; }
41 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
42
Chris Lattner149a5202002-12-03 19:40:16 +000043#define HANDLE_INST(N, OPCODE, CLASS) \
Chris Lattnera1af8bd2002-12-07 23:24:24 +000044 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000045
Chris Lattner149a5202002-12-03 19:40:16 +000046#include "llvm/Instruction.def"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000047
Chris Lattner149a5202002-12-03 19:40:16 +000048 void visitInstruction(Instruction &I) {
David Greeneb64ca132009-12-23 23:29:28 +000049 errs() << "Instruction Count does not know about " << I;
Torok Edwinc23197a2009-07-14 16:55:14 +000050 llvm_unreachable(0);
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000051 }
52 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000053 static char ID; // Pass identification, replacement for typeid
Owen Anderson9ccaf532010-08-05 23:42:04 +000054 InstCount() : FunctionPass(ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000055
Chris Lattner6666a042003-08-29 14:43:17 +000056 virtual bool runOnFunction(Function &F);
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000057
58 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.setPreservesAll();
60 }
Chris Lattner45cfe542009-08-23 06:03:38 +000061 virtual void print(raw_ostream &O, const Module *M) const {}
Chris Lattner149a5202002-12-03 19:40:16 +000062
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000063 };
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000064}
65
Dan Gohman844731a2008-05-13 00:00:25 +000066char InstCount::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000067INITIALIZE_PASS(InstCount, "instcount",
68 "Counts the various types of Instructions", false, true);
Dan Gohman844731a2008-05-13 00:00:25 +000069
Chris Lattner4fb1b212005-10-24 01:00:45 +000070FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
71
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000072// InstCount::run - This is the main Analysis entry point for a
73// function.
74//
Chris Lattner6666a042003-08-29 14:43:17 +000075bool InstCount::runOnFunction(Function &F) {
Chris Lattner6ae7e982005-03-22 03:55:10 +000076 unsigned StartMemInsts =
Misha Brukman2b37d7c2005-04-21 21:13:18 +000077 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
Victor Hernandez046e78c2009-10-26 23:43:48 +000078 NumInvokeInst + NumAllocaInst;
Chris Lattner6666a042003-08-29 14:43:17 +000079 visit(F);
Chris Lattner6ae7e982005-03-22 03:55:10 +000080 unsigned EndMemInsts =
Misha Brukman2b37d7c2005-04-21 21:13:18 +000081 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
Victor Hernandez046e78c2009-10-26 23:43:48 +000082 NumInvokeInst + NumAllocaInst;
Chris Lattner6ae7e982005-03-22 03:55:10 +000083 TotalMemInst += EndMemInsts-StartMemInsts;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000084 return false;
85}