-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranform_pass.cpp
More file actions
122 lines (103 loc) · 3.53 KB
/
Copy pathtranform_pass.cpp
File metadata and controls
122 lines (103 loc) · 3.53 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
// #include "llvm/ADT/SmallPtrSet.h"
// #include "/home/c310/ipaco/llvm-project/llvm/include/llvm/ADT/SmallPtrSet.h"
// #include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
#include <bits/stdc++.h>
#include <map>
#include <queue>
#include <vector>
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/IRPrintingPasses.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Pass.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
#include <fstream>
using namespace llvm;
namespace {
struct ConstantAdditionPass : public FunctionPass {
static char ID;
std::ofstream OutFile;
ConstantAdditionPass() : FunctionPass(ID) {}
bool init_fileopen()
{
}
bool runOnFunction(Function &F) override {
bool changed = false;
APInt value(64, 100); // 64-bit APInt with value 100
ConstantInt *newConst = ConstantInt::get(F.getContext(), value);
for (auto &BB : F) {
std::error_code EC;
raw_fd_ostream OutFile("output.ll", EC, sys::fs::OF_Text);
if (EC) {
errs() << "Failed to open output file: " << EC.message() << "\n";
return false;
}
for (Instruction &I : BB) {
errs() << "Hello"
<< "\n";
OutFile << I << "\n";
if (auto *retInst = dyn_cast<ReturnInst>(&I)) {
errs() << "This is an return stmt"
<< "\n";
// int val = 100;
APInt val_to_replace(32, 100);
IRBuilder<> Builder(retInst);
APInt val_apint(64, 109);
int val = val_apint.getLimitedValue();
Value *newReturnValue =
ConstantInt::get(Type::getInt32Ty(F.getContext()), val);
retInst->setOperand(0, newReturnValue);
errs() << "New Inst : " << I << "\n";
// retInst->eraseFromParent();
errs() << "The line was errased "
<< "\n";
changed = true;
OutFile << I << "\n";
} else if (auto *binaryOP = dyn_cast<BinaryOperator>(&I)) {
if (binaryOP->getOpcode() == Instruction::Add) {
errs() << "This is an add instr. "
<< "\n";
errs() << "instr before : " << I << "\n";
APInt val_to_get(64, 69);
IRBuilder<> Builder(binaryOP);
int val_in_int = val_to_get.getLimitedValue();
Value *newVal =
ConstantInt::get(Type::getInt32Ty(F.getContext()), val_in_int);
binaryOP->setOperand(0, newVal);
errs() << "Instr. after : " << I << "\n";
OutFile<<I<<"\n";
changed = true;
// Write the modified LLVM IR to a new file
}
}
}
}
OutFile.close();
return changed;
}
};
} // namespace
char ConstantAdditionPass::ID = 0;
static RegisterPass<ConstantAdditionPass>
X("HelloWorld",
"Replace add instructions with the computed sum of their operands");
// Command to run the pass: opt -S -load path_to_your_pass.so -constant-addition
// <input.ll> -o <output.ll>