|
1 | 1 | #include "llvm/Passes/PassPlugin.h" |
2 | 2 | #include "llvm/Passes/PassBuilder.h" |
3 | 3 | #include "llvm/IR/IRBuilder.h" |
| 4 | +#include "llvm/IR/Instructions.h" |
| 5 | +#include "llvm/IR/Constants.h" |
| 6 | +#include "llvm/Support/raw_ostream.h" |
4 | 7 |
|
5 | 8 | using namespace llvm; |
6 | 9 |
|
| 10 | +bool registerCustomPipeline(StringRef, ModulePassManager&, ArrayRef<PassBuilder::PipelineElement>); |
| 11 | +void injectAtStart(ModulePassManager&, OptimizationLevel); |
| 12 | + |
| 13 | +namespace { |
7 | 14 | struct LLVMPass : public PassInfoMixin<LLVMPass> { |
8 | | - PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); |
9 | | -}; |
| 15 | + PreservedAnalyses run(Module &Mod, ModuleAnalysisManager &) { |
| 16 | + Function *MainFunc = Mod.getFunction("main"); |
| 17 | + if (!MainFunc) return PreservedAnalyses::all(); |
| 18 | + |
| 19 | + LLVMContext &Context = Mod.getContext(); |
| 20 | + IRBuilder<> Builder(&*MainFunc->getEntryBlock().getFirstInsertionPt()); |
| 21 | + |
| 22 | + FunctionCallee DebugCall = Mod.getOrInsertFunction( |
| 23 | + "debug", FunctionType::get(Type::getVoidTy(Context), {Type::getInt32Ty(Context)}, false)); |
| 24 | + Builder.CreateCall(DebugCall, ConstantInt::get(Type::getInt32Ty(Context), 48763)); |
| 25 | + |
| 26 | + auto ArgIter = MainFunc->arg_begin(); |
| 27 | + Argument *ArgCount = &*ArgIter++; |
| 28 | + Argument *ArgList = &*ArgIter; |
| 29 | + |
| 30 | + Value *FixedVal = ConstantInt::get(Type::getInt32Ty(Context), 48763); |
| 31 | + AllocaInst *FakeArgc = Builder.CreateAlloca(Type::getInt32Ty(Context), nullptr, "argc_temp"); |
| 32 | + Builder.CreateStore(FixedVal, FakeArgc); |
10 | 33 |
|
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); |
| 34 | + for (auto UI = ArgCount->use_begin(), UE = ArgCount->use_end(); UI != UE;) { |
| 35 | + Use &U = *UI++; |
| 36 | + U.set(Builder.CreateLoad(Type::getInt32Ty(Context), FakeArgc)); |
| 37 | + } |
16 | 38 |
|
17 | | - for (auto &F : M) { |
18 | | - errs() << "func: " << F.getName() << "\n"; |
| 39 | + Value *Str = Builder.CreateGlobalStringPtr("hayaku... motohayaku!", "msg_str"); |
| 40 | + Value *Index = ConstantInt::get(Type::getInt32Ty(Context), 1); |
| 41 | + Value *ArgvSlot = Builder.CreateGEP( |
| 42 | + ArgList->getType()->getPointerElementType(), ArgList, Index); |
| 43 | + Builder.CreateStore(Str, ArgvSlot); |
19 | 44 |
|
| 45 | + return PreservedAnalyses::none(); |
20 | 46 | } |
21 | | - return PreservedAnalyses::none(); |
| 47 | +}; |
22 | 48 | } |
23 | 49 |
|
24 | | -extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK |
25 | | -llvmGetPassPluginInfo() { |
26 | | - return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0", |
| 50 | +extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo() { |
| 51 | + static const PassPluginLibraryInfo Info{ |
| 52 | + LLVM_PLUGIN_API_VERSION, "LLVMPass", LLVM_VERSION_STRING, |
27 | 53 | [](PassBuilder &PB) { |
28 | | - PB.registerOptimizerLastEPCallback( |
29 | | - [](ModulePassManager &MPM, OptimizationLevel OL) { |
30 | | - MPM.addPass(LLVMPass()); |
31 | | - }); |
32 | | - }}; |
| 54 | + PB.registerPipelineParsingCallback(registerCustomPipeline); |
| 55 | + PB.registerPipelineStartEPCallback(injectAtStart); |
| 56 | + } |
| 57 | + }; |
| 58 | + return Info; |
33 | 59 | } |
34 | 60 |
|
| 61 | +bool registerCustomPipeline(StringRef Name, ModulePassManager &PM, ArrayRef<PassBuilder::PipelineElement>) { |
| 62 | + if (Name == "llvm-pass") { |
| 63 | + PM.addPass(LLVMPass()); |
| 64 | + return true; |
| 65 | + } |
| 66 | + return false; |
| 67 | +} |
| 68 | + |
| 69 | +void injectAtStart(ModulePassManager &PM, OptimizationLevel) { |
| 70 | + PM.addPass(LLVMPass()); |
| 71 | +} |
0 commit comments