|
1 |
| -#include "llvm/IR/Constants.h" |
2 |
| -#include "llvm/IR/GlobalVariable.h" |
3 |
| -#include "llvm/IR/IRBuilder.h" |
4 |
| -#include "llvm/IR/Instructions.h" |
5 |
| -#include "llvm/Passes/PassBuilder.h" |
6 | 1 | #include "llvm/Passes/PassPlugin.h"
|
| 2 | +#include "llvm/Passes/PassBuilder.h" |
| 3 | +#include "llvm/IR/IRBuilder.h" |
7 | 4 |
|
8 | 5 | using namespace llvm;
|
9 | 6 |
|
10 | 7 | struct LLVMPass : public PassInfoMixin<LLVMPass> {
|
11 |
| - PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); |
| 8 | + PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); |
12 | 9 | };
|
13 | 10 |
|
14 | 11 | PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) {
|
15 |
| - LLVMContext &ctx = M.getContext(); |
16 |
| - |
17 |
| - // Find the main function |
18 |
| - Function *main_fn = M.getFunction("main"); |
19 |
| - assert(main_fn && main_fn->arg_size() >= 2); |
20 |
| - |
21 |
| - // Find the debug function |
22 |
| - auto debug_fn = M.getFunction("debug"); |
23 |
| - assert(debug_fn); |
| 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); |
24 | 16 |
|
25 |
| - // Insert the debug function call at the beginning of main |
26 |
| - IRBuilder<> main_ir_builder(&main_fn->getEntryBlock().front()); |
27 |
| - // call the debug function with 48763 |
28 |
| - main_ir_builder.CreateCall(debug_fn, {ConstantInt::get(Type::getInt32Ty(ctx), 48763)}); |
| 17 | + for (auto &F : M) { |
| 18 | + errs() << "func: " << F.getName() << "\n"; |
29 | 19 |
|
30 |
| - // Find argc and argv |
31 |
| - auto args_iter = main_fn->arg_begin(); |
32 |
| - Argument &argc = *args_iter++; |
33 |
| - Argument &argv = *args_iter++; |
34 |
| - |
35 |
| - // Replace argc to 48763 |
36 |
| - auto new_argc = ConstantInt::get(argc.getType(), 48763); |
37 |
| - argc.replaceAllUsesWith(new_argc); |
38 |
| - |
39 |
| - // create the target string |
40 |
| - auto target_value = ConstantDataArray::getString(ctx, "hayaku... motohayaku!", true); |
41 |
| - auto target = cast<GlobalVariable>(M.getOrInsertGlobal("target", target_value->getType())); |
42 |
| - target->setInitializer(target_value); |
43 |
| - target->setLinkage(GlobalValue::InternalLinkage); |
44 |
| - |
45 |
| - // create a {..., target} array |
46 |
| - auto new_argv_value = ConstantArray::get(ArrayType::get(target->getType(), 2), |
47 |
| - ArrayRef<Constant *>{target, target}); |
48 |
| - auto new_argv = cast<GlobalVariable>(M.getOrInsertGlobal("my_argv", new_argv_value->getType())); |
49 |
| - new_argv->setInitializer(new_argv_value); |
50 |
| - new_argv->setLinkage(GlobalValue::InternalLinkage); |
51 |
| - |
52 |
| - // Replace argv to target_array |
53 |
| - argv.replaceAllUsesWith(new_argv); |
54 |
| - |
55 |
| - return PreservedAnalyses::none(); |
| 20 | + } |
| 21 | + return PreservedAnalyses::none(); |
56 | 22 | }
|
57 | 23 |
|
58 |
| -extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK llvmGetPassPluginInfo() { |
59 |
| - return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0", [](PassBuilder &PB) { |
60 |
| - PB.registerOptimizerLastEPCallback( |
61 |
| - [](ModulePassManager &MPM, OptimizationLevel OL) { MPM.addPass(LLVMPass()); }); |
62 |
| - }}; |
| 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 | + }}; |
63 | 33 | }
|
| 34 | + |
0 commit comments