|
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" |
7 | 4 |
|
8 | 5 | using namespace llvm; |
9 | 6 |
|
10 | | -bool registerCustomPipeline(StringRef, ModulePassManager&, ArrayRef<PassBuilder::PipelineElement>); |
11 | | -void injectAtStart(ModulePassManager&, OptimizationLevel); |
12 | | - |
13 | | -namespace { |
14 | 7 | struct LLVMPass : public PassInfoMixin<LLVMPass> { |
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); |
| 8 | + PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); |
| 9 | +}; |
33 | 10 |
|
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 | | - } |
| 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); |
38 | 16 |
|
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); |
| 17 | + for (auto &F : M) { |
| 18 | + errs() << "func: " << F.getName() << "\n"; |
44 | 19 |
|
45 | | - return PreservedAnalyses::none(); |
46 | 20 | } |
47 | | -}; |
| 21 | + return PreservedAnalyses::none(); |
48 | 22 | } |
49 | 23 |
|
50 | | -extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo() { |
51 | | - static const PassPluginLibraryInfo Info{ |
52 | | - LLVM_PLUGIN_API_VERSION, "LLVMPass", LLVM_VERSION_STRING, |
| 24 | +extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK |
| 25 | +llvmGetPassPluginInfo() { |
| 26 | + return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0", |
53 | 27 | [](PassBuilder &PB) { |
54 | | - PB.registerPipelineParsingCallback(registerCustomPipeline); |
55 | | - PB.registerPipelineStartEPCallback(injectAtStart); |
56 | | - } |
57 | | - }; |
58 | | - return Info; |
| 28 | + PB.registerOptimizerLastEPCallback( |
| 29 | + [](ModulePassManager &MPM, OptimizationLevel OL) { |
| 30 | + MPM.addPass(LLVMPass()); |
| 31 | + }); |
| 32 | + }}; |
59 | 33 | } |
60 | 34 |
|
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