-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNoPointerArithmetic.cc
More file actions
186 lines (171 loc) · 6.55 KB
/
NoPointerArithmetic.cc
File metadata and controls
186 lines (171 loc) · 6.55 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
#define DEBUG_TYPE "no-pointer-arithmetic"
#define TESTING 0
#include <fstream>
#include <llvm/IR/Constants.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/Module.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/Debug.h>
#include <llvm/Support/raw_os_ostream.h>
using namespace llvm;
using namespace std;
namespace {
class NoPointerArithmetic : public ModulePass {
public:
// standard LLVM pass interface
NoPointerArithmetic();
static char ID;
void getAnalysisUsage(AnalysisUsage &) const final override;
bool runOnModule(Module &) final override;
void print(raw_ostream &, const Module *) const final override;
};
static const RegisterPass<NoPointerArithmetic> registration("no-pointer-arithmetic",
"Replace pointer arithmetic with GEP instructions",
true, false);
static llvm::cl::opt<std::string>
testOutputName("test-no-pointer-arithmetic",
llvm::cl::Optional,
llvm::cl::value_desc("filename"),
llvm::cl::desc("Filename to write results to for regression tests"));
char NoPointerArithmetic::ID;
inline NoPointerArithmetic::NoPointerArithmetic()
: ModulePass(ID) {
}
void NoPointerArithmetic::getAnalysisUsage(AnalysisUsage &) const {
}
bool NoPointerArithmetic::runOnModule(Module &module) {
bool modified = false;
for (Function &func : module) {
for(Argument &arg : func.getArgumentList()) {
if (arg.getType()->isPointerTy()) {
for (User *user : arg.users()) {
if (PHINode *node = dyn_cast<PHINode>(user)) {
if (node->getNumIncomingValues() != 2) {
DEBUG(dbgs() << "Found something close to pattern with other than 2 args\n");
DEBUG(dbgs() << *node << "\n");
continue;
}
int argument = 0;
int other = 1;
/*Value *toAdd = nullptr;
BinaryOperator *oldAdd = nullptr;*/
if (node->getIncomingValue(0) != &arg) {
argument = 1;
other = 0;
}
/*if (BinaryOperator *binOp = dyn_cast<BinaryOperator>(node->getIncomingValue(other))) {
if (!binOp->getOpcode() != Instruction::Add) {
errs() << "Not matching pattern because the non-argument was not an add\n";
errs() << *node << "\n";
errs() << "\t" << *binOp << "\n";
continue;
}
else {
oldAdd = binOp;
bool foundNode = false;
for (int i = 0; i < 2; i++) {
Value *operand = binOp->getOperand(i);
if (operand == node) {
foundNode = true;
}
else {
toAdd = operand;
}
}
if (!foundNode) {
errs() << "Not matching pattern because the add didn't have the phi in it\n";
errs() << *node << "\n";
continue;
}
}
modified = true;
IntegerType* tInt = Type::getInt64Ty(context);
PHINode *replacement = PHINode::Create(tInt, node->getNumIncomingValues(), "", node);
replacement->addIncoming(ConstantInt::get(tInt, 0), node->getIncomingBlock(argument));
replacement->addIncoming(BinaryOperator::Create(Instruction::Add, replacement, toAdd, "", oldAdd), node->getIncomingBlock(other));
GetElementPtrInst *gep = GetElementPtrInst::Create(&arg, ArrayRef<Value*>(replacement),
"", node->getParent()->getFirstInsertionPt());
errs() << "Replacing " << *node << "\n";
errs() << "With " << *replacement << "\n";
node->replaceAllUsesWith(gep);
}
else*/ if (GetElementPtrInst *gepi = dyn_cast<GetElementPtrInst>(node->getIncomingValue(other))){
DEBUG(dbgs() << "Found a gep!\n");
if (TESTING) gepi->dump();
if (gepi->getPointerOperand() != node) {
DEBUG(dbgs() << "Not matching pattern because the gep didn't have the pointer in it.\n");
DEBUG(dbgs() << *gepi << "\n");
continue;
}
else if (gepi->getNumIndices() != 1) {
DEBUG(dbgs() << "Not matching pattern because there is more than one index.\n");
DEBUG(dbgs() << *gepi << "\n");
continue;
}
else {
DEBUG(dbgs() << *(*gepi->idx_begin())->getType() << "\n");
if (TESTING) node->getParent()->getTerminator()->dump();
modified = true;
Type* tInt = (*gepi->idx_begin())->getType();
DEBUG(dbgs() << *tInt << "\n");
PHINode *addPHI = PHINode::Create(tInt, 2, "", node->getParent()->begin());
if (TESTING) node->getParent()->getTerminator()->dump();
BinaryOperator *add = BinaryOperator::CreateNSW(Instruction::Add, addPHI, *gepi->idx_begin(), "", gepi);
DEBUG(dbgs() << "Add \n");
if (TESTING) {
add->dump();
node->getParent()->getTerminator()->dump();
}
addPHI->addIncoming(ConstantInt::get(tInt, 0), node->getIncomingBlock(argument));
addPHI->addIncoming(add, node->getIncomingBlock(other));
DEBUG(dbgs() << "AddPHI: \n");
if (TESTING) addPHI->dump();
GetElementPtrInst *replacement = GetElementPtrInst::Create(
#if (1000 * LLVM_VERSION_MAJOR + LLVM_VERSION_MINOR) >= 3007
node->getType(),
#endif // LLVM 3.7 or later
&arg, ArrayRef<Value*>(addPHI), "", node->getParent()->getFirstInsertionPt());
DEBUG(dbgs() << "Replacing " << *node << "\n");
DEBUG(dbgs() << "With " << *replacement << "\n");
node->replaceAllUsesWith(replacement);
node->eraseFromParent();
}
//pointer should be node,
//index can be k
}
else {
DEBUG(dbgs() << "Not matching pattern because the non-argument was not a binary operator and not a gep\n");
DEBUG(dbgs() << *node << "\n");
DEBUG(dbgs() << "\t" << *node->getIncomingValue(other) << "\n");
continue;
}
}
}
}
}
}
if (TESTING) {
for (const Function &func : module) {
for (const BasicBlock &block : func) {
block.dump();
}
}
}
if (!testOutputName.empty()) {
std::ofstream out(testOutputName);
llvm::raw_os_ostream sink(out);
print(sink, &module);
sink.flush();
out.close();
}
return modified;
}
void NoPointerArithmetic::print(raw_ostream &out, const Module *module) const {
if (!TESTING) return;
for (const Function &func : *module) {
for (const BasicBlock &block : func) {
out << block;
}
}
}
}