1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-linux-gnu -S -emit-llvm %s -o - | FileCheck %s
2*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -triple arm64-linux-gnu -S -emit-llvm %s -o - | FileCheck %s
3*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -triple armv7-linux-gnu -S -emit-llvm %s -o - | FileCheck %s
4*0a6a1f1dSLionel Sambuc 
5*0a6a1f1dSLionel Sambuc // CHECK-NOT: @sp = common global
6*0a6a1f1dSLionel Sambuc register unsigned long current_stack_pointer asm("sp");
7*0a6a1f1dSLionel Sambuc struct p4_Thread {
8*0a6a1f1dSLionel Sambuc   struct {
9*0a6a1f1dSLionel Sambuc     int len;
10*0a6a1f1dSLionel Sambuc   } word;
11*0a6a1f1dSLionel Sambuc };
12*0a6a1f1dSLionel Sambuc // Testing pointer types as well
13*0a6a1f1dSLionel Sambuc register struct p4_Thread *p4TH asm("sp");
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc // CHECK: define{{.*}} i[[bits:[0-9]+]] @get_stack_pointer_addr()
16*0a6a1f1dSLionel Sambuc // CHECK: [[ret:%[0-9]+]] = call i[[bits]] @llvm.read_register.i[[bits]](metadata !0)
17*0a6a1f1dSLionel Sambuc // CHECK: ret i[[bits]] [[ret]]
get_stack_pointer_addr()18*0a6a1f1dSLionel Sambuc unsigned long get_stack_pointer_addr() {
19*0a6a1f1dSLionel Sambuc   return current_stack_pointer;
20*0a6a1f1dSLionel Sambuc }
21*0a6a1f1dSLionel Sambuc // CHECK: declare{{.*}} i[[bits]] @llvm.read_register.i[[bits]](metadata)
22*0a6a1f1dSLionel Sambuc 
23*0a6a1f1dSLionel Sambuc // CHECK: define{{.*}} void @set_stack_pointer_addr(i[[bits]] %addr) #0 {
24*0a6a1f1dSLionel Sambuc // CHECK: [[sto:%[0-9]+]] = load i[[bits]]* %
25*0a6a1f1dSLionel Sambuc // CHECK: call void @llvm.write_register.i[[bits]](metadata !0, i[[bits]] [[sto]])
26*0a6a1f1dSLionel Sambuc // CHECK: ret void
set_stack_pointer_addr(unsigned long addr)27*0a6a1f1dSLionel Sambuc void set_stack_pointer_addr(unsigned long addr) {
28*0a6a1f1dSLionel Sambuc   current_stack_pointer = addr;
29*0a6a1f1dSLionel Sambuc }
30*0a6a1f1dSLionel Sambuc // CHECK: declare{{.*}} void @llvm.write_register.i[[bits]](metadata, i[[bits]])
31*0a6a1f1dSLionel Sambuc 
32*0a6a1f1dSLionel Sambuc // CHECK: define {{.*}}@fn1
fn1()33*0a6a1f1dSLionel Sambuc int fn1() {
34*0a6a1f1dSLionel Sambuc   return (*p4TH).word.len;
35*0a6a1f1dSLionel Sambuc }
36*0a6a1f1dSLionel Sambuc // CHECK: %[[regr:[0-9]+]] = call i[[bits]] @llvm.read_register.i[[bits]](metadata !0)
37*0a6a1f1dSLionel Sambuc // CHECK: inttoptr i[[bits]] %[[regr]] to %struct.p4_Thread*
38*0a6a1f1dSLionel Sambuc 
39*0a6a1f1dSLionel Sambuc // CHECK: define {{.*}}@fn2
fn2(struct p4_Thread * val)40*0a6a1f1dSLionel Sambuc void fn2(struct p4_Thread *val) {
41*0a6a1f1dSLionel Sambuc   p4TH = val;
42*0a6a1f1dSLionel Sambuc }
43*0a6a1f1dSLionel Sambuc // CHECK: %[[regw:[0-9]+]] = ptrtoint %struct.p4_Thread* %{{.*}} to i[[bits]]
44*0a6a1f1dSLionel Sambuc // CHECK: call void @llvm.write_register.i[[bits]](metadata !0, i[[bits]] %[[regw]])
45*0a6a1f1dSLionel Sambuc 
46*0a6a1f1dSLionel Sambuc // CHECK: !llvm.named.register.sp = !{!0}
47*0a6a1f1dSLionel Sambuc // CHECK: !0 = !{!"sp"}
48