xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGen/builtin-memfns.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple i386-pc-linux-gnu -emit-llvm < %s| FileCheck %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc // CHECK: @test1
4*f4a2713aSLionel Sambuc // CHECK: call void @llvm.memset.p0i8.i32
5*f4a2713aSLionel Sambuc // CHECK: call void @llvm.memset.p0i8.i32
6*f4a2713aSLionel Sambuc // CHECK: call void @llvm.memcpy.p0i8.p0i8.i32
7*f4a2713aSLionel Sambuc // CHECK: call void @llvm.memmove.p0i8.p0i8.i32
8*f4a2713aSLionel Sambuc // CHECK-NOT: __builtin
9*f4a2713aSLionel Sambuc // CHECK: ret
test1(int argc,char ** argv)10*f4a2713aSLionel Sambuc int test1(int argc, char **argv) {
11*f4a2713aSLionel Sambuc   unsigned char a = 0x11223344;
12*f4a2713aSLionel Sambuc   unsigned char b = 0x11223344;
13*f4a2713aSLionel Sambuc   __builtin_bzero(&a, sizeof(a));
14*f4a2713aSLionel Sambuc   __builtin_memset(&a, 0, sizeof(a));
15*f4a2713aSLionel Sambuc   __builtin_memcpy(&a, &b, sizeof(a));
16*f4a2713aSLionel Sambuc   __builtin_memmove(&a, &b, sizeof(a));
17*f4a2713aSLionel Sambuc   return 0;
18*f4a2713aSLionel Sambuc }
19*f4a2713aSLionel Sambuc 
20*f4a2713aSLionel Sambuc // rdar://9289468
21*f4a2713aSLionel Sambuc 
22*f4a2713aSLionel Sambuc // CHECK: @test2
23*f4a2713aSLionel Sambuc // CHECK: call void @llvm.memcpy.p0i8.p0i8.i32
test2(char * a,char * b)24*f4a2713aSLionel Sambuc char* test2(char* a, char* b) {
25*f4a2713aSLionel Sambuc   return __builtin_memcpy(a, b, 4);
26*f4a2713aSLionel Sambuc }
27*f4a2713aSLionel Sambuc 
28*f4a2713aSLionel Sambuc // CHECK: @test3
29*f4a2713aSLionel Sambuc // CHECK: call void @llvm.memset
test3(char * P)30*f4a2713aSLionel Sambuc void test3(char *P) {
31*f4a2713aSLionel Sambuc   __builtin___memset_chk(P, 42, 128, 128);
32*f4a2713aSLionel Sambuc }
33*f4a2713aSLionel Sambuc 
34*f4a2713aSLionel Sambuc // CHECK: @test4
35*f4a2713aSLionel Sambuc // CHECK: call void @llvm.memcpy
test4(char * P,char * Q)36*f4a2713aSLionel Sambuc void test4(char *P, char *Q) {
37*f4a2713aSLionel Sambuc   __builtin___memcpy_chk(P, Q, 128, 128);
38*f4a2713aSLionel Sambuc }
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc // CHECK: @test5
41*f4a2713aSLionel Sambuc // CHECK: call void @llvm.memmove
test5(char * P,char * Q)42*f4a2713aSLionel Sambuc void test5(char *P, char *Q) {
43*f4a2713aSLionel Sambuc   __builtin___memmove_chk(P, Q, 128, 128);
44*f4a2713aSLionel Sambuc }
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc // CHECK: @test6
47*f4a2713aSLionel Sambuc // CHECK: call void @llvm.memcpy
test6(char * X)48*f4a2713aSLionel Sambuc int test6(char *X) {
49*f4a2713aSLionel Sambuc   return __builtin___memcpy_chk(X, X, 42, 42) != 0;
50*f4a2713aSLionel Sambuc }
51*f4a2713aSLionel Sambuc 
52*f4a2713aSLionel Sambuc // CHECK: @test7
53*f4a2713aSLionel Sambuc // PR12094
test7(int * p)54*f4a2713aSLionel Sambuc int test7(int *p) {
55*f4a2713aSLionel Sambuc   struct snd_pcm_hw_params_t* hwparams;  // incomplete type.
56*f4a2713aSLionel Sambuc 
57*f4a2713aSLionel Sambuc   // CHECK: call void @llvm.memset{{.*}}256, i32 4, i1 false)
58*f4a2713aSLionel Sambuc   __builtin_memset(p, 0, 256);  // Should be alignment = 4
59*f4a2713aSLionel Sambuc 
60*f4a2713aSLionel Sambuc   // CHECK: call void @llvm.memset{{.*}}256, i32 1, i1 false)
61*f4a2713aSLionel Sambuc   __builtin_memset((char*)p, 0, 256);  // Should be alignment = 1
62*f4a2713aSLionel Sambuc 
63*f4a2713aSLionel Sambuc   __builtin_memset(hwparams, 0, 256);  // No crash alignment = 1
64*f4a2713aSLionel Sambuc   // CHECK: call void @llvm.memset{{.*}}256, i32 1, i1 false)
65*f4a2713aSLionel Sambuc }
66*f4a2713aSLionel Sambuc 
67*f4a2713aSLionel Sambuc // <rdar://problem/11314941>
68*f4a2713aSLionel Sambuc // Make sure we don't over-estimate the alignment of fields of
69*f4a2713aSLionel Sambuc // packed structs.
70*f4a2713aSLionel Sambuc struct PS {
71*f4a2713aSLionel Sambuc   int modes[4];
72*f4a2713aSLionel Sambuc } __attribute__((packed));
73*f4a2713aSLionel Sambuc struct PS ps;
test8(int * arg)74*f4a2713aSLionel Sambuc void test8(int *arg) {
75*f4a2713aSLionel Sambuc   // CHECK: @test8
76*f4a2713aSLionel Sambuc   // CHECK: call void @llvm.memcpy{{.*}} 16, i32 1, i1 false)
77*f4a2713aSLionel Sambuc   __builtin_memcpy(arg, ps.modes, sizeof(struct PS));
78*f4a2713aSLionel Sambuc }
79*f4a2713aSLionel Sambuc 
80*f4a2713aSLionel Sambuc __attribute((aligned(16))) int x[4], y[4];
test9()81*f4a2713aSLionel Sambuc void test9() {
82*f4a2713aSLionel Sambuc   // CHECK: @test9
83*f4a2713aSLionel Sambuc   // CHECK: call void @llvm.memcpy{{.*}} 16, i32 16, i1 false)
84*f4a2713aSLionel Sambuc   __builtin_memcpy(x, y, sizeof(y));
85*f4a2713aSLionel Sambuc }
86