aboutsummaryrefslogtreecommitdiff
path: root/src/llvm_backend.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2021-04-22 10:35:17 +0100
committergingerBill <bill@gingerbill.org>2021-04-22 10:35:17 +0100
commit47c7dc6a9bfc679f027984bd68523743b4d7734f (patch)
tree8ba1ec35c1e2c0b0229a78ccee3dfc91381b69f5 /src/llvm_backend.cpp
parent65551ba8fb862a9caef97b3c84b17baa7715bbb6 (diff)
Add new intrinsics: debug_trap, trap, read_cycle_counter, expect
Diffstat (limited to 'src/llvm_backend.cpp')
-rw-r--r--src/llvm_backend.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp
index fc29be59c..48c276771 100644
--- a/src/llvm_backend.cpp
+++ b/src/llvm_backend.cpp
@@ -9049,6 +9049,40 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
}
return {};
+
+ case BuiltinProc_debug_trap:
+ case BuiltinProc_trap:
+ {
+ char const *name = nullptr;
+ switch (id) {
+ case BuiltinProc_debug_trap: name = "llvm.debugtrap"; break;
+ case BuiltinProc_trap: name = "llvm.trap"; break;
+ }
+
+ unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
+ GB_ASSERT_MSG(id != 0, "Unable to find %s", name);
+ LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, nullptr, 0);
+
+ LLVMBuildCall(p->builder, ip, nullptr, 0, "");
+ if (id == BuiltinProc_trap) {
+ LLVMBuildUnreachable(p->builder);
+ }
+ return {};
+ }
+
+ case BuiltinProc_read_cycle_counter:
+ {
+ char const *name = "llvm.readcyclecounter";
+ unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
+ GB_ASSERT_MSG(id != 0, "Unable to find %s", name);
+ LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, nullptr, 0);
+
+ lbValue res = {};
+ res.value = LLVMBuildCall(p->builder, ip, nullptr, 0, "");
+ res.type = tv.type;
+ return res;
+ }
+
case BuiltinProc_atomic_fence:
LLVMBuildFence(p->builder, LLVMAtomicOrderingSequentiallyConsistent, false, "");
return {};
@@ -9322,6 +9356,30 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
res.type = platform_type;
return lb_emit_conv(p, res, tv.type);
}
+
+ case BuiltinProc_expect:
+ {
+ Type *t = default_type(tv.type);
+ lbValue x = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t);
+ lbValue y = lb_emit_conv(p, lb_build_expr(p, ce->args[1]), t);
+
+ char const *name = "llvm.expect";
+
+ LLVMTypeRef types[1] = {lb_type(p->module, t)};
+ 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));
+
+ lbValue res = {};
+
+ LLVMValueRef args[2] = {};
+ args[0] = x.value;
+ args[1] = y.value;
+
+ res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), "");
+ res.type = t;
+ return lb_emit_conv(p, res, t);
+ }
}
GB_PANIC("Unhandled built-in procedure %.*s", LIT(builtin_procs[id].name));