blob: 2b34ad3b070de240eef3a67889f5d2c90f347d88 [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"
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"
Chris Lattner6ae7e982005-03-22 03:55:10 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattner3b27d682006-12-19 22:30:33 +000024STATISTIC(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");
Chris Lattnera1af8bd2002-12-07 23:24:24 +000028
Chris Lattner149a5202002-12-03 19:40:16 +000029#define HANDLE_INST(N, OPCODE, CLASS) \
Chris Lattner3b27d682006-12-19 22:30:33 +000030 STATISTIC(Num ## OPCODE ## Inst, "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 Lattner3b27d682006-12-19 22:30:33 +000034
35namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +000036 class VISIBILITY_HIDDEN InstCount
37 : 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) {
Bill Wendlinge8156192006-12-07 01:30:32 +000049 cerr << "Instruction Count does not know about " << I;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000050 abort();
51 }
52 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000053 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +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 Lattnera1af8bd2002-12-07 23:24:24 +000061 virtual void print(std::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;
67static RegisterPass<InstCount>
68X("instcount", "Counts the various types of Instructions", false, true);
69
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 +
Chris Lattner6ae7e982005-03-22 03:55:10 +000078 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
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 +
Chris Lattner6ae7e982005-03-22 03:55:10 +000082 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
83 TotalMemInst += EndMemInsts-StartMemInsts;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000084 return false;
85}