Replace many foreign llvm calls with intrinsics

This commit is contained in:
gingerBill
2021-04-25 20:22:26 +01:00
parent cb2e6ea31d
commit 72aa0e6e38
12 changed files with 78 additions and 247 deletions

View File

@@ -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);