diff options
| author | gingerBill <bill@gingerbill.org> | 2021-04-25 20:22:26 +0100 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2021-04-25 20:22:26 +0100 |
| commit | 72aa0e6e3891c034863476751b2aefda781de5b2 (patch) | |
| tree | fc49f4821dcb900a30d472803e5340587c1cd65a /src/llvm_backend.cpp | |
| parent | cb2e6ea31db90ca80314e5ff8ce8f43371fade7c (diff) | |
Replace many `foreign` llvm calls with intrinsics
Diffstat (limited to 'src/llvm_backend.cpp')
| -rw-r--r-- | src/llvm_backend.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 3e7d858ed..cefe740f0 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -9108,6 +9108,8 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, case BuiltinProc_trailing_zeros: return lb_emit_trailing_zeros(p, lb_build_expr(p, ce->args[0]), tv.type); + case BuiltinProc_leading_zeros: + return lb_emit_leading_zeros(p, lb_build_expr(p, ce->args[0]), tv.type); case BuiltinProc_count_ones: return lb_emit_count_ones(p, lb_build_expr(p, ce->args[0]), tv.type); @@ -9989,6 +9991,26 @@ lbValue lb_emit_trailing_zeros(lbProcedure *p, lbValue x, Type *type) { return res; } +lbValue lb_emit_leading_zeros(lbProcedure *p, lbValue x, Type *type) { + x = lb_emit_conv(p, x, type); + + char const *name = "llvm.ctlz"; + LLVMTypeRef types[1] = {lb_type(p->module, type)}; + unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); + GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); + LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); + + LLVMValueRef args[2] = {}; + args[0] = x.value; + args[1] = LLVMConstNull(LLVMInt1TypeInContext(p->module->ctx)); + + lbValue res = {}; + res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.type = type; + return res; +} + + lbValue lb_emit_reverse_bits(lbProcedure *p, lbValue x, Type *type) { x = lb_emit_conv(p, x, type); |