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