Skip to content

Commit efcfb49

Browse files
committed
test run
1 parent 1b84cd0 commit efcfb49

File tree

1 file changed

+25
-47
lines changed

1 file changed

+25
-47
lines changed

lab6/llvm-pass.so.cc

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,49 @@
22
#include "llvm/Passes/PassBuilder.h"
33
#include "llvm/IR/IRBuilder.h"
44
using namespace llvm;
5-
65
struct LLVMPass : public PassInfoMixin<LLVMPass> {
76
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
87
};
9-
108
PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) {
119
LLVMContext &Ctx = M.getContext();
1210
IntegerType *Int32Ty = IntegerType::getInt32Ty(Ctx);
1311

14-
// Get or insert debug function declaration
1512
FunctionType *debugTy = FunctionType::get(Type::getVoidTy(Ctx), {Int32Ty}, false);
16-
FunctionCallee debugFunc = M.getOrInsertFunction("debug", debugTy);
17-
ConstantInt *debugArg = ConstantInt::get(Int32Ty, 48763);
13+
FunctionCallee debug_func = M.getOrInsertFunction("debug", debugTy);
14+
ConstantInt *debug_arg = ConstantInt::get(Int32Ty, 48763);
1815

1916
for (auto &F : M) {
20-
if (F.getName() != "main" || F.isDeclaration())
21-
continue;
22-
23-
errs() << "instrumenting main\n";
24-
25-
IRBuilder<> Builder(&*F.getEntryBlock().getFirstInsertionPt());
17+
//errs() << "func: " << F.getName() << "\n";
18+
if(F.getName() != "main" || F.isDeclaration())
19+
continue;
2620

27-
// Call debug(48763)
28-
Builder.CreateCall(debugFunc, {debugArg});
21+
errs() << "func: " << F.getName() << "\n";
2922

30-
// Get arguments
3123
auto ArgIter = F.arg_begin();
3224
Argument *argcArg = &*ArgIter++;
3325
Argument *argvArg = &*ArgIter;
3426

35-
// Allocate space and store 48763 to replace argc
36-
AllocaInst *argcAlloca = Builder.CreateAlloca(Int32Ty, nullptr, "argcVar");
37-
Builder.CreateStore(debugArg, argcAlloca);
27+
Instruction *InsertPt = &*F.getEntryBlock().getFirstInsertionPt();
28+
IRBuilder<> Builder(InsertPt);
3829

39-
// Replace uses of argcArg with loaded value
40-
for (auto &BB : F) {
41-
for (auto &I : BB) {
42-
for (unsigned i = 0; i < I.getNumOperands(); ++i) {
43-
if (I.getOperand(i) == argcArg) {
44-
// Load from new variable instead of using original argc
45-
IRBuilder<> B(&I);
46-
Value *loadedArgc = B.CreateLoad(Int32Ty, argcAlloca);
47-
I.setOperand(i, loadedArgc);
48-
}
49-
}
50-
}
30+
Builder.CreateCall(debug_func, {debug_arg});
31+
32+
for(auto &BB : F){
33+
for(auto &I : BB){
34+
for(unsigned i = 0; i < I.getNumOperands(); ++i){
35+
if(I.getOperand(i) == argcArg){
36+
I.setOperand(i, debug_arg);
37+
}
38+
}
39+
}
5140
}
5241

53-
// Replace argv[1] = "hayaku... motohayaku!"
54-
Value *index1 = ConstantInt::get(Int32Ty, 1);
55-
Value *argv1Ptr = Builder.CreateInBoundsGEP(argvArg->getType()->getPointerElementType(), argvArg, index1);
56-
Value *hayakuStr = Builder.CreateGlobalStringPtr("hayaku... motohayaku!");
57-
Builder.CreateStore(hayakuStr, argv1Ptr);
58-
}
42+
Value *idx1 = ConstantInt::get(Int32Ty, 1);
43+
Value *argv1Ptr = Builder.CreateInBoundsGEP(argvArg->getType()->getPointerElementType(), argvArg, idx1);
44+
Value *strPtr = Builder.CreateGlobalStringPtr("hayaku... motohayaku!");
45+
Builder.CreateStore(strPtr, argv1Ptr);
5946

60-
return PreservedAnalyses::none();
61-
}
47+
//Builder.CreateStore(debug_arg, argcArg);
6248

63-
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
64-
llvmGetPassPluginInfo() {
65-
return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0",
66-
[](PassBuilder &PB) {
67-
PB.registerOptimizerLastEPCallback(
68-
[](ModulePassManager &MPM, OptimizationLevel OL) {
69-
MPM.addPass(LLVMPass());
70-
});
71-
}};
72-
}
49+
}
50+
return PreservedAnalyses::none();

0 commit comments

Comments
 (0)