1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -passes=instsimplify < %s | FileCheck %s
3 ; RUN: opt -S -passes=instsimplify -use-constant-int-for-fixed-length-splat < %s | FileCheck %s
5 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
7 %struct.A = type { [7 x i8] }
9 @g = external global i8
11 define ptr @test1(ptr %b, ptr %e) {
12 ; CHECK-LABEL: @test1(
13 ; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint ptr [[E:%.*]] to i64
14 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[B:%.*]] to i64
15 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[E_PTR]], [[B_PTR]]
16 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv exact i64 [[SUB]], 7
17 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [[STRUCT_A:%.*]], ptr [[B]], i64 [[SDIV]]
18 ; CHECK-NEXT: ret ptr [[GEP]]
20 %e_ptr = ptrtoint ptr %e to i64
21 %b_ptr = ptrtoint ptr %b to i64
22 %sub = sub i64 %e_ptr, %b_ptr
23 %sdiv = sdiv exact i64 %sub, 7
24 %gep = getelementptr inbounds %struct.A, ptr %b, i64 %sdiv
28 define ptr @test2(ptr %b, ptr %e) {
29 ; CHECK-LABEL: @test2(
30 ; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint ptr [[E:%.*]] to i64
31 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[B:%.*]] to i64
32 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[E_PTR]], [[B_PTR]]
33 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr [[B]], i64 [[SUB]]
34 ; CHECK-NEXT: ret ptr [[GEP]]
36 %e_ptr = ptrtoint ptr %e to i64
37 %b_ptr = ptrtoint ptr %b to i64
38 %sub = sub i64 %e_ptr, %b_ptr
39 %gep = getelementptr inbounds i8, ptr %b, i64 %sub
43 define ptr @test3(ptr %b, ptr %e) {
44 ; CHECK-LABEL: @test3(
45 ; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint ptr [[E:%.*]] to i64
46 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[B:%.*]] to i64
47 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[E_PTR]], [[B_PTR]]
48 ; CHECK-NEXT: [[ASHR:%.*]] = ashr exact i64 [[SUB]], 3
49 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i64, ptr [[B]], i64 [[ASHR]]
50 ; CHECK-NEXT: ret ptr [[GEP]]
52 %e_ptr = ptrtoint ptr %e to i64
53 %b_ptr = ptrtoint ptr %b to i64
54 %sub = sub i64 %e_ptr, %b_ptr
55 %ashr = ashr exact i64 %sub, 3
56 %gep = getelementptr inbounds i64, ptr %b, i64 %ashr
60 ; The following tests should not be folded to null, because this would
61 ; lose provenance of the base pointer %b.
63 define ptr @test4(ptr %b) {
64 ; CHECK-LABEL: @test4(
65 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[B:%.*]] to i64
66 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[B_PTR]]
67 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv exact i64 [[SUB]], 7
68 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr [[STRUCT_A:%.*]], ptr [[B]], i64 [[SDIV]]
69 ; CHECK-NEXT: ret ptr [[GEP]]
71 %b_ptr = ptrtoint ptr %b to i64
72 %sub = sub i64 0, %b_ptr
73 %sdiv = sdiv exact i64 %sub, 7
74 %gep = getelementptr %struct.A, ptr %b, i64 %sdiv
78 define ptr @test4_inbounds(ptr %b) {
79 ; CHECK-LABEL: @test4_inbounds(
80 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[B:%.*]] to i64
81 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[B_PTR]]
82 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv exact i64 [[SUB]], 7
83 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [[STRUCT_A:%.*]], ptr [[B]], i64 [[SDIV]]
84 ; CHECK-NEXT: ret ptr [[GEP]]
86 %b_ptr = ptrtoint ptr %b to i64
87 %sub = sub i64 0, %b_ptr
88 %sdiv = sdiv exact i64 %sub, 7
89 %gep = getelementptr inbounds %struct.A, ptr %b, i64 %sdiv
93 define ptr @test5(ptr %b) {
94 ; CHECK-LABEL: @test5(
95 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[B:%.*]] to i64
96 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[B_PTR]]
97 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[B]], i64 [[SUB]]
98 ; CHECK-NEXT: ret ptr [[GEP]]
100 %b_ptr = ptrtoint ptr %b to i64
101 %sub = sub i64 0, %b_ptr
102 %gep = getelementptr i8, ptr %b, i64 %sub
106 define ptr @test5_inbounds(ptr %b) {
107 ; CHECK-LABEL: @test5_inbounds(
108 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[B:%.*]] to i64
109 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[B_PTR]]
110 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr [[B]], i64 [[SUB]]
111 ; CHECK-NEXT: ret ptr [[GEP]]
113 %b_ptr = ptrtoint ptr %b to i64
114 %sub = sub i64 0, %b_ptr
115 %gep = getelementptr inbounds i8, ptr %b, i64 %sub
119 define ptr @test6(ptr %b) {
120 ; CHECK-LABEL: @test6(
121 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[B:%.*]] to i64
122 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[B_PTR]]
123 ; CHECK-NEXT: [[ASHR:%.*]] = ashr exact i64 [[SUB]], 3
124 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i64, ptr [[B]], i64 [[ASHR]]
125 ; CHECK-NEXT: ret ptr [[GEP]]
127 %b_ptr = ptrtoint ptr %b to i64
128 %sub = sub i64 0, %b_ptr
129 %ashr = ashr exact i64 %sub, 3
130 %gep = getelementptr i64, ptr %b, i64 %ashr
134 define ptr @test6_inbounds(ptr %b) {
135 ; CHECK-LABEL: @test6_inbounds(
136 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[B:%.*]] to i64
137 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[B_PTR]]
138 ; CHECK-NEXT: [[ASHR:%.*]] = ashr exact i64 [[SUB]], 3
139 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i64, ptr [[B]], i64 [[ASHR]]
140 ; CHECK-NEXT: ret ptr [[GEP]]
142 %b_ptr = ptrtoint ptr %b to i64
143 %sub = sub i64 0, %b_ptr
144 %ashr = ashr exact i64 %sub, 3
145 %gep = getelementptr inbounds i64, ptr %b, i64 %ashr
149 define ptr @test7(ptr %b, ptr %e) {
150 ; CHECK-LABEL: @test7(
151 ; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint ptr [[E:%.*]] to i64
152 ; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint ptr [[B:%.*]] to i64
153 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[E_PTR]], [[B_PTR]]
154 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr [[B]], i64 [[SUB]]
155 ; CHECK-NEXT: ret ptr [[GEP]]
157 %e_ptr = ptrtoint ptr %e to i64
158 %b_ptr = ptrtoint ptr %b to i64
159 %sub = sub i64 %e_ptr, %b_ptr
160 %gep = getelementptr inbounds i8, ptr %b, i64 %sub
164 define ptr @undef_inbounds_var_idx(i64 %idx) {
165 ; CHECK-LABEL: @undef_inbounds_var_idx(
166 ; CHECK-NEXT: ret ptr undef
168 %el = getelementptr inbounds i64, ptr undef, i64 %idx
172 define ptr @undef_no_inbounds_var_idx(i64 %idx) {
173 ; CHECK-LABEL: @undef_no_inbounds_var_idx(
174 ; CHECK-NEXT: ret ptr undef
176 %el = getelementptr i64, ptr undef, i64 %idx
180 define <8 x ptr> @undef_vec1() {
181 ; CHECK-LABEL: @undef_vec1(
182 ; CHECK-NEXT: ret <8 x ptr> undef
184 %el = getelementptr inbounds i64, ptr undef, <8 x i64> undef
188 define <8 x ptr> @undef_vec2() {
189 ; CHECK-LABEL: @undef_vec2(
190 ; CHECK-NEXT: ret <8 x ptr> undef
192 %el = getelementptr i64, <8 x ptr> undef, <8 x i64> undef
196 ; Check ConstantExpr::getGetElementPtr() using ElementCount for size queries - begin.
200 define ptr @ptr_idx_scalar() {
201 ; CHECK-LABEL: @ptr_idx_scalar(
202 ; CHECK-NEXT: ret ptr inttoptr (i64 4 to ptr)
204 %gep = getelementptr <4 x i32>, ptr null, i64 0, i64 1
208 define <2 x ptr> @ptr_idx_vector() {
209 ; CHECK-LABEL: @ptr_idx_vector(
210 ; CHECK-NEXT: ret <2 x ptr> getelementptr (i32, ptr null, <2 x i64> splat (i64 1))
212 %gep = getelementptr i32, ptr null, <2 x i64> <i64 1, i64 1>
216 define <4 x ptr> @ptr_idx_mix_scalar_vector(){
217 ; CHECK-LABEL: @ptr_idx_mix_scalar_vector(
218 ; 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)
220 %gep = getelementptr [42 x [3 x i32]], ptr null, i64 0, <4 x i64> <i64 0, i64 1, i64 2, i64 3>, i64 0
226 define <4 x ptr> @vector_idx_scalar() {
227 ; CHECK-LABEL: @vector_idx_scalar(
228 ; CHECK-NEXT: ret <4 x ptr> getelementptr (i32, <4 x ptr> zeroinitializer, <4 x i64> splat (i64 1))
230 %gep = getelementptr i32, <4 x ptr> zeroinitializer, i64 1
234 define <4 x ptr> @vector_idx_vector() {
235 ; CHECK-LABEL: @vector_idx_vector(
236 ; CHECK-NEXT: ret <4 x ptr> getelementptr (i32, <4 x ptr> zeroinitializer, <4 x i64> splat (i64 1))
238 %gep = getelementptr i32, <4 x ptr> zeroinitializer, <4 x i64> <i64 1, i64 1, i64 1, i64 1>
242 %struct = type { double, float }
243 define <4 x ptr> @vector_idx_mix_scalar_vector() {
244 ; CHECK-LABEL: @vector_idx_mix_scalar_vector(
245 ; CHECK-NEXT: ret <4 x ptr> getelementptr ([[STRUCT:%.*]], <4 x ptr> zeroinitializer, <4 x i64> zeroinitializer, i32 1)
247 %gep = getelementptr %struct, <4 x ptr> zeroinitializer, i32 0, <4 x i32> <i32 1, i32 1, i32 1, i32 1>
253 define <vscale x 4 x ptr> @scalable_idx_scalar() {
254 ; CHECK-LABEL: @scalable_idx_scalar(
255 ; CHECK-NEXT: ret <vscale x 4 x ptr> getelementptr (i32, <vscale x 4 x ptr> zeroinitializer, <vscale x 4 x i64> splat (i64 1))
257 %gep = getelementptr i32, <vscale x 4 x ptr> zeroinitializer, i64 1
258 ret <vscale x 4 x ptr> %gep
261 define <vscale x 4 x ptr> @scalable_vector_idx_mix_scalar_vector() {
262 ; CHECK-LABEL: @scalable_vector_idx_mix_scalar_vector(
263 ; CHECK-NEXT: ret <vscale x 4 x ptr> getelementptr ([[STRUCT:%.*]], <vscale x 4 x ptr> zeroinitializer, <vscale x 4 x i64> zeroinitializer, i32 1)
265 %gep = getelementptr %struct, <vscale x 4 x ptr> zeroinitializer, i32 0, i32 1
266 ret <vscale x 4 x ptr> %gep
269 define <vscale x 2 x ptr> @ptr_idx_mix_scalar_scalable_vector() {
270 ; CHECK-LABEL: @ptr_idx_mix_scalar_scalable_vector(
271 ; CHECK-NEXT: ret <vscale x 2 x ptr> zeroinitializer
273 %v = getelementptr [2 x i64], ptr null, i64 0, <vscale x 2 x i64> zeroinitializer
274 ret <vscale x 2 x ptr> %v
277 define ptr @constexpr_gep_nusw_nuw() {
278 ; CHECK-LABEL: @constexpr_gep_nusw_nuw(
279 ; CHECK-NEXT: ret ptr getelementptr nusw nuw (i8, ptr @g, i64 ptrtoint (ptr @g to i64))
281 %v = getelementptr nusw nuw i8, ptr @g, i64 ptrtoint (ptr @g to i64)
285 ; Check ConstantExpr::getGetElementPtr() using ElementCount for size queries - end.
287 define ptr @poison() {
288 ; CHECK-LABEL: @poison(
289 ; CHECK-NEXT: ret ptr poison
291 %v = getelementptr i8, ptr poison, i64 1
295 define ptr @poison2(ptr %baseptr) {
296 ; CHECK-LABEL: @poison2(
297 ; CHECK-NEXT: ret ptr poison
299 %v = getelementptr i8, ptr %baseptr, i64 poison
303 define ptr @D98611_1(ptr %c1, i64 %offset) {
304 ; CHECK-LABEL: @D98611_1(
305 ; CHECK-NEXT: [[C2:%.*]] = getelementptr inbounds i8, ptr [[C1:%.*]], i64 [[OFFSET:%.*]]
306 ; CHECK-NEXT: ret ptr [[C2]]
308 %c2 = getelementptr inbounds i8, ptr %c1, i64 %offset
309 %ptrtoint1 = ptrtoint ptr %c1 to i64
310 %ptrtoint2 = ptrtoint ptr %c2 to i64
311 %sub = sub i64 %ptrtoint2, %ptrtoint1
312 %gep = getelementptr inbounds i8, ptr %c1, i64 %sub
316 define ptr @D98611_2(ptr %c1, i64 %offset) {
317 ; CHECK-LABEL: @D98611_2(
318 ; CHECK-NEXT: [[C2:%.*]] = getelementptr inbounds [[STRUCT_A:%.*]], ptr [[C1:%.*]], i64 [[OFFSET:%.*]]
319 ; CHECK-NEXT: ret ptr [[C2]]
321 %c2 = getelementptr inbounds %struct.A, ptr %c1, i64 %offset
322 %ptrtoint1 = ptrtoint ptr %c1 to i64
323 %ptrtoint2 = ptrtoint ptr %c2 to i64
324 %sub = sub i64 %ptrtoint2, %ptrtoint1
325 %sdiv = sdiv exact i64 %sub, 7
326 %gep = getelementptr inbounds %struct.A, ptr %c1, i64 %sdiv
330 define ptr @D98611_3(ptr %c1, i64 %offset) {
331 ; CHECK-LABEL: @D98611_3(
332 ; CHECK-NEXT: [[C2:%.*]] = getelementptr inbounds i32, ptr [[C1:%.*]], i64 [[OFFSET:%.*]]
333 ; CHECK-NEXT: ret ptr [[C2]]
335 %c2 = getelementptr inbounds i32, ptr %c1, i64 %offset
336 %ptrtoint1 = ptrtoint ptr %c1 to i64
337 %ptrtoint2 = ptrtoint ptr %c2 to i64
338 %sub = sub i64 %ptrtoint2, %ptrtoint1
339 %ashr = ashr exact i64 %sub, 2
340 %gep = getelementptr inbounds i32, ptr %c1, i64 %ashr
344 define <8 x ptr> @gep_vector_index_op2_poison(ptr %ptr) {
345 ; CHECK-LABEL: @gep_vector_index_op2_poison(
346 ; CHECK-NEXT: ret <8 x ptr> poison
348 %res = getelementptr inbounds [144 x i32], ptr %ptr, i64 0, <8 x i64> poison
352 %t.1 = type { i32, [144 x i32] }
354 define <8 x ptr> @gep_vector_index_op3_poison(ptr %ptr) {
355 ; CHECK-LABEL: @gep_vector_index_op3_poison(
356 ; CHECK-NEXT: ret <8 x ptr> poison
358 %res = getelementptr inbounds %t.1, ptr %ptr, i64 0, i32 1, <8 x i64> poison
362 %t.2 = type { i32, i32 }
363 %t.3 = type { i32, [144 x %t.2 ] }
365 define <8 x ptr> @gep_vector_index_op3_poison_constant_index_afterwards(ptr %ptr) {
366 ; CHECK-LABEL: @gep_vector_index_op3_poison_constant_index_afterwards(
367 ; CHECK-NEXT: ret <8 x ptr> poison
369 %res = getelementptr inbounds %t.3, ptr %ptr, i64 0, i32 1, <8 x i64> poison, i32 1
373 define i64 @gep_array_of_scalable_vectors_ptrdiff(ptr %ptr) {
374 ; CHECK-LABEL: @gep_array_of_scalable_vectors_ptrdiff(
375 ; CHECK-NEXT: [[C1:%.*]] = getelementptr inbounds [8 x <vscale x 4 x i32>], ptr [[PTR:%.*]], i64 4
376 ; CHECK-NEXT: [[C2:%.*]] = getelementptr inbounds [8 x <vscale x 4 x i32>], ptr [[PTR]], i64 6
377 ; CHECK-NEXT: [[C1_INT:%.*]] = ptrtoint ptr [[C1]] to i64
378 ; CHECK-NEXT: [[C2_INT:%.*]] = ptrtoint ptr [[C2]] to i64
379 ; CHECK-NEXT: [[DIFF:%.*]] = sub i64 [[C2_INT]], [[C1_INT]]
380 ; CHECK-NEXT: ret i64 [[DIFF]]
382 %c1 = getelementptr inbounds [8 x <vscale x 4 x i32>], ptr %ptr, i64 4
383 %c2 = getelementptr inbounds [8 x <vscale x 4 x i32>], ptr %ptr, i64 6
384 %c1.int = ptrtoint ptr %c1 to i64
385 %c2.int = ptrtoint ptr %c2 to i64
386 %diff = sub i64 %c2.int, %c1.int