Reverting back to original 1.8 version so I can manually merge in patch.
[llvm-complete.git] / lib / Analysis / InstCount.cpp
blobdf11fc4895aa2ce4149fb3415fc02685dbc3daf2
1 //===-- InstCount.cpp - Collects the count of all instructions ------------===//
2 //
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 //===----------------------------------------------------------------------===//
9 //
10 // This pass collects the count of all instructions and reports them
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/Passes.h"
15 #include "llvm/Pass.h"
16 #include "llvm/Function.h"
17 #include "llvm/Support/InstVisitor.h"
18 #include "llvm/ADT/Statistic.h"
19 #include <iostream>
20 using namespace llvm;
22 namespace {
23 Statistic<> TotalInsts ("instcount", "Number of instructions (of all types)");
24 Statistic<> TotalBlocks("instcount", "Number of basic blocks");
25 Statistic<> TotalFuncs ("instcount", "Number of non-external functions");
26 Statistic<> TotalMemInst("instcount", "Number of memory instructions");
28 #define HANDLE_INST(N, OPCODE, CLASS) \
29 Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
31 #include "llvm/Instruction.def"
33 class InstCount : public FunctionPass, public InstVisitor<InstCount> {
34 friend class InstVisitor<InstCount>;
36 void visitFunction (Function &F) { ++TotalFuncs; }
37 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
39 #define HANDLE_INST(N, OPCODE, CLASS) \
40 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
42 #include "llvm/Instruction.def"
44 void visitInstruction(Instruction &I) {
45 std::cerr << "Instruction Count does not know about " << I;
46 abort();
48 public:
49 virtual bool runOnFunction(Function &F);
51 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.setPreservesAll();
54 virtual void print(std::ostream &O, const Module *M) const {}
58 RegisterAnalysis<InstCount> X("instcount",
59 "Counts the various types of Instructions");
62 FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
64 // InstCount::run - This is the main Analysis entry point for a
65 // function.
67 bool InstCount::runOnFunction(Function &F) {
68 unsigned StartMemInsts =
69 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
70 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
71 visit(F);
72 unsigned EndMemInsts =
73 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
74 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
75 TotalMemInst += EndMemInsts-StartMemInsts;
76 return false;