blob: 4d49478a9e62b291a1fcd8f6afad014d39064eb4 [file] [log] [blame]
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +00001//===-- InstCount.cpp - Collects the count of all instructions ------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +00009//
10// This pass collects the count of all instructions and reports them
11//
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000012//===----------------------------------------------------------------------===//
13
14#include "llvm/Pass.h"
Chris Lattner5f9be672003-08-29 14:43:17 +000015#include "llvm/Function.h"
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000016#include "llvm/Support/InstVisitor.h"
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000017#include "Support/Statistic.h"
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000018
19namespace {
Chris Lattner3935d2b2002-12-07 23:24:24 +000020 Statistic<> TotalInsts ("instcount", "Number of instructions (of all types)");
21 Statistic<> TotalBlocks("instcount", "Number of basic blocks");
22 Statistic<> TotalFuncs ("instcount", "Number of non-external functions");
23
Chris Lattner5e058172002-12-03 19:40:16 +000024#define HANDLE_INST(N, OPCODE, CLASS) \
25 Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000026
Chris Lattner5e058172002-12-03 19:40:16 +000027#include "llvm/Instruction.def"
28
Chris Lattner5f9be672003-08-29 14:43:17 +000029 class InstCount : public FunctionPass, public InstVisitor<InstCount> {
Chris Lattner5e058172002-12-03 19:40:16 +000030 friend class InstVisitor<InstCount>;
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000031
Chris Lattner3935d2b2002-12-07 23:24:24 +000032 void visitFunction (Function &F) { ++TotalFuncs; }
33 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
34
Chris Lattner5e058172002-12-03 19:40:16 +000035#define HANDLE_INST(N, OPCODE, CLASS) \
Chris Lattner3935d2b2002-12-07 23:24:24 +000036 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000037
Chris Lattner5e058172002-12-03 19:40:16 +000038#include "llvm/Instruction.def"
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000039
Chris Lattner5e058172002-12-03 19:40:16 +000040 void visitInstruction(Instruction &I) {
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000041 std::cerr << "Instruction Count does not know about " << I;
42 abort();
43 }
44 public:
Chris Lattner5f9be672003-08-29 14:43:17 +000045 virtual bool runOnFunction(Function &F);
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000046
47 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
48 AU.setPreservesAll();
49 }
Chris Lattner3935d2b2002-12-07 23:24:24 +000050 virtual void print(std::ostream &O, const Module *M) const {}
Chris Lattner5e058172002-12-03 19:40:16 +000051
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000052 };
53
Chris Lattner342f6812002-11-17 22:15:40 +000054 RegisterAnalysis<InstCount> X("instcount",
Chris Lattner5e058172002-12-03 19:40:16 +000055 "Counts the various types of Instructions");
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000056}
57
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000058// InstCount::run - This is the main Analysis entry point for a
59// function.
60//
Chris Lattner5f9be672003-08-29 14:43:17 +000061bool InstCount::runOnFunction(Function &F) {
62 visit(F);
Dinakar Dhurjatia7be9a72002-11-13 18:22:13 +000063 return false;
64}