1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -passes=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 ptr @test1(ptr %b, ptr %e) {
10 ; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint ptr [[E:%.*]] to i64
11 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[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:%.*]], ptr [[B]], i64 [[SDIV]]
15 ; CHECK-NEXT: ret ptr [[GEP]]
17 %e_ptr = ptrtoint ptr %e to i64
18 %b_ptr = ptrtoint ptr %b to i64
19 %sub = sub i64 %e_ptr, %b_ptr
20 %sdiv = sdiv exact i64 %sub, 7
21 %gep = getelementptr inbounds %struct.A, ptr %b, i64 %sdiv
25 define ptr @test2(ptr %b, ptr %e) {
26 ; CHECK-LABEL: @test2(
27 ; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint ptr [[E:%.*]] to i64
28 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[B:%.*]] to i64
29 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[E_PTR]], [[B_PTR]]
30 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr [[B]], i64 [[SUB]]
31 ; CHECK-NEXT: ret ptr [[GEP]]
33 %e_ptr = ptrtoint ptr %e to i64
34 %b_ptr = ptrtoint ptr %b to i64
35 %sub = sub i64 %e_ptr, %b_ptr
36 %gep = getelementptr inbounds i8, ptr %b, i64 %sub
40 define ptr @test3(ptr %b, ptr %e) {
41 ; CHECK-LABEL: @test3(
42 ; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint ptr [[E:%.*]] to i64
43 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[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, ptr [[B]], i64 [[ASHR]]
47 ; CHECK-NEXT: ret ptr [[GEP]]
49 %e_ptr = ptrtoint ptr %e to i64
50 %b_ptr = ptrtoint ptr %b to i64
51 %sub = sub i64 %e_ptr, %b_ptr
52 %ashr = ashr exact i64 %sub, 3
53 %gep = getelementptr inbounds i64, ptr %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 ptr @test4(ptr %b) {
61 ; CHECK-LABEL: @test4(
62 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[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:%.*]], ptr [[B]], i64 [[SDIV]]
66 ; CHECK-NEXT: ret ptr [[GEP]]
68 %b_ptr = ptrtoint ptr %b to i64
69 %sub = sub i64 0, %b_ptr
70 %sdiv = sdiv exact i64 %sub, 7
71 %gep = getelementptr %struct.A, ptr %b, i64 %sdiv
75 define ptr @test4_inbounds(ptr %b) {
76 ; CHECK-LABEL: @test4_inbounds(
77 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[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:%.*]], ptr [[B]], i64 [[SDIV]]
81 ; CHECK-NEXT: ret ptr [[GEP]]
83 %b_ptr = ptrtoint ptr %b to i64
84 %sub = sub i64 0, %b_ptr
85 %sdiv = sdiv exact i64 %sub, 7
86 %gep = getelementptr inbounds %struct.A, ptr %b, i64 %sdiv
90 define ptr @test5(ptr %b) {
91 ; CHECK-LABEL: @test5(
92 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[B:%.*]] to i64
93 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[B_PTR]]
94 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[B]], i64 [[SUB]]
95 ; CHECK-NEXT: ret ptr [[GEP]]
97 %b_ptr = ptrtoint ptr %b to i64
98 %sub = sub i64 0, %b_ptr
99 %gep = getelementptr i8, ptr %b, i64 %sub
103 define ptr @test5_inbounds(ptr %b) {
104 ; CHECK-LABEL: @test5_inbounds(
105 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[B:%.*]] to i64
106 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[B_PTR]]
107 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr [[B]], i64 [[SUB]]
108 ; CHECK-NEXT: ret ptr [[GEP]]
110 %b_ptr = ptrtoint ptr %b to i64
111 %sub = sub i64 0, %b_ptr
112 %gep = getelementptr inbounds i8, ptr %b, i64 %sub
116 define ptr @test6(ptr %b) {
117 ; CHECK-LABEL: @test6(
118 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[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, ptr [[B]], i64 [[ASHR]]
122 ; CHECK-NEXT: ret ptr [[GEP]]
124 %b_ptr = ptrtoint ptr %b to i64
125 %sub = sub i64 0, %b_ptr
126 %ashr = ashr exact i64 %sub, 3
127 %gep = getelementptr i64, ptr %b, i64 %ashr
131 define ptr @test6_inbounds(ptr %b) {
132 ; CHECK-LABEL: @test6_inbounds(
133 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[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, ptr [[B]], i64 [[ASHR]]
137 ; CHECK-NEXT: ret ptr [[GEP]]
139 %b_ptr = ptrtoint ptr %b to i64
140 %sub = sub i64 0, %b_ptr
141 %ashr = ashr exact i64 %sub, 3
142 %gep = getelementptr inbounds i64, ptr %b, i64 %ashr
146 define ptr @test7(ptr %b, ptr %e) {
147 ; CHECK-LABEL: @test7(
148 ; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint ptr [[E:%.*]] to i64
149 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[B:%.*]] to i64
150 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[E_PTR]], [[B_PTR]]
151 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr [[B]], i64 [[SUB]]
152 ; CHECK-NEXT: ret ptr [[GEP]]
154 %e_ptr = ptrtoint ptr %e to i64
155 %b_ptr = ptrtoint ptr %b to i64
156 %sub = sub i64 %e_ptr, %b_ptr
157 %gep = getelementptr inbounds i8, ptr %b, i64 %sub
161 define ptr @undef_inbounds_var_idx(i64 %idx) {
162 ; CHECK-LABEL: @undef_inbounds_var_idx(
163 ; CHECK-NEXT: ret ptr undef
165 %el = getelementptr inbounds i64, ptr undef, i64 %idx
169 define ptr @undef_no_inbounds_var_idx(i64 %idx) {
170 ; CHECK-LABEL: @undef_no_inbounds_var_idx(
171 ; CHECK-NEXT: ret ptr undef
173 %el = getelementptr i64, ptr undef, i64 %idx
177 define <8 x ptr> @undef_vec1() {
178 ; CHECK-LABEL: @undef_vec1(
179 ; CHECK-NEXT: ret <8 x ptr> undef
181 %el = getelementptr inbounds i64, ptr undef, <8 x i64> undef
185 define <8 x ptr> @undef_vec2() {
186 ; CHECK-LABEL: @undef_vec2(
187 ; CHECK-NEXT: ret <8 x ptr> undef
189 %el = getelementptr i64, <8 x ptr> undef, <8 x i64> undef
193 ; Check ConstantExpr::getGetElementPtr() using ElementCount for size queries - begin.
197 define ptr @ptr_idx_scalar() {
198 ; CHECK-LABEL: @ptr_idx_scalar(
199 ; CHECK-NEXT: ret ptr inttoptr (i64 4 to ptr)
201 %gep = getelementptr <4 x i32>, ptr null, i64 0, i64 1
205 define <2 x ptr> @ptr_idx_vector() {
206 ; CHECK-LABEL: @ptr_idx_vector(
207 ; CHECK-NEXT: ret <2 x ptr> getelementptr (i32, ptr null, <2 x i64> <i64 1, i64 1>)
209 %gep = getelementptr i32, ptr null, <2 x i64> <i64 1, i64 1>
213 define <4 x ptr> @ptr_idx_mix_scalar_vector(){
214 ; CHECK-LABEL: @ptr_idx_mix_scalar_vector(
215 ; CHECK-NEXT: ret <4 x ptr> getelementptr ([42 x [3 x i32]], ptr null, <4 x i64> zeroinitializer, <4 x i64> <i64 0, i64 1, i64 2, i64 3>, <4 x i64> zeroinitializer)
217 %gep = getelementptr [42 x [3 x i32]], ptr null, i64 0, <4 x i64> <i64 0, i64 1, i64 2, i64 3>, i64 0
223 define <4 x ptr> @vector_idx_scalar() {
224 ; CHECK-LABEL: @vector_idx_scalar(
225 ; CHECK-NEXT: ret <4 x ptr> getelementptr (i32, <4 x ptr> zeroinitializer, <4 x i64> <i64 1, i64 1, i64 1, i64 1>)
227 %gep = getelementptr i32, <4 x ptr> zeroinitializer, i64 1
231 define <4 x ptr> @vector_idx_vector() {
232 ; CHECK-LABEL: @vector_idx_vector(
233 ; CHECK-NEXT: ret <4 x ptr> getelementptr (i32, <4 x ptr> zeroinitializer, <4 x i64> <i64 1, i64 1, i64 1, i64 1>)
235 %gep = getelementptr i32, <4 x ptr> zeroinitializer, <4 x i64> <i64 1, i64 1, i64 1, i64 1>
239 %struct = type { double, float }
240 define <4 x ptr> @vector_idx_mix_scalar_vector() {
241 ; CHECK-LABEL: @vector_idx_mix_scalar_vector(
242 ; CHECK-NEXT: ret <4 x ptr> getelementptr ([[STRUCT:%.*]], <4 x ptr> zeroinitializer, <4 x i64> zeroinitializer, i32 1)
244 %gep = getelementptr %struct, <4 x ptr> zeroinitializer, i32 0, <4 x i32> <i32 1, i32 1, i32 1, i32 1>
250 define <vscale x 4 x ptr> @scalable_idx_scalar() {
251 ; CHECK-LABEL: @scalable_idx_scalar(
252 ; CHECK-NEXT: ret <vscale x 4 x ptr> getelementptr (i32, <vscale x 4 x ptr> zeroinitializer, <vscale x 4 x i64> shufflevector (<vscale x 4 x i64> insertelement (<vscale x 4 x i64> poison, i64 1, i64 0), <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer))
254 %gep = getelementptr i32, <vscale x 4 x ptr> zeroinitializer, i64 1
255 ret <vscale x 4 x ptr> %gep
258 define <vscale x 4 x ptr> @scalable_vector_idx_mix_scalar_vector() {
259 ; CHECK-LABEL: @scalable_vector_idx_mix_scalar_vector(
260 ; CHECK-NEXT: ret <vscale x 4 x ptr> getelementptr ([[STRUCT:%.*]], <vscale x 4 x ptr> zeroinitializer, <vscale x 4 x i64> zeroinitializer, i32 1)
262 %gep = getelementptr %struct, <vscale x 4 x ptr> zeroinitializer, i32 0, i32 1
263 ret <vscale x 4 x ptr> %gep
266 define <vscale x 2 x ptr> @ptr_idx_mix_scalar_scalable_vector() {
267 ; CHECK-LABEL: @ptr_idx_mix_scalar_scalable_vector(
268 ; CHECK-NEXT: ret <vscale x 2 x ptr> zeroinitializer
270 %v = getelementptr [2 x i64], ptr null, i64 0, <vscale x 2 x i64> zeroinitializer
271 ret <vscale x 2 x ptr> %v
274 ; Check ConstantExpr::getGetElementPtr() using ElementCount for size queries - end.
276 define ptr @poison() {
277 ; CHECK-LABEL: @poison(
278 ; CHECK-NEXT: ret ptr poison
280 %v = getelementptr i8, ptr poison, i64 1
284 define ptr @poison2(ptr %baseptr) {
285 ; CHECK-LABEL: @poison2(
286 ; CHECK-NEXT: ret ptr poison
288 %v = getelementptr i8, ptr %baseptr, i64 poison
292 define ptr @D98611_1(ptr %c1, i64 %offset) {
293 ; CHECK-LABEL: @D98611_1(
294 ; CHECK-NEXT: [[C2:%.*]] = getelementptr inbounds i8, ptr [[C1:%.*]], i64 [[OFFSET:%.*]]
295 ; CHECK-NEXT: ret ptr [[C2]]
297 %c2 = getelementptr inbounds i8, ptr %c1, i64 %offset
298 %ptrtoint1 = ptrtoint ptr %c1 to i64
299 %ptrtoint2 = ptrtoint ptr %c2 to i64
300 %sub = sub i64 %ptrtoint2, %ptrtoint1
301 %gep = getelementptr inbounds i8, ptr %c1, i64 %sub
305 define ptr @D98611_2(ptr %c1, i64 %offset) {
306 ; CHECK-LABEL: @D98611_2(
307 ; CHECK-NEXT: [[C2:%.*]] = getelementptr inbounds [[STRUCT_A:%.*]], ptr [[C1:%.*]], i64 [[OFFSET:%.*]]
308 ; CHECK-NEXT: ret ptr [[C2]]
310 %c2 = getelementptr inbounds %struct.A, ptr %c1, i64 %offset
311 %ptrtoint1 = ptrtoint ptr %c1 to i64
312 %ptrtoint2 = ptrtoint ptr %c2 to i64
313 %sub = sub i64 %ptrtoint2, %ptrtoint1
314 %sdiv = sdiv exact i64 %sub, 7
315 %gep = getelementptr inbounds %struct.A, ptr %c1, i64 %sdiv
319 define ptr @D98611_3(ptr %c1, i64 %offset) {
320 ; CHECK-LABEL: @D98611_3(
321 ; CHECK-NEXT: [[C2:%.*]] = getelementptr inbounds i32, ptr [[C1:%.*]], i64 [[OFFSET:%.*]]
322 ; CHECK-NEXT: ret ptr [[C2]]
324 %c2 = getelementptr inbounds i32, ptr %c1, i64 %offset
325 %ptrtoint1 = ptrtoint ptr %c1 to i64
326 %ptrtoint2 = ptrtoint ptr %c2 to i64
327 %sub = sub i64 %ptrtoint2, %ptrtoint1
328 %ashr = ashr exact i64 %sub, 2
329 %gep = getelementptr inbounds i32, ptr %c1, i64 %ashr
333 define <8 x ptr> @gep_vector_index_op2_poison(ptr %ptr) {
334 ; CHECK-LABEL: @gep_vector_index_op2_poison(
335 ; CHECK-NEXT: ret <8 x ptr> poison
337 %res = getelementptr inbounds [144 x i32], ptr %ptr, i64 0, <8 x i64> poison
341 %t.1 = type { i32, [144 x i32] }
343 define <8 x ptr> @gep_vector_index_op3_poison(ptr %ptr) {
344 ; CHECK-LABEL: @gep_vector_index_op3_poison(
345 ; CHECK-NEXT: ret <8 x ptr> poison
347 %res = getelementptr inbounds %t.1, ptr %ptr, i64 0, i32 1, <8 x i64> poison
351 %t.2 = type { i32, i32 }
352 %t.3 = type { i32, [144 x %t.2 ] }
354 define <8 x ptr> @gep_vector_index_op3_poison_constant_index_afterwards(ptr %ptr) {
355 ; CHECK-LABEL: @gep_vector_index_op3_poison_constant_index_afterwards(
356 ; CHECK-NEXT: ret <8 x ptr> poison
358 %res = getelementptr inbounds %t.3, ptr %ptr, i64 0, i32 1, <8 x i64> poison, i32 1
362 define i64 @gep_array_of_scalable_vectors_ptrdiff(ptr %ptr) {
363 %c1 = getelementptr inbounds [8 x <vscale x 4 x i32>], ptr %ptr, i64 4
364 %c2 = getelementptr inbounds [8 x <vscale x 4 x i32>], ptr %ptr, i64 6
365 %c1.int = ptrtoint ptr %c1 to i64
366 %c2.int = ptrtoint ptr %c2 to i64
367 %diff = sub i64 %c2.int, %c1.int