1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
2 ; RUN: opt < %s -mtriple=nvptx64-nvidia-cuda -passes=separate-const-offset-from-gep \
3 ; RUN: -reassociate-geps-verify-no-dead-code -S | FileCheck %s
5 ; Several unit tests for separate-const-offset-from-gep. The transformation
6 ; heavily relies on TargetTransformInfo, so we put these tests under
7 ; target-specific folders.
9 %struct.S = type { float, double }
11 @struct_array = global [1024 x %struct.S] zeroinitializer, align 16
12 @float_2d_array = global [32 x [32 x float]] zeroinitializer, align 4
14 ; We should not extract any struct field indices, because fields in a struct
15 ; may have different types.
16 define ptr @struct(i32 %i) {
17 ; CHECK-LABEL: define ptr @struct(
18 ; CHECK-SAME: i32 [[I:%.*]]) {
20 ; CHECK-NEXT: [[TMP0:%.*]] = sext i32 [[I]] to i64
21 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr [1024 x %struct.S], ptr @struct_array, i64 0, i64 [[TMP0]], i32 1
22 ; CHECK-NEXT: [[P2:%.*]] = getelementptr inbounds i8, ptr [[TMP1]], i64 80
23 ; CHECK-NEXT: ret ptr [[P2]]
26 %add = add nsw i32 %i, 5
27 %idxprom = sext i32 %add to i64
28 %p = getelementptr inbounds [1024 x %struct.S], ptr @struct_array, i64 0, i64 %idxprom, i32 1
32 ; We should be able to trace into sext(a + b) if a + b is non-negative
33 ; (e.g., used as an index of an inbounds GEP) and one of a and b is
35 define ptr @sext_add(i32 %i, i32 %j) {
36 ; CHECK-LABEL: define ptr @sext_add(
37 ; CHECK-SAME: i32 [[I:%.*]], i32 [[J:%.*]]) {
39 ; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[J]], -2
40 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[TMP0]] to i64
41 ; CHECK-NEXT: [[TMP2:%.*]] = sext i32 [[I]] to i64
42 ; CHECK-NEXT: [[TMP3:%.*]] = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 [[TMP2]], i64 [[TMP1]]
43 ; CHECK-NEXT: [[P1:%.*]] = getelementptr inbounds i8, ptr [[TMP3]], i64 128
44 ; CHECK-NEXT: ret ptr [[P1]]
48 %1 = sext i32 %0 to i64 ; inbound sext(i + 1) = sext(i) + 1
50 ; However, inbound sext(j + -2) != sext(j) + -2, e.g., j = INT_MIN
51 %3 = sext i32 %2 to i64
52 %p = getelementptr inbounds [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 %1, i64 %3
56 ; We should be able to trace into sext/zext if it can be distributed to both
57 ; operands, e.g., sext (add nsw a, b) == add nsw (sext a), (sext b)
59 ; This test verifies we can transform
60 ; gep base, a + sext(b +nsw 1), c + zext(d +nuw 1)
62 ; gep base, a + sext(b), c + zext(d); gep ..., 1 * 32 + 1
63 define ptr @ext_add_no_overflow(i64 %a, i32 %b, i64 %c, i32 %d) {
64 ; CHECK-LABEL: define ptr @ext_add_no_overflow(
65 ; CHECK-SAME: i64 [[A:%.*]], i32 [[B:%.*]], i64 [[C:%.*]], i32 [[D:%.*]]) {
66 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[B]] to i64
67 ; CHECK-NEXT: [[I2:%.*]] = add i64 [[A]], [[TMP1]]
68 ; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[D]] to i64
69 ; CHECK-NEXT: [[J4:%.*]] = add i64 [[C]], [[TMP2]]
70 ; CHECK-NEXT: [[TMP3:%.*]] = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 [[I2]], i64 [[J4]]
71 ; CHECK-NEXT: [[P5:%.*]] = getelementptr inbounds i8, ptr [[TMP3]], i64 132
72 ; CHECK-NEXT: ret ptr [[P5]]
74 %b1 = add nsw i32 %b, 1
75 %b2 = sext i32 %b1 to i64
76 %i = add i64 %a, %b2 ; i = a + sext(b +nsw 1)
77 %d1 = add nuw i32 %d, 1
78 %d2 = zext i32 %d1 to i64
79 %j = add i64 %c, %d2 ; j = c + zext(d +nuw 1)
80 %p = getelementptr inbounds [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 %i, i64 %j
84 ; Verifies we handle nested sext/zext correctly.
85 define void @sext_zext(i32 %a, i32 %b, ptr %out1, ptr %out2) {
86 ; CHECK-LABEL: define void @sext_zext(
87 ; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], ptr [[OUT1:%.*]], ptr [[OUT2:%.*]]) {
89 ; CHECK-NEXT: [[TMP0:%.*]] = add nsw i32 [[B]], 2
90 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[TMP0]] to i48
91 ; CHECK-NEXT: [[TMP2:%.*]] = zext i48 [[TMP1]] to i64
92 ; CHECK-NEXT: [[TMP3:%.*]] = sext i32 [[A]] to i48
93 ; CHECK-NEXT: [[TMP4:%.*]] = zext i48 [[TMP3]] to i64
94 ; CHECK-NEXT: [[TMP5:%.*]] = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 [[TMP4]], i64 [[TMP2]]
95 ; CHECK-NEXT: [[P11:%.*]] = getelementptr i8, ptr [[TMP5]], i64 128
96 ; CHECK-NEXT: store ptr [[P11]], ptr [[OUT1]], align 8
97 ; CHECK-NEXT: [[TMP6:%.*]] = add nsw i32 [[B]], 4
98 ; CHECK-NEXT: [[TMP7:%.*]] = zext i32 [[TMP6]] to i48
99 ; CHECK-NEXT: [[TMP8:%.*]] = sext i48 [[TMP7]] to i64
100 ; CHECK-NEXT: [[TMP9:%.*]] = zext i32 [[A]] to i48
101 ; CHECK-NEXT: [[TMP10:%.*]] = sext i48 [[TMP9]] to i64
102 ; CHECK-NEXT: [[TMP11:%.*]] = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 [[TMP10]], i64 [[TMP8]]
103 ; CHECK-NEXT: [[P22:%.*]] = getelementptr i8, ptr [[TMP11]], i64 384
104 ; CHECK-NEXT: store ptr [[P22]], ptr [[OUT2]], align 8
105 ; CHECK-NEXT: ret void
108 %0 = add nsw nuw i32 %a, 1
109 %1 = sext i32 %0 to i48
110 %2 = zext i48 %1 to i64 ; zext(sext(a +nsw nuw 1)) = zext(sext(a)) + 1
111 %3 = add nsw i32 %b, 2
112 %4 = sext i32 %3 to i48
113 %5 = zext i48 %4 to i64 ; zext(sext(b +nsw 2)) != zext(sext(b)) + 2
114 %p1 = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 %2, i64 %5
115 store ptr %p1, ptr %out1
116 %6 = add nuw i32 %a, 3
117 %7 = zext i32 %6 to i48
118 %8 = sext i48 %7 to i64 ; sext(zext(a +nuw 3)) = zext(a +nuw 3) = zext(a) + 3
119 %9 = add nsw i32 %b, 4
120 %10 = zext i32 %9 to i48
121 %11 = sext i48 %10 to i64 ; sext(zext(b +nsw 4)) != zext(b) + 4
122 %p2 = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 %8, i64 %11
123 store ptr %p2, ptr %out2
127 ; Similar to @ext_add_no_overflow, we should be able to trace into s/zext if
128 ; its operand is an OR and the two operands of the OR have no common bits.
129 define ptr @sext_or(i64 %a, i32 %b) {
130 ; CHECK-LABEL: define ptr @sext_or(
131 ; CHECK-SAME: i64 [[A:%.*]], i32 [[B:%.*]]) {
133 ; CHECK-NEXT: [[B1:%.*]] = shl i32 [[B]], 2
134 ; CHECK-NEXT: [[B3:%.*]] = or i32 [[B1]], 4
135 ; CHECK-NEXT: [[B3_EXT:%.*]] = sext i32 [[B3]] to i64
136 ; CHECK-NEXT: [[J:%.*]] = add i64 [[A]], [[B3_EXT]]
137 ; CHECK-NEXT: [[TMP0:%.*]] = zext i32 [[B1]] to i64
138 ; CHECK-NEXT: [[I2:%.*]] = add i64 [[A]], [[TMP0]]
139 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 [[I2]], i64 [[J]]
140 ; CHECK-NEXT: [[P3:%.*]] = getelementptr inbounds i8, ptr [[TMP1]], i64 128
141 ; CHECK-NEXT: ret ptr [[P3]]
145 %b2 = or i32 %b1, 1 ; (b << 2) and 1 have no common bits
146 %b3 = or i32 %b1, 4 ; (b << 2) and 4 may have common bits
147 %b2.ext = zext i32 %b2 to i64
148 %b3.ext = sext i32 %b3 to i64
149 %i = add i64 %a, %b2.ext
150 %j = add i64 %a, %b3.ext
151 %p = getelementptr inbounds [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 %i, i64 %j
155 ; The subexpression (b + 5) is used in both "i = a + (b + 5)" and "*out = b +
156 ; 5". When extracting the constant offset 5, make sure "*out = b + 5" isn't
158 define ptr @expr(i64 %a, i64 %b, ptr %out) {
159 ; CHECK-LABEL: define ptr @expr(
160 ; CHECK-SAME: i64 [[A:%.*]], i64 [[B:%.*]], ptr [[OUT:%.*]]) {
162 ; CHECK-NEXT: [[B5:%.*]] = add i64 [[B]], 5
163 ; CHECK-NEXT: [[I2:%.*]] = add i64 [[B]], [[A]]
164 ; CHECK-NEXT: [[TMP0:%.*]] = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 [[I2]], i64 0
165 ; CHECK-NEXT: [[P3:%.*]] = getelementptr inbounds i8, ptr [[TMP0]], i64 640
166 ; CHECK-NEXT: store i64 [[B5]], ptr [[OUT]], align 8
167 ; CHECK-NEXT: ret ptr [[P3]]
172 %p = getelementptr inbounds [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 %i, i64 0
173 store i64 %b5, ptr %out
177 ; d + sext(a +nsw (b +nsw (c +nsw 8))) => (d + sext(a) + sext(b) + sext(c)) + 8
178 define ptr @sext_expr(i32 %a, i32 %b, i32 %c, i64 %d) {
179 ; CHECK-LABEL: define ptr @sext_expr(
180 ; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i32 [[C:%.*]], i64 [[D:%.*]]) {
182 ; CHECK-NEXT: [[TMP0:%.*]] = sext i32 [[A]] to i64
183 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[B]] to i64
184 ; CHECK-NEXT: [[TMP2:%.*]] = sext i32 [[C]] to i64
185 ; CHECK-NEXT: [[TMP3:%.*]] = add i64 [[TMP1]], [[TMP2]]
186 ; CHECK-NEXT: [[TMP4:%.*]] = add i64 [[TMP0]], [[TMP3]]
187 ; CHECK-NEXT: [[I1:%.*]] = add i64 [[D]], [[TMP4]]
188 ; CHECK-NEXT: [[TMP5:%.*]] = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 0, i64 [[I1]]
189 ; CHECK-NEXT: [[P2:%.*]] = getelementptr inbounds i8, ptr [[TMP5]], i64 32
190 ; CHECK-NEXT: ret ptr [[P2]]
193 %0 = add nsw i32 %c, 8
194 %1 = add nsw i32 %b, %0
195 %2 = add nsw i32 %a, %1
196 %3 = sext i32 %2 to i64
198 %p = getelementptr inbounds [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 0, i64 %i
202 ; Verifies we handle "sub" correctly.
203 define ptr @sub(i64 %i, i64 %j) {
204 ; CHECK-LABEL: define ptr @sub(
205 ; CHECK-SAME: i64 [[I:%.*]], i64 [[J:%.*]]) {
206 ; CHECK-NEXT: [[J22:%.*]] = sub i64 0, [[J]]
207 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 [[I]], i64 [[J22]]
208 ; CHECK-NEXT: [[P3:%.*]] = getelementptr inbounds i8, ptr [[TMP1]], i64 -620
209 ; CHECK-NEXT: ret ptr [[P3]]
211 %i2 = sub i64 %i, 5 ; i - 5
212 %j2 = sub i64 5, %j ; 5 - i
213 %p = getelementptr inbounds [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 %i2, i64 %j2
217 %struct.Packed = type <{ [3 x i32], [8 x i64] }> ; <> means packed
219 ; Verifies we can emit correct uglygep if the address is not natually aligned.
220 define ptr @packed_struct(i32 %i, i32 %j) {
221 ; CHECK-LABEL: define ptr @packed_struct(
222 ; CHECK-SAME: i32 [[I:%.*]], i32 [[J:%.*]]) {
224 ; CHECK-NEXT: [[S:%.*]] = alloca [1024 x %struct.Packed], align 16
225 ; CHECK-NEXT: [[TMP0:%.*]] = sext i32 [[I]] to i64
226 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[J]] to i64
227 ; CHECK-NEXT: [[TMP2:%.*]] = getelementptr [1024 x %struct.Packed], ptr [[S]], i64 0, i64 [[TMP0]], i32 1, i64 [[TMP1]]
228 ; CHECK-NEXT: [[ARRAYIDX33:%.*]] = getelementptr inbounds i8, ptr [[TMP2]], i64 100
229 ; CHECK-NEXT: ret ptr [[ARRAYIDX33]]
232 %s = alloca [1024 x %struct.Packed], align 16
233 %add = add nsw i32 %j, 3
234 %idxprom = sext i32 %add to i64
235 %add1 = add nsw i32 %i, 1
236 %idxprom2 = sext i32 %add1 to i64
237 %arrayidx3 = getelementptr inbounds [1024 x %struct.Packed], ptr %s, i64 0, i64 %idxprom2, i32 1, i64 %idxprom
241 ; We shouldn't be able to extract the 8 from "zext(a +nuw (b + 8))",
242 ; because "zext(b + 8) != zext(b) + 8"
243 define ptr @zext_expr(i32 %a, i32 %b) {
244 ; CHECK-LABEL: define ptr @zext_expr(
245 ; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
247 ; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[B]], 8
248 ; CHECK-NEXT: [[TMP1:%.*]] = add nuw i32 [[A]], [[TMP0]]
249 ; CHECK-NEXT: [[I:%.*]] = zext i32 [[TMP1]] to i64
250 ; CHECK-NEXT: [[P:%.*]] = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 0, i64 [[I]]
251 ; CHECK-NEXT: ret ptr [[P]]
255 %1 = add nuw i32 %a, %0
256 %i = zext i32 %1 to i64
257 %p = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 0, i64 %i
261 ; Per http://llvm.org/docs/LangRef.html#id181, the indices of a off-bound gep
262 ; should be considered sign-extended to the pointer size. Therefore,
263 ; gep base, (add i32 a, b) != gep (gep base, i32 a), i32 b
265 ; sext(a + b) != sext(a) + sext(b)
267 ; This test verifies we do not illegitimately extract the 8 from
268 ; gep base, (i32 a + 8)
269 define ptr @i32_add(i32 %a) {
270 ; CHECK-LABEL: define ptr @i32_add(
271 ; CHECK-SAME: i32 [[A:%.*]]) {
273 ; CHECK-NEXT: [[I:%.*]] = add i32 [[A]], 8
274 ; CHECK-NEXT: [[IDXPROM:%.*]] = sext i32 [[I]] to i64
275 ; CHECK-NEXT: [[P:%.*]] = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 0, i64 [[IDXPROM]]
276 ; CHECK-NEXT: ret ptr [[P]]
280 %p = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 0, i32 %i
284 ; Verifies that we compute the correct constant offset when the index is
285 ; sign-extended and then zero-extended. The old version of our code failed to
286 ; handle this case because it simply computed the constant offset as the
287 ; sign-extended value of the constant part of the GEP index.
288 define ptr @apint(i1 %a) {
289 ; CHECK-LABEL: define ptr @apint(
290 ; CHECK-SAME: i1 [[A:%.*]]) {
292 ; CHECK-NEXT: [[TMP0:%.*]] = sext i1 [[A]] to i4
293 ; CHECK-NEXT: [[TMP1:%.*]] = zext i4 [[TMP0]] to i64
294 ; CHECK-NEXT: [[TMP2:%.*]] = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 0, i64 [[TMP1]]
295 ; CHECK-NEXT: [[P1:%.*]] = getelementptr i8, ptr [[TMP2]], i64 60
296 ; CHECK-NEXT: ret ptr [[P1]]
299 %0 = add nsw nuw i1 %a, 1
300 %1 = sext i1 %0 to i4
301 %2 = zext i4 %1 to i64 ; zext (sext i1 1 to i4) to i64 = 15
302 %p = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 0, i64 %2
306 ; Do not trace into binary operators other than ADD, SUB, and OR.
307 define ptr @and(i64 %a) {
308 ; CHECK-LABEL: define ptr @and(
309 ; CHECK-SAME: i64 [[A:%.*]]) {
311 ; CHECK-NEXT: [[TMP0:%.*]] = shl i64 [[A]], 2
312 ; CHECK-NEXT: [[TMP1:%.*]] = and i64 [[TMP0]], 1
313 ; CHECK-NEXT: [[P:%.*]] = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 0, i64 [[TMP1]]
314 ; CHECK-NEXT: ret ptr [[P]]
319 %p = getelementptr [32 x [32 x float]], ptr @float_2d_array, i64 0, i64 0, i64 %1
323 ; The code that rebuilds an OR expression used to be buggy, and failed on this
325 define ptr @shl_add_or(i64 %a, ptr %ptr) {
326 ; CHECK-LABEL: define ptr @shl_add_or(
327 ; CHECK-SAME: i64 [[A:%.*]], ptr [[PTR:%.*]]) {
329 ; CHECK-NEXT: [[SHL:%.*]] = shl i64 [[A]], 2
330 ; CHECK-NEXT: [[OR2:%.*]] = add i64 [[SHL]], 1
331 ; CHECK-NEXT: [[TMP0:%.*]] = getelementptr float, ptr [[PTR]], i64 [[OR2]]
332 ; CHECK-NEXT: [[P3:%.*]] = getelementptr i8, ptr [[TMP0]], i64 48
333 ; CHECK-NEXT: ret ptr [[P3]]
337 %add = add i64 %shl, 12
339 ; ((a << 2) + 12) and 1 have no common bits. Therefore,
340 ; SeparateConstOffsetFromGEP is able to extract the 12.
341 ; TODO(jingyue): We could reassociate the expression to combine 12 and 1.
342 %p = getelementptr float, ptr %ptr, i64 %or
346 ; The source code used to be buggy in checking
347 ; (AccumulativeByteOffset % ElementTypeSizeOfGEP == 0)
348 ; where AccumulativeByteOffset is signed but ElementTypeSizeOfGEP is unsigned.
349 ; The compiler would promote AccumulativeByteOffset to unsigned, causing
350 ; unexpected results. For example, while -64 % (int64_t)24 != 0,
351 ; -64 % (uint64_t)24 == 0.
352 %struct3 = type { i64, i32 }
353 %struct2 = type { %struct3, i32 }
354 %struct1 = type { i64, %struct2 }
355 %struct0 = type { i32, i32, ptr, [100 x %struct1] }
356 define ptr @sign_mod_unsign(ptr %ptr, i64 %idx) {
357 ; CHECK-LABEL: define ptr @sign_mod_unsign(
358 ; CHECK-SAME: ptr [[PTR:%.*]], i64 [[IDX:%.*]]) {
360 ; CHECK-NEXT: [[TMP0:%.*]] = getelementptr [[STRUCT0:%.*]], ptr [[PTR]], i64 0, i32 3, i64 [[IDX]], i32 1
361 ; CHECK-NEXT: [[PTR22:%.*]] = getelementptr inbounds i8, ptr [[TMP0]], i64 -64
362 ; CHECK-NEXT: ret ptr [[PTR22]]
365 %arrayidx = add nsw i64 %idx, -2
366 %ptr2 = getelementptr inbounds %struct0, ptr %ptr, i64 0, i32 3, i64 %arrayidx, i32 1
370 ; Check that we can see through explicit trunc() instruction.
371 define ptr @trunk_explicit(ptr %ptr, i64 %idx) {
372 ; CHECK-LABEL: define ptr @trunk_explicit(
373 ; CHECK-SAME: ptr [[PTR:%.*]], i64 [[IDX:%.*]]) {
375 ; CHECK-NEXT: [[TMP0:%.*]] = getelementptr [[STRUCT0:%.*]], ptr [[PTR]], i64 0, i32 3, i64 [[IDX]], i32 1
376 ; CHECK-NEXT: [[PTR21:%.*]] = getelementptr inbounds i8, ptr [[TMP0]], i64 3216
377 ; CHECK-NEXT: ret ptr [[PTR21]]
380 %idx0 = trunc i64 1 to i32
381 %ptr2 = getelementptr inbounds %struct0, ptr %ptr, i32 %idx0, i32 3, i64 %idx, i32 1
385 ; Check that we can deal with trunc inserted by
386 ; canonicalizeArrayIndicesToPointerSize() if size of an index is larger than
387 ; that of the pointer.
388 define ptr @trunk_long_idx(ptr %ptr, i64 %idx) {
389 ; CHECK-LABEL: define ptr @trunk_long_idx(
390 ; CHECK-SAME: ptr [[PTR:%.*]], i64 [[IDX:%.*]]) {
392 ; CHECK-NEXT: [[TMP0:%.*]] = getelementptr [[STRUCT0:%.*]], ptr [[PTR]], i64 0, i32 3, i64 [[IDX]], i32 1
393 ; CHECK-NEXT: [[PTR21:%.*]] = getelementptr inbounds i8, ptr [[TMP0]], i64 3216
394 ; CHECK-NEXT: ret ptr [[PTR21]]
397 %ptr2 = getelementptr inbounds %struct0, ptr %ptr, i65 1, i32 3, i64 %idx, i32 1