-
Notifications
You must be signed in to change notification settings - Fork 1
/
memcpy-find.cc
135 lines (117 loc) · 4.23 KB
/
memcpy-find.cc
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
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Debug.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Bitcode/BitcodeReader.h"
#include <iostream>
using namespace llvm;
using namespace std;
void print_all(vector<tuple<CallInst*, uint64_t, Function*>> &memcpys) {
for (auto& i : memcpys) {
auto callInst = get<0>(i);
auto size = get<1>(i);
auto function = get<2>(i);
cout << "memcpy" << " of " << size << " in " << function->getName().data() << " @ " << std::endl;
MDNode* metadata = callInst->getMetadata("dbg");
if (!metadata) {
cout << " no debug info" << endl;
continue;
}
DILocation *debugLocation = dyn_cast<DILocation>(metadata);
while (debugLocation) {
DILocalScope *scope = debugLocation->getScope();
cout << " ";
if (scope) {
DISubprogram *subprogram = scope->getSubprogram();
if (subprogram) {
const char* name = subprogram->getName().data();
cout << name << " ";
}
}
cout << debugLocation->getFilename().data() << ":" << debugLocation->getLine() << std::endl;
debugLocation = debugLocation->getInlinedAt();
}
}
}
void print_summary(vector<tuple<CallInst*, uint64_t, Function*>> &memcpys) {
for (auto& i : memcpys) {
auto callInst = get<0>(i);
auto size = get<1>(i);
auto function = get<2>(i);
cout << "memcpy" << " of " << size << std::endl;
}
}
int main(int argc, char **argv) {
LLVMContext context;
SMDiagnostic Err;
const char *fileName;
if (argc > 1) {
fileName = argv[1];
} else {
printf("missing ir filename\n");
abort();
}
bool summary = false;
if (argc > 2) {
if (std::string(argv[2]) == "summary") {
summary = true;
}
}
Expected<std::unique_ptr<Module> > m = parseIRFile(fileName, Err, context);
if (!m) {
errs() << toString(m.takeError()) << "\n";
}
vector<tuple<CallInst*, uint64_t, Function*>> memcpys;
{
auto &functionList = m->get()->getFunctionList();
for (auto &function : functionList) {
//printf("%s\n", function.getName().data());
for (auto &bb : function) {
for (auto &instruction : bb) {
//printf(" %s\n", instruction.getOpcodeName());
CallInst *callInst = dyn_cast<CallInst>(&instruction);
if (callInst == nullptr) {
continue;
}
//printf("have call\n");
Function *calledFunction = callInst->getCalledFunction();
if (calledFunction == nullptr) {
//printf("no calledFunction\n");
continue;
}
StringRef cfName = calledFunction->getName();
if (cfName.find("llvm.memcpy") != std::string::npos) {
auto size_operand = callInst->getOperand(2);
auto size_constant = dyn_cast<ConstantInt>(size_operand);
if (!size_constant) {
//printf("not constant\n");
continue;
}
auto size = size_constant->getValue().getLimitedValue();
memcpys.push_back({callInst, size, &function});
}
}
}
}
}
std::sort(memcpys.begin(), memcpys.end(), [](auto& x, auto &y) {
if (get<1>(y) == get<1>(x)) {
return get<2>(x) > get<2>(y);
} else {
return get<1>(x) > get<1>(y);
}
});
if (summary) {
print_summary(memcpys);
} else {
print_all(memcpys);
}
}