1 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm < %s | FileCheck %s --check-prefix=ALIGN16
2 // RUN: %clang_cc1 -triple x86_64-windows-msvc      -emit-llvm < %s | FileCheck %s --check-prefix=ALIGN16
3 // RUN: %clang_cc1 -triple i386-apple-darwin        -emit-llvm < %s | FileCheck %s --check-prefix=ALIGN16
4 // RUN: %clang_cc1 -triple i386-unknown-linux-gnu   -emit-llvm < %s | FileCheck %s --check-prefix=ALIGN8
5 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-builtin-malloc  -emit-llvm < %s  | FileCheck %s --check-prefix=NOBUILTIN-MALLOC
6 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-builtin-calloc  -emit-llvm < %s  | FileCheck %s --check-prefix=NOBUILTIN-CALLOC
7 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-builtin-realloc -emit-llvm < %s  | FileCheck %s --check-prefix=NOBUILTIN-REALLOC
8 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-builtin-aligned_alloc -emit-llvm < %s  | FileCheck %s --check-prefix=NOBUILTIN-ALIGNED_ALLOC
9 
10 typedef __SIZE_TYPE__ size_t;
11 
12 void *malloc(size_t);
13 void *calloc(size_t, size_t);
14 void *realloc(void *, size_t);
15 void *aligned_alloc(size_t, size_t);
16 
17 void *malloc_test(size_t n) {
18   return malloc(n);
19 }
20 
21 void *calloc_test(size_t n) {
22   return calloc(1, n);
23 }
24 
25 void *realloc_test(void *p, size_t n) {
26   return realloc(p, n);
27 }
28 
29 void *aligned_alloc_variable_test(size_t n, size_t a) {
30   return aligned_alloc(a, n);
31 }
32 
33 void *aligned_alloc_constant_test(size_t n) {
34   return aligned_alloc(8, n);
35 }
36 
37 void *aligned_alloc_large_constant_test(size_t n) {
38   return aligned_alloc(4096, n);
39 }
40 
41 // CHECK-LABEL: @malloc_test
42 // ALIGN16: align 16 i8* @malloc
43 
44 // CHECK-LABEL: @calloc_test
45 // ALIGN16: align 16 i8* @calloc
46 
47 // CHECK-LABEL: @realloc_test
48 // ALIGN16: align 16 i8* @realloc
49 
50 // CHECK-LABEL: @aligned_alloc_variable_test
51 // ALIGN16:      %[[ALLOCATED:.*]] = call align 16 i8* @aligned_alloc({{i32|i64}} noundef %[[ALIGN:.*]], {{i32|i64}} noundef %[[NBYTES:.*]])
52 // ALIGN16-NEXT: call void @llvm.assume(i1 true) [ "align"(i8* %[[ALLOCATED]], {{i32|i64}} %[[ALIGN]]) ]
53 
54 // CHECK-LABEL: @aligned_alloc_constant_test
55 // ALIGN16: align 16 i8* @aligned_alloc
56 
57 // CHECK-LABEL: @aligned_alloc_large_constant_test
58 // ALIGN16: align 4096 i8* @aligned_alloc
59 
60 // CHECK-LABEL: @malloc_test
61 // ALIGN8: align 8 i8* @malloc
62 
63 // CHECK-LABEL: @calloc_test
64 // ALIGN8: align 8 i8* @calloc
65 
66 // CHECK-LABEL: @realloc_test
67 // ALIGN8: align 8 i8* @realloc
68 
69 // CHECK-LABEL: @aligned_alloc_variable_test
70 // ALIGN8: align 8 i8* @aligned_alloc
71 
72 // CHECK-LABEL: @aligned_alloc_constant_test
73 // ALIGN8: align 8 i8* @aligned_alloc
74 
75 // CHECK-LABEL: @aligned_alloc_large_constant_test
76 // ALIGN8: align 4096 i8* @aligned_alloc
77 
78 // NOBUILTIN-MALLOC: declare i8* @malloc
79 // NOBUILTIN-CALLOC: declare i8* @calloc
80 // NOBUILTIN-REALLOC: declare i8* @realloc
81 // NOBUILTIN-ALIGNED_ALLOC: declare i8* @aligned_alloc
82