blob: 80f8bd88180bf46b0b178d57278beec68c01d22a [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 Lattner4fb1b212005-10-24 01:00:45 +000014#include "llvm/Analysis/Passes.h"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000015#include "llvm/Pass.h"
Chris Lattner6666a042003-08-29 14:43:17 +000016#include "llvm/Function.h"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000017#include "llvm/Support/InstVisitor.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000018#include "llvm/Support/Streams.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/ADT/Statistic.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000020#include <ostream>
Chris Lattner6ae7e982005-03-22 03:55:10 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000023namespace {
Chris Lattnera1af8bd2002-12-07 23:24:24 +000024 Statistic<> TotalInsts ("instcount", "Number of instructions (of all types)");
25 Statistic<> TotalBlocks("instcount", "Number of basic blocks");
26 Statistic<> TotalFuncs ("instcount", "Number of non-external functions");
Chris Lattner6ae7e982005-03-22 03:55:10 +000027 Statistic<> TotalMemInst("instcount", "Number of memory instructions");
Chris Lattnera1af8bd2002-12-07 23:24:24 +000028
Chris Lattner149a5202002-12-03 19:40:16 +000029#define HANDLE_INST(N, OPCODE, CLASS) \
30 Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000031
Chris Lattner149a5202002-12-03 19:40:16 +000032#include "llvm/Instruction.def"
33
Chris Lattner6666a042003-08-29 14:43:17 +000034 class InstCount : public FunctionPass, public InstVisitor<InstCount> {
Reid Spencere26057a2004-11-16 06:58:55 +000035 friend class InstVisitor<InstCount>;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000036
Chris Lattnera1af8bd2002-12-07 23:24:24 +000037 void visitFunction (Function &F) { ++TotalFuncs; }
38 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
39
Chris Lattner149a5202002-12-03 19:40:16 +000040#define HANDLE_INST(N, OPCODE, CLASS) \
Chris Lattnera1af8bd2002-12-07 23:24:24 +000041 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000042
Chris Lattner149a5202002-12-03 19:40:16 +000043#include "llvm/Instruction.def"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000044
Chris Lattner149a5202002-12-03 19:40:16 +000045 void visitInstruction(Instruction &I) {
Bill Wendling6f81b512006-11-28 22:46:12 +000046 llvm_cerr << "Instruction Count does not know about " << I;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000047 abort();
48 }
49 public:
Chris Lattner6666a042003-08-29 14:43:17 +000050 virtual bool runOnFunction(Function &F);
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000051
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 AU.setPreservesAll();
54 }
Chris Lattnera1af8bd2002-12-07 23:24:24 +000055 virtual void print(std::ostream &O, const Module *M) const {}
Chris Lattner149a5202002-12-03 19:40:16 +000056
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000057 };
58
Chris Lattner5d8925c2006-08-27 22:30:17 +000059 RegisterPass<InstCount> X("instcount",
60 "Counts the various types of Instructions");
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000061}
62
Chris Lattner4fb1b212005-10-24 01:00:45 +000063FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
64
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000065// InstCount::run - This is the main Analysis entry point for a
66// function.
67//
Chris Lattner6666a042003-08-29 14:43:17 +000068bool InstCount::runOnFunction(Function &F) {
Chris Lattner6ae7e982005-03-22 03:55:10 +000069 unsigned StartMemInsts =
Misha Brukman2b37d7c2005-04-21 21:13:18 +000070 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
Chris Lattner6ae7e982005-03-22 03:55:10 +000071 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
Chris Lattner6666a042003-08-29 14:43:17 +000072 visit(F);
Chris Lattner6ae7e982005-03-22 03:55:10 +000073 unsigned EndMemInsts =
Misha Brukman2b37d7c2005-04-21 21:13:18 +000074 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
Chris Lattner6ae7e982005-03-22 03:55:10 +000075 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
76 TotalMemInst += EndMemInsts-StartMemInsts;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000077 return false;
78}