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