2
2
#include " llvm/Passes/PassBuilder.h"
3
3
#include " llvm/IR/IRBuilder.h"
4
4
using namespace llvm ;
5
-
6
5
struct LLVMPass : public PassInfoMixin <LLVMPass> {
7
6
PreservedAnalyses run (Module &M, ModuleAnalysisManager &MAM);
8
7
};
9
-
10
8
PreservedAnalyses LLVMPass::run (Module &M, ModuleAnalysisManager &MAM) {
11
9
LLVMContext &Ctx = M.getContext ();
12
10
IntegerType *Int32Ty = IntegerType::getInt32Ty (Ctx);
13
11
14
- // Get or insert debug function declaration
15
12
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 );
18
15
19
16
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 ;
26
20
27
- // Call debug(48763)
28
- Builder.CreateCall (debugFunc, {debugArg});
21
+ errs () << " func: " << F.getName () << " \n " ;
29
22
30
- // Get arguments
31
23
auto ArgIter = F.arg_begin ();
32
24
Argument *argcArg = &*ArgIter++;
33
25
Argument *argvArg = &*ArgIter;
34
26
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);
38
29
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
+ }
51
40
}
52
41
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);
59
46
60
- return PreservedAnalyses::none ();
61
- }
47
+ // Builder.CreateStore(debug_arg, argcArg);
62
48
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