|
1 | 1 | #include "llvm/Passes/PassPlugin.h" |
2 | 2 | #include "llvm/Passes/PassBuilder.h" |
3 | 3 | #include "llvm/IR/IRBuilder.h" |
4 | | -#include "llvm/Support/raw_ostream.h" |
5 | 4 |
|
6 | 5 | using namespace llvm; |
7 | 6 |
|
8 | | -struct LLVMPass : public PassInfoMixin<LLVMPass> |
9 | | -{ |
10 | | - PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); |
| 7 | +struct LLVMPass : public PassInfoMixin<LLVMPass> { |
| 8 | + PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); |
11 | 9 | }; |
12 | 10 |
|
13 | | -PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) |
14 | | -{ |
15 | | - LLVMContext &Ctx = M.getContext(); |
16 | | - IntegerType *Int32Ty = IntegerType::getInt32Ty(Ctx); |
17 | | - PointerType *Int8PtrTy = Type::getInt8PtrTy(Ctx); |
| 11 | +PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) { |
| 12 | + LLVMContext &Ctx = M.getContext(); |
| 13 | + IntegerType *Int32Ty = IntegerType::getInt32Ty(Ctx); |
| 14 | + FunctionCallee debug_func = M.getOrInsertFunction("debug", Int32Ty); |
| 15 | + ConstantInt *debug_arg = ConstantInt::get(Int32Ty, 48763); |
18 | 16 |
|
19 | | - |
20 | | - FunctionCallee debug_func = M.getOrInsertFunction("debug", Int32Ty); |
21 | | - ConstantInt *debug_arg = ConstantInt::get(Int32Ty, 48763); |
| 17 | + for (auto &F : M) { |
| 18 | + errs() << "func: " << F.getName() << "\n"; |
22 | 19 |
|
23 | | - // find main |
24 | | - for (Function &F : M) |
25 | | - { |
26 | | - if (F.getName() != "main") |
27 | | - continue; |
28 | | - |
29 | | - // Entry block |
30 | | - BasicBlock &EntryBB = F.getEntryBlock(); |
31 | | - IRBuilder<> Builder(&*EntryBB.getFirstInsertionPt()); |
32 | | - |
33 | | - // debug(48763) |
34 | | - Builder.CreateCall(debug_func, {debug_arg}); |
35 | | - |
36 | | - // argc argv |
37 | | - if (F.arg_size() >= 2) |
38 | | - { |
39 | | - auto ArgIt = F.arg_begin(); |
40 | | - Argument *Argc = ArgIt++; |
41 | | - Argument *Argv = ArgIt; |
42 | | - |
43 | | - // argc replace into 48763 |
44 | | - Argc->replaceAllUsesWith(debug_arg); |
45 | | - |
46 | | - // argv[1] = "hayaku... motohayaku!" |
47 | | - Constant *Str = Builder.CreateGlobalStringPtr("hayaku... motohayaku!", "hayaku_str"); |
48 | | - Value *One = ConstantInt::get(Int32Ty, 1); |
49 | | - // argv get argv[1] |
50 | | - Value *GEP = Builder.CreateGEP(Int8PtrTy, Argv, One, "argv1_ptr"); |
51 | | - Builder.CreateStore(Str, GEP); |
52 | | - } |
53 | | - |
54 | | - break; |
55 | | - } |
56 | | - |
57 | | - return PreservedAnalyses::none(); |
| 20 | + } |
| 21 | + return PreservedAnalyses::none(); |
58 | 22 | } |
59 | 23 |
|
60 | | -extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK llvmGetPassPluginInfo() |
61 | | -{ |
62 | | - return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0", |
63 | | - [](PassBuilder &PB) |
64 | | - { |
65 | | - PB.registerOptimizerLastEPCallback( |
66 | | - [](ModulePassManager &MPM, OptimizationLevel OL) |
67 | | - { |
68 | | - MPM.addPass(LLVMPass()); |
69 | | - }); |
70 | | - }}; |
| 24 | +extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK |
| 25 | +llvmGetPassPluginInfo() { |
| 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 | + }}; |
71 | 33 | } |
| 34 | + |
0 commit comments