llvm.org GIT mirror llvm / 149a520
Simplify code git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4886 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 17 years ago
1 changed file(s) with 13 addition(s) and 118 deletion(s). Raw diff Collapse all Expand all
0 //===-- InstCount.cpp - Collects the count of all instructions ------------===//
11 //
22 // This pass collects the count of all instructions and reports them
3 //
43 //
54 //===----------------------------------------------------------------------===//
65
76 #include "llvm/Pass.h"
87 #include "llvm/Module.h"
9 #include "llvm/iMemory.h"
10 #include "llvm/iTerminators.h"
11 #include "llvm/iPHINode.h"
12 #include "llvm/iOther.h"
13 #include "llvm/iOperators.h"
148 #include "llvm/Support/InstVisitor.h"
15 #include "llvm/Support/InstIterator.h"
16 #include "llvm/Support/InstIterator.h"
179 #include "Support/Statistic.h"
18 #include
1910
2011 namespace {
21 static Statistic<> NumReturnInst("instcount","Number of ReturnInsts");
22 static Statistic<> NumBranchInst("instcount", "Number of BranchInsts");
23 static Statistic<> NumPHINode("instcount", "Number of PHINodes");
24 static Statistic<> NumCastInst("instcount", "Number of CastInsts");
25 static Statistic<> NumCallInst("instcount", "Number of CallInsts");
26 static Statistic<> NumMallocInst("instcount", "Number of MallocInsts");
27 static Statistic<> NumAllocaInst("instcount", "Number of AllocaInsts");
28 static Statistic<> NumFreeInst("instcount", "Number of FreeInsts");
29 static Statistic<> NumLoadInst("instcount", "Number of LoadInsts");
30 static Statistic<> NumStoreInst("instcount", "Number of StoreInsts");
31 static Statistic<> NumGetElementPtrInst("instcount",
32 "Number of GetElementPtrInsts");
33
34 static Statistic<> NumSwitchInst("instcount", "Number of SwitchInsts");
35 static Statistic<> NumInvokeInst("instcount", "Number of InvokeInsts");
36 static Statistic<> NumBinaryOperator("instcount",
37 "Total Number of BinaryOperators");
38
39 static Statistic<> NumShiftInst("instcount", " Total Number of ShiftInsts");
40 static Statistic<> NumShlInst("instcount", "Number of Left ShiftInsts");
41
42 static Statistic<> NumShrInst("instcount", "Number of Right ShiftInsts");
43
12 #define HANDLE_INST(N, OPCODE, CLASS) \
13 Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
4414
45 static Statistic<> NumAddInst("instcount", "Number of AddInsts");
46 static Statistic<> NumSubInst("instcount", "Number of SubInsts");
47 static Statistic<> NumMulInst("instcount", "Number of MulInsts");
48 static Statistic<> NumDivInst("instcount", "Number of DivInsts");
49 static Statistic<> NumRemInst("instcount", "Number of RemInsts");
50 static Statistic<> NumAndInst("instcount", "Number of AndInsts");
51 static Statistic<> NumOrInst("instcount", "Number of OrInsts");
52 static Statistic<> NumXorInst("instcount", "Number of XorInsts");
53 static Statistic<> NumSetCondInst("instcount", "Total Number of SetCondInsts");
54 static Statistic<> NumSetEQInst("instcount", "Number of SetEQInsts");
55 static Statistic<> NumSetNEInst("instcount", "Number of SetNEInsts");
56 static Statistic<> NumSetLEInst("instcount", "Number of SetLEInsts");
57 static Statistic<> NumSetGEInst("instcount", "Number of SetGEInsts");
58 static Statistic<> NumSetLTInst("instcount", "Number of SetLTInsts");
59 static Statistic<> NumSetGTInst("instcount", "Number of SetGTInsts");
60
15 #include "llvm/Instruction.def"
16
6117 class InstCount : public Pass, public InstVisitor {
62 private:
63 friend class InstVisitor;
18 friend class InstVisitor;
6419
20 #define HANDLE_INST(N, OPCODE, CLASS) \
21 void visit##OPCODE(CLASS &) { Num##OPCODE##Inst++; }
6522
66 void visitBinaryOperator(BinaryOperator &I);
67 void visitShiftInst(ShiftInst &I);
68 void visitSetCondInst(SetCondInst &I);
69
70 inline void visitSwitchInst(SwitchInst &I) { NumSwitchInst++; }
71 inline void visitInvokeInst(InvokeInst &I) { NumInvokeInst++; }
72 inline void visitReturnInst(ReturnInst &I) { NumReturnInst++; }
73 inline void visitBranchInst(BranchInst &I) { NumBranchInst++; }
74 inline void visitPHINode(PHINode &I) { NumPHINode++; }
75 inline void visitCastInst (CastInst &I) { NumCastInst++; }
76 inline void visitCallInst (CallInst &I) { NumCallInst++; }
77 inline void visitMallocInst(MallocInst &I) { NumMallocInst++; }
78 inline void visitAllocaInst(AllocaInst &I) { NumAllocaInst++; }
79 inline void visitFreeInst (FreeInst &I) { NumFreeInst++; }
80 inline void visitLoadInst (LoadInst &I) { NumLoadInst++; }
81 inline void visitStoreInst (StoreInst &I) { NumStoreInst++; }
82 inline void visitGetElementPtrInst(GetElementPtrInst &I) {
83 NumGetElementPtrInst++; }
23 #include "llvm/Instruction.def"
8424
85 inline void visitInstruction(Instruction &I) {
25 void visitInstruction(Instruction &I) {
8626 std::cerr << "Instruction Count does not know about " << I;
8727 abort();
8828 }
9232 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
9333 AU.setPreservesAll();
9434 }
35 virtual void print(std::ostream &O, Module *M) const {}
36
9537 };
9638
9739 RegisterAnalysis X("instcount",
98 "Counts the various types of Instructions");
99
40 "Counts the various types of Instructions");
10041 }
101
102 // createInstCountPass - The public interface to this file...
103 Pass *createInstCountPass() { return new InstCount(); }
104
10542
10643 // InstCount::run - This is the main Analysis entry point for a
10744 // function.
10845 //
10946 bool InstCount::run(Module &M) {
110 for (Module::iterator mI = M.begin(), mE = M.end(); mI != mE; ++mI)
111 for (inst_iterator I = inst_begin(*mI), E = inst_end(*mI); I != E; ++I)
112 visit(*I);
47 visit(M);
11348 return false;
11449 }
115
116
117
118 void InstCount::visitBinaryOperator(BinaryOperator &I) {
119 NumBinaryOperator++;
120 switch (I.getOpcode()) {
121 case Instruction::Add: NumAddInst++; break;
122 case Instruction::Sub: NumSubInst++; break;
123 case Instruction::Mul: NumMulInst++; break;
124 case Instruction::Div: NumDivInst++; break;
125 case Instruction::Rem: NumRemInst++; break;
126 case Instruction::And: NumAndInst++; break;
127 case Instruction::Or: NumOrInst++; break;
128 case Instruction::Xor: NumXorInst++; break;
129 default : std::cerr<< " Wrong binary operator \n";
130 }
131 }
132
133 void InstCount::visitSetCondInst(SetCondInst &I) {
134 NumBinaryOperator++;
135 NumSetCondInst++;
136 switch (I.getOpcode()) {
137 case Instruction::SetEQ: NumSetEQInst++; break;
138 case Instruction::SetNE: NumSetNEInst++; break;
139 case Instruction::SetLE: NumSetLEInst++; break;
140 case Instruction::SetGE: NumSetGEInst++; break;
141 case Instruction::SetLT: NumSetLTInst++; break;
142 case Instruction::SetGT: NumSetGTInst++; break;
143 default : std::cerr<< " Wrong SetCond Inst \n";
144 }
145 }
146
147 void InstCount::visitShiftInst(ShiftInst &I) {
148 NumShiftInst++;
149 switch (I.getOpcode()) {
150 case Instruction::Shl: NumShlInst++; break;
151 case Instruction::Shr: NumShrInst++; break;
152 default : std::cerr<< " Wrong ShiftInst \n";
153 }
154 }