1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -instsimplify < %s | FileCheck %s
4 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
6 %struct.A = type { [7 x i8] }
8 define %struct.A* @test1(%struct.A* %b, %struct.A* %e) {
10 ; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint %struct.A* [[E:%.*]] to i64
11 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint %struct.A* [[B:%.*]] to i64
12 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[E_PTR]], [[B_PTR]]
13 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv exact i64 [[SUB]], 7
14 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [[STRUCT_A:%.*]], %struct.A* [[B]], i64 [[SDIV]]
15 ; CHECK-NEXT: ret %struct.A* [[GEP]]
17 %e_ptr = ptrtoint %struct.A* %e to i64
18 %b_ptr = ptrtoint %struct.A* %b to i64
19 %sub = sub i64 %e_ptr, %b_ptr
20 %sdiv = sdiv exact i64 %sub, 7
21 %gep = getelementptr inbounds %struct.A, %struct.A* %b, i64 %sdiv
25 define i8* @test2(i8* %b, i8* %e) {
26 ; CHECK-LABEL: @test2(
27 ; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint i8* [[E:%.*]] to i64
28 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint i8* [[B:%.*]] to i64
29 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[E_PTR]], [[B_PTR]]
30 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, i8* [[B]], i64 [[SUB]]
31 ; CHECK-NEXT: ret i8* [[GEP]]
33 %e_ptr = ptrtoint i8* %e to i64
34 %b_ptr = ptrtoint i8* %b to i64
35 %sub = sub i64 %e_ptr, %b_ptr
36 %gep = getelementptr inbounds i8, i8* %b, i64 %sub
40 define i64* @test3(i64* %b, i64* %e) {
41 ; CHECK-LABEL: @test3(
42 ; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint i64* [[E:%.*]] to i64
43 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint i64* [[B:%.*]] to i64
44 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[E_PTR]], [[B_PTR]]
45 ; CHECK-NEXT: [[ASHR:%.*]] = ashr exact i64 [[SUB]], 3
46 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i64, i64* [[B]], i64 [[ASHR]]
47 ; CHECK-NEXT: ret i64* [[GEP]]
49 %e_ptr = ptrtoint i64* %e to i64
50 %b_ptr = ptrtoint i64* %b to i64
51 %sub = sub i64 %e_ptr, %b_ptr
52 %ashr = ashr exact i64 %sub, 3
53 %gep = getelementptr inbounds i64, i64* %b, i64 %ashr
57 ; The following tests should not be folded to null, because this would
58 ; lose provenance of the base pointer %b.
60 define %struct.A* @test4(%struct.A* %b) {
61 ; CHECK-LABEL: @test4(
62 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint %struct.A* [[B:%.*]] to i64
63 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[B_PTR]]
64 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv exact i64 [[SUB]], 7
65 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr [[STRUCT_A:%.*]], %struct.A* [[B]], i64 [[SDIV]]
66 ; CHECK-NEXT: ret %struct.A* [[GEP]]
68 %b_ptr = ptrtoint %struct.A* %b to i64
69 %sub = sub i64 0, %b_ptr
70 %sdiv = sdiv exact i64 %sub, 7
71 %gep = getelementptr %struct.A, %struct.A* %b, i64 %sdiv
75 define %struct.A* @test4_inbounds(%struct.A* %b) {
76 ; CHECK-LABEL: @test4_inbounds(
77 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint %struct.A* [[B:%.*]] to i64
78 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[B_PTR]]
79 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv exact i64 [[SUB]], 7
80 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [[STRUCT_A:%.*]], %struct.A* [[B]], i64 [[SDIV]]
81 ; CHECK-NEXT: ret %struct.A* [[GEP]]
83 %b_ptr = ptrtoint %struct.A* %b to i64
84 %sub = sub i64 0, %b_ptr
85 %sdiv = sdiv exact i64 %sub, 7
86 %gep = getelementptr inbounds %struct.A, %struct.A* %b, i64 %sdiv
90 define i8* @test5(i8* %b) {
91 ; CHECK-LABEL: @test5(
92 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint i8* [[B:%.*]] to i64
93 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[B_PTR]]
94 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, i8* [[B]], i64 [[SUB]]
95 ; CHECK-NEXT: ret i8* [[GEP]]
97 %b_ptr = ptrtoint i8* %b to i64
98 %sub = sub i64 0, %b_ptr
99 %gep = getelementptr i8, i8* %b, i64 %sub
103 define i8* @test5_inbounds(i8* %b) {
104 ; CHECK-LABEL: @test5_inbounds(
105 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint i8* [[B:%.*]] to i64
106 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[B_PTR]]
107 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, i8* [[B]], i64 [[SUB]]
108 ; CHECK-NEXT: ret i8* [[GEP]]
110 %b_ptr = ptrtoint i8* %b to i64
111 %sub = sub i64 0, %b_ptr
112 %gep = getelementptr inbounds i8, i8* %b, i64 %sub
116 define i64* @test6(i64* %b) {
117 ; CHECK-LABEL: @test6(
118 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint i64* [[B:%.*]] to i64
119 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[B_PTR]]
120 ; CHECK-NEXT: [[ASHR:%.*]] = ashr exact i64 [[SUB]], 3
121 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i64, i64* [[B]], i64 [[ASHR]]
122 ; CHECK-NEXT: ret i64* [[GEP]]
124 %b_ptr = ptrtoint i64* %b to i64
125 %sub = sub i64 0, %b_ptr
126 %ashr = ashr exact i64 %sub, 3
127 %gep = getelementptr i64, i64* %b, i64 %ashr
131 define i64* @test6_inbounds(i64* %b) {
132 ; CHECK-LABEL: @test6_inbounds(
133 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint i64* [[B:%.*]] to i64
134 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[B_PTR]]
135 ; CHECK-NEXT: [[ASHR:%.*]] = ashr exact i64 [[SUB]], 3
136 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i64, i64* [[B]], i64 [[ASHR]]
137 ; CHECK-NEXT: ret i64* [[GEP]]
139 %b_ptr = ptrtoint i64* %b to i64
140 %sub = sub i64 0, %b_ptr
141 %ashr = ashr exact i64 %sub, 3
142 %gep = getelementptr inbounds i64, i64* %b, i64 %ashr
146 define i8* @test7(i8* %b, i8** %e) {
147 ; CHECK-LABEL: @test7(
148 ; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint i8** [[E:%.*]] to i64
149 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint i8* [[B:%.*]] to i64
150 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[E_PTR]], [[B_PTR]]
151 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, i8* [[B]], i64 [[SUB]]
152 ; CHECK-NEXT: ret i8* [[GEP]]
154 %e_ptr = ptrtoint i8** %e to i64
155 %b_ptr = ptrtoint i8* %b to i64
156 %sub = sub i64 %e_ptr, %b_ptr
157 %gep = getelementptr inbounds i8, i8* %b, i64 %sub
161 define <8 x i64*> @undef_vec1() {
162 ; CHECK-LABEL: @undef_vec1(
163 ; CHECK-NEXT: ret <8 x i64*> undef
165 %el = getelementptr inbounds i64, i64* undef, <8 x i64> undef
169 define <8 x i64*> @undef_vec2() {
170 ; CHECK-LABEL: @undef_vec2(
171 ; CHECK-NEXT: ret <8 x i64*> undef
173 %el = getelementptr i64, <8 x i64*> undef, <8 x i64> undef
177 ; Check ConstantExpr::getGetElementPtr() using ElementCount for size queries - begin.
181 define i32* @ptr_idx_scalar() {
182 ; CHECK-LABEL: @ptr_idx_scalar(
183 ; CHECK-NEXT: ret i32* inttoptr (i64 4 to i32*)
185 %gep = getelementptr <4 x i32>, <4 x i32>* null, i64 0, i64 1
189 define <2 x i32*> @ptr_idx_vector() {
190 ; CHECK-LABEL: @ptr_idx_vector(
191 ; CHECK-NEXT: ret <2 x i32*> getelementptr (i32, i32* null, <2 x i64> <i64 1, i64 1>)
193 %gep = getelementptr i32, i32* null, <2 x i64> <i64 1, i64 1>
197 define <4 x i32*> @ptr_idx_mix_scalar_vector(){
198 ; CHECK-LABEL: @ptr_idx_mix_scalar_vector(
199 ; CHECK-NEXT: ret <4 x i32*> getelementptr ([42 x [3 x i32]], [42 x [3 x i32]]* null, <4 x i64> zeroinitializer, <4 x i64> <i64 0, i64 1, i64 2, i64 3>, <4 x i64> zeroinitializer)
201 %gep = getelementptr [42 x [3 x i32]], [42 x [3 x i32]]* null, i64 0, <4 x i64> <i64 0, i64 1, i64 2, i64 3>, i64 0
207 define <4 x i32*> @vector_idx_scalar() {
208 ; CHECK-LABEL: @vector_idx_scalar(
209 ; CHECK-NEXT: ret <4 x i32*> getelementptr (i32, <4 x i32*> zeroinitializer, <4 x i64> <i64 1, i64 1, i64 1, i64 1>)
211 %gep = getelementptr i32, <4 x i32*> zeroinitializer, i64 1
215 define <4 x i32*> @vector_idx_vector() {
216 ; CHECK-LABEL: @vector_idx_vector(
217 ; CHECK-NEXT: ret <4 x i32*> getelementptr (i32, <4 x i32*> zeroinitializer, <4 x i64> <i64 1, i64 1, i64 1, i64 1>)
219 %gep = getelementptr i32, <4 x i32*> zeroinitializer, <4 x i64> <i64 1, i64 1, i64 1, i64 1>
223 %struct = type { double, float }
224 define <4 x float*> @vector_idx_mix_scalar_vector() {
225 ; CHECK-LABEL: @vector_idx_mix_scalar_vector(
226 ; CHECK-NEXT: ret <4 x float*> getelementptr ([[STRUCT:%.*]], <4 x %struct*> zeroinitializer, <4 x i64> zeroinitializer, i32 1)
228 %gep = getelementptr %struct, <4 x %struct*> zeroinitializer, i32 0, <4 x i32> <i32 1, i32 1, i32 1, i32 1>
229 ret <4 x float*> %gep
234 define <vscale x 4 x i32*> @scalable_idx_scalar() {
235 ; CHECK-LABEL: @scalable_idx_scalar(
236 ; CHECK-NEXT: ret <vscale x 4 x i32*> getelementptr (i32, <vscale x 4 x i32*> zeroinitializer, <vscale x 4 x i64> shufflevector (<vscale x 4 x i64> insertelement (<vscale x 4 x i64> poison, i64 1, i32 0), <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer))
238 %gep = getelementptr i32, <vscale x 4 x i32*> zeroinitializer, i64 1
239 ret <vscale x 4 x i32*> %gep
242 define <vscale x 4 x float*> @scalable_vector_idx_mix_scalar_vector() {
243 ; CHECK-LABEL: @scalable_vector_idx_mix_scalar_vector(
244 ; CHECK-NEXT: ret <vscale x 4 x float*> getelementptr ([[STRUCT:%.*]], <vscale x 4 x %struct*> zeroinitializer, <vscale x 4 x i64> zeroinitializer, i32 1)
246 %gep = getelementptr %struct, <vscale x 4 x %struct*> zeroinitializer, i32 0, i32 1
247 ret <vscale x 4 x float*> %gep
250 define <vscale x 2 x i64*> @ptr_idx_mix_scalar_scalable_vector() {
251 ; CHECK-LABEL: @ptr_idx_mix_scalar_scalable_vector(
252 ; CHECK-NEXT: ret <vscale x 2 x i64*> zeroinitializer
254 %v = getelementptr [2 x i64], [2 x i64]* null, i64 0, <vscale x 2 x i64> zeroinitializer
255 ret <vscale x 2 x i64*> %v
258 ; Check ConstantExpr::getGetElementPtr() using ElementCount for size queries - end.
260 ; TODO: these should return poison
262 define i8* @poison() {
263 ; CHECK-LABEL: @poison(
264 ; CHECK-NEXT: ret i8* poison
266 %v = getelementptr i8, i8* poison, i64 1
270 define i8* @poison2(i8* %baseptr) {
271 ; CHECK-LABEL: @poison2(
272 ; CHECK-NEXT: ret i8* poison
274 %v = getelementptr i8, i8* %baseptr, i64 poison
278 define i8* @D98611_1(i8* %c1, i64 %offset) {
279 ; CHECK-LABEL: @D98611_1(
280 ; CHECK-NEXT: [[C2:%.*]] = getelementptr inbounds i8, i8* [[C1:%.*]], i64 [[OFFSET:%.*]]
281 ; CHECK-NEXT: ret i8* [[C2]]
283 %c2 = getelementptr inbounds i8, i8* %c1, i64 %offset
284 %ptrtoint1 = ptrtoint i8* %c1 to i64
285 %ptrtoint2 = ptrtoint i8* %c2 to i64
286 %sub = sub i64 %ptrtoint2, %ptrtoint1
287 %gep = getelementptr inbounds i8, i8* %c1, i64 %sub
291 define %struct.A* @D98611_2(%struct.A* %c1, i64 %offset) {
292 ; CHECK-LABEL: @D98611_2(
293 ; CHECK-NEXT: [[C2:%.*]] = getelementptr inbounds [[STRUCT_A:%.*]], %struct.A* [[C1:%.*]], i64 [[OFFSET:%.*]]
294 ; CHECK-NEXT: ret %struct.A* [[C2]]
296 %c2 = getelementptr inbounds %struct.A, %struct.A* %c1, i64 %offset
297 %ptrtoint1 = ptrtoint %struct.A* %c1 to i64
298 %ptrtoint2 = ptrtoint %struct.A* %c2 to i64
299 %sub = sub i64 %ptrtoint2, %ptrtoint1
300 %sdiv = sdiv exact i64 %sub, 7
301 %gep = getelementptr inbounds %struct.A, %struct.A* %c1, i64 %sdiv
305 define i32* @D98611_3(i32* %c1, i64 %offset) {
306 ; CHECK-LABEL: @D98611_3(
307 ; CHECK-NEXT: [[C2:%.*]] = getelementptr inbounds i32, i32* [[C1:%.*]], i64 [[OFFSET:%.*]]
308 ; CHECK-NEXT: ret i32* [[C2]]
310 %c2 = getelementptr inbounds i32, i32* %c1, i64 %offset
311 %ptrtoint1 = ptrtoint i32* %c1 to i64
312 %ptrtoint2 = ptrtoint i32* %c2 to i64
313 %sub = sub i64 %ptrtoint2, %ptrtoint1
314 %ashr = ashr exact i64 %sub, 2
315 %gep = getelementptr inbounds i32, i32* %c1, i64 %ashr
319 define <8 x i32*> @gep_vector_index_op2_poison([144 x i32]* %ptr) {
320 ; CHECK-LABEL: @gep_vector_index_op2_poison(
321 ; CHECK-NEXT: ret <8 x i32*> poison
323 %res = getelementptr inbounds [144 x i32], [144 x i32]* %ptr, i64 0, <8 x i64> poison
327 %t.1 = type { i32, [144 x i32] }
329 define <8 x i32*> @gep_vector_index_op3_poison(%t.1* %ptr) {
330 ; CHECK-LABEL: @gep_vector_index_op3_poison(
331 ; CHECK-NEXT: ret <8 x i32*> poison
333 %res = getelementptr inbounds %t.1, %t.1* %ptr, i64 0, i32 1, <8 x i64> poison
337 %t.2 = type { i32, i32 }
338 %t.3 = type { i32, [144 x %t.2 ] }
340 define <8 x i32*> @gep_vector_index_op3_poison_constant_index_afterwards(%t.3* %ptr) {
341 ; CHECK-LABEL: @gep_vector_index_op3_poison_constant_index_afterwards(
342 ; CHECK-NEXT: ret <8 x i32*> poison
344 %res = getelementptr inbounds %t.3, %t.3* %ptr, i64 0, i32 1, <8 x i64> poison, i32 1