llvm.org GIT mirror llvm / e555460
Gives the count for various instructions. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4706 91177308-0d34-0410-b5e6-96231b3b80d8 Dinakar Dhurjati 17 years ago
1 changed file(s) with 189 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===-- InstCount.cpp - Collects the count of all instructions ------------===//
1 //
2 // This pass collects the count of all instructions and reports them
3 //
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Pass.h"
8 #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"
14 #include "llvm/Support/InstVisitor.h"
15 #include "llvm/Support/InstIterator.h"
16 #include "llvm/Support/InstIterator.h"
17 #include "Support/Statistic.h"
18 #include
19
20 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
44
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
61 class InstCount : public Pass, public InstVisitor {
62 private:
63 friend class InstVisitor;
64
65
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) { NumCastInst++; }
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++; }
84
85 inline void visitInstruction(Instruction &I) {
86 std::cerr << "Instruction Count does not know about " << I;
87 abort();
88 }
89 public:
90 virtual bool run(Module &M);
91
92 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
93 AU.setPreservesAll();
94 }
95 };
96
97 RegisterOpt X("instcount",
98 "Counts the various types of Instructions");
99
100 }
101
102 // createInstCountPass - The public interface to this file...
103 Pass *createInstCountPass() { return new InstCount(); }
104
105
106 // InstCount::run - This is the main Analysis entry point for a
107 // function.
108 //
109 bool InstCount::run(Module &M) {
110 /* Initialization */
111 NumReturnInst = 0;
112 NumBranchInst = 0;
113 NumPHINode = 0;
114 NumCastInst = 0;
115 NumCallInst = 0;
116 NumMallocInst = 0;
117 NumAllocaInst = 0;
118 NumFreeInst = 0;
119 NumLoadInst = 0;
120 NumStoreInst = 0;
121 NumGetElementPtrInst = 0;
122 NumSwitchInst = 0;
123 NumInvokeInst = 0;
124 NumBinaryOperator = 0;
125 NumShiftInst = 0;
126 NumShlInst = 0;
127 NumShrInst = 0;
128 NumAddInst = 0;
129 NumSubInst = 0;
130 NumMulInst = 0;
131 NumDivInst = 0;
132 NumRemInst = 0;
133 NumAndInst = 0;
134 NumOrInst = 0;
135 NumXorInst = 0;
136 NumSetCondInst = 0;
137 NumSetEQInst = 0;
138 NumSetNEInst = 0;
139 NumSetLEInst = 0;
140 NumSetGEInst = 0;
141 NumSetLTInst = 0;
142 NumSetGTInst = 0;
143
144 for (Module::iterator mI = M.begin(), mE = M.end(); mI != mE; ++mI)
145 for (inst_iterator I = inst_begin(*mI), E = inst_end(*mI); I != E; ++I)
146 visit(*I);
147 return false;
148 }
149
150
151
152 void InstCount::visitBinaryOperator(BinaryOperator &I) {
153 NumBinaryOperator++;
154 switch (I.getOpcode()) {
155 case Instruction::Add: NumAddInst++; break;
156 case Instruction::Sub: NumSubInst++; break;
157 case Instruction::Mul: NumMulInst++; break;
158 case Instruction::Div: NumDivInst++; break;
159 case Instruction::Rem: NumRemInst++; break;
160 case Instruction::And: NumAndInst++; break;
161 case Instruction::Or: NumOrInst++; break;
162 case Instruction::Xor: NumXorInst++; break;
163 default : std::cerr<< " Wrong binary operator \n";
164 }
165 }
166
167 void InstCount::visitSetCondInst(SetCondInst &I) {
168 NumBinaryOperator++;
169 NumSetCondInst++;
170 switch (I.getOpcode()) {
171 case Instruction::SetEQ: NumSetEQInst++; break;
172 case Instruction::SetNE: NumSetNEInst++; break;
173 case Instruction::SetLE: NumSetLEInst++; break;
174 case Instruction::SetGE: NumSetGEInst++; break;
175 case Instruction::SetLT: NumSetLTInst++; break;
176 case Instruction::SetGT: NumSetGTInst++; break;
177 default : std::cerr<< " Wrong SetCond Inst \n";
178 }
179 }
180
181 void InstCount::visitShiftInst(ShiftInst &I) {
182 NumShiftInst++;
183 switch (I.getOpcode()) {
184 case Instruction::Shl: NumShlInst++; break;
185 case Instruction::Shr: NumShrInst++; break;
186 default : std::cerr<< " Wrong ShiftInst \n";
187 }
188 }