blob: 4cde793577283b7c30cc93291937d5ad92776b75 [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"
Reid Spencerd7d83db2007-02-05 23:42:17 +000018#include "llvm/Support/Compiler.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 {
Reid Spencerd7d83db2007-02-05 23:42:17 +000037 class VISIBILITY_HIDDEN InstCount
38 : public FunctionPass, public InstVisitor<InstCount> {
Reid Spencere26057a2004-11-16 06:58:55 +000039 friend class InstVisitor<InstCount>;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000040
Chris Lattnera1af8bd2002-12-07 23:24:24 +000041 void visitFunction (Function &F) { ++TotalFuncs; }
42 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
43
Chris Lattner149a5202002-12-03 19:40:16 +000044#define HANDLE_INST(N, OPCODE, CLASS) \
Chris Lattnera1af8bd2002-12-07 23:24:24 +000045 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000046
Chris Lattner149a5202002-12-03 19:40:16 +000047#include "llvm/Instruction.def"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000048
Chris Lattner149a5202002-12-03 19:40:16 +000049 void visitInstruction(Instruction &I) {
Chris Lattnerbdff5482009-08-23 04:37:46 +000050 errs() << "Instruction Count does not know about " << I;
Torok Edwinc23197a2009-07-14 16:55:14 +000051 llvm_unreachable(0);
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000052 }
53 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000054 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000055 InstCount() : FunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000056
Chris Lattner6666a042003-08-29 14:43:17 +000057 virtual bool runOnFunction(Function &F);
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000058
59 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.setPreservesAll();
61 }
Chris Lattner45cfe542009-08-23 06:03:38 +000062 virtual void print(raw_ostream &O, const Module *M) const {}
Chris Lattner149a5202002-12-03 19:40:16 +000063
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000064 };
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000065}
66
Dan Gohman844731a2008-05-13 00:00:25 +000067char InstCount::ID = 0;
68static RegisterPass<InstCount>
69X("instcount", "Counts the various types of Instructions", false, true);
70
Chris Lattner4fb1b212005-10-24 01:00:45 +000071FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
72
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000073// InstCount::run - This is the main Analysis entry point for a
74// function.
75//
Chris Lattner6666a042003-08-29 14:43:17 +000076bool InstCount::runOnFunction(Function &F) {
Chris Lattner6ae7e982005-03-22 03:55:10 +000077 unsigned StartMemInsts =
Misha Brukman2b37d7c2005-04-21 21:13:18 +000078 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
Victor Hernandeza276c602009-10-17 01:18:07 +000079 NumInvokeInst + NumAllocaInst + NumFreeInst;
Chris Lattner6666a042003-08-29 14:43:17 +000080 visit(F);
Chris Lattner6ae7e982005-03-22 03:55:10 +000081 unsigned EndMemInsts =
Misha Brukman2b37d7c2005-04-21 21:13:18 +000082 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
Victor Hernandeza276c602009-10-17 01:18:07 +000083 NumInvokeInst + NumAllocaInst + NumFreeInst;
Chris Lattner6ae7e982005-03-22 03:55:10 +000084 TotalMemInst += EndMemInsts-StartMemInsts;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000085 return false;
86}