diff options
| author | gingerBill <bill@gingerbill.org> | 2021-10-31 00:11:38 +0100 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2021-10-31 00:11:38 +0100 |
| commit | 8ef6f9dd7bbb1611dd7166c4e14034e53df4a8b6 (patch) | |
| tree | d13e60bb55f8d8167ebd906ee66485a0097c208b /src/llvm_backend_opt.cpp | |
| parent | 5bc8a491a7768da0019b7b17da637e681f2ace90 (diff) | |
Compile `wasm64`; Add `lb_run_remove_unused_function_pass`
Diffstat (limited to 'src/llvm_backend_opt.cpp')
| -rw-r--r-- | src/llvm_backend_opt.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/llvm_backend_opt.cpp b/src/llvm_backend_opt.cpp index d5ea90aea..25e290d70 100644 --- a/src/llvm_backend_opt.cpp +++ b/src/llvm_backend_opt.cpp @@ -355,3 +355,52 @@ void lb_run_function_pass_manager(LLVMPassManagerRef fpm, lbProcedure *p) { // are not removed lb_run_remove_dead_instruction_pass(p); } + + +void lb_run_remove_unused_function_pass(LLVMModuleRef mod) { + isize removal_count = 0; + isize pass_count = 0; + isize const max_pass_count = 10; + // Custom remove dead function pass + for (; pass_count < max_pass_count; pass_count++) { + bool was_dead_function = false; + for (LLVMValueRef func = LLVMGetFirstFunction(mod); + func != nullptr; + /**/ + ) { + LLVMValueRef curr_func = func; + func = LLVMGetNextFunction(func); + + LLVMUseRef first_use = LLVMGetFirstUse(curr_func); + if (first_use != nullptr) { + continue; + } + String name = {}; + name.text = cast(u8 *)LLVMGetValueName2(curr_func, cast(size_t *)&name.len); + + if (LLVMIsDeclaration(curr_func)) { + // Ignore for the time being + continue; + } + + + LLVMLinkage linkage = LLVMGetLinkage(curr_func); + + switch (linkage) { + case LLVMExternalLinkage: + case LLVMDLLImportLinkage: + case LLVMDLLExportLinkage: + default: + continue; + case LLVMInternalLinkage: + break; + } + LLVMDeleteFunction(curr_func); + was_dead_function = true; + removal_count += 1; + } + if (!was_dead_function) { + break; + } + } +} |