blob: 0d84cc8c4fda3649c556cd79d027c83cb216ede6 [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"
8#include "llvm/Module.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 Lattner5e058172002-12-03 19:40:16 +000013#define HANDLE_INST(N, OPCODE, CLASS) \
14 Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000015
Chris Lattner5e058172002-12-03 19:40:16 +000016#include "llvm/Instruction.def"
17
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000018 class InstCount : public Pass, public InstVisitor<InstCount> {
Chris Lattner5e058172002-12-03 19:40:16 +000019 friend class InstVisitor<InstCount>;
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000020
Chris Lattner5e058172002-12-03 19:40:16 +000021#define HANDLE_INST(N, OPCODE, CLASS) \
22 void visit##OPCODE(CLASS &) { Num##OPCODE##Inst++; }
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000023
Chris Lattner5e058172002-12-03 19:40:16 +000024#include "llvm/Instruction.def"
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000025
Chris Lattner5e058172002-12-03 19:40:16 +000026 void visitInstruction(Instruction &I) {
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000027 std::cerr << "Instruction Count does not know about " << I;
28 abort();
29 }
30 public:
31 virtual bool run(Module &M);
32
33 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
34 AU.setPreservesAll();
35 }
Chris Lattner5e058172002-12-03 19:40:16 +000036 virtual void print(std::ostream &O, Module *M) const {}
37
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000038 };
39
Chris Lattner342f6812002-11-17 22:15:40 +000040 RegisterAnalysis<InstCount> X("instcount",
Chris Lattner5e058172002-12-03 19:40:16 +000041 "Counts the various types of Instructions");
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000042}
43
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000044// InstCount::run - This is the main Analysis entry point for a
45// function.
46//
47bool InstCount::run(Module &M) {
Chris Lattner5e058172002-12-03 19:40:16 +000048 visit(M);
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000049 return false;
50}