blob: df11fc4895aa2ce4149fb3415fc02685dbc3daf2 [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"
Reid Spencer551ccae2004-09-01 22:55:40 +000018#include "llvm/ADT/Statistic.h"
Chris Lattner72382102006-01-22 23:19:18 +000019#include <iostream>
Chris Lattner6ae7e982005-03-22 03:55:10 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000022namespace {
Chris Lattnera1af8bd2002-12-07 23:24:24 +000023 Statistic<> TotalInsts ("instcount", "Number of instructions (of all types)");
24 Statistic<> TotalBlocks("instcount", "Number of basic blocks");
25 Statistic<> TotalFuncs ("instcount", "Number of non-external functions");
Chris Lattner6ae7e982005-03-22 03:55:10 +000026 Statistic<> TotalMemInst("instcount", "Number of memory instructions");
Chris Lattnera1af8bd2002-12-07 23:24:24 +000027
Chris Lattner149a5202002-12-03 19:40:16 +000028#define HANDLE_INST(N, OPCODE, CLASS) \
29 Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000030
Chris Lattner149a5202002-12-03 19:40:16 +000031#include "llvm/Instruction.def"
32
Chris Lattner6666a042003-08-29 14:43:17 +000033 class InstCount : public FunctionPass, public InstVisitor<InstCount> {
Reid Spencere26057a2004-11-16 06:58:55 +000034 friend class InstVisitor<InstCount>;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000035
Chris Lattnera1af8bd2002-12-07 23:24:24 +000036 void visitFunction (Function &F) { ++TotalFuncs; }
37 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
38
Chris Lattner149a5202002-12-03 19:40:16 +000039#define HANDLE_INST(N, OPCODE, CLASS) \
Chris Lattnera1af8bd2002-12-07 23:24:24 +000040 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000041
Chris Lattner149a5202002-12-03 19:40:16 +000042#include "llvm/Instruction.def"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000043
Chris Lattner149a5202002-12-03 19:40:16 +000044 void visitInstruction(Instruction &I) {
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000045 std::cerr << "Instruction Count does not know about " << I;
46 abort();
47 }
48 public:
Chris Lattner6666a042003-08-29 14:43:17 +000049 virtual bool runOnFunction(Function &F);
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000050
51 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.setPreservesAll();
53 }
Chris Lattnera1af8bd2002-12-07 23:24:24 +000054 virtual void print(std::ostream &O, const Module *M) const {}
Chris Lattner149a5202002-12-03 19:40:16 +000055
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000056 };
57
Chris Lattnerdb85e382002-11-17 22:15:40 +000058 RegisterAnalysis<InstCount> X("instcount",
Chris Lattner149a5202002-12-03 19:40:16 +000059 "Counts the various types of Instructions");
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000060}
61
Chris Lattner4fb1b212005-10-24 01:00:45 +000062FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
63
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000064// InstCount::run - This is the main Analysis entry point for a
65// function.
66//
Chris Lattner6666a042003-08-29 14:43:17 +000067bool InstCount::runOnFunction(Function &F) {
Chris Lattner6ae7e982005-03-22 03:55:10 +000068 unsigned StartMemInsts =
Misha Brukman2b37d7c2005-04-21 21:13:18 +000069 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
Chris Lattner6ae7e982005-03-22 03:55:10 +000070 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
Chris Lattner6666a042003-08-29 14:43:17 +000071 visit(F);
Chris Lattner6ae7e982005-03-22 03:55:10 +000072 unsigned EndMemInsts =
Misha Brukman2b37d7c2005-04-21 21:13:18 +000073 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
Chris Lattner6ae7e982005-03-22 03:55:10 +000074 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
75 TotalMemInst += EndMemInsts-StartMemInsts;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000076 return false;
77}