1 // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin %s -emit-llvm -o - | FileCheck %s 2 3 template<typename T> 4 struct S { 5 static int n; 6 }; 7 template<typename T> int S<T>::n = 5; 8 9 int f() { 10 // Make sure that the reference here is enough to trigger the instantiation of 11 // the static data member. 12 // CHECK: @_ZN1SIiE1nE = linkonce_odr global i32 5 13 int a[S<int>::n]; 14 return sizeof a; 15 } 16 17 // rdar://problem/9506377 18 void test0(void *array, int n) { 19 // CHECK-LABEL: define void @_Z5test0Pvi( 20 // CHECK: [[ARRAY:%.*]] = alloca i8*, align 8 21 // CHECK-NEXT: [[N:%.*]] = alloca i32, align 4 22 // CHECK-NEXT: [[REF:%.*]] = alloca i16*, align 8 23 // CHECK-NEXT: [[S:%.*]] = alloca i16, align 2 24 // CHECK-NEXT: store i8* 25 // CHECK-NEXT: store i32 26 27 // Capture the bounds. 28 // CHECK-NEXT: [[T0:%.*]] = load i32, i32* [[N]], align 4 29 // CHECK-NEXT: [[DIM0:%.*]] = zext i32 [[T0]] to i64 30 // CHECK-NEXT: [[T0:%.*]] = load i32, i32* [[N]], align 4 31 // CHECK-NEXT: [[T1:%.*]] = add nsw i32 [[T0]], 1 32 // CHECK-NEXT: [[DIM1:%.*]] = zext i32 [[T1]] to i64 33 typedef short array_t[n][n+1]; 34 35 // CHECK-NEXT: [[T0:%.*]] = load i8*, i8** [[ARRAY]], align 8 36 // CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[T0]] to i16* 37 // CHECK-NEXT: store i16* [[T1]], i16** [[REF]], align 8 38 array_t &ref = *(array_t*) array; 39 40 // CHECK-NEXT: [[T0:%.*]] = load i16*, i16** [[REF]] 41 // CHECK-NEXT: [[T1:%.*]] = mul nsw i64 1, [[DIM1]] 42 // CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds i16, i16* [[T0]], i64 [[T1]] 43 // CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds i16, i16* [[T2]], i64 2 44 // CHECK-NEXT: store i16 3, i16* [[T3]] 45 ref[1][2] = 3; 46 47 // CHECK-NEXT: [[T0:%.*]] = load i16*, i16** [[REF]] 48 // CHECK-NEXT: [[T1:%.*]] = mul nsw i64 4, [[DIM1]] 49 // CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds i16, i16* [[T0]], i64 [[T1]] 50 // CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds i16, i16* [[T2]], i64 5 51 // CHECK-NEXT: [[T4:%.*]] = load i16, i16* [[T3]] 52 // CHECK-NEXT: store i16 [[T4]], i16* [[S]], align 2 53 short s = ref[4][5]; 54 55 // CHECK-NEXT: ret void 56 } 57 58 59 void test2(int b) { 60 // CHECK-LABEL: define void {{.*}}test2{{.*}}(i32 %b) 61 int varr[b]; 62 // get the address of %b by checking the first store that stores it 63 //CHECK: store i32 %b, i32* [[PTR_B:%.*]] 64 65 // get the size of the VLA by getting the first load of the PTR_B 66 //CHECK: [[VLA_NUM_ELEMENTS_PREZEXT:%.*]] = load i32, i32* [[PTR_B]] 67 //CHECK-NEXT: [[VLA_NUM_ELEMENTS_PRE:%.*]] = zext i32 [[VLA_NUM_ELEMENTS_PREZEXT]] 68 69 b = 15; 70 //CHECK: store i32 15, i32* [[PTR_B]] 71 72 // Now get the sizeof, and then divide by the element size 73 74 75 //CHECK: [[VLA_SIZEOF:%.*]] = mul nuw i64 4, [[VLA_NUM_ELEMENTS_PRE]] 76 //CHECK-NEXT: [[VLA_NUM_ELEMENTS_POST:%.*]] = udiv i64 [[VLA_SIZEOF]], 4 77 //CHECK-NEXT: [[VLA_END_PTR:%.*]] = getelementptr inbounds i32, i32* {{%.*}}, i64 [[VLA_NUM_ELEMENTS_POST]] 78 //CHECK-NEXT: store i32* [[VLA_END_PTR]], i32** %__end 79 for (int d : varr) 0; 80 } 81 82 void test3(int b, int c) { 83 // CHECK-LABEL: define void {{.*}}test3{{.*}}(i32 %b, i32 %c) 84 int varr[b][c]; 85 // get the address of %b by checking the first store that stores it 86 //CHECK: store i32 %b, i32* [[PTR_B:%.*]] 87 //CHECK-NEXT: store i32 %c, i32* [[PTR_C:%.*]] 88 89 // get the size of the VLA by getting the first load of the PTR_B 90 //CHECK: [[VLA_DIM1_PREZEXT:%.*]] = load i32, i32* [[PTR_B]] 91 //CHECK-NEXT: [[VLA_DIM1_PRE:%.*]] = zext i32 [[VLA_DIM1_PREZEXT]] 92 //CHECK: [[VLA_DIM2_PREZEXT:%.*]] = load i32, i32* [[PTR_C]] 93 //CHECK-NEXT: [[VLA_DIM2_PRE:%.*]] = zext i32 [[VLA_DIM2_PREZEXT]] 94 95 b = 15; 96 c = 15; 97 //CHECK: store i32 15, i32* [[PTR_B]] 98 //CHECK: store i32 15, i32* [[PTR_C]] 99 // Now get the sizeof, and then divide by the element size 100 101 // multiply the two dimensions, then by the element type and then divide by the sizeof dim2 102 //CHECK: [[VLA_DIM1_X_DIM2:%.*]] = mul nuw i64 [[VLA_DIM1_PRE]], [[VLA_DIM2_PRE]] 103 //CHECK-NEXT: [[VLA_SIZEOF:%.*]] = mul nuw i64 4, [[VLA_DIM1_X_DIM2]] 104 //CHECK-NEXT: [[VLA_SIZEOF_DIM2:%.*]] = mul nuw i64 4, [[VLA_DIM2_PRE]] 105 //CHECK-NEXT: [[VLA_NUM_ELEMENTS:%.*]] = udiv i64 [[VLA_SIZEOF]], [[VLA_SIZEOF_DIM2]] 106 //CHECK-NEXT: [[VLA_END_INDEX:%.*]] = mul nsw i64 [[VLA_NUM_ELEMENTS]], [[VLA_DIM2_PRE]] 107 //CHECK-NEXT: [[VLA_END_PTR:%.*]] = getelementptr inbounds i32, i32* {{%.*}}, i64 [[VLA_END_INDEX]] 108 //CHECK-NEXT: store i32* [[VLA_END_PTR]], i32** %__end 109 110 for (auto &d : varr) 0; 111 } 112 113 114