|
1 | | -#include "llvm/IR/IRBuilder.h" |
2 | | -#include "llvm/IR/Instructions.h" |
3 | | -#include "llvm/IR/Module.h" |
4 | | -#include "llvm/Passes/PassBuilder.h" |
5 | 1 | #include "llvm/Passes/PassPlugin.h" |
| 2 | +#include "llvm/Passes/PassBuilder.h" |
| 3 | +#include "llvm/IR/IRBuilder.h" |
6 | 4 |
|
7 | 5 | using namespace llvm; |
8 | 6 |
|
9 | 7 | struct LLVMPass : public PassInfoMixin<LLVMPass> { |
10 | | - PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM) { |
11 | | - LLVMContext &Ctx = M.getContext(); |
12 | | - IntegerType *Int32Ty = Type::getInt32Ty(Ctx); |
13 | | - PointerType *i8PtrTy = Type::getInt8PtrTy(Ctx); |
14 | | - |
15 | | - // debug(int) |
16 | | - FunctionCallee debugFunc = M.getOrInsertFunction("debug", FunctionType::get(Type::getVoidTy(Ctx), {Int32Ty}, false)); |
17 | | - ConstantInt *debugArg = ConstantInt::get(Int32Ty, 48763); |
18 | | - |
19 | | - Function *mainFunc = M.getFunction("main"); |
20 | | - if (!mainFunc) return PreservedAnalyses::none(); |
21 | | - |
22 | | - IRBuilder<> Builder(&*mainFunc->getEntryBlock().getFirstInsertionPt()); |
23 | | - |
24 | | - // call debug(48763) |
25 | | - Builder.CreateCall(debugFunc, {debugArg}); |
26 | | - |
27 | | - // replace argc (arg0) to 48763 |
28 | | - Argument *argcArg = nullptr, *argvArg = nullptr; |
29 | | - auto it = mainFunc->arg_begin(); |
30 | | - argcArg = it++; |
31 | | - argvArg = it; |
32 | | - |
33 | | - // alloc argc, store 48763 |
34 | | - AllocaInst *argcAlloca = Builder.CreateAlloca(Int32Ty, nullptr, "argcVar"); |
35 | | - Builder.CreateStore(debugArg, argcAlloca); |
36 | | - |
37 | | - // replace all uses of argcArg (except alloca) with loaded version |
38 | | - argcArg->replaceAllUsesWith(Builder.CreateLoad(Int32Ty, argcAlloca)); |
39 | | - |
40 | | - // insert global string "hayaku... motohayaku!" |
41 | | - std::string msg = "hayaku... motohayaku!"; |
42 | | - Constant *strConstant = ConstantDataArray::getString(Ctx, msg, true); |
43 | | - GlobalVariable *strVar = new GlobalVariable( |
44 | | - M, strConstant->getType(), true, |
45 | | - GlobalValue::PrivateLinkage, strConstant, "msg"); |
| 8 | + PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); |
| 9 | +}; |
46 | 10 |
|
47 | | - // get i8* to string |
48 | | - Value *strPtr = Builder.CreateGEP( |
49 | | - strConstant->getType(), |
50 | | - strVar, |
51 | | - {ConstantInt::get(Int32Ty, 0), ConstantInt::get(Int32Ty, 0)} |
52 | | - ); |
| 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); |
53 | 16 |
|
54 | | - // argv[1] = strPtr |
55 | | - Value *argv1Ptr = Builder.CreateGEP( |
56 | | - i8PtrTy, argvArg, |
57 | | - ConstantInt::get(Int32Ty, 1) |
58 | | - ); |
59 | | - Builder.CreateStore(strPtr, argv1Ptr); |
| 17 | + for (auto &F : M) { |
| 18 | + errs() << "func: " << F.getName() << "\n"; |
60 | 19 |
|
61 | | - return PreservedAnalyses::none(); |
62 | 20 | } |
63 | | -}; |
| 21 | + return PreservedAnalyses::none(); |
| 22 | +} |
64 | 23 |
|
65 | | -extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK llvmGetPassPluginInfo() { |
| 24 | +extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK |
| 25 | +llvmGetPassPluginInfo() { |
66 | 26 | return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0", |
67 | | - [](PassBuilder &PB) { |
68 | | - PB.registerOptimizerLastEPCallback( |
69 | | - [](ModulePassManager &MPM, OptimizationLevel OL) { |
70 | | - MPM.addPass(LLVMPass()); |
71 | | - }); |
72 | | - }}; |
| 27 | + [](PassBuilder &PB) { |
| 28 | + PB.registerOptimizerLastEPCallback( |
| 29 | + [](ModulePassManager &MPM, OptimizationLevel OL) { |
| 30 | + MPM.addPass(LLVMPass()); |
| 31 | + }); |
| 32 | + }}; |
73 | 33 | } |
| 34 | + |
0 commit comments