-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
107 lines (96 loc) · 3.23 KB
/
main.cpp
File metadata and controls
107 lines (96 loc) · 3.23 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
#include "main.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/User.h"
#include "llvm/Pass.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/PassPlugin.h"
#include <iostream>
#include <iterator>
#include <map>
#include <utility>
using namespace llvm;
PreservedAnalyses itf::run(Module &M, ModuleAnalysisManager &MAM) {
int i = 0;
std::set<Function *> FtoMap;
for (Function &F : M.getFunctionList())
FtoMap.insert(&F);
for (Function *F : FtoMap) {
for (Instruction &I : instructions(F)) {
if (isa<BinaryOperator>(I)) {
typedef std::vector<std::pair<int, int>> ItoJ;
DenseMap<Instruction *, ItoJ>
mps; // On instruction I, substitute operator i with function
// argument j
Instruction *newI = I.clone();
// Make Function
std::vector<Value *> vFArgsTypes;
std::set<Value *> lookvFArgsTypes;
int posK = 0;
for (int i = 0; i < newI->getNumOperands(); ++i) {
auto X = newI->getOperand(i);
if (!isa<Constant>(X)) {
if (!lookvFArgsTypes.count(X)) {
lookvFArgsTypes.insert(X);
vFArgsTypes.push_back(X);
int posNew = vFArgsTypes.size() - 1;
auto itM = mps.insert({newI, ItoJ()});
itM.first->second.push_back({i, posNew});
} else {
auto K = mps.find(newI);
int posK = 0;
for (auto &p : vFArgsTypes) {
if (p == X)
break;
posK++;
}
K->second.push_back({i, posK});
}
}
}
SmallVector<Type *> v;
for (auto &p : vFArgsTypes)
v.push_back(p->getType());
FunctionType *fType = FunctionType::get(I.getType(), v, false);
Function *newF = Function::Create(fType, Function::ExternalLinkage,
I.getName().str(), M);
auto T = new Argument(newI->getType(), "", newF);
for (auto &e : mps) {
for (auto &k : e.second) {
e.first->setOperand(k.first, newF->getArg(k.second));
}
}
// Make BasicBlock and add Inst
BasicBlock *BB = BasicBlock::Create(M.getContext(), "BB", newF);
BB->getInstList().push_front(newI);
// Return type to Function
ReturnInst::Create(M.getContext(), newI, BB);
dbgs() << *newF;
}
}
}
return PreservedAnalyses::all();
}
bool registerPipeline(StringRef Name, ModulePassManager &MPM,
ArrayRef<PassBuilder::PipelineElement>) {
if (Name == "itf") {
MPM.addPass(itf());
return true;
}
return false;
}
PassPluginLibraryInfo instToFuncPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, "instToFunc", LLVM_VERSION_STRING,
[](PassBuilder &PB) {
PB.registerPipelineParsingCallback(registerPipeline);
}};
}
extern "C" LLVM_ATTRIBUTE_WEAK PassPluginLibraryInfo llvmGetPassPluginInfo() {
return instToFuncPluginInfo();
}