1 ; RUN: opt < %s -mtriple=nvptx64-nvidia-cuda -separate-const-offset-from-gep \
2 ; RUN: -reassociate-geps-verify-no-dead-code -S | FileCheck %s
4 ; Several unit tests for -separate-const-offset-from-gep. The transformation
5 ; heavily relies on TargetTransformInfo, so we put these tests under
6 ; target-specific folders.
8 %struct.S = type { float, double }
10 @struct_array = global [1024 x %struct.S] zeroinitializer, align 16
11 @float_2d_array = global [32 x [32 x float]] zeroinitializer, align 4
13 ; We should not extract any struct field indices, because fields in a struct
14 ; may have different types.
15 define double* @struct(i32 %i) {
17 %add = add nsw i32 %i, 5
18 %idxprom = sext i32 %add to i64
19 %p = getelementptr inbounds [1024 x %struct.S], [1024 x %struct.S]* @struct_array, i64 0, i64 %idxprom, i32 1
22 ; CHECK-LABEL: @struct(
23 ; CHECK: getelementptr [1024 x %struct.S], [1024 x %struct.S]* @struct_array, i64 0, i64 %{{[a-zA-Z0-9]+}}, i32 1
25 ; We should be able to trace into sext(a + b) if a + b is non-negative
26 ; (e.g., used as an index of an inbounds GEP) and one of a and b is
28 define float* @sext_add(i32 %i, i32 %j) {
31 %1 = sext i32 %0 to i64 ; inbound sext(i + 1) = sext(i) + 1
33 ; However, inbound sext(j + -2) != sext(j) + -2, e.g., j = INT_MIN
34 %3 = sext i32 %2 to i64
35 %p = getelementptr inbounds [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 %1, i64 %3
38 ; CHECK-LABEL: @sext_add(
40 ; CHECK: add i32 %j, -2
42 ; CHECK: getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 %{{[a-zA-Z0-9]+}}, i64 %{{[a-zA-Z0-9]+}}
43 ; CHECK: getelementptr inbounds float, float* %{{[a-zA-Z0-9]+}}, i64 32
45 ; We should be able to trace into sext/zext if it can be distributed to both
46 ; operands, e.g., sext (add nsw a, b) == add nsw (sext a), (sext b)
48 ; This test verifies we can transform
49 ; gep base, a + sext(b +nsw 1), c + zext(d +nuw 1)
51 ; gep base, a + sext(b), c + zext(d); gep ..., 1 * 32 + 1
52 define float* @ext_add_no_overflow(i64 %a, i32 %b, i64 %c, i32 %d) {
53 %b1 = add nsw i32 %b, 1
54 %b2 = sext i32 %b1 to i64
55 %i = add i64 %a, %b2 ; i = a + sext(b +nsw 1)
56 %d1 = add nuw i32 %d, 1
57 %d2 = zext i32 %d1 to i64
58 %j = add i64 %c, %d2 ; j = c + zext(d +nuw 1)
59 %p = getelementptr inbounds [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 %i, i64 %j
62 ; CHECK-LABEL: @ext_add_no_overflow(
63 ; CHECK: [[BASE_PTR:%[a-zA-Z0-9]+]] = getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 %{{[a-zA-Z0-9]+}}, i64 %{{[a-zA-Z0-9]+}}
64 ; CHECK: getelementptr inbounds float, float* [[BASE_PTR]], i64 33
66 ; Verifies we handle nested sext/zext correctly.
67 define void @sext_zext(i32 %a, i32 %b, float** %out1, float** %out2) {
69 %0 = add nsw nuw i32 %a, 1
70 %1 = sext i32 %0 to i48
71 %2 = zext i48 %1 to i64 ; zext(sext(a +nsw nuw 1)) = zext(sext(a)) + 1
72 %3 = add nsw i32 %b, 2
73 %4 = sext i32 %3 to i48
74 %5 = zext i48 %4 to i64 ; zext(sext(b +nsw 2)) != zext(sext(b)) + 2
75 %p1 = getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 %2, i64 %5
76 store float* %p1, float** %out1
77 %6 = add nuw i32 %a, 3
78 %7 = zext i32 %6 to i48
79 %8 = sext i48 %7 to i64 ; sext(zext(a +nuw 3)) = zext(a +nuw 3) = zext(a) + 3
80 %9 = add nsw i32 %b, 4
81 %10 = zext i32 %9 to i48
82 %11 = sext i48 %10 to i64 ; sext(zext(b +nsw 4)) != zext(b) + 4
83 %p2 = getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 %8, i64 %11
84 store float* %p2, float** %out2
87 ; CHECK-LABEL: @sext_zext(
88 ; CHECK: [[BASE_PTR_1:%[a-zA-Z0-9]+]] = getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 %{{[a-zA-Z0-9]+}}, i64 %{{[a-zA-Z0-9]+}}
89 ; CHECK: getelementptr float, float* [[BASE_PTR_1]], i64 32
90 ; CHECK: [[BASE_PTR_2:%[a-zA-Z0-9]+]] = getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 %{{[a-zA-Z0-9]+}}, i64 %{{[a-zA-Z0-9]+}}
91 ; CHECK: getelementptr float, float* [[BASE_PTR_2]], i64 96
93 ; Similar to @ext_add_no_overflow, we should be able to trace into s/zext if
94 ; its operand is an OR and the two operands of the OR have no common bits.
95 define float* @sext_or(i64 %a, i32 %b) {
98 %b2 = or i32 %b1, 1 ; (b << 2) and 1 have no common bits
99 %b3 = or i32 %b1, 4 ; (b << 2) and 4 may have common bits
100 %b2.ext = zext i32 %b2 to i64
101 %b3.ext = sext i32 %b3 to i64
102 %i = add i64 %a, %b2.ext
103 %j = add i64 %a, %b3.ext
104 %p = getelementptr inbounds [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 %i, i64 %j
107 ; CHECK-LABEL: @sext_or(
108 ; CHECK: [[BASE_PTR:%[a-zA-Z0-9]+]] = getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 %{{[a-zA-Z0-9]+}}, i64 %{{[a-zA-Z0-9]+}}
109 ; CHECK: getelementptr inbounds float, float* [[BASE_PTR]], i64 32
111 ; The subexpression (b + 5) is used in both "i = a + (b + 5)" and "*out = b +
112 ; 5". When extracting the constant offset 5, make sure "*out = b + 5" isn't
114 define float* @expr(i64 %a, i64 %b, i64* %out) {
118 %p = getelementptr inbounds [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 %i, i64 0
119 store i64 %b5, i64* %out
122 ; CHECK-LABEL: @expr(
123 ; CHECK: [[BASE_PTR:%[a-zA-Z0-9]+]] = getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 %{{[a-zA-Z0-9]+}}, i64 0
124 ; CHECK: getelementptr inbounds float, float* [[BASE_PTR]], i64 160
125 ; CHECK: store i64 %b5, i64* %out
127 ; d + sext(a +nsw (b +nsw (c +nsw 8))) => (d + sext(a) + sext(b) + sext(c)) + 8
128 define float* @sext_expr(i32 %a, i32 %b, i32 %c, i64 %d) {
130 %0 = add nsw i32 %c, 8
131 %1 = add nsw i32 %b, %0
132 %2 = add nsw i32 %a, %1
133 %3 = sext i32 %2 to i64
135 %p = getelementptr inbounds [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 0, i64 %i
138 ; CHECK-LABEL: @sext_expr(
142 ; CHECK: getelementptr inbounds float, float* %{{[a-zA-Z0-9]+}}, i64 8
144 ; Verifies we handle "sub" correctly.
145 define float* @sub(i64 %i, i64 %j) {
146 %i2 = sub i64 %i, 5 ; i - 5
147 %j2 = sub i64 5, %j ; 5 - i
148 %p = getelementptr inbounds [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 %i2, i64 %j2
152 ; CHECK: %[[j2:[a-zA-Z0-9]+]] = sub i64 0, %j
153 ; CHECK: [[BASE_PTR:%[a-zA-Z0-9]+]] = getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 %i, i64 %[[j2]]
154 ; CHECK: getelementptr inbounds float, float* [[BASE_PTR]], i64 -155
156 %struct.Packed = type <{ [3 x i32], [8 x i64] }> ; <> means packed
158 ; Verifies we can emit correct uglygep if the address is not natually aligned.
159 define i64* @packed_struct(i32 %i, i32 %j) {
161 %s = alloca [1024 x %struct.Packed], align 16
162 %add = add nsw i32 %j, 3
163 %idxprom = sext i32 %add to i64
164 %add1 = add nsw i32 %i, 1
165 %idxprom2 = sext i32 %add1 to i64
166 %arrayidx3 = getelementptr inbounds [1024 x %struct.Packed], [1024 x %struct.Packed]* %s, i64 0, i64 %idxprom2, i32 1, i64 %idxprom
169 ; CHECK-LABEL: @packed_struct(
170 ; CHECK: [[BASE_PTR:%[a-zA-Z0-9]+]] = getelementptr [1024 x %struct.Packed], [1024 x %struct.Packed]* %s, i64 0, i64 %{{[a-zA-Z0-9]+}}, i32 1, i64 %{{[a-zA-Z0-9]+}}
171 ; CHECK: [[CASTED_PTR:%[a-zA-Z0-9]+]] = bitcast i64* [[BASE_PTR]] to i8*
172 ; CHECK: %uglygep = getelementptr inbounds i8, i8* [[CASTED_PTR]], i64 100
173 ; CHECK: bitcast i8* %uglygep to i64*
175 ; We shouldn't be able to extract the 8 from "zext(a +nuw (b + 8))",
176 ; because "zext(b + 8) != zext(b) + 8"
177 define float* @zext_expr(i32 %a, i32 %b) {
180 %1 = add nuw i32 %a, %0
181 %i = zext i32 %1 to i64
182 %p = getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 0, i64 %i
185 ; CHECK-LABEL: zext_expr(
186 ; CHECK: getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 0, i64 %i
188 ; Per http://llvm.org/docs/LangRef.html#id181, the indices of a off-bound gep
189 ; should be considered sign-extended to the pointer size. Therefore,
190 ; gep base, (add i32 a, b) != gep (gep base, i32 a), i32 b
192 ; sext(a + b) != sext(a) + sext(b)
194 ; This test verifies we do not illegitimately extract the 8 from
195 ; gep base, (i32 a + 8)
196 define float* @i32_add(i32 %a) {
199 %p = getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 0, i32 %i
202 ; CHECK-LABEL: @i32_add(
203 ; CHECK: getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 0, i64 %{{[a-zA-Z0-9]+}}
204 ; CHECK-NOT: getelementptr
206 ; Verifies that we compute the correct constant offset when the index is
207 ; sign-extended and then zero-extended. The old version of our code failed to
208 ; handle this case because it simply computed the constant offset as the
209 ; sign-extended value of the constant part of the GEP index.
210 define float* @apint(i1 %a) {
212 %0 = add nsw nuw i1 %a, 1
213 %1 = sext i1 %0 to i4
214 %2 = zext i4 %1 to i64 ; zext (sext i1 1 to i4) to i64 = 15
215 %p = getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 0, i64 %2
218 ; CHECK-LABEL: @apint(
219 ; CHECK: [[BASE_PTR:%[a-zA-Z0-9]+]] = getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 0, i64 %{{[a-zA-Z0-9]+}}
220 ; CHECK: getelementptr float, float* [[BASE_PTR]], i64 15
222 ; Do not trace into binary operators other than ADD, SUB, and OR.
223 define float* @and(i64 %a) {
227 %p = getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array, i64 0, i64 0, i64 %1
231 ; CHECK: getelementptr [32 x [32 x float]], [32 x [32 x float]]* @float_2d_array
232 ; CHECK-NOT: getelementptr
234 ; The code that rebuilds an OR expression used to be buggy, and failed on this
236 define float* @shl_add_or(i64 %a, float* %ptr) {
237 ; CHECK-LABEL: @shl_add_or(
240 %add = add i64 %shl, 12
242 ; CHECK: [[OR:%or[0-9]*]] = add i64 %shl, 1
243 ; ((a << 2) + 12) and 1 have no common bits. Therefore,
244 ; SeparateConstOffsetFromGEP is able to extract the 12.
245 ; TODO(jingyue): We could reassociate the expression to combine 12 and 1.
246 %p = getelementptr float, float* %ptr, i64 %or
247 ; CHECK: [[PTR:%[a-zA-Z0-9]+]] = getelementptr float, float* %ptr, i64 [[OR]]
248 ; CHECK: getelementptr float, float* [[PTR]], i64 12
253 ; The source code used to be buggy in checking
254 ; (AccumulativeByteOffset % ElementTypeSizeOfGEP == 0)
255 ; where AccumulativeByteOffset is signed but ElementTypeSizeOfGEP is unsigned.
256 ; The compiler would promote AccumulativeByteOffset to unsigned, causing
257 ; unexpected results. For example, while -64 % (int64_t)24 != 0,
258 ; -64 % (uint64_t)24 == 0.
259 %struct3 = type { i64, i32 }
260 %struct2 = type { %struct3, i32 }
261 %struct1 = type { i64, %struct2 }
262 %struct0 = type { i32, i32, i64*, [100 x %struct1] }
263 define %struct2* @sign_mod_unsign(%struct0* %ptr, i64 %idx) {
264 ; CHECK-LABEL: @sign_mod_unsign(
266 %arrayidx = add nsw i64 %idx, -2
268 %ptr2 = getelementptr inbounds %struct0, %struct0* %ptr, i64 0, i32 3, i64 %arrayidx, i32 1
269 ; CHECK: [[PTR:%[a-zA-Z0-9]+]] = getelementptr %struct0, %struct0* %ptr, i64 0, i32 3, i64 %idx, i32 1
270 ; CHECK: getelementptr inbounds %struct2, %struct2* [[PTR]], i64 -3
275 ; Check that we can see through explicit trunc() instruction.
276 define %struct2* @trunk_explicit(%struct0* %ptr, i64 %idx) {
277 ; CHECK-LABEL: @trunk_explicit(
279 %idx0 = trunc i64 1 to i32
280 %ptr2 = getelementptr inbounds %struct0, %struct0* %ptr, i32 %idx0, i32 3, i64 %idx, i32 1
282 ; CHECK: [[PTR:%[a-zA-Z0-9]+]] = getelementptr %struct0, %struct0* %ptr, i64 0, i32 3, i64 %idx, i32 1
283 ; CHECK: getelementptr inbounds %struct2, %struct2* %0, i64 151
288 ; Check that we can deal with trunc inserted by
289 ; canonicalizeArrayIndicesToPointerSize() if size of an index is larger than
290 ; that of the pointer.
291 define %struct2* @trunk_long_idx(%struct0* %ptr, i64 %idx) {
292 ; CHECK-LABEL: @trunk_long_idx(
294 %ptr2 = getelementptr inbounds %struct0, %struct0* %ptr, i65 1, i32 3, i64 %idx, i32 1
296 ; CHECK: [[PTR:%[a-zA-Z0-9]+]] = getelementptr %struct0, %struct0* %ptr, i64 0, i32 3, i64 %idx, i32 1
297 ; CHECK: getelementptr inbounds %struct2, %struct2* %0, i64 151