xref: /llvm-project/clang/test/CodeGenCXX/x86_64-arguments.cpp (revision 158d72d728261c1e54dc77931372b2322c52849f)
1 // RUN: %clang_cc1 -no-enable-noundef-analysis -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck %s
2 
3 // Basic base class test.
4 struct f0_s0 { unsigned a; };
5 struct f0_s1 : public f0_s0 { void *b; };
6 // CHECK-LABEL: define{{.*}} void @_Z2f05f0_s1(i32 %a0.coerce0, ptr %a0.coerce1)
f0(f0_s1 a0)7 void f0(f0_s1 a0) { }
8 
9 // Check with two eight-bytes in base class.
10 struct f1_s0 { unsigned a; unsigned b; float c; };
11 struct f1_s1 : public f1_s0 { float d;};
12 // CHECK-LABEL: define{{.*}} void @_Z2f15f1_s1(i64 %a0.coerce0, <2 x float> %a0.coerce1)
f1(f1_s1 a0)13 void f1(f1_s1 a0) { }
14 
15 // Check with two eight-bytes in base class and merge.
16 struct f2_s0 { unsigned a; unsigned b; float c; };
17 struct f2_s1 : public f2_s0 { char d;};
18 // CHECK-LABEL: define{{.*}} void @_Z2f25f2_s1(i64 %a0.coerce0, i64 %a0.coerce1)
f2(f2_s1 a0)19 void f2(f2_s1 a0) { }
20 
21 // PR5831
22 // CHECK-LABEL: define{{.*}} void @_Z2f34s3_1(i64 %x.coerce)
23 struct s3_0 {};
24 struct s3_1 { struct s3_0 a; long b; };
f3(struct s3_1 x)25 void f3(struct s3_1 x) {}
26 
27 // CHECK-LABEL: define{{.*}} i64 @_Z4f4_0M2s4i(i64 %a)
28 // CHECK: define {{.*}} @_Z4f4_1M2s4FivE(i64 %a.coerce0, i64 %a.coerce1)
29 struct s4 {};
30 typedef int s4::* s4_mdp;
31 typedef int (s4::*s4_mfp)();
f4_0(s4_mdp a)32 s4_mdp f4_0(s4_mdp a) { return a; }
f4_1(s4_mfp a)33 s4_mfp f4_1(s4_mfp a) { return a; }
34 
35 // A struct with <= one eightbyte before a member data pointer should still
36 // be allowed in registers.
37 // CHECK-LABEL: define{{.*}} void @{{.*}}f_struct_with_mdp{{.*}}(ptr %a.coerce0, i64 %a.coerce1)
38 struct struct_with_mdp { char *a; s4_mdp b; };
f_struct_with_mdp(struct_with_mdp a)39 void f_struct_with_mdp(struct_with_mdp a) { (void)a; }
40 
41 // A struct with anything before a member function will be too big and
42 // goes in memory.
43 // CHECK-LABEL: define{{.*}} void @{{.*}}f_struct_with_mfp_0{{.*}}(ptr byval(%struct{{.*}}) align 8 %a)
44 struct struct_with_mfp_0 { char a; s4_mfp b; };
f_struct_with_mfp_0(struct_with_mfp_0 a)45 void f_struct_with_mfp_0(struct_with_mfp_0 a) { (void)a; }
46 
47 // CHECK-LABEL: define{{.*}} void @{{.*}}f_struct_with_mfp_1{{.*}}(ptr byval(%struct{{.*}}) align 8 %a)
48 struct struct_with_mfp_1 { void *a; s4_mfp b; };
f_struct_with_mfp_1(struct_with_mfp_1 a)49 void f_struct_with_mfp_1(struct_with_mfp_1 a) { (void)a; }
50 
51 namespace PR7523 {
52 struct StringRef {
53   char *a;
54 };
55 
56 void AddKeyword(StringRef, int x);
57 
foo()58 void foo() {
59   // CHECK-LABEL: define{{.*}} void @_ZN6PR75233fooEv()
60   // CHECK: call void @_ZN6PR752310AddKeywordENS_9StringRefEi(ptr {{.*}}, i32 4)
61   AddKeyword(StringRef(), 4);
62 }
63 }
64 
65 namespace PR7742 {
66   struct s2 {
67     float a[2];
68   };
69 
70   struct c2 : public s2 {};
71 
72   // CHECK-LABEL: define{{.*}} <2 x float> @_ZN6PR77423fooEPNS_2c2E(ptr %P)
foo(c2 * P)73   c2 foo(c2 *P) {
74     return c2();
75   }
76 
77 }
78 
79 namespace PR5179 {
80   struct B {};
81 
82   struct B1 : B {
83     int* pa;
84   };
85 
86   struct B2 : B {
87     B1 b1;
88   };
89 
90   // CHECK-LABEL: define{{.*}} ptr @_ZN6PR51793barENS_2B2E(ptr %b2.coerce)
bar(B2 b2)91   const void *bar(B2 b2) {
92     return b2.b1.pa;
93   }
94 }
95 
96 namespace test5 {
97   struct Xbase { };
98   struct Empty { };
99   struct Y;
100   struct X : public Xbase {
101     Empty empty;
102     Y f();
103   };
104   struct Y : public X {
105     Empty empty;
106   };
107   X getX();
108   int takeY(const Y&, int y);
g()109   void g() {
110     // The temporary for the X object needs to have a defined address when
111     // passed into X::f as 'this'.
112     takeY(getX().f(), 42);
113   }
114   // CHECK: void @_ZN5test51gEv()
115   // CHECK: alloca %"struct.test5::Y"
116   // CHECK: alloca %"struct.test5::X"
117   // CHECK: alloca %"struct.test5::Y"
118 }
119 
120 namespace test6 {
121   struct outer {
122     int x;
123     struct epsilon_matcher {} e;
124     int f;
125   };
126 
test(outer x)127   int test(outer x) {
128     return x.x + x.f;
129   }
130   // CHECK-LABEL: define{{.*}} i32 @_ZN5test64testENS_5outerE(i64 %x.coerce0, i32 %x.coerce1)
131 }
132 
133 namespace test7 {
134   struct StringRef {char* ptr; long len; };
135   class A { public: ~A(); };
x(A,A,long,long,StringRef)136   A x(A, A, long, long, StringRef) { return A(); }
137   // Check that the StringRef is passed byval instead of expanded
138   // (which would split it between registers and memory).
139   // CHECK: define{{.*}} void @_ZN5test71xENS_1AES0_llNS_9StringRefE({{.*}} byval({{.*}}) align 8 {{%.*}})
140 
141   // And a couple extra related tests:
y(A,long double,long,long,StringRef)142   A y(A, long double, long, long, StringRef) { return A(); }
143   // CHECK: define{{.*}} void @_ZN5test71yENS_1AEellNS_9StringRefE({{.*}} ptr
144   struct StringDouble {char * ptr; double d;};
z(A,A,A,A,A,StringDouble)145   A z(A, A, A, A, A, StringDouble) { return A(); }
zz(A,A,A,A,StringDouble)146   A zz(A, A, A, A, StringDouble) { return A(); }
147   // CHECK: define{{.*}} void @_ZN5test71zENS_1AES0_S0_S0_S0_NS_12StringDoubleE({{.*}} byval({{.*}}) align 8 {{%.*}})
148   // CHECK: define{{.*}} void @_ZN5test72zzENS_1AES0_S0_S0_NS_12StringDoubleE({{.*}} ptr
149 }
150 
151 namespace test8 {
152   // CHECK: declare void @_ZN5test83fooENS_1BE(ptr byval(%"class.test8::B") align 8)
153   class A {
154    char big[17];
155   };
156 
157   class B : public A {};
158 
159   void foo(B b);
bar()160   void bar() {
161    B b;
162    foo(b);
163   }
164 }
165 
166 // PR4242
167 namespace test9 {
168   // Large enough to be passed indirectly.
169   struct S { void *data[3]; };
170 
171   struct T { void *data[2]; };
172 
173   // CHECK: define{{.*}} void @_ZN5test93fooEPNS_1SEPNS_1TE(ptr %0, ptr %1)
foo(S *,T *)174   void foo(S*, T*) {}
175 
176   // CHECK: define{{.*}} void @_ZN5test91aEiiiiNS_1TEPv(ptr dead_on_unwind noalias writable sret([[S:%.*]]) align 8 {{%.*}}, i32 %0, i32 %1, i32 %2, i32 %3, ptr byval([[T:%.*]]) align 8 %4, ptr %5)
a(int,int,int,int,T,void *)177   S a(int, int, int, int, T, void*) {
178     return S();
179   }
180 
181   // CHECK: define{{.*}} ptr @_ZN5test91bEPNS_1SEiiiiNS_1TEPv(ptr {{%.*}}, i32 %0, i32 %1, i32 %2, i32 %3, ptr byval([[T]]) align 8 %4, ptr %5)
b(S * sret,int,int,int,int,T,void *)182   S* b(S* sret, int, int, int, int, T, void*) {
183     return sret;
184   }
185 
186   // CHECK: define{{.*}} void @_ZN5test91cEiiiNS_1TEPv(ptr dead_on_unwind noalias writable sret([[S]]) align 8 {{%.*}}, i32 %0, i32 %1, i32 %2, ptr {{%.*}}, ptr {{%.*}}, ptr %3)
c(int,int,int,T,void *)187   S c(int, int, int, T, void*) {
188     return S();
189   }
190 
191   // CHECK: define{{.*}} ptr @_ZN5test91dEPNS_1SEiiiNS_1TEPv(ptr {{%.*}}, i32 %0, i32 %1, i32 %2, ptr {{%.*}}, ptr {{%.*}}, ptr %3)
d(S * sret,int,int,int,T,void *)192   S* d(S* sret, int, int, int, T, void*) {
193     return sret;
194   }
195 }
196 
197 namespace test10 {
198 #pragma pack(1)
199 struct BasePacked {
200   char one;
201   short two;
202 };
203 #pragma pack()
204 struct DerivedPacked : public BasePacked {
205   int three;
206 };
207 // CHECK-LABEL: define{{.*}} i32 @_ZN6test1020FuncForDerivedPackedENS_13DerivedPackedE(ptr byval({{.*}}) align 8
FuncForDerivedPacked(DerivedPacked d)208 int FuncForDerivedPacked(DerivedPacked d) {
209   return d.three;
210 }
211 }
212 
213 namespace test11 {
214 union U {
215   float f1;
216   char __attribute__((__vector_size__(1))) f2;
217 };
f(union U u)218 int f(union U u) { return u.f2[1]; }
219 // CHECK-LABEL: define{{.*}} i32 @_ZN6test111fENS_1UE(i32
220 }
221