Add explicit iostream #includes
[oota-llvm.git] / lib / Analysis / InstCount.cpp
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
11 //
12 //===----------------------------------------------------------------------===//
13
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;
21
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");
27
28 #define HANDLE_INST(N, OPCODE, CLASS) \
29     Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
30
31 #include "llvm/Instruction.def"
32
33   class InstCount : public FunctionPass, public InstVisitor<InstCount> {
34     friend class InstVisitor<InstCount>;
35
36     void visitFunction  (Function &F) { ++TotalFuncs; }
37     void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
38
39 #define HANDLE_INST(N, OPCODE, CLASS) \
40     void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
41
42 #include "llvm/Instruction.def"
43
44     void visitInstruction(Instruction &I) {
45       std::cerr << "Instruction Count does not know about " << I;
46       abort();
47     }
48   public:
49     virtual bool runOnFunction(Function &F);
50
51     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52       AU.setPreservesAll();
53     }
54     virtual void print(std::ostream &O, const Module *M) const {}
55
56   };
57
58   RegisterAnalysis<InstCount> X("instcount",
59                                 "Counts the various types of Instructions");
60 }
61
62 FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
63
64 // InstCount::run - This is the main Analysis entry point for a
65 // function.
66 //
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;
77 }