xref: /llvm-project/clang/test/CodeGen/asm.c (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm %s -o - | FileCheck %s
2 
3 // PR10415
4 __asm__ ("foo1");
5 __asm__ ("foo2");
6 __asm__ ("foo3");
7 // CHECK: module asm "foo1"
8 // CHECK-NEXT: module asm "foo2"
9 // CHECK-NEXT: module asm "foo3"
10 
t1(int len)11 void t1(int len) {
12   __asm__ volatile("" : "=&r"(len), "+&r"(len));
13 }
14 
t2(unsigned long long t)15 void t2(unsigned long long t)  {
16   __asm__ volatile("" : "+m"(t));
17 }
18 
t3(unsigned char * src,unsigned long long temp)19 void t3(unsigned char *src, unsigned long long temp) {
20   __asm__ volatile("" : "+m"(temp), "+r"(src));
21 }
22 
t4(void)23 void t4(void) {
24   unsigned long long a;
25   struct reg { unsigned long long a, b; } b;
26 
27   __asm__ volatile ("":: "m"(a), "m"(b));
28 }
29 
30 // PR3417
t5(int i)31 void t5(int i) {
32   asm("nop" : "=r"(i) : "0"(t5));
33 }
34 
35 // PR3641
t6(void)36 void t6(void) {
37   __asm__ volatile("" : : "i" (t6));
38 }
39 
t7(int a)40 void t7(int a) {
41   __asm__ volatile("T7 NAMED: %[input]" : "+r"(a): [input] "i" (4));
42   // CHECK: @t7(i32
43   // CHECK: T7 NAMED: $1
44 }
45 
t8(void)46 void t8(void) {
47   __asm__ volatile("T8 NAMED MODIFIER: %c[input]" :: [input] "i" (4));
48   // CHECK: @t8()
49   // CHECK: T8 NAMED MODIFIER: ${0:c}
50 }
51 
52 // PR3682
t9(unsigned int a)53 unsigned t9(unsigned int a) {
54   asm("bswap %0 %1" : "+r" (a));
55   return a;
56 }
57 
58 // PR3908
t10(int r)59 void t10(int r) {
60   __asm__("PR3908 %[lf] %[xx] %[li] %[r]" : [r] "+r" (r) : [lf] "mx" (0), [li] "mr" (0), [xx] "x" ((double)(0)));
61 
62 // CHECK: @t10(
63 // CHECK:PR3908 $1 $3 $2 $0
64 }
65 
66 // PR3373
t11(signed char input)67 unsigned t11(signed char input) {
68   unsigned  output;
69   __asm__("xyz"
70           : "=a" (output)
71           : "0" (input));
72   return output;
73 }
74 
75 // PR3373
t12(unsigned input)76 unsigned char t12(unsigned input) {
77   unsigned char output;
78   __asm__("xyz"
79           : "=a" (output)
80           : "0" (input));
81   return output;
82 }
83 
t13(unsigned input)84 unsigned char t13(unsigned input) {
85   unsigned char output;
86   __asm__("xyz %1"
87           : "=a" (output)
88           : "0" (input));
89   return output;
90 }
91 
92 struct large {
93   int x[1000];
94 };
95 
t15(int x,struct large * P)96 unsigned long t15(int x, struct large *P) {
97   __asm__("xyz "
98           : "=r" (x)
99           : "m" (*P), "0" (x));
100   return x;
101 }
102 
103 // bitfield destination of an asm.
104 struct S {
105   int a : 4;
106 };
107 
t14(struct S * P)108 void t14(struct S *P) {
109   __asm__("abc %0" : "=r"(P->a) );
110 }
111 
112 // PR4938
t16(void)113 int t16(void) {
114   int a,b;
115   asm ( "nop;"
116        :"=%c" (a)
117        : "r" (b)
118        );
119   return 0;
120 }
121 
122 // PR6475
t17(void)123 void t17(void) {
124   int i;
125   __asm__ ( "nop": "=m"(i));
126 
127 // CHECK: @t17()
128 // CHECK: call void asm "nop", "=*m,
129 }
130 
t18(unsigned data)131 int t18(unsigned data) {
132   int a, b;
133 
134   asm("xyz" :"=a"(a), "=d"(b) : "a"(data));
135   return a + b;
136 // CHECK: t18(i32
137 // CHECK: = call {{.*}}asm "xyz"
138 // CHECK-NEXT: extractvalue
139 // CHECK-NEXT: extractvalue
140 }
141 
142 // PR6780
t19(unsigned data)143 int t19(unsigned data) {
144   int a, b;
145 
146   asm("x{abc|def|ghi}z" :"=r"(a): "r"(data));
147   return a + b;
148   // CHECK: t19(i32
149   // CHECK: = call {{.*}}asm "x$(abc$|def$|ghi$)z"
150 }
151 
152 // PR6845 - Mismatching source/dest fp types.
t20(double x)153 double t20(double x) {
154   register long double result;
155   __asm __volatile ("frndint"  : "=t" (result) : "0" (x));
156   return result;
157 
158   // CHECK: @t20
159   // CHECK: fpext double {{.*}} to x86_fp80
160   // CHECK-NEXT: call x86_fp80 asm sideeffect "frndint"
161   // CHECK: fptrunc x86_fp80 {{.*}} to double
162 }
163 
t21(long double x)164 float t21(long double x) {
165   register float result;
166   __asm __volatile ("frndint"  : "=t" (result) : "0" (x));
167   return result;
168   // CHECK: @t21
169   // CHECK: call x86_fp80 asm sideeffect "frndint"
170   // CHECK-NEXT: fptrunc x86_fp80 {{.*}} to float
171 }
172 
173 // accept 'l' constraint
t22(unsigned char a,unsigned char b)174 unsigned char t22(unsigned char a, unsigned char b) {
175   unsigned int la = a;
176   unsigned int lb = b;
177   unsigned int bigres;
178   unsigned char res;
179   __asm__ ("0:\n1:\n" : [bigres] "=la"(bigres) : [la] "0"(la), [lb] "c"(lb) :
180                         "edx", "cc");
181   res = bigres;
182   return res;
183 }
184 
185 // accept 'l' constraint
t23(unsigned char a,unsigned char b)186 unsigned char t23(unsigned char a, unsigned char b) {
187   unsigned int la = a;
188   unsigned int lb = b;
189   unsigned char res;
190   __asm__ ("0:\n1:\n" : [res] "=la"(res) : [la] "0"(la), [lb] "c"(lb) :
191                         "edx", "cc");
192   return res;
193 }
194 
t24(char c)195 void *t24(char c) {
196   void *addr;
197   // CHECK: @t24
198   // CHECK: zext i8 {{.*}} to i32
199   // CHECK-NEXT: call ptr asm "foobar"
200   __asm__ ("foobar" : "=a" (addr) : "0" (c));
201   return addr;
202 }
203 
204 // PR10299 - fpsr, fpcr
t25(void)205 void t25(void)
206 {
207   __asm__ __volatile__(					   \
208 		       "finit"				   \
209 		       :				   \
210 		       :				   \
211 		       :"st","st(1)","st(2)","st(3)",	   \
212 			"st(4)","st(5)","st(6)","st(7)",   \
213 			"fpsr","fpcr"			   \
214 							   );
215 }
216 
217 // AVX registers
218 typedef long long __m256i __attribute__((__vector_size__(32)));
t26(__m256i * p)219 void t26 (__m256i *p) {
220   __asm__ volatile("vmovaps  %0, %%ymm0" :: "m" (*(__m256i*)p) : "ymm0");
221 }
222 
223 // Check to make sure the inline asm non-standard dialect attribute _not_ is
224 // emitted.
t27(void)225 void t27(void) {
226   asm volatile("nop");
227 // CHECK: @t27
228 // CHECK: call void asm sideeffect "nop"
229 // CHECK-NOT: ia_nsdialect
230 // CHECK: ret void
231 }
232 
233 // Check handling of '*' and '#' constraint modifiers.
t28(void)234 void t28(void)
235 {
236   asm volatile ("/* %0 */" : : "i#*X,*r" (1));
237 // CHECK: @t28
238 // CHECK: call void asm sideeffect "/* $0 */", "i|r,~{dirflag},~{fpsr},~{flags}"(i32 1)
239 }
240 
241 static unsigned t29_var[1];
242 
t29(void)243 void t29(void) {
244   asm volatile("movl %%eax, %0"
245                :
246                : "m"(t29_var));
247   // CHECK: @t29
248   // CHECK: call void asm sideeffect "movl %eax, $0", "*m,~{dirflag},~{fpsr},~{flags}"(ptr elementtype([1 x i32]) @t29_var)
249 }
250 
t30(int len)251 void t30(int len) {
252   __asm__ volatile(""
253                    : "+&&rm"(len));
254   // CHECK: @t30
255   // CHECK: call void asm sideeffect "", "=*&rm,0,~{dirflag},~{fpsr},~{flags}"
256 }
257 
t31(int len)258 void t31(int len) {
259   __asm__ volatile(""
260                    : "+%%rm"(len), "+rm"(len));
261   // CHECK: @t31
262   // CHECK: call void asm sideeffect "", "=*%rm,=*rm,0,1,~{dirflag},~{fpsr},~{flags}"
263 }
264 
265 // CHECK: @t32
t32(int cond)266 int t32(int cond)
267 {
268   asm goto("testl %0, %0; jne %l1;" :: "r"(cond)::label_true, loop);
269   // CHECK: callbr void asm sideeffect "testl $0, $0; jne ${1:l};", "r,!i,!i,~{dirflag},~{fpsr},~{flags}"(i32 %0) #1
270   // CHECK-NEXT: to label %asm.fallthrough [label %label_true, label %loop]
271   return 0;
272 loop:
273   return 0;
274 label_true:
275   return 1;
276 }
277 
t33(void * ptr)278 void *t33(void *ptr)
279 {
280   void *ret;
281   asm ("lea %1, %0" : "=r" (ret) : "p" (ptr));
282   return ret;
283 
284   // CHECK: @t33
285   // CHECK: %1 = call ptr asm "lea $1, $0", "=r,p,~{dirflag},~{fpsr},~{flags}"(ptr %0)
286 }
287