1 ; RUN: opt < %s -separate-const-offset-from-gep -dce -S | FileCheck %s
3 ; Several unit tests for -separate-const-offset-from-gep. The transformation
4 ; heavily relies on TargetTransformInfo, so we put these tests under
5 ; target-specific folders.
7 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
8 ; target triple is necessary; otherwise TargetTransformInfo rejects any
10 target triple = "nvptx64-unknown-unknown"
12 %struct.S = type { float, double }
14 @struct_array = global [1024 x %struct.S] zeroinitializer, align 16
15 @float_2d_array = global [32 x [32 x float]] zeroinitializer, align 4
17 ; We should not extract any struct field indices, because fields in a struct
18 ; may have different types.
19 define double* @struct(i32 %i) {
21 %add = add nsw i32 %i, 5
22 %idxprom = sext i32 %add to i64
23 %p = getelementptr inbounds [1024 x %struct.S]* @struct_array, i64 0, i64 %idxprom, i32 1
26 ; CHECK-LABEL: @struct
27 ; CHECK: getelementptr [1024 x %struct.S]* @struct_array, i64 0, i32 %i, i32 1
29 ; We should be able to trace into sext/zext if it's directly used as a GEP
31 define float* @sext_zext(i32 %i, i32 %j) {
35 %i1.ext = sext i32 %i1 to i64
36 %j2.ext = zext i32 %j2 to i64
37 %p = getelementptr inbounds [32 x [32 x float]]* @float_2d_array, i64 0, i64 %i1.ext, i64 %j2.ext
40 ; CHECK-LABEL: @sext_zext
41 ; CHECK: getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i32 %i, i32 %j
42 ; CHECK: getelementptr float* %{{[0-9]+}}, i64 34
44 ; We should be able to trace into sext/zext if it can be distributed to both
45 ; operands, e.g., sext (add nsw a, b) == add nsw (sext a), (sext b)
46 define float* @ext_add_no_overflow(i64 %a, i32 %b, i64 %c, i32 %d) {
47 %b1 = add nsw i32 %b, 1
48 %b2 = sext i32 %b1 to i64
50 %d1 = add nuw i32 %d, 1
51 %d2 = zext i32 %d1 to i64
53 %p = getelementptr inbounds [32 x [32 x float]]* @float_2d_array, i64 0, i64 %i, i64 %j
56 ; CHECK-LABEL: @ext_add_no_overflow
57 ; CHECK: [[BASE_PTR:%[0-9]+]] = getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 %{{[0-9]+}}, i64 %{{[0-9]+}}
58 ; CHECK: getelementptr float* [[BASE_PTR]], i64 33
60 ; Similar to @ext_add_no_overflow, we should be able to trace into sext/zext if
61 ; its operand is an "or" instruction.
62 define float* @ext_or(i64 %a, i32 %b) {
67 %b2.ext = sext i32 %b2 to i64
68 %b3.ext = sext i32 %b3 to i64
69 %i = add i64 %a, %b2.ext
70 %j = add i64 %a, %b3.ext
71 %p = getelementptr inbounds [32 x [32 x float]]* @float_2d_array, i64 0, i64 %i, i64 %j
74 ; CHECK-LABEL: @ext_or
75 ; CHECK: [[BASE_PTR:%[0-9]+]] = getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 %{{[0-9]+}}, i64 %{{[0-9]+}}
76 ; CHECK: getelementptr float* [[BASE_PTR]], i64 34
78 ; We should treat "or" with no common bits (%k) as "add", and leave "or" with
79 ; potentially common bits (%l) as is.
80 define float* @or(i64 %i) {
83 %k = or i64 %j, 3 ; no common bits
84 %l = or i64 %j, 4 ; potentially common bits
85 %p = getelementptr inbounds [32 x [32 x float]]* @float_2d_array, i64 0, i64 %k, i64 %l
89 ; CHECK: [[BASE_PTR:%[0-9]+]] = getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 %j, i64 %l
90 ; CHECK: getelementptr float* [[BASE_PTR]], i64 96
92 ; The subexpression (b + 5) is used in both "i = a + (b + 5)" and "*out = b +
93 ; 5". When extracting the constant offset 5, make sure "*out = b + 5" isn't
95 define float* @expr(i64 %a, i64 %b, i64* %out) {
99 %p = getelementptr inbounds [32 x [32 x float]]* @float_2d_array, i64 0, i64 %i, i64 0
100 store i64 %b5, i64* %out
104 ; CHECK: [[BASE_PTR:%[0-9]+]] = getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 %0, i64 0
105 ; CHECK: getelementptr float* [[BASE_PTR]], i64 160
106 ; CHECK: store i64 %b5, i64* %out
108 ; Verifies we handle "sub" correctly.
109 define float* @sub(i64 %i, i64 %j) {
110 %i2 = sub i64 %i, 5 ; i - 5
111 %j2 = sub i64 5, %j ; 5 - i
112 %p = getelementptr inbounds [32 x [32 x float]]* @float_2d_array, i64 0, i64 %i2, i64 %j2
116 ; CHECK: %[[j2:[0-9]+]] = sub i64 0, %j
117 ; CHECK: [[BASE_PTR:%[0-9]+]] = getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 %i, i64 %[[j2]]
118 ; CHECK: getelementptr float* [[BASE_PTR]], i64 -155
120 %struct.Packed = type <{ [3 x i32], [8 x i64] }> ; <> means packed
122 ; Verifies we can emit correct uglygep if the address is not natually aligned.
123 define i64* @packed_struct(i32 %i, i32 %j) {
125 %s = alloca [1024 x %struct.Packed], align 16
126 %add = add nsw i32 %j, 3
127 %idxprom = sext i32 %add to i64
128 %add1 = add nsw i32 %i, 1
129 %idxprom2 = sext i32 %add1 to i64
130 %arrayidx3 = getelementptr inbounds [1024 x %struct.Packed]* %s, i64 0, i64 %idxprom2, i32 1, i64 %idxprom
133 ; CHECK-LABEL: @packed_struct
134 ; CHECK: [[BASE_PTR:%[0-9]+]] = getelementptr [1024 x %struct.Packed]* %s, i64 0, i32 %i, i32 1, i32 %j
135 ; CHECK: [[CASTED_PTR:%[0-9]+]] = bitcast i64* [[BASE_PTR]] to i8*
136 ; CHECK: %uglygep = getelementptr i8* [[CASTED_PTR]], i64 100
137 ; CHECK: bitcast i8* %uglygep to i64*