blob: 18e0b9aceae389bf1d4b8fa0ebf4ece8dcedee46 [file] [log] [blame]
Dinakar Dhurjatie5554602002-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 Dhurjatie5554602002-11-13 18:22:13 +00005//===----------------------------------------------------------------------===//
6
7#include "llvm/Pass.h"
8#include "llvm/Module.h"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +00009#include "llvm/Support/InstVisitor.h"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000010#include "Support/Statistic.h"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000011
12namespace {
Chris Lattnera1af8bd2002-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 Lattner149a5202002-12-03 19:40:16 +000017#define HANDLE_INST(N, OPCODE, CLASS) \
18 Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000019
Chris Lattner149a5202002-12-03 19:40:16 +000020#include "llvm/Instruction.def"
21
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000022 class InstCount : public Pass, public InstVisitor<InstCount> {
Chris Lattner149a5202002-12-03 19:40:16 +000023 friend class InstVisitor<InstCount>;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000024
Chris Lattnera1af8bd2002-12-07 23:24:24 +000025 void visitFunction (Function &F) { ++TotalFuncs; }
26 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
27
Chris Lattner149a5202002-12-03 19:40:16 +000028#define HANDLE_INST(N, OPCODE, CLASS) \
Chris Lattnera1af8bd2002-12-07 23:24:24 +000029 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000030
Chris Lattner149a5202002-12-03 19:40:16 +000031#include "llvm/Instruction.def"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000032
Chris Lattner149a5202002-12-03 19:40:16 +000033 void visitInstruction(Instruction &I) {
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000034 std::cerr << "Instruction Count does not know about " << I;
35 abort();
36 }
37 public:
38 virtual bool run(Module &M);
39
40 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
41 AU.setPreservesAll();
42 }
Chris Lattnera1af8bd2002-12-07 23:24:24 +000043 virtual void print(std::ostream &O, const Module *M) const {}
Chris Lattner149a5202002-12-03 19:40:16 +000044
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000045 };
46
Chris Lattnerdb85e382002-11-17 22:15:40 +000047 RegisterAnalysis<InstCount> X("instcount",
Chris Lattner149a5202002-12-03 19:40:16 +000048 "Counts the various types of Instructions");
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000049}
50
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000051// InstCount::run - This is the main Analysis entry point for a
52// function.
53//
54bool InstCount::run(Module &M) {
Chris Lattner149a5202002-12-03 19:40:16 +000055 visit(M);
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000056 return false;
57}