blob: 75a49eb90a88fa3357a5da2ba54b0d01ef73be33 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/Statistic.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Function.h"
Chandler Carruthdbd69582012-11-30 03:08:41 +000018#include "llvm/InstVisitor.h"
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000019#include "llvm/Pass.h"
David Greenea7b92ee2009-12-23 20:34:27 +000020#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb25de3f2009-08-23 04:37:46 +000022#include "llvm/Support/raw_ostream.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
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/Instruction.def"
Chris Lattner5e058172002-12-03 19:40:16 +000034
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
Chandler Carruth9fb823b2013-01-02 11:36:10 +000046#include "llvm/IR/Instruction.def"
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000047
Argyrios Kyrtzidisd0fcc9a2010-08-15 10:27:23 +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
Owen Anderson6c18d1a2010-10-19 17:21:58 +000054 InstCount() : FunctionPass(ID) {
55 initializeInstCountPass(*PassRegistry::getPassRegistry());
56 }
Devang Patel09f162c2007-05-01 21:15:47 +000057
Chris Lattner5f9be672003-08-29 14:43:17 +000058 virtual bool runOnFunction(Function &F);
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000059
60 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
61 AU.setPreservesAll();
62 }
Chris Lattner13626022009-08-23 06:03:38 +000063 virtual void print(raw_ostream &O, const Module *M) const {}
Chris Lattner5e058172002-12-03 19:40:16 +000064
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000065 };
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000066}
67
Dan Gohmand78c4002008-05-13 00:00:25 +000068char InstCount::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000069INITIALIZE_PASS(InstCount, "instcount",
Owen Andersondf7a4f22010-10-07 22:25:06 +000070 "Counts the various types of Instructions", false, true)
Dan Gohmand78c4002008-05-13 00:00:25 +000071
Chris Lattnerca014ad2005-10-24 01:00:45 +000072FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
73
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000074// InstCount::run - This is the main Analysis entry point for a
75// function.
76//
Chris Lattner5f9be672003-08-29 14:43:17 +000077bool InstCount::runOnFunction(Function &F) {
Chris Lattnerf0068402005-03-22 03:55:10 +000078 unsigned StartMemInsts =
Misha Brukman01808ca2005-04-21 21:13:18 +000079 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
Victor Hernandezde5ad422009-10-26 23:43:48 +000080 NumInvokeInst + NumAllocaInst;
Chris Lattner5f9be672003-08-29 14:43:17 +000081 visit(F);
Chris Lattnerf0068402005-03-22 03:55:10 +000082 unsigned EndMemInsts =
Misha Brukman01808ca2005-04-21 21:13:18 +000083 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
Victor Hernandezde5ad422009-10-26 23:43:48 +000084 NumInvokeInst + NumAllocaInst;
Chris Lattnerf0068402005-03-22 03:55:10 +000085 TotalMemInst += EndMemInsts-StartMemInsts;
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000086 return false;
87}