11#include " llvm/Passes/PassPlugin.h"
22#include " llvm/Passes/PassBuilder.h"
33#include " llvm/IR/IRBuilder.h"
4- #include " llvm/IR/Constants.h"
5- #include " llvm/IR/GlobalVariable.h"
64
75using namespace llvm ;
86
@@ -13,38 +11,13 @@ struct LLVMPass : public PassInfoMixin<LLVMPass> {
1311PreservedAnalyses LLVMPass::run (Module &M, ModuleAnalysisManager &MAM) {
1412 LLVMContext &Ctx = M.getContext ();
1513 IntegerType *Int32Ty = IntegerType::getInt32Ty (Ctx);
16- PointerType *Int8PtrTy = Type::getInt8PtrTy (Ctx);
17-
18- FunctionType *DebugTy = FunctionType::get (Type::getVoidTy (Ctx), {Int32Ty}, false );
19- FunctionCallee debug_func = M.getOrInsertFunction (" debug" , DebugTy);
14+ FunctionCallee debug_func = M.getOrInsertFunction (" debug" , Int32Ty);
2015 ConstantInt *debug_arg = ConstantInt::get (Int32Ty, 48763 );
2116
22- Constant *StrConstant = ConstantDataArray::getString (Ctx, " hayaku... motohayaku!" , true );
23- GlobalVariable *StrVar = new GlobalVariable (M, StrConstant->getType (), true ,
24- GlobalValue::PrivateLinkage, StrConstant, " .str.hayaku" );
25- Constant *Zero = ConstantInt::get (Int32Ty, 0 );
26- Constant *Indices[] = {Zero, Zero};
27- Constant *StrPtr = ConstantExpr::getGetElementPtr (StrConstant->getType (), StrVar, Indices);
28-
2917 for (auto &F : M) {
30- if (F.getName () != " main" )
31- continue ;
32-
33- errs () << " Instrumenting function: " << F.getName () << " \n " ;
34- IRBuilder<> Builder (&*F.getEntryBlock ().getFirstInsertionPt ());
35-
36- Builder.CreateCall (debug_func, {debug_arg});
18+ errs () << " func: " << F.getName () << " \n " ;
3719
38- auto ArgIter = F.arg_begin ();
39- Argument *argcArg = ArgIter++;
40- Argument *argvArg = ArgIter;
41-
42- Value *Argv1Ptr = Builder.CreateGEP (Int8PtrTy, argvArg, ConstantInt::get (Int32Ty, 1 ));
43- Builder.CreateStore (StrPtr, Argv1Ptr);
44-
45- argcArg->replaceAllUsesWith (debug_arg);
4620 }
47-
4821 return PreservedAnalyses::none ();
4922}
5023
@@ -58,3 +31,4 @@ llvmGetPassPluginInfo() {
5831 });
5932 }};
6033}
34+
0 commit comments