blob: 85f580c25d639682a92b824007dd7a47f8536a91 [file] [log] [blame]
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +00001//===-- InstCount.cpp - Collects the count of all instructions ------------===//
2//
3// This pass collects the count of all instructions and reports them
4//
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +00005//===----------------------------------------------------------------------===//
6
7#include "llvm/Pass.h"
Chris Lattner5f9be672003-08-29 14:43:17 +00008#include "llvm/Function.h"
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +00009#include "llvm/Support/InstVisitor.h"
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000010#include "Support/Statistic.h"
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000011
12namespace {
Chris Lattner3935d2b2002-12-07 23:24:24 +000013 Statistic<> TotalInsts ("instcount", "Number of instructions (of all types)");
14 Statistic<> TotalBlocks("instcount", "Number of basic blocks");
15 Statistic<> TotalFuncs ("instcount", "Number of non-external functions");
16
Chris Lattner5e058172002-12-03 19:40:16 +000017#define HANDLE_INST(N, OPCODE, CLASS) \
18 Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000019
Chris Lattner5e058172002-12-03 19:40:16 +000020#include "llvm/Instruction.def"
21
Chris Lattner5f9be672003-08-29 14:43:17 +000022 class InstCount : public FunctionPass, public InstVisitor<InstCount> {
Chris Lattner5e058172002-12-03 19:40:16 +000023 friend class InstVisitor<InstCount>;
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000024
Chris Lattner3935d2b2002-12-07 23:24:24 +000025 void visitFunction (Function &F) { ++TotalFuncs; }
26 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
27
Chris Lattner5e058172002-12-03 19:40:16 +000028#define HANDLE_INST(N, OPCODE, CLASS) \
Chris Lattner3935d2b2002-12-07 23:24:24 +000029 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000030
Chris Lattner5e058172002-12-03 19:40:16 +000031#include "llvm/Instruction.def"
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000032
Chris Lattner5e058172002-12-03 19:40:16 +000033 void visitInstruction(Instruction &I) {
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000034 std::cerr << "Instruction Count does not know about " << I;
35 abort();
36 }
37 public:
Chris Lattner5f9be672003-08-29 14:43:17 +000038 virtual bool runOnFunction(Function &F);
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000039
40 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
41 AU.setPreservesAll();
42 }
Chris Lattner3935d2b2002-12-07 23:24:24 +000043 virtual void print(std::ostream &O, const Module *M) const {}
Chris Lattner5e058172002-12-03 19:40:16 +000044
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000045 };
46
Chris Lattner342f6812002-11-17 22:15:40 +000047 RegisterAnalysis<InstCount> X("instcount",
Chris Lattner5e058172002-12-03 19:40:16 +000048 "Counts the various types of Instructions");
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000049}
50
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000051// InstCount::run - This is the main Analysis entry point for a
52// function.
53//
Chris Lattner5f9be672003-08-29 14:43:17 +000054bool InstCount::runOnFunction(Function &F) {
55 visit(F);
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000056 return false;
57}