blob: 9177e4480004704a30f9f85376c0271128f87544 [file] [log] [blame]
Dinakar Dhurjatie5554602002-11-13 18:22:13 +00001//===-- InstCount.cpp - Collects the count of all instructions ------------===//
John Criswellb576c942003-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 Dhurjatie5554602002-11-13 18:22:13 +00009//
10// This pass collects the count of all instructions and reports them
11//
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000012//===----------------------------------------------------------------------===//
13
14#include "llvm/Pass.h"
Chris Lattner6666a042003-08-29 14:43:17 +000015#include "llvm/Function.h"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000016#include "llvm/Support/InstVisitor.h"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000017#include "Support/Statistic.h"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000018
Brian Gaeked0fde302003-11-11 22:41:34 +000019namespace llvm {
20
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");
25
Chris Lattner149a5202002-12-03 19:40:16 +000026#define HANDLE_INST(N, OPCODE, CLASS) \
27 Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000028
Chris Lattner149a5202002-12-03 19:40:16 +000029#include "llvm/Instruction.def"
30
Chris Lattner6666a042003-08-29 14:43:17 +000031 class InstCount : public FunctionPass, public InstVisitor<InstCount> {
Chris Lattner149a5202002-12-03 19:40:16 +000032 friend class InstVisitor<InstCount>;
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000033
Chris Lattnera1af8bd2002-12-07 23:24:24 +000034 void visitFunction (Function &F) { ++TotalFuncs; }
35 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
36
Chris Lattner149a5202002-12-03 19:40:16 +000037#define HANDLE_INST(N, OPCODE, CLASS) \
Chris Lattnera1af8bd2002-12-07 23:24:24 +000038 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000039
Chris Lattner149a5202002-12-03 19:40:16 +000040#include "llvm/Instruction.def"
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000041
Chris Lattner149a5202002-12-03 19:40:16 +000042 void visitInstruction(Instruction &I) {
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000043 std::cerr << "Instruction Count does not know about " << I;
44 abort();
45 }
46 public:
Chris Lattner6666a042003-08-29 14:43:17 +000047 virtual bool runOnFunction(Function &F);
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000048
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
50 AU.setPreservesAll();
51 }
Chris Lattnera1af8bd2002-12-07 23:24:24 +000052 virtual void print(std::ostream &O, const Module *M) const {}
Chris Lattner149a5202002-12-03 19:40:16 +000053
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000054 };
55
Chris Lattnerdb85e382002-11-17 22:15:40 +000056 RegisterAnalysis<InstCount> X("instcount",
Chris Lattner149a5202002-12-03 19:40:16 +000057 "Counts the various types of Instructions");
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000058}
59
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000060// InstCount::run - This is the main Analysis entry point for a
61// function.
62//
Chris Lattner6666a042003-08-29 14:43:17 +000063bool InstCount::runOnFunction(Function &F) {
64 visit(F);
Dinakar Dhurjatie5554602002-11-13 18:22:13 +000065 return false;
66}
Brian Gaeked0fde302003-11-11 22:41:34 +000067
68} // End llvm namespace