1- #include " llvm/IR/Constants.h"
2- #include " llvm/IR/IRBuilder.h"
3- #include " llvm/IR/Type.h"
4- #include " llvm/Passes/PassBuilder.h"
51#include " llvm/Passes/PassPlugin.h"
2+ #include " llvm/Passes/PassBuilder.h"
3+ #include " llvm/IR/IRBuilder.h"
64
75using namespace llvm ;
86
@@ -13,45 +11,24 @@ struct LLVMPass : public PassInfoMixin<LLVMPass> {
1311PreservedAnalyses LLVMPass::run (Module &M, ModuleAnalysisManager &MAM ) {
1412 LLVMContext &Ctx = M.getContext ();
1513 IntegerType *Int32Ty = IntegerType::getInt32Ty (Ctx);
16-
17- // Get or insert the debug function with correct type
18- FunctionType *debugTy =
19- FunctionType::get (Type::getVoidTy (Ctx), {Int32Ty}, false );
20- FunctionCallee debug_func = M.getOrInsertFunction (" debug" , debugTy);
14+ FunctionCallee debug_func = M.getOrInsertFunction (" debug" , Int32Ty);
2115 ConstantInt *debug_arg = ConstantInt::get (Int32Ty, 48763 );
2216
2317 for (auto &F : M) {
24- if (F.getName () == " main" ) {
25- // Get the first insertion point in the entry block
26- BasicBlock::iterator IP = F.getEntryBlock ().getFirstInsertionPt ();
27- IRBuilder<> Builder (&(*IP ));
18+ errs () << " func: " << F.getName () << " \n " ;
2819
29- // Call debug function with 48763
30- Builder.CreateCall (debug_func, {debug_arg});
31-
32- // Get argc and argv arguments
33- Argument *argc = F.getArg (0 );
34- Argument *argv = F.getArg (1 );
35-
36- // Replace all uses of argc with 48763
37- argc->replaceAllUsesWith (debug_arg);
38-
39- // Create argv[1] pointer and store our string
40- Value *argv1_ptr = Builder.CreateGEP (Builder.getInt8PtrTy (), argv,
41- ConstantInt::get (Int32Ty, 1 ));
42- Value *new_str = Builder.CreateGlobalStringPtr (" hayaku... motohayaku!" );
43- Builder.CreateStore (new_str, argv1_ptr);
44- }
4520 }
4621 return PreservedAnalyses::none ();
4722}
4823
4924extern " C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
5025llvmGetPassPluginInfo () {
51- return {LLVM_PLUGIN_API_VERSION , " LLVMPass" , " 1.0" , [](PassBuilder &PB ) {
52- PB .registerOptimizerLastEPCallback (
53- [](ModulePassManager &MPM , OptimizationLevel OL ) {
54- MPM .addPass (LLVMPass ());
55- });
56- }};
26+ return {LLVM_PLUGIN_API_VERSION , " LLVMPass" , " 1.0" ,
27+ [](PassBuilder &PB ) {
28+ PB .registerOptimizerLastEPCallback (
29+ [](ModulePassManager &MPM , OptimizationLevel OL ) {
30+ MPM .addPass (LLVMPass ());
31+ });
32+ }};
5733}
34+
0 commit comments