aboutsummaryrefslogtreecommitdiff
path: root/src/llvm_backend_opt.cpp
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2021-10-31 19:19:48 +0000
committerGitHub <noreply@github.com>2021-10-31 19:19:48 +0000
commitb1de429d2cf5c2d1643acc73ec3ced22e57d6a07 (patch)
tree99d74a813fc16bbe3389f69cb0ca516744cbb561 /src/llvm_backend_opt.cpp
parent3de1719c172771c2cb5ed41725274e71906b7e0a (diff)
parent5f51337a01fa4a1e7a461604d564fa64601727cf (diff)
Merge pull request #1255 from odin-lang/wasi-wasm
`wasi_wasm32` support
Diffstat (limited to 'src/llvm_backend_opt.cpp')
-rw-r--r--src/llvm_backend_opt.cpp60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/llvm_backend_opt.cpp b/src/llvm_backend_opt.cpp
index d5ea90aea..75a377e5b 100644
--- a/src/llvm_backend_opt.cpp
+++ b/src/llvm_backend_opt.cpp
@@ -355,3 +355,63 @@ 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;
+ }
+
+ if (name == "memset" ||
+ name == "memmove" ||
+ name == "memcpy") {
+ continue;
+ }
+ if (is_arch_wasm()) {
+ if (name == "__ashlti3") {
+ LLVMSetLinkage(curr_func, LLVMExternalLinkage);
+ 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;
+ }
+ }
+}