blob: 9b743fee317afb40b1ccfd53510d3b3849f4802c [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000019#include "llvm/Support/InstVisitor.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000020#include "llvm/Support/Streams.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/Statistic.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000022#include <ostream>
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) {
Bill Wendlinge8156192006-12-07 01:30:32 +000050 cerr << "Instruction Count does not know about " << I;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000051 abort();
52 }
53 public:
Devang Patel19974732007-05-03 01:11:54 +000054 static char ID; // Pass identifcation, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000055 InstCount() : FunctionPass((intptr_t)&ID) {}
56
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 Lattnera1af8bd2002-12-07 23:24:24 +000062 virtual void print(std::ostream &O, const Module *M) const {}
Chris Lattner149a5202002-12-03 19:40:16 +000063
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000064 };
65
Devang Patel19974732007-05-03 01:11:54 +000066 char InstCount::ID = 0;
Chris Lattner5d8925c2006-08-27 22:30:17 +000067 RegisterPass<InstCount> X("instcount",
68 "Counts the various types of Instructions");
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000069}
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 +
Chris Lattner6ae7e982005-03-22 03:55:10 +000079 NumInvokeInst + NumAllocaInst + NumMallocInst + 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 +
Chris Lattner6ae7e982005-03-22 03:55:10 +000083 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
84 TotalMemInst += EndMemInsts-StartMemInsts;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000085 return false;
86}