blob: 831cead4ce0aec3559cb8d74d37f569359a67884 [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 Lattner6ae7e982005-03-22 03:55:10 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000021namespace {
Chris Lattnera1af8bd2002-12-07 23:24:24 +000022 Statistic<> TotalInsts ("instcount", "Number of instructions (of all types)");
23 Statistic<> TotalBlocks("instcount", "Number of basic blocks");
24 Statistic<> TotalFuncs ("instcount", "Number of non-external functions");
Chris Lattner6ae7e982005-03-22 03:55:10 +000025 Statistic<> TotalMemInst("instcount", "Number of memory instructions");
Chris Lattnera1af8bd2002-12-07 23:24:24 +000026
Chris Lattner149a5202002-12-03 19:40:16 +000027#define HANDLE_INST(N, OPCODE, CLASS) \
28 Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000029
Chris Lattner149a5202002-12-03 19:40:16 +000030#include "llvm/Instruction.def"
31
Chris Lattner6666a042003-08-29 14:43:17 +000032 class InstCount : public FunctionPass, public InstVisitor<InstCount> {
Reid Spencere26057a2004-11-16 06:58:55 +000033 friend class InstVisitor<InstCount>;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000034
Chris Lattnera1af8bd2002-12-07 23:24:24 +000035 void visitFunction (Function &F) { ++TotalFuncs; }
36 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
37
Chris Lattner149a5202002-12-03 19:40:16 +000038#define HANDLE_INST(N, OPCODE, CLASS) \
Chris Lattnera1af8bd2002-12-07 23:24:24 +000039 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000040
Chris Lattner149a5202002-12-03 19:40:16 +000041#include "llvm/Instruction.def"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000042
Chris Lattner149a5202002-12-03 19:40:16 +000043 void visitInstruction(Instruction &I) {
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000044 std::cerr << "Instruction Count does not know about " << I;
45 abort();
46 }
47 public:
Chris Lattner6666a042003-08-29 14:43:17 +000048 virtual bool runOnFunction(Function &F);
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000049
50 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
51 AU.setPreservesAll();
52 }
Chris Lattnera1af8bd2002-12-07 23:24:24 +000053 virtual void print(std::ostream &O, const Module *M) const {}
Chris Lattner149a5202002-12-03 19:40:16 +000054
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000055 };
56
Chris Lattnerdb85e382002-11-17 22:15:40 +000057 RegisterAnalysis<InstCount> X("instcount",
Chris Lattner149a5202002-12-03 19:40:16 +000058 "Counts the various types of Instructions");
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000059}
60
Chris Lattner4fb1b212005-10-24 01:00:45 +000061FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
62
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000063// InstCount::run - This is the main Analysis entry point for a
64// function.
65//
Chris Lattner6666a042003-08-29 14:43:17 +000066bool InstCount::runOnFunction(Function &F) {
Chris Lattner6ae7e982005-03-22 03:55:10 +000067 unsigned StartMemInsts =
Misha Brukman2b37d7c2005-04-21 21:13:18 +000068 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
Chris Lattner6ae7e982005-03-22 03:55:10 +000069 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
Chris Lattner6666a042003-08-29 14:43:17 +000070 visit(F);
Chris Lattner6ae7e982005-03-22 03:55:10 +000071 unsigned EndMemInsts =
Misha Brukman2b37d7c2005-04-21 21:13:18 +000072 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
Chris Lattner6ae7e982005-03-22 03:55:10 +000073 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
74 TotalMemInst += EndMemInsts-StartMemInsts;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000075 return false;
76}