1 // RUN: %clang_cc1 -triple i386-pc-linux-gnu -emit-llvm < %s| FileCheck %s
2
3 typedef __WCHAR_TYPE__ wchar_t;
4 typedef __SIZE_TYPE__ size_t;
5
6 void *memcpy(void *, void const *, size_t);
7 void *memccpy(void *, void const *, int, size_t);
8
9 // CHECK: @test1
10 // CHECK: call void @llvm.memset.p0i8.i32
11 // CHECK: call void @llvm.memset.p0i8.i32
12 // CHECK: call void @llvm.memcpy.p0i8.p0i8.i32
13 // CHECK: call void @llvm.memmove.p0i8.p0i8.i32
14 // CHECK-NOT: __builtin
15 // CHECK: ret
test1(int argc,char ** argv)16 int test1(int argc, char **argv) {
17 unsigned char a = 0x11223344;
18 unsigned char b = 0x11223344;
19 __builtin_bzero(&a, sizeof(a));
20 __builtin_memset(&a, 0, sizeof(a));
21 __builtin_memcpy(&a, &b, sizeof(a));
22 __builtin_memmove(&a, &b, sizeof(a));
23 return 0;
24 }
25
26 // rdar://9289468
27
28 // CHECK: @test2
29 // CHECK: call void @llvm.memcpy.p0i8.p0i8.i32
test2(char * a,char * b)30 char* test2(char* a, char* b) {
31 return __builtin_memcpy(a, b, 4);
32 }
33
34 // CHECK: @test3
35 // CHECK: call void @llvm.memset
test3(char * P)36 void test3(char *P) {
37 __builtin___memset_chk(P, 42, 128, 128);
38 }
39
40 // CHECK: @test4
41 // CHECK: call void @llvm.memcpy
test4(char * P,char * Q)42 void test4(char *P, char *Q) {
43 __builtin___memcpy_chk(P, Q, 128, 128);
44 }
45
46 // CHECK: @test5
47 // CHECK: call void @llvm.memmove
test5(char * P,char * Q)48 void test5(char *P, char *Q) {
49 __builtin___memmove_chk(P, Q, 128, 128);
50 }
51
52 // CHECK: @test6
53 // CHECK: call void @llvm.memcpy
test6(char * X)54 int test6(char *X) {
55 return __builtin___memcpy_chk(X, X, 42, 42) != 0;
56 }
57
58 // CHECK: @test7
59 // PR12094
test7(int * p)60 int test7(int *p) {
61 struct snd_pcm_hw_params_t* hwparams; // incomplete type.
62
63 // CHECK: call void @llvm.memset{{.*}} align 4 {{.*}}256, i1 false)
64 __builtin_memset(p, 0, 256); // Should be alignment = 4
65
66 // CHECK: call void @llvm.memset{{.*}} align 1 {{.*}}256, i1 false)
67 __builtin_memset((char*)p, 0, 256); // Should be alignment = 1
68
69 __builtin_memset(hwparams, 0, 256); // No crash alignment = 1
70 // CHECK: call void @llvm.memset{{.*}} align 1{{.*}}256, i1 false)
71 }
72
73 // <rdar://problem/11314941>
74 // Make sure we don't over-estimate the alignment of fields of
75 // packed structs.
76 struct PS {
77 int modes[4];
78 } __attribute__((packed));
79 struct PS ps;
test8(int * arg)80 void test8(int *arg) {
81 // CHECK: @test8
82 // CHECK: call void @llvm.memcpy{{.*}} align 4 {{.*}} align 1 {{.*}} 16, i1 false)
83 __builtin_memcpy(arg, ps.modes, sizeof(struct PS));
84 }
85
86 __attribute((aligned(16))) int x[4], y[4];
test9()87 void test9() {
88 // CHECK: @test9
89 // CHECK: call void @llvm.memcpy{{.*}} align 16 {{.*}} align 16 {{.*}} 16, i1 false)
90 __builtin_memcpy(x, y, sizeof(y));
91 }
92
93 wchar_t dest;
94 wchar_t src;
95
96 // CHECK-LABEL: @test10
97 // FIXME: Consider lowering these to llvm.memcpy / llvm.memmove.
test10()98 void test10() {
99 // CHECK: call i32* @wmemcpy(i32* @dest, i32* @src, i32 4)
100 __builtin_wmemcpy(&dest, &src, 4);
101
102 // CHECK: call i32* @wmemmove(i32* @dest, i32* @src, i32 4)
103 __builtin_wmemmove(&dest, &src, 4);
104 }
105
106 // CHECK-LABEL: @test11
test11()107 void test11() {
108 typedef struct { int a; } b;
109 int d;
110 b e;
111 // CHECK: call void @llvm.memcpy{{.*}}(
112 memcpy(&d, (char *)&e.a, sizeof(e));
113 }
114
115 // CHECK-LABEL: @test12
116 extern char dest_array[];
117 extern char src_array[];
test12()118 void test12() {
119 // CHECK: call void @llvm.memcpy{{.*}}(
120 memcpy(&dest_array, &dest_array, 2);
121 }
122
123 // CHECK-LABEL: @test13
test13(char * d,char * s,int c,size_t n)124 void test13(char *d, char *s, int c, size_t n) {
125 // CHECK: call i8* @memccpy
126 memccpy(d, s, c, n);
127 }
128