1 // RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-unknown-freebsd10.0 -emit-llvm < %s | FileCheck -check-prefix=FREEBSD %s
2 // RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-pc-win32 -emit-llvm < %s | FileCheck -check-prefix=WIN64 %s
3 
4 struct foo {
5   int x;
6   float y;
7   char z;
8 };
9 // FREEBSD: %[[STRUCT_FOO:.*]] = type { i32, float, i8 }
10 // WIN64: %[[STRUCT_FOO:.*]] = type { i32, float, i8 }
11 
12 void __attribute__((ms_abi)) f1(void);
13 void __attribute__((sysv_abi)) f2(void);
14 void f3(void) {
15   // FREEBSD-LABEL: define{{.*}} void @f3()
16   // WIN64-LABEL: define dso_local void @f3()
17   f1();
18   // FREEBSD: call win64cc void @f1()
19   // WIN64: call void @f1()
20   f2();
21   // FREEBSD: call void @f2()
22   // WIN64: call x86_64_sysvcc void @f2()
23 }
24 // FREEBSD: declare win64cc void @f1()
25 // FREEBSD: declare void @f2()
26 // WIN64: declare dso_local void @f1()
27 // WIN64: declare dso_local x86_64_sysvcc void @f2()
28 
29 // Win64 ABI varargs
30 void __attribute__((ms_abi)) f4(int a, ...) {
31   // FREEBSD-LABEL: define{{.*}} win64cc void @f4
32   // WIN64-LABEL: define dso_local void @f4
33   __builtin_ms_va_list ap;
34   __builtin_ms_va_start(ap, a);
35   // FREEBSD: %[[AP:.*]] = alloca i8*
36   // FREEBSD: call void @llvm.va_start
37   // WIN64: %[[AP:.*]] = alloca i8*
38   // WIN64: call void @llvm.va_start
39   int b = __builtin_va_arg(ap, int);
40   // FREEBSD: %[[AP_CUR:.*]] = load i8*, i8** %[[AP]]
41   // FREEBSD-NEXT: %[[AP_NEXT:.*]] = getelementptr inbounds i8, i8* %[[AP_CUR]], i64 8
42   // FREEBSD-NEXT: store i8* %[[AP_NEXT]], i8** %[[AP]]
43   // FREEBSD-NEXT: bitcast i8* %[[AP_CUR]] to i32*
44   // WIN64: %[[AP_CUR:.*]] = load i8*, i8** %[[AP]]
45   // WIN64-NEXT: %[[AP_NEXT:.*]] = getelementptr inbounds i8, i8* %[[AP_CUR]], i64 8
46   // WIN64-NEXT: store i8* %[[AP_NEXT]], i8** %[[AP]]
47   // WIN64-NEXT: bitcast i8* %[[AP_CUR]] to i32*
48   double _Complex c = __builtin_va_arg(ap, double _Complex);
49   // FREEBSD: %[[AP_CUR2:.*]] = load i8*, i8** %[[AP]]
50   // FREEBSD-NEXT: %[[AP_NEXT2:.*]] = getelementptr inbounds i8, i8* %[[AP_CUR2]], i64 8
51   // FREEBSD-NEXT: store i8* %[[AP_NEXT2]], i8** %[[AP]]
52   // FREEBSD-NEXT: %[[CUR2:.*]] = bitcast i8* %[[AP_CUR2]] to { double, double }**
53   // FREEBSD-NEXT: load { double, double }*, { double, double }** %[[CUR2]]
54   // WIN64: %[[AP_CUR2:.*]] = load i8*, i8** %[[AP]]
55   // WIN64-NEXT: %[[AP_NEXT2:.*]] = getelementptr inbounds i8, i8* %[[AP_CUR2]], i64 8
56   // WIN64-NEXT: store i8* %[[AP_NEXT2]], i8** %[[AP]]
57   // WIN64-NEXT: %[[CUR2:.*]] = bitcast i8* %[[AP_CUR2]] to { double, double }**
58   // WIN64-NEXT: load { double, double }*, { double, double }** %[[CUR2]]
59   struct foo d = __builtin_va_arg(ap, struct foo);
60   // FREEBSD: %[[AP_CUR3:.*]] = load i8*, i8** %[[AP]]
61   // FREEBSD-NEXT: %[[AP_NEXT3:.*]] = getelementptr inbounds i8, i8* %[[AP_CUR3]], i64 8
62   // FREEBSD-NEXT: store i8* %[[AP_NEXT3]], i8** %[[AP]]
63   // FREEBSD-NEXT: %[[CUR3:.*]] = bitcast i8* %[[AP_CUR3]] to %[[STRUCT_FOO]]*
64   // FREEBSD-NEXT: load %[[STRUCT_FOO]]*, %[[STRUCT_FOO]]** %[[CUR3]]
65   // WIN64: %[[AP_CUR3:.*]] = load i8*, i8** %[[AP]]
66   // WIN64-NEXT: %[[AP_NEXT3:.*]] = getelementptr inbounds i8, i8* %[[AP_CUR3]], i64 8
67   // WIN64-NEXT: store i8* %[[AP_NEXT3]], i8** %[[AP]]
68   // WIN64-NEXT: %[[CUR3:.*]] = bitcast i8* %[[AP_CUR3]] to %[[STRUCT_FOO]]*
69   // WIN64-NEXT: load %[[STRUCT_FOO]]*, %[[STRUCT_FOO]]** %[[CUR3]]
70   __builtin_ms_va_list ap2;
71   __builtin_ms_va_copy(ap2, ap);
72   // FREEBSD: %[[AP_VAL:.*]] = load i8*, i8** %[[AP]]
73   // FREEBSD-NEXT: store i8* %[[AP_VAL]], i8** %[[AP2:.*]]
74   // WIN64: %[[AP_VAL:.*]] = load i8*, i8** %[[AP]]
75   // WIN64-NEXT: store i8* %[[AP_VAL]], i8** %[[AP2:.*]]
76   __builtin_ms_va_end(ap);
77   // FREEBSD: call void @llvm.va_end
78   // WIN64: call void @llvm.va_end
79 }
80 
81 // Let's verify that normal va_lists work right on Win64, too.
82 void f5(int a, ...) {
83   // WIN64-LABEL: define dso_local void @f5
84   __builtin_va_list ap;
85   __builtin_va_start(ap, a);
86   // WIN64: %[[AP:.*]] = alloca i8*
87   // WIN64: call void @llvm.va_start
88   int b = __builtin_va_arg(ap, int);
89   // WIN64: %[[AP_CUR:.*]] = load i8*, i8** %[[AP]]
90   // WIN64-NEXT: %[[AP_NEXT:.*]] = getelementptr inbounds i8, i8* %[[AP_CUR]], i64 8
91   // WIN64-NEXT: store i8* %[[AP_NEXT]], i8** %[[AP]]
92   // WIN64-NEXT: bitcast i8* %[[AP_CUR]] to i32*
93   double _Complex c = __builtin_va_arg(ap, double _Complex);
94   // WIN64: %[[AP_CUR2:.*]] = load i8*, i8** %[[AP]]
95   // WIN64-NEXT: %[[AP_NEXT2:.*]] = getelementptr inbounds i8, i8* %[[AP_CUR2]], i64 8
96   // WIN64-NEXT: store i8* %[[AP_NEXT2]], i8** %[[AP]]
97   // WIN64-NEXT: bitcast i8* %[[AP_CUR2]] to { double, double }*
98   struct foo d = __builtin_va_arg(ap, struct foo);
99   // WIN64: %[[AP_CUR3:.*]] = load i8*, i8** %[[AP]]
100   // WIN64-NEXT: %[[AP_NEXT3:.*]] = getelementptr inbounds i8, i8* %[[AP_CUR3]], i64 8
101   // WIN64-NEXT: store i8* %[[AP_NEXT3]], i8** %[[AP]]
102   // WIN64-NEXT: bitcast i8* %[[AP_CUR3]] to %[[STRUCT_FOO]]*
103   __builtin_va_list ap2;
104   __builtin_va_copy(ap2, ap);
105   // WIN64: call void @llvm.va_copy
106   __builtin_va_end(ap);
107   // WIN64: call void @llvm.va_end
108 }
109 
110 // Verify that using a Win64 va_list from a System V function works.
111 void __attribute__((sysv_abi)) f6(__builtin_ms_va_list ap) {
112   // FREEBSD-LABEL: define{{.*}} void @f6
113   // FREEBSD: store i8* %ap, i8** %[[AP:.*]]
114   // WIN64-LABEL: define dso_local x86_64_sysvcc void @f6
115   // WIN64: store i8* %ap, i8** %[[AP:.*]]
116   int b = __builtin_va_arg(ap, int);
117   // FREEBSD: %[[AP_CUR:.*]] = load i8*, i8** %[[AP]]
118   // FREEBSD-NEXT: %[[AP_NEXT:.*]] = getelementptr inbounds i8, i8* %[[AP_CUR]], i64 8
119   // FREEBSD-NEXT: store i8* %[[AP_NEXT]], i8** %[[AP]]
120   // FREEBSD-NEXT: bitcast i8* %[[AP_CUR]] to i32*
121   // WIN64: %[[AP_CUR:.*]] = load i8*, i8** %[[AP]]
122   // WIN64-NEXT: %[[AP_NEXT:.*]] = getelementptr inbounds i8, i8* %[[AP_CUR]], i64 8
123   // WIN64-NEXT: store i8* %[[AP_NEXT]], i8** %[[AP]]
124   // WIN64-NEXT: bitcast i8* %[[AP_CUR]] to i32*
125   double _Complex c = __builtin_va_arg(ap, double _Complex);
126   // FREEBSD: %[[AP_CUR2:.*]] = load i8*, i8** %[[AP]]
127   // FREEBSD-NEXT: %[[AP_NEXT2:.*]] = getelementptr inbounds i8, i8* %[[AP_CUR2]], i64 8
128   // FREEBSD-NEXT: store i8* %[[AP_NEXT2]], i8** %[[AP]]
129   // FREEBSD-NEXT: bitcast i8* %[[AP_CUR2]] to { double, double }**
130   // WIN64: %[[AP_CUR2:.*]] = load i8*, i8** %[[AP]]
131   // WIN64-NEXT: %[[AP_NEXT2:.*]] = getelementptr inbounds i8, i8* %[[AP_CUR2]], i64 8
132   // WIN64-NEXT: store i8* %[[AP_NEXT2]], i8** %[[AP]]
133   // WIN64-NEXT: bitcast i8* %[[AP_CUR2]] to { double, double }**
134   struct foo d = __builtin_va_arg(ap, struct foo);
135   // FREEBSD: %[[AP_CUR3:.*]] = load i8*, i8** %[[AP]]
136   // FREEBSD-NEXT: %[[AP_NEXT3:.*]] = getelementptr inbounds i8, i8* %[[AP_CUR3]], i64 8
137   // FREEBSD-NEXT: store i8* %[[AP_NEXT3]], i8** %[[AP]]
138   // FREEBSD-NEXT: bitcast i8* %[[AP_CUR3]] to %[[STRUCT_FOO]]**
139   // WIN64: %[[AP_CUR3:.*]] = load i8*, i8** %[[AP]]
140   // WIN64-NEXT: %[[AP_NEXT3:.*]] = getelementptr inbounds i8, i8* %[[AP_CUR3]], i64 8
141   // WIN64-NEXT: store i8* %[[AP_NEXT3]], i8** %[[AP]]
142   // WIN64-NEXT: bitcast i8* %[[AP_CUR3]] to %[[STRUCT_FOO]]**
143   __builtin_ms_va_list ap2;
144   __builtin_ms_va_copy(ap2, ap);
145   // FREEBSD: %[[AP_VAL:.*]] = load i8*, i8** %[[AP]]
146   // FREEBSD-NEXT: store i8* %[[AP_VAL]], i8** %[[AP2:.*]]
147   // WIN64: %[[AP_VAL:.*]] = load i8*, i8** %[[AP]]
148   // WIN64-NEXT: store i8* %[[AP_VAL]], i8** %[[AP2:.*]]
149 }
150 
151 // This test checks if structs are passed according to Win64 calling convention
152 // when it's enforced by __attribute((ms_abi)).
153 struct i128 {
154   unsigned long long a;
155   unsigned long long b;
156 };
157 
158 __attribute__((ms_abi)) struct i128 f7(struct i128 a) {
159   // WIN64: define dso_local void @f7(%struct.i128* noalias sret(%struct.i128) align 8 %agg.result, %struct.i128* noundef %a)
160   // FREEBSD: define{{.*}} win64cc void @f7(%struct.i128* noalias sret(%struct.i128) align 8 %agg.result, %struct.i128* noundef %a)
161   return a;
162 }
163