Fixed several correctness issues in SeparateConstOffsetFromGEP
[oota-llvm.git] / test / Transforms / SeparateConstOffsetFromGEP / NVPTX / split-gep.ll
1 ; RUN: opt < %s -separate-const-offset-from-gep -dce -S | FileCheck %s
2
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.
6
7 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
8 ; target triple is necessary; otherwise TargetTransformInfo rejects any
9 ; addressing mode.
10 target triple = "nvptx64-unknown-unknown"
11
12 %struct.S = type { float, double }
13
14 @struct_array = global [1024 x %struct.S] zeroinitializer, align 16
15 @float_2d_array = global [32 x [32 x float]] zeroinitializer, align 4
16
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) {
20 entry:
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
24   ret double* %p
25 }
26 ; CHECK-LABEL: @struct(
27 ; CHECK: getelementptr [1024 x %struct.S]* @struct_array, i64 0, i64 %{{[a-zA-Z0-9]+}}, i32 1
28
29 ; We should be able to trace into s/zext(a + b) if a + b is non-negative
30 ; (e.g., used as an index of an inbounds GEP) and one of a and b is
31 ; non-negative.
32 define float* @sext_add(i32 %i, i32 %j) {
33 entry:
34   %0 = add i32 %i, 1
35   %1 = sext i32 %0 to i64  ; inbound sext(i + 1) = sext(i) + 1
36   %2 = sub i32 %j, 2
37   ; However, inbound sext(j - 2) != sext(j) - 2, e.g., j = INT_MIN
38   %3 = sext i32 %2 to i64
39   %p = getelementptr inbounds [32 x [32 x float]]* @float_2d_array, i64 0, i64 %1, i64 %3
40   ret float* %p
41 }
42 ; CHECK-LABEL: @sext_add(
43 ; CHECK-NOT: = add
44 ; CHECK: getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 %{{[a-zA-Z0-9]+}}, i64 %{{[a-zA-Z0-9]+}}
45 ; CHECK: getelementptr float* %{{[a-zA-Z0-9]+}}, i64 32
46
47 ; We should be able to trace into sext/zext if it can be distributed to both
48 ; operands, e.g., sext (add nsw a, b) == add nsw (sext a), (sext b)
49 ;
50 ; This test verifies we can transform
51 ;   gep base, a + sext(b +nsw 1), c + zext(d +nuw 1)
52 ; to
53 ;   gep base, a + sext(b), c + zext(d); gep ..., 1 * 32 + 1
54 define float* @ext_add_no_overflow(i64 %a, i32 %b, i64 %c, i32 %d) {
55   %b1 = add nsw i32 %b, 1
56   %b2 = sext i32 %b1 to i64
57   %i = add i64 %a, %b2       ; i = a + sext(b +nsw 1)
58   %d1 = add nuw i32 %d, 1
59   %d2 = zext i32 %d1 to i64
60   %j = add i64 %c, %d2       ; j = c + zext(d +nuw 1)
61   %p = getelementptr inbounds [32 x [32 x float]]* @float_2d_array, i64 0, i64 %i, i64 %j
62   ret float* %p
63 }
64 ; CHECK-LABEL: @ext_add_no_overflow(
65 ; CHECK: [[BASE_PTR:%[a-zA-Z0-9]+]] = getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 %{{[a-zA-Z0-9]+}}, i64 %{{[a-zA-Z0-9]+}}
66 ; CHECK: getelementptr float* [[BASE_PTR]], i64 33
67
68 ; Verifies we handle nested sext/zext correctly.
69 define void @sext_zext(i32 %a, i32 %b, float** %out1, float** %out2) {
70 entry:
71   %0 = add nsw nuw i32 %a, 1
72   %1 = sext i32 %0 to i48
73   %2 = zext i48 %1 to i64    ; zext(sext(a +nsw nuw 1)) = zext(sext(a)) + 1
74   %3 = add nsw i32 %b, 2
75   %4 = sext i32 %3 to i48
76   %5 = zext i48 %4 to i64    ; zext(sext(a +nsw 2)) != zext(sext(a)) + 2
77   %p1 = getelementptr inbounds [32 x [32 x float]]* @float_2d_array, i64 0, i64 %2, i64 %5
78   store float* %p1, float** %out1
79   %6 = add nuw i32 %a, 3
80   %7 = zext i32 %6 to i48
81   %8 = sext i48 %7 to i64 ; sext(zext(b +nuw 3)) = zext(b +nuw 3) = zext(b) + 3
82   %9 = add nsw i32 %b, 4
83   %10 = zext i32 %9 to i48
84   %11 = sext i48 %10 to i64  ; sext(zext(b +nsw 4)) != zext(b) + 4
85   %p2 = getelementptr inbounds [32 x [32 x float]]* @float_2d_array, i64 0, i64 %8, i64 %11
86   store float* %p2, float** %out2
87   ret void
88 }
89 ; CHECK-LABEL: @sext_zext(
90 ; CHECK: [[BASE_PTR_1:%[a-zA-Z0-9]+]] = getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 %{{[a-zA-Z0-9]+}}, i64 %{{[a-zA-Z0-9]+}}
91 ; CHECK: getelementptr float* [[BASE_PTR_1]], i64 32
92 ; CHECK: [[BASE_PTR_2:%[a-zA-Z0-9]+]] = getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 %{{[a-zA-Z0-9]+}}, i64 %{{[a-zA-Z0-9]+}}
93 ; CHECK: getelementptr float* [[BASE_PTR_2]], i64 96
94
95 ; Similar to @ext_add_no_overflow, we should be able to trace into s/zext if
96 ; its operand is an OR and the two operands of the OR have no common bits.
97 define float* @sext_or(i64 %a, i32 %b) {
98 entry:
99   %b1 = shl i32 %b, 2
100   %b2 = or i32 %b1, 1 ; (b << 2) and 1 have no common bits
101   %b3 = or i32 %b1, 4 ; (b << 2) and 4 may have common bits
102   %b2.ext = zext i32 %b2 to i64
103   %b3.ext = sext i32 %b3 to i64
104   %i = add i64 %a, %b2.ext
105   %j = add i64 %a, %b3.ext
106   %p = getelementptr inbounds [32 x [32 x float]]* @float_2d_array, i64 0, i64 %i, i64 %j
107   ret float* %p
108 }
109 ; CHECK-LABEL: @sext_or(
110 ; CHECK: [[BASE_PTR:%[a-zA-Z0-9]+]] = getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 %{{[a-zA-Z0-9]+}}, i64 %{{[a-zA-Z0-9]+}}
111 ; CHECK: getelementptr float* [[BASE_PTR]], i64 32
112
113 ; The subexpression (b + 5) is used in both "i = a + (b + 5)" and "*out = b +
114 ; 5". When extracting the constant offset 5, make sure "*out = b + 5" isn't
115 ; affected.
116 define float* @expr(i64 %a, i64 %b, i64* %out) {
117 entry:
118   %b5 = add i64 %b, 5
119   %i = add i64 %b5, %a
120   %p = getelementptr inbounds [32 x [32 x float]]* @float_2d_array, i64 0, i64 %i, i64 0
121   store i64 %b5, i64* %out
122   ret float* %p
123 }
124 ; CHECK-LABEL: @expr(
125 ; CHECK: [[BASE_PTR:%[a-zA-Z0-9]+]] = getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 %{{[a-zA-Z0-9]+}}, i64 0
126 ; CHECK: getelementptr float* [[BASE_PTR]], i64 160
127 ; CHECK: store i64 %b5, i64* %out
128
129 ; d + sext(a +nsw (b +nsw (c +nsw 8))) => (d + sext(a) + sext(b) + sext(c)) + 8
130 define float* @sext_expr(i32 %a, i32 %b, i32 %c, i64 %d) {
131 entry:
132   %0 = add nsw i32 %c, 8
133   %1 = add nsw i32 %b, %0
134   %2 = add nsw i32 %a, %1
135   %3 = sext i32 %2 to i64
136   %i = add i64 %d, %3
137   %p = getelementptr inbounds [32 x [32 x float]]* @float_2d_array, i64 0, i64 0, i64 %i
138   ret float* %p
139 }
140 ; CHECK-LABEL: @sext_expr(
141 ; CHECK: sext i32
142 ; CHECK: sext i32
143 ; CHECK: sext i32
144 ; CHECK: getelementptr float* %{{[a-zA-Z0-9]+}}, i64 8
145
146 ; Verifies we handle "sub" correctly.
147 define float* @sub(i64 %i, i64 %j) {
148   %i2 = sub i64 %i, 5 ; i - 5
149   %j2 = sub i64 5, %j ; 5 - i
150   %p = getelementptr inbounds [32 x [32 x float]]* @float_2d_array, i64 0, i64 %i2, i64 %j2
151   ret float* %p
152 }
153 ; CHECK-LABEL: @sub(
154 ; CHECK: %[[j2:[a-zA-Z0-9]+]] = sub i64 0, %j
155 ; CHECK: [[BASE_PTR:%[a-zA-Z0-9]+]] = getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 %i, i64 %[[j2]]
156 ; CHECK: getelementptr float* [[BASE_PTR]], i64 -155
157
158 %struct.Packed = type <{ [3 x i32], [8 x i64] }> ; <> means packed
159
160 ; Verifies we can emit correct uglygep if the address is not natually aligned.
161 define i64* @packed_struct(i32 %i, i32 %j) {
162 entry:
163   %s = alloca [1024 x %struct.Packed], align 16
164   %add = add nsw i32 %j, 3
165   %idxprom = sext i32 %add to i64
166   %add1 = add nsw i32 %i, 1
167   %idxprom2 = sext i32 %add1 to i64
168   %arrayidx3 = getelementptr inbounds [1024 x %struct.Packed]* %s, i64 0, i64 %idxprom2, i32 1, i64 %idxprom
169   ret i64* %arrayidx3
170 }
171 ; CHECK-LABEL: @packed_struct(
172 ; CHECK: [[BASE_PTR:%[a-zA-Z0-9]+]] = getelementptr [1024 x %struct.Packed]* %s, i64 0, i64 %{{[a-zA-Z0-9]+}}, i32 1, i64 %{{[a-zA-Z0-9]+}}
173 ; CHECK: [[CASTED_PTR:%[a-zA-Z0-9]+]] = bitcast i64* [[BASE_PTR]] to i8*
174 ; CHECK: %uglygep = getelementptr i8* [[CASTED_PTR]], i64 100
175 ; CHECK: bitcast i8* %uglygep to i64*
176
177 ; We shouldn't be able to extract the 8 from "zext(a +nuw (b + 8))",
178 ; because "zext(b + 8) != zext(b) + 8"
179 define float* @zext_expr(i32 %a, i32 %b) {
180 entry:
181   %0 = add i32 %b, 8
182   %1 = add nuw i32 %a, %0
183   %i = zext i32 %1 to i64
184   %p = getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 0, i64 %i
185   ret float* %p
186 }
187 ; CHECK-LABEL: zext_expr(
188 ; CHECK: getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 0, i64 %i
189
190 ; Per http://llvm.org/docs/LangRef.html#id181, the indices of a off-bound gep
191 ; should be considered sign-extended to the pointer size. Therefore,
192 ;   gep base, (add i32 a, b) != gep (gep base, i32 a), i32 b
193 ; because
194 ;   sext(a + b) != sext(a) + sext(b)
195 ;
196 ; This test verifies we do not illegitimately extract the 8 from
197 ;   gep base, (i32 a + 8)
198 define float* @i32_add(i32 %a) {
199 entry:
200   %i = add i32 %a, 8
201   %p = getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 0, i32 %i
202   ret float* %p
203 }
204 ; CHECK-LABEL: @i32_add(
205 ; CHECK: getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 0, i64 %{{[a-zA-Z0-9]+}}
206 ; CHECK-NOT: getelementptr
207
208 ; Verifies that we compute the correct constant offset when the index is
209 ; sign-extended and then zero-extended. The old version of our code failed to
210 ; handle this case because it simply computed the constant offset as the
211 ; sign-extended value of the constant part of the GEP index.
212 define float* @apint(i1 %a) {
213 entry:
214   %0 = add nsw nuw i1 %a, 1
215   %1 = sext i1 %0 to i4
216   %2 = zext i4 %1 to i64         ; zext (sext i1 1 to i4) to i64 = 15
217   %p = getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 0, i64 %2
218   ret float* %p
219 }
220 ; CHECK-LABEL: @apint(
221 ; CHECK: [[BASE_PTR:%[a-zA-Z0-9]+]] = getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 0, i64 %{{[a-zA-Z0-9]+}}
222 ; CHECK: getelementptr float* [[BASE_PTR]], i64 15
223
224 ; Do not trace into binary operators other than ADD, SUB, and OR.
225 define float* @and(i64 %a) {
226 entry:
227   %0 = shl i64 %a, 2
228   %1 = and i64 %0, 1
229   %p = getelementptr [32 x [32 x float]]* @float_2d_array, i64 0, i64 0, i64 %1
230   ret float* %p
231 }
232 ; CHECK-LABEL: @and(
233 ; CHECK: getelementptr [32 x [32 x float]]* @float_2d_array
234 ; CHECK-NOT: getelementptr