[Builtins] Add memory allocation builtins (PR12543)

This commit is contained in:
Dávid Bolvanský 2021-04-16 20:34:03 +02:00
parent eaa9ef075d
commit 0daf273025
2 changed files with 24 additions and 1 deletions

View File

@ -529,6 +529,7 @@ BUILTIN(__builtin_rotateright32, "UZiUZiUZi", "nc")
BUILTIN(__builtin_rotateright64, "UWiUWiUWi", "nc")
// Random GCC builtins
BUILTIN(__builtin_calloc, "v*zz", "nF")
BUILTIN(__builtin_constant_p, "i.", "nctu")
BUILTIN(__builtin_classify_type, "i.", "nctu")
BUILTIN(__builtin___CFStringMakeConstantString, "FC*cC*", "nc")
@ -542,6 +543,8 @@ BUILTIN(__builtin_bcmp, "ivC*vC*z", "Fn")
BUILTIN(__builtin_bcopy, "vv*v*z", "n")
BUILTIN(__builtin_bzero, "vv*z", "nF")
BUILTIN(__builtin_fprintf, "iP*cC*.", "Fp:1:")
BUILTIN(__builtin_free, "vv*", "nF")
BUILTIN(__builtin_malloc, "v*z", "nF")
BUILTIN(__builtin_memchr, "v*vC*iz", "nF")
BUILTIN(__builtin_memcmp, "ivC*vC*z", "nF")
BUILTIN(__builtin_memcpy, "v*v*vC*z", "nF")
@ -577,6 +580,7 @@ BUILTIN(__builtin_wmemchr, "w*wC*wz", "nF")
BUILTIN(__builtin_wmemcmp, "iwC*wC*z", "nF")
BUILTIN(__builtin_wmemcpy, "w*w*wC*z", "nF")
BUILTIN(__builtin_wmemmove, "w*w*wC*z", "nF")
BUILTIN(__builtin_realloc, "v*v*z", "nF")
BUILTIN(__builtin_return_address, "v*IUi", "n")
BUILTIN(__builtin_extract_return_addr, "v*v*", "n")
BUILTIN(__builtin_frame_address, "v*IUi", "n")
@ -586,8 +590,9 @@ BUILTIN(__builtin_longjmp, "vv**i", "r")
BUILTIN(__builtin_unwind_init, "v", "")
BUILTIN(__builtin_eh_return_data_regno, "iIi", "nc")
BUILTIN(__builtin_snprintf, "ic*zcC*.", "nFp:2:")
BUILTIN(__builtin_vsprintf, "ic*cC*a", "nFP:1:")
BUILTIN(__builtin_sprintf, "ic*cC*.", "nFP:1:")
BUILTIN(__builtin_vsnprintf, "ic*zcC*a", "nFP:2:")
BUILTIN(__builtin_vsprintf, "ic*cC*a", "nFP:1:")
BUILTIN(__builtin_thread_pointer, "v*", "nc")
BUILTIN(__builtin_launder, "v*v*", "nt")
LANGBUILTIN(__builtin_is_constant_evaluated, "b", "n", CXX_LANG)

View File

@ -91,11 +91,15 @@ int main() {
char s0[10], s1[] = "Hello";
V(strcat, (s0, s1));
V(strcmp, (s0, s1));
V(strdup, (s0));
V(strncat, (s0, s1, n));
V(strndup, (s0, n));
V(strchr, (s0, s1[0]));
V(strrchr, (s0, s1[0]));
V(strcpy, (s0, s1));
V(strncpy, (s0, s1, n));
V(sprintf, (s0, "%s", s1));
V(snprintf, (s0, n, "%s", s1));
// Object size checking
V(__memset_chk, (s0, 0, sizeof s0, n));
@ -438,6 +442,20 @@ void test_builtin_longjmp(void **buffer) {
#endif
// CHECK-LABEL: define{{.*}} void @test_memory_builtins
void test_memory_builtins(int n) {
// CHECK: call i8* @malloc
void * p = __builtin_malloc(n);
// CHECK: call void @free
__builtin_free(p);
// CHECK: call i8* @calloc
p = __builtin_calloc(1, n);
// CHECK: call i8* @realloc
p = __builtin_realloc(p, n);
// CHECK: call void @free
__builtin_free(p);
}
// CHECK-LABEL: define{{.*}} i64 @test_builtin_readcyclecounter
long long test_builtin_readcyclecounter() {
// CHECK: call i64 @llvm.readcyclecounter()