forked from aqjune/llvmscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instcounter.cpp
156 lines (136 loc) · 4.31 KB
/
instcounter.cpp
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
// http://stackoverflow.com/questions/30195204/how-to-parse-llvm-ir-line-by-line
// http://llvm.org/docs/doxygen/html/InstCount_8cpp_source.html
#include <iostream>
#include <string>
#include <sstream>
#include <set>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/ErrorOr.h>
#include <llvm/Pass.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/InstVisitor.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/Operator.h>
#include <llvm/Bitcode/BitcodeReader.h>
#include <llvm/Support/raw_ostream.h>
using namespace llvm;
namespace{
class InstCountPass : public FunctionPass, public InstVisitor<InstCountPass> {
friend class InstVisitor<InstCountPass>;
void visitFunction(Function &F) {
++TotalFuncs;
}
void visitBasicBlock(BasicBlock &BB) {
++TotalBlocks;
}
#define HANDLE_INST(N, OPCODE, CLASS) \
void visit##OPCODE(CLASS &I) { \
std::string str = ""#OPCODE; \
std::string str2 = str; \
std::transform(str.begin(), str.end(), str2.begin(), ::tolower); \
++NumInst[str2]; ++TotalInsts; \
countIntrinsics(&I); visitConstExpr(&I); \
}
#include <llvm/IR/Instruction.def>
void visitInstruction(Instruction &I) {
errs() << "Instruction Count does not know about " << I;
llvm_unreachable(nullptr);
}
void countIntrinsics(Instruction *I) {
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
NumIntrinsics[II->getCalledFunction()->getName()]++;
}
}
void visitConstExpr(User *U) {
if (isa<ConstantExpr>(U)) {
ConstantExpr *CE = dyn_cast<ConstantExpr>(U);
if (Visited.find(CE) != Visited.end()) return;
Visited.insert(CE);
NumConstExpr[Instruction::getOpcodeName(CE->getOpcode())]++;
}
for (auto I = U->op_begin(); I != U->op_end(); ++I) {
Value *V = *I;
if (!isa<ConstantExpr>(V)) continue;
visitConstExpr(dyn_cast<ConstantExpr>(V));
}
}
public:
static char ID;
InstCountPass():FunctionPass(ID) { }
virtual bool runOnFunction(Function &F);
int TotalInsts = 0;
int TotalFuncs = 0;
int TotalBlocks = 0;
std::set<ConstantExpr *> Visited;
std::map<std::string, int> NumInst;
std::map<std::string, int> NumConstExpr;
std::map<std::string, int> NumIntrinsics;
};
bool InstCountPass::runOnFunction(Function &F) {
visit(F);
return false;
}
}
char InstCountPass::ID = 0;
static RegisterPass<InstCountPass> X("hello", "Hello World Pass",
false /* Only looks at CFG */,
false /* Analysis Pass */);
void printMapAsJson(const std::map<std::string, int> &m, std::stringstream &ss) {
bool first = true;
for (auto itr = m.begin(); itr != m.end(); itr++) {
if (!first)
ss << ",\n";
ss << "\t\t\"" << itr->first << "\":" << itr->second;
first = false;
}
}
int main(int argc, char *argv[]){
if (argc != 2 && argc != 3) {
errs() << "Usage : " << argv[0] << " <.bc file>" << "\n";
return 1;
}
StringRef filename = argv[1];
LLVMContext context;
ErrorOr<std::unique_ptr<MemoryBuffer>> fileOrErr =
MemoryBuffer::getFileOrSTDIN(filename);
if (std::error_code ec = fileOrErr.getError()) {
errs() << "Error opening input file: " << ec.message() << "\n";
return 2;
}
ErrorOr<Expected<std::unique_ptr<llvm::Module>>> moduleOrErr =
parseBitcodeFile(fileOrErr.get()->getMemBufferRef(), context);
if (std::error_code ec = moduleOrErr.getError()) {
errs() << "Error reading module : " << ec.message() << "\n";
return 3;
}
Expected<std::unique_ptr<llvm::Module>> moduleExpct = std::move(moduleOrErr.get());
std::unique_ptr<Module> m;
if (moduleExpct) {
m = std::move(moduleExpct.get());
} else {
errs() << "Error reading module\n";
return 3;
}
InstCountPass *ip = new InstCountPass();
for (auto fitr = m->getFunctionList().begin();
fitr != m->getFunctionList().end(); fitr++) {
Function &f = *fitr;
ip->runOnFunction(f);
}
std::stringstream ss;
ss << "{\n";
ss << "\t\"total\":" << ip->TotalInsts << ",\n";
ss << "\t\"instrs\": {\n";
printMapAsJson(ip->NumInst, ss);
ss << "\n\t},\n";
ss << "\t\"intrinsics\": {\n";
printMapAsJson(ip->NumIntrinsics, ss);
ss << "\n\t},\n";
ss << "\t\"constexprs\": {\n";
printMapAsJson(ip->NumConstExpr, ss);
ss << "\n\t}\n";
ss << "}";
std::cout << ss.str();
return 0;
}