InstCount.cpp revision db85e389ae1d11f004a4b415b87e636037c87e44
1//===-- InstCount.cpp - Collects the count of all instructions ------------===// 2// 3// This pass collects the count of all instructions and reports them 4// 5// 6//===----------------------------------------------------------------------===// 7 8#include "llvm/Pass.h" 9#include "llvm/Module.h" 10#include "llvm/iMemory.h" 11#include "llvm/iTerminators.h" 12#include "llvm/iPHINode.h" 13#include "llvm/iOther.h" 14#include "llvm/iOperators.h" 15#include "llvm/Support/InstVisitor.h" 16#include "llvm/Support/InstIterator.h" 17#include "llvm/Support/InstIterator.h" 18#include "Support/Statistic.h" 19#include <algorithm> 20 21namespace { 22 static Statistic<> NumReturnInst("instcount","Number of ReturnInsts"); 23 static Statistic<> NumBranchInst("instcount", "Number of BranchInsts"); 24 static Statistic<> NumPHINode("instcount", "Number of PHINodes"); 25 static Statistic<> NumCastInst("instcount", "Number of CastInsts"); 26 static Statistic<> NumCallInst("instcount", "Number of CallInsts"); 27 static Statistic<> NumMallocInst("instcount", "Number of MallocInsts"); 28 static Statistic<> NumAllocaInst("instcount", "Number of AllocaInsts"); 29 static Statistic<> NumFreeInst("instcount", "Number of FreeInsts"); 30 static Statistic<> NumLoadInst("instcount", "Number of LoadInsts"); 31 static Statistic<> NumStoreInst("instcount", "Number of StoreInsts"); 32 static Statistic<> NumGetElementPtrInst("instcount", 33 "Number of GetElementPtrInsts"); 34 35 static Statistic<> NumSwitchInst("instcount", "Number of SwitchInsts"); 36 static Statistic<> NumInvokeInst("instcount", "Number of InvokeInsts"); 37 static Statistic<> NumBinaryOperator("instcount", 38 "Total Number of BinaryOperators"); 39 40 static Statistic<> NumShiftInst("instcount", " Total Number of ShiftInsts"); 41 static Statistic<> NumShlInst("instcount", "Number of Left ShiftInsts"); 42 43 static Statistic<> NumShrInst("instcount", "Number of Right ShiftInsts"); 44 45 46 static Statistic<> NumAddInst("instcount", "Number of AddInsts"); 47 static Statistic<> NumSubInst("instcount", "Number of SubInsts"); 48 static Statistic<> NumMulInst("instcount", "Number of MulInsts"); 49 static Statistic<> NumDivInst("instcount", "Number of DivInsts"); 50 static Statistic<> NumRemInst("instcount", "Number of RemInsts"); 51 static Statistic<> NumAndInst("instcount", "Number of AndInsts"); 52 static Statistic<> NumOrInst("instcount", "Number of OrInsts"); 53 static Statistic<> NumXorInst("instcount", "Number of XorInsts"); 54 static Statistic<> NumSetCondInst("instcount", "Total Number of SetCondInsts"); 55 static Statistic<> NumSetEQInst("instcount", "Number of SetEQInsts"); 56 static Statistic<> NumSetNEInst("instcount", "Number of SetNEInsts"); 57 static Statistic<> NumSetLEInst("instcount", "Number of SetLEInsts"); 58 static Statistic<> NumSetGEInst("instcount", "Number of SetGEInsts"); 59 static Statistic<> NumSetLTInst("instcount", "Number of SetLTInsts"); 60 static Statistic<> NumSetGTInst("instcount", "Number of SetGTInsts"); 61 62 class InstCount : public Pass, public InstVisitor<InstCount> { 63 private: 64 friend class InstVisitor<InstCount>; 65 66 67 void visitBinaryOperator(BinaryOperator &I); 68 void visitShiftInst(ShiftInst &I); 69 void visitSetCondInst(SetCondInst &I); 70 71 inline void visitSwitchInst(SwitchInst &I) { NumSwitchInst++; } 72 inline void visitInvokeInst(InvokeInst &I) { NumInvokeInst++; } 73 inline void visitReturnInst(ReturnInst &I) { NumReturnInst++; } 74 inline void visitBranchInst(BranchInst &I) { NumBranchInst++; } 75 inline void visitPHINode(PHINode &I) { NumPHINode++; } 76 inline void visitCastInst (CastInst &I) { NumCastInst++; } 77 inline void visitCallInst (CallInst &I) { NumCallInst++; } 78 inline void visitMallocInst(MallocInst &I) { NumMallocInst++; } 79 inline void visitAllocaInst(AllocaInst &I) { NumAllocaInst++; } 80 inline void visitFreeInst (FreeInst &I) { NumFreeInst++; } 81 inline void visitLoadInst (LoadInst &I) { NumLoadInst++; } 82 inline void visitStoreInst (StoreInst &I) { NumStoreInst++; } 83 inline void visitGetElementPtrInst(GetElementPtrInst &I) { 84 NumGetElementPtrInst++; } 85 86 inline void visitInstruction(Instruction &I) { 87 std::cerr << "Instruction Count does not know about " << I; 88 abort(); 89 } 90 public: 91 virtual bool run(Module &M); 92 93 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 94 AU.setPreservesAll(); 95 } 96 }; 97 98 RegisterAnalysis<InstCount> X("instcount", 99 "Counts the various types of Instructions"); 100 101} 102 103// createInstCountPass - The public interface to this file... 104Pass *createInstCountPass() { return new InstCount(); } 105 106 107// InstCount::run - This is the main Analysis entry point for a 108// function. 109// 110bool InstCount::run(Module &M) { 111 for (Module::iterator mI = M.begin(), mE = M.end(); mI != mE; ++mI) 112 for (inst_iterator I = inst_begin(*mI), E = inst_end(*mI); I != E; ++I) 113 visit(*I); 114 return false; 115} 116 117 118 119void InstCount::visitBinaryOperator(BinaryOperator &I) { 120 NumBinaryOperator++; 121 switch (I.getOpcode()) { 122 case Instruction::Add: NumAddInst++; break; 123 case Instruction::Sub: NumSubInst++; break; 124 case Instruction::Mul: NumMulInst++; break; 125 case Instruction::Div: NumDivInst++; break; 126 case Instruction::Rem: NumRemInst++; break; 127 case Instruction::And: NumAndInst++; break; 128 case Instruction::Or: NumOrInst++; break; 129 case Instruction::Xor: NumXorInst++; break; 130 default : std::cerr<< " Wrong binary operator \n"; 131 } 132} 133 134void InstCount::visitSetCondInst(SetCondInst &I) { 135 NumBinaryOperator++; 136 NumSetCondInst++; 137 switch (I.getOpcode()) { 138 case Instruction::SetEQ: NumSetEQInst++; break; 139 case Instruction::SetNE: NumSetNEInst++; break; 140 case Instruction::SetLE: NumSetLEInst++; break; 141 case Instruction::SetGE: NumSetGEInst++; break; 142 case Instruction::SetLT: NumSetLTInst++; break; 143 case Instruction::SetGT: NumSetGTInst++; break; 144 default : std::cerr<< " Wrong SetCond Inst \n"; 145 } 146} 147 148void InstCount::visitShiftInst(ShiftInst &I) { 149 NumShiftInst++; 150 switch (I.getOpcode()) { 151 case Instruction::Shl: NumShlInst++; break; 152 case Instruction::Shr: NumShrInst++; break; 153 default : std::cerr<< " Wrong ShiftInst \n"; 154 } 155} 156