-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstCount.cpp
More file actions
244 lines (222 loc) · 7.34 KB
/
Copy pathInstCount.cpp
File metadata and controls
244 lines (222 loc) · 7.34 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//===-- InstCount.cpp - Collects the count of all instructions ------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This pass collects the count of all instructions and reports them
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/Passes.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstVisitor.h"
#include "llvm/Pass.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Analysis/CFG.h"
#include "llvm/IR/CFG.h"
using namespace llvm;
#define DEBUG_TYPE "instcount"
STATISTIC(TotalInsts , "Number of instructions (of all types)");
STATISTIC(TotalBlocks, "Number of basic blocks");
STATISTIC(BlockLow, "Number of BB's with less than 15 instructions");
STATISTIC(BlockMid, "Number of BB's with instructions between [15, 500]");
STATISTIC(BlockHigh, "Number of BB's with more than 500 instructions");
STATISTIC(TotalFuncs , "Number of non-external functions");
STATISTIC(TotalMemInst, "Number of memory instructions");
STATISTIC(BeginPhi, "# of Phi-nodes at beginning of BB");
STATISTIC(ArgsPhi, "Total arguments to Phi nodes");
STATISTIC(BBNoPhi, "# of BB's with no Phi nodes");
STATISTIC(BB03Phi, "# of BB's with Phi node # in range (0, 3]");
STATISTIC(BBHiPhi, "# of BB's with more than 3 Phi nodes");
STATISTIC(BBNumArgsHi, "# of BB where total args for phi nodes > 5");
STATISTIC(BBNumArgsLo, "# of BB where total args for phi nodes is [1, 5]");
STATISTIC(testUnary, "Unary");
STATISTIC(binaryConstArg, "Binary operations with a constant operand");
STATISTIC(callLargeNumArgs, "# of calls with number of arguments > 4");
STATISTIC(returnInt, "# of calls that return an int");
STATISTIC(oneSuccessor, "# of BB's with 1 successor");
STATISTIC(twoSuccessor, "# of BB's with 2 successors");
STATISTIC(moreSuccessors, "# of BB's with >2 successors");
STATISTIC(onePred, "# of BB's with 1 predecessor");
STATISTIC(twoPred, "# of BB's with 2 predecessors");
STATISTIC(morePreds, "# of BB's with >2 predecessors");
STATISTIC(onePredOneSuc, "# of BB's with 1 predecessor and 1 successor");
STATISTIC(onePredTwoSuc, "# of BB's with 1 predecessor and 2 successors");
STATISTIC(twoPredOneSuc, "# of BB's with 2 predecessors and 1 successor");
STATISTIC(twoEach, "# of BB's with 2 predecessors and successors");
STATISTIC(moreEach, "# of BB's with >2 predecessors and successors");
STATISTIC(NumEdges, "# of edges");
STATISTIC(CriticalCount, "# of critical edges");
STATISTIC(BranchCount, "# of branches");
STATISTIC(numConstOnes, "# of occurrences of constant 1");
STATISTIC(numConstZeroes, "# of occurrences of constant 0");
STATISTIC(const32Bit, "# of occurrences of 32-bit integer constants");
STATISTIC(const64Bit, "# of occurrences of 64-bit integer constants");
STATISTIC(UncondBranches, "# of unconditional branches");
#define HANDLE_INST(N, OPCODE, CLASS) \
STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts");
#include "llvm/IR/Instruction.def"
namespace {
class InstCount : public FunctionPass, public InstVisitor<InstCount> {
friend class InstVisitor<InstCount>;
void visitFunction (Function &F) { ++TotalFuncs; }
void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks;
TerminatorInst* term = BB.getTerminator();
unsigned numSuccessors = term->getNumSuccessors();
for (int i = 0; i < numSuccessors; i++) {
NumEdges++;
if (isCriticalEdge(term, i)) {
CriticalCount++;
}
}
unsigned numPreds = 0;
for (pred_iterator pi = pred_begin(&BB), E = pred_end(&BB); pi != E; ++pi) {
numPreds++;
}
if (numSuccessors == 1) {
oneSuccessor++;
} else if (numSuccessors == 2) {
twoSuccessor++;
} else if (numSuccessors > 2) {
moreSuccessors++;
}
if (numPreds == 1) {
onePred++;
} else if (numPreds == 2) {
twoPred++;
} else if (numPreds > 2) {
morePreds++;
}
if (numPreds == 1 && numSuccessors == 1) {
onePredOneSuc++;
} else if (numPreds == 2 && numSuccessors == 1) {
twoPredOneSuc++;
} else if (numPreds == 1 && numSuccessors == 2) {
onePredTwoSuc++;
} else if (numPreds == 2 && numSuccessors == 2) {
twoEach++;
} else if (numPreds > 2 && numSuccessors > 2) {
moreEach++;
}
unsigned tempCount = 0;
bool isFirst = true;
unsigned phiCount = 0;
unsigned BBArgs = 0;
for (Instruction &I : BB) {
if (auto *bi = dyn_cast<BranchInst>(&I)) {
BranchCount++;
if (bi->isUnconditional()) {
UncondBranches++;
}
}
for (int i = 0 ; i < I.getNumOperands(); i++) {
Value* v = I.getOperand(i);
//Type* t = v->getType();
if (auto *c = dyn_cast<Constant>(v)) {
if (auto *ci = dyn_cast<ConstantInt>(c)) {
APInt val = ci->getValue();
unsigned bitWidth = val.getBitWidth();
if (bitWidth == 32) {
const32Bit++;
} else if (bitWidth == 64) {
const64Bit++;
}
if (val == 1) {
numConstOnes++;
} else if (val == 0) {
numConstZeroes++;
}
}
}
}
if (isa<CallInst>(I)) {
if (cast<CallInst>(I).getNumArgOperands() > 4) {
callLargeNumArgs++;
}
if (cast<CallInst>(I).getCalledFunction()->getReturnType()->isIntegerTy()) {
returnInt++;
}
}
if (isa<UnaryInstruction>(I)){
testUnary++;
}
if (isa<BinaryOperator>(I)) {
if (isa<Constant>(I.getOperand(0)) || isa<Constant>(I.getOperand(1))) {
binaryConstArg++;
}
}
if (isFirst && isa<PHINode>(I)) {
BeginPhi++;
}
if (isa<PHINode>(I)) {
phiCount++;
unsigned inc = cast<PHINode>(I).getNumIncomingValues();
ArgsPhi += inc;
BBArgs += inc;
}
isFirst = false;
tempCount++;
}
if (phiCount == 0) {
BBNoPhi++;
} else if (phiCount <= 3) {
BB03Phi++;
} else {
BBHiPhi++;
}
if (BBArgs > 5) {
BBNumArgsHi++;
} else if (BBArgs>=1) {
BBNumArgsLo++;
}
if (tempCount <15) {
BlockLow++;
} else if (tempCount <= 500) {
BlockMid++;
} else {
BlockHigh++;
}
}
#define HANDLE_INST(N, OPCODE, CLASS) \
void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
#include "llvm/IR/Instruction.def"
void visitInstruction(Instruction &I) {
errs() << "Instruction Count does not know about " << I;
llvm_unreachable(nullptr);
}
public:
static char ID; // Pass identification, replacement for typeid
InstCount() : FunctionPass(ID) {
initializeInstCountPass(*PassRegistry::getPassRegistry());
}
bool runOnFunction(Function &F) override;
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesAll();
}
void print(raw_ostream &O, const Module *M) const override {}
};
}
char InstCount::ID = 0;
INITIALIZE_PASS(InstCount, "instcount",
"Counts the various types of Instructions", false, true)
FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
// InstCount::run - This is the main Analysis entry point for a
// function.
//
bool InstCount::runOnFunction(Function &F) {
unsigned StartMemInsts =
NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
NumInvokeInst + NumAllocaInst;
visit(F);
unsigned EndMemInsts =
NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
NumInvokeInst + NumAllocaInst;
TotalMemInst += EndMemInsts-StartMemInsts;
return false;
}