1 // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin %s -emit-llvm -o - | FileCheck -check-prefixes=X64,CHECK %s
2 // RUN: %clang_cc1 -std=c++11 -triple amdgcn %s -emit-llvm -o - | FileCheck -check-prefixes=AMDGCN,CHECK %s
8 template<typename T
> int S
<T
>::n
= 5;
11 // Make sure that the reference here is enough to trigger the instantiation of
12 // the static data member.
13 // CHECK: @_ZN1SIiE1nE = linkonce_odr{{.*}} global i32 5
18 // rdar://problem/9506377
19 void test0(void *array
, int n
) {
20 // CHECK-LABEL: define{{.*}} void @_Z5test0Pvi(
21 // AMDGCN: [[ARRAY0:%.*]] = alloca ptr, align 8, addrspace(5)
22 // AMDGCN-NEXT: [[N0:%.*]] = alloca i32, align 4, addrspace(5)
23 // AMDGCN-NEXT: [[REF0:%.*]] = alloca ptr, align 8, addrspace(5)
24 // AMDGCN-NEXT: [[S0:%.*]] = alloca i16, align 2, addrspace(5)
25 // AMDGCN-NEXT: [[ARRAY:%.*]] = addrspacecast ptr addrspace(5) [[ARRAY0]] to ptr
26 // AMDGCN-NEXT: [[N:%.*]] = addrspacecast ptr addrspace(5) [[N0]] to ptr
27 // AMDGCN-NEXT: [[REF:%.*]] = addrspacecast ptr addrspace(5) [[REF0]] to ptr
28 // AMDGCN-NEXT: [[S:%.*]] = addrspacecast ptr addrspace(5) [[S0]] to ptr
29 // X64: [[ARRAY:%.*]] = alloca ptr, align 8
30 // X64-NEXT: [[N:%.*]] = alloca i32, align 4
31 // X64-NEXT: [[REF:%.*]] = alloca ptr, align 8
32 // X64-NEXT: [[S:%.*]] = alloca i16, align 2
33 // CHECK-NEXT: store ptr
34 // CHECK-NEXT: store i32
36 // Capture the bounds.
37 // CHECK-NEXT: [[T0:%.*]] = load i32, ptr [[N]], align 4
38 // CHECK-NEXT: [[DIM0:%.*]] = zext i32 [[T0]] to i64
39 // CHECK-NEXT: [[T0:%.*]] = load i32, ptr [[N]], align 4
40 // CHECK-NEXT: [[T1:%.*]] = add nsw i32 [[T0]], 1
41 // CHECK-NEXT: [[DIM1:%.*]] = zext i32 [[T1]] to i64
42 typedef short array_t
[n
][n
+1];
44 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[ARRAY]], align 8
45 // CHECK-NEXT: store ptr [[T0]], ptr [[REF]], align 8
46 array_t
&ref
= *(array_t
*) array
;
48 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[REF]]
49 // CHECK-NEXT: [[T1:%.*]] = mul nsw i64 1, [[DIM1]]
50 // CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds i16, ptr [[T0]], i64 [[T1]]
51 // CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds i16, ptr [[T2]], i64 2
52 // CHECK-NEXT: store i16 3, ptr [[T3]]
55 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[REF]]
56 // CHECK-NEXT: [[T1:%.*]] = mul nsw i64 4, [[DIM1]]
57 // CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds i16, ptr [[T0]], i64 [[T1]]
58 // CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds i16, ptr [[T2]], i64 5
59 // CHECK-NEXT: [[T4:%.*]] = load i16, ptr [[T3]]
60 // CHECK-NEXT: store i16 [[T4]], ptr [[S]], align 2
63 // CHECK-NEXT: ret void
68 // CHECK-LABEL: define{{.*}} void {{.*}}test2{{.*}}(i32 noundef %b)
70 // AMDGCN: %__end1 = alloca ptr, align 8, addrspace(5)
71 // AMDGCN: [[END:%.*]] = addrspacecast ptr addrspace(5) %__end1 to ptr
72 // get the address of %b by checking the first store that stores it
73 //CHECK: store i32 %b, ptr [[PTR_B:%.*]]
75 // get the size of the VLA by getting the first load of the PTR_B
76 //CHECK: [[VLA_NUM_ELEMENTS_PREZEXT:%.*]] = load i32, ptr [[PTR_B]]
77 //CHECK-NEXT: [[VLA_NUM_ELEMENTS_PRE:%.*]] = zext i32 [[VLA_NUM_ELEMENTS_PREZEXT]]
80 //CHECK: store i32 15, ptr [[PTR_B]]
82 // Now get the sizeof, and then divide by the element size
85 //CHECK: [[VLA_SIZEOF:%.*]] = mul nuw i64 4, [[VLA_NUM_ELEMENTS_PRE]]
86 //CHECK-NEXT: [[VLA_NUM_ELEMENTS_POST:%.*]] = udiv i64 [[VLA_SIZEOF]], 4
87 //CHECK-NEXT: [[VLA_END_PTR:%.*]] = getelementptr inbounds i32, ptr {{%.*}}, i64 [[VLA_NUM_ELEMENTS_POST]]
88 //X64-NEXT: store ptr [[VLA_END_PTR]], ptr %__end1
89 //AMDGCN-NEXT: store ptr [[VLA_END_PTR]], ptr [[END]]
93 void test3(int b
, int c
) {
94 // CHECK-LABEL: define{{.*}} void {{.*}}test3{{.*}}(i32 noundef %b, i32 noundef %c)
96 // AMDGCN: %__end1 = alloca ptr, align 8, addrspace(5)
97 // AMDGCN: [[END:%.*]] = addrspacecast ptr addrspace(5) %__end1 to ptr
98 // get the address of %b by checking the first store that stores it
99 //CHECK: store i32 %b, ptr [[PTR_B:%.*]]
100 //CHECK-NEXT: store i32 %c, ptr [[PTR_C:%.*]]
102 // get the size of the VLA by getting the first load of the PTR_B
103 //CHECK: [[VLA_DIM1_PREZEXT:%.*]] = load i32, ptr [[PTR_B]]
104 //CHECK-NEXT: [[VLA_DIM1_PRE:%.*]] = zext i32 [[VLA_DIM1_PREZEXT]]
105 //CHECK: [[VLA_DIM2_PREZEXT:%.*]] = load i32, ptr [[PTR_C]]
106 //CHECK-NEXT: [[VLA_DIM2_PRE:%.*]] = zext i32 [[VLA_DIM2_PREZEXT]]
110 //CHECK: store i32 15, ptr [[PTR_B]]
111 //CHECK: store i32 15, ptr [[PTR_C]]
112 // Now get the sizeof, and then divide by the element size
114 // multiply the two dimensions, then by the element type and then divide by the sizeof dim2
115 //CHECK: [[VLA_DIM1_X_DIM2:%.*]] = mul nuw i64 [[VLA_DIM1_PRE]], [[VLA_DIM2_PRE]]
116 //CHECK-NEXT: [[VLA_SIZEOF:%.*]] = mul nuw i64 4, [[VLA_DIM1_X_DIM2]]
117 //CHECK-NEXT: [[VLA_SIZEOF_DIM2:%.*]] = mul nuw i64 4, [[VLA_DIM2_PRE]]
118 //CHECK-NEXT: [[VLA_NUM_ELEMENTS:%.*]] = udiv i64 [[VLA_SIZEOF]], [[VLA_SIZEOF_DIM2]]
119 //CHECK-NEXT: [[VLA_END_INDEX:%.*]] = mul nsw i64 [[VLA_NUM_ELEMENTS]], [[VLA_DIM2_PRE]]
120 //CHECK-NEXT: [[VLA_END_PTR:%.*]] = getelementptr inbounds i32, ptr {{%.*}}, i64 [[VLA_END_INDEX]]
121 //X64-NEXT: store ptr [[VLA_END_PTR]], ptr %__end
122 //AMDGCN-NEXT: store ptr [[VLA_END_PTR]], ptr [[END]]
124 for (auto &d
: varr
) 0;