1 ; RUN: opt < %s -instcombine -S | FileCheck %s
3 target datalayout = "e-p:64:64-p1:16:16-p2:32:32:32-p3:64:64:64"
5 %intstruct = type { i32 }
6 %pair = type { i32, i32 }
7 %struct.B = type { double }
8 %struct.A = type { %struct.B, i32, i32 }
9 %struct.C = type { [7 x i8] }
12 @Global = external global [10 x i8]
13 @Global_as1 = external addrspace(1) global [10 x i8]
15 ; Test noop elimination
16 define i32* @test1(i32* %I) {
17 %A = getelementptr i32, i32* %I, i64 0
19 ; CHECK-LABEL: @test1(
23 define i32 addrspace(1)* @test1_as1(i32 addrspace(1)* %I) {
24 %A = getelementptr i32, i32 addrspace(1)* %I, i64 0
25 ret i32 addrspace(1)* %A
26 ; CHECK-LABEL: @test1_as1(
27 ; CHECK: ret i32 addrspace(1)* %I
30 ; Test noop elimination
31 define i32* @test2(i32* %I) {
32 %A = getelementptr i32, i32* %I
34 ; CHECK-LABEL: @test2(
38 ; Test that two array indexing geps fold
39 define i32* @test3(i32* %I) {
40 %A = getelementptr i32, i32* %I, i64 17
41 %B = getelementptr i32, i32* %A, i64 4
43 ; CHECK-LABEL: @test3(
44 ; CHECK: getelementptr i32, i32* %I, i64 21
47 ; Test that two getelementptr insts fold
48 define i32* @test4({ i32 }* %I) {
49 %A = getelementptr { i32 }, { i32 }* %I, i64 1
50 %B = getelementptr { i32 }, { i32 }* %A, i64 0, i32 0
52 ; CHECK-LABEL: @test4(
53 ; CHECK: getelementptr { i32 }, { i32 }* %I, i64 1, i32 0
56 define void @test5(i8 %B) {
57 ; This should be turned into a constexpr instead of being an instruction
58 %A = getelementptr [10 x i8], [10 x i8]* @Global, i64 0, i64 4
61 ; CHECK-LABEL: @test5(
62 ; CHECK: store i8 %B, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @Global, i64 0, i64 4)
65 define void @test5_as1(i8 %B) {
66 ; This should be turned into a constexpr instead of being an instruction
67 %A = getelementptr [10 x i8], [10 x i8] addrspace(1)* @Global_as1, i16 0, i16 4
68 store i8 %B, i8 addrspace(1)* %A
70 ; CHECK-LABEL: @test5_as1(
71 ; CHECK: store i8 %B, i8 addrspace(1)* getelementptr inbounds ([10 x i8], [10 x i8] addrspace(1)* @Global_as1, i16 0, i16 4)
74 %as1_ptr_struct = type { i32 addrspace(1)* }
75 %as2_ptr_struct = type { i32 addrspace(2)* }
77 @global_as2 = addrspace(2) global i32 zeroinitializer
78 @global_as1_as2_ptr = addrspace(1) global %as2_ptr_struct { i32 addrspace(2)* @global_as2 }
80 ; This should be turned into a constexpr instead of being an instruction
81 define void @test_evaluate_gep_nested_as_ptrs(i32 addrspace(2)* %B) {
82 ; CHECK-LABEL: @test_evaluate_gep_nested_as_ptrs(
83 ; CHECK-NEXT: store i32 addrspace(2)* %B, i32 addrspace(2)* addrspace(1)* getelementptr inbounds (%as2_ptr_struct, %as2_ptr_struct addrspace(1)* @global_as1_as2_ptr, i16 0, i32 0), align 8
84 ; CHECK-NEXT: ret void
85 %A = getelementptr %as2_ptr_struct, %as2_ptr_struct addrspace(1)* @global_as1_as2_ptr, i16 0, i32 0
86 store i32 addrspace(2)* %B, i32 addrspace(2)* addrspace(1)* %A
90 @arst = addrspace(1) global [4 x i8 addrspace(2)*] zeroinitializer
92 define void @test_evaluate_gep_as_ptrs_array(i8 addrspace(2)* %B) {
93 ; CHECK-LABEL: @test_evaluate_gep_as_ptrs_array(
94 ; CHECK-NEXT: store i8 addrspace(2)* %B, i8 addrspace(2)* addrspace(1)* getelementptr inbounds ([4 x i8 addrspace(2)*], [4 x i8 addrspace(2)*] addrspace(1)* @arst, i16 0, i16 2), align 4
96 ; CHECK-NEXT: ret void
97 %A = getelementptr [4 x i8 addrspace(2)*], [4 x i8 addrspace(2)*] addrspace(1)* @arst, i16 0, i16 2
98 store i8 addrspace(2)* %B, i8 addrspace(2)* addrspace(1)* %A
102 define i32* @test7(i32* %I, i64 %C, i64 %D) {
103 %A = getelementptr i32, i32* %I, i64 %C
104 %B = getelementptr i32, i32* %A, i64 %D
106 ; CHECK-LABEL: @test7(
107 ; CHECK: %A = getelementptr i32, i32* %I, i64 %C
108 ; CHECK: %B = getelementptr i32, i32* %A, i64 %D
111 define i8* @test8([10 x i32]* %X) {
112 ;; Fold into the cast.
113 %A = getelementptr [10 x i32], [10 x i32]* %X, i64 0, i64 0
114 %B = bitcast i32* %A to i8*
116 ; CHECK-LABEL: @test8(
117 ; CHECK: bitcast [10 x i32]* %X to i8*
120 define i32 @test9() {
121 %A = getelementptr { i32, double }, { i32, double }* null, i32 0, i32 1
122 %B = ptrtoint double* %A to i32
124 ; CHECK-LABEL: @test9(
128 define i1 @test10({ i32, i32 }* %x, { i32, i32 }* %y) {
129 %tmp.1 = getelementptr { i32, i32 }, { i32, i32 }* %x, i32 0, i32 1
130 %tmp.3 = getelementptr { i32, i32 }, { i32, i32 }* %y, i32 0, i32 1
132 %tmp.4 = icmp eq i32* %tmp.1, %tmp.3
134 ; CHECK-LABEL: @test10(
135 ; CHECK: icmp eq { i32, i32 }* %x, %y
138 define i1 @test11({ i32, i32 }* %X) {
139 %P = getelementptr { i32, i32 }, { i32, i32 }* %X, i32 0, i32 0
140 %Q = icmp eq i32* %P, null
142 ; CHECK-LABEL: @test11(
143 ; CHECK: icmp eq { i32, i32 }* %X, null
148 define i32 @test12(%struct.A* %a) {
150 %g3 = getelementptr %struct.A, %struct.A* %a, i32 0, i32 1
151 store i32 10, i32* %g3, align 4
153 %g4 = getelementptr %struct.A, %struct.A* %a, i32 0, i32 0
155 %new_a = bitcast %struct.B* %g4 to %struct.A*
157 %g5 = getelementptr %struct.A, %struct.A* %new_a, i32 0, i32 1
158 %a_a = load i32, i32* %g5, align 4
160 ; CHECK-LABEL: @test12(
161 ; CHECK: getelementptr %struct.A, %struct.A* %a, i64 0, i32 1
162 ; CHECK-NEXT: store i32 10, i32* %g3
163 ; CHECK-NEXT: ret i32 10
168 %S = type { i32, [ 100 x i32] }
169 define i1 @test13(i64 %X, %S* %P) {
170 %A = getelementptr inbounds %S, %S* %P, i32 0, i32 1, i64 %X
171 %B = getelementptr inbounds %S, %S* %P, i32 0, i32 0
172 %C = icmp eq i32* %A, %B
174 ; CHECK-LABEL: @test13(
175 ; CHECK: %C = icmp eq i64 %X, -1
178 ; This is a test of icmp + shl nuw in disguise - 4611... is 0x3fff...
179 define <2 x i1> @test13_vector(<2 x i64> %X, <2 x %S*> %P) nounwind {
180 ; CHECK-LABEL: @test13_vector(
181 ; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i64> %X, <i64 4611686018427387903, i64 4611686018427387903>
182 ; CHECK-NEXT: ret <2 x i1> [[C]]
184 %A = getelementptr inbounds %S, <2 x %S*> %P, <2 x i64> zeroinitializer, <2 x i32> <i32 1, i32 1>, <2 x i64> %X
185 %B = getelementptr inbounds %S, <2 x %S*> %P, <2 x i64> <i64 0, i64 0>, <2 x i32> <i32 0, i32 0>
186 %C = icmp eq <2 x i32*> %A, %B
190 ; This is a test of icmp + shl nuw in disguise - 4611... is 0x3fff...
191 define <2 x i1> @test13_vector2(i64 %X, <2 x %S*> %P) nounwind {
192 ; CHECK-LABEL: @test13_vector2(
193 ; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <2 x i64> undef, i64 [[X:%.*]], i32 0
194 ; CHECK-NEXT: [[TMP1:%.*]] = shl nuw <2 x i64> [[DOTSPLATINSERT]], <i64 2, i64 undef>
195 ; CHECK-NEXT: [[A_IDX:%.*]] = shufflevector <2 x i64> [[TMP1]], <2 x i64> undef, <2 x i32> zeroinitializer
196 ; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i64> [[A_IDX]], <i64 -4, i64 -4>
197 ; CHECK-NEXT: ret <2 x i1> [[C]]
199 %A = getelementptr inbounds %S, <2 x %S*> %P, <2 x i64> zeroinitializer, <2 x i32> <i32 1, i32 1>, i64 %X
200 %B = getelementptr inbounds %S, <2 x %S*> %P, <2 x i64> <i64 0, i64 0>, <2 x i32> <i32 0, i32 0>
201 %C = icmp eq <2 x i32*> %A, %B
205 ; This is a test of icmp + shl nuw in disguise - 4611... is 0x3fff...
206 define <2 x i1> @test13_vector3(i64 %X, <2 x %S*> %P) nounwind {
207 ; CHECK-LABEL: @test13_vector3(
208 ; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <2 x i64> undef, i64 [[X:%.*]], i32 0
209 ; CHECK-NEXT: [[TMP1:%.*]] = shl nuw <2 x i64> [[DOTSPLATINSERT]], <i64 2, i64 undef>
210 ; CHECK-NEXT: [[A_IDX:%.*]] = shufflevector <2 x i64> [[TMP1]], <2 x i64> undef, <2 x i32> zeroinitializer
211 ; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i64> [[A_IDX]], <i64 4, i64 4>
212 ; CHECK-NEXT: ret <2 x i1> [[C]]
214 %A = getelementptr inbounds %S, <2 x %S*> %P, <2 x i64> zeroinitializer, <2 x i32> <i32 1, i32 1>, i64 %X
215 %B = getelementptr inbounds %S, <2 x %S*> %P, <2 x i64> <i64 0, i64 0>, <2 x i32> <i32 1, i32 1>, i64 1
216 %C = icmp eq <2 x i32*> %A, %B
220 define i1 @test13_as1(i16 %X, %S addrspace(1)* %P) {
221 ; CHECK-LABEL: @test13_as1(
222 ; CHECK-NEXT: %C = icmp eq i16 %X, -1
223 ; CHECK-NEXT: ret i1 %C
224 %A = getelementptr inbounds %S, %S addrspace(1)* %P, i16 0, i32 1, i16 %X
225 %B = getelementptr inbounds %S, %S addrspace(1)* %P, i16 0, i32 0
226 %C = icmp eq i32 addrspace(1)* %A, %B
230 ; This is a test of icmp + shl nuw in disguise - 16383 is 0x3fff.
231 define <2 x i1> @test13_vector_as1(<2 x i16> %X, <2 x %S addrspace(1)*> %P) {
232 ; CHECK-LABEL: @test13_vector_as1(
233 ; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i16> %X, <i16 16383, i16 16383>
234 ; CHECK-NEXT: ret <2 x i1> [[C]]
236 %A = getelementptr inbounds %S, <2 x %S addrspace(1)*> %P, <2 x i16> <i16 0, i16 0>, <2 x i32> <i32 1, i32 1>, <2 x i16> %X
237 %B = getelementptr inbounds %S, <2 x %S addrspace(1)*> %P, <2 x i16> <i16 0, i16 0>, <2 x i32> <i32 0, i32 0>
238 %C = icmp eq <2 x i32 addrspace(1)*> %A, %B
242 define i1 @test13_i32(i32 %X, %S* %P) {
243 ; CHECK-LABEL: @test13_i32(
244 ; CHECK: %C = icmp eq i32 %X, -1
245 %A = getelementptr inbounds %S, %S* %P, i32 0, i32 1, i32 %X
246 %B = getelementptr inbounds %S, %S* %P, i32 0, i32 0
247 %C = icmp eq i32* %A, %B
251 define i1 @test13_i16(i16 %X, %S* %P) {
252 ; CHECK-LABEL: @test13_i16(
253 ; CHECK: %C = icmp eq i16 %X, -1
254 %A = getelementptr inbounds %S, %S* %P, i16 0, i32 1, i16 %X
255 %B = getelementptr inbounds %S, %S* %P, i16 0, i32 0
256 %C = icmp eq i32* %A, %B
260 define i1 @test13_i128(i128 %X, %S* %P) {
261 ; CHECK-LABEL: @test13_i128(
262 ; CHECK: %C = icmp eq i64 %1, -1
263 %A = getelementptr inbounds %S, %S* %P, i128 0, i32 1, i128 %X
264 %B = getelementptr inbounds %S, %S* %P, i128 0, i32 0
265 %C = icmp eq i32* %A, %B
270 @G = external global [3 x i8]
271 define i8* @test14(i32 %Idx) {
272 %idx = zext i32 %Idx to i64
273 %tmp = getelementptr i8, i8* getelementptr ([3 x i8], [3 x i8]* @G, i32 0, i32 0), i64 %idx
275 ; CHECK-LABEL: @test14(
276 ; CHECK: getelementptr [3 x i8], [3 x i8]* @G, i64 0, i64 %idx
280 ; Test folding of constantexpr geps into normal geps.
281 @Array = external global [40 x i32]
282 define i32 *@test15(i64 %X) {
283 %A = getelementptr i32, i32* getelementptr ([40 x i32], [40 x i32]* @Array, i64 0, i64 0), i64 %X
285 ; CHECK-LABEL: @test15(
286 ; CHECK: getelementptr [40 x i32], [40 x i32]* @Array, i64 0, i64 %X
290 define i32* @test16(i32* %X, i32 %Idx) {
291 %R = getelementptr i32, i32* %X, i32 %Idx
293 ; CHECK-LABEL: @test16(
294 ; CHECK: sext i32 %Idx to i64
298 define i1 @test17(i16* %P, i32 %I, i32 %J) {
299 %X = getelementptr inbounds i16, i16* %P, i32 %I
300 %Y = getelementptr inbounds i16, i16* %P, i32 %J
301 %C = icmp ult i16* %X, %Y
303 ; CHECK-LABEL: @test17(
304 ; CHECK: %C = icmp slt i32 %I, %J
307 define i1 @test18(i16* %P, i32 %I) {
308 %X = getelementptr inbounds i16, i16* %P, i32 %I
309 %C = icmp ult i16* %X, %P
311 ; CHECK-LABEL: @test18(
312 ; CHECK: %C = icmp slt i32 %I, 0
315 ; Larger than the pointer size for a non-zero address space
316 define i1 @test18_as1(i16 addrspace(1)* %P, i32 %I) {
317 ; CHECK-LABEL: @test18_as1(
318 ; CHECK-NEXT: %1 = trunc i32 %I to i16
319 ; CHECK-NEXT: %C = icmp slt i16 %1, 0
320 ; CHECK-NEXT: ret i1 %C
321 %X = getelementptr inbounds i16, i16 addrspace(1)* %P, i32 %I
322 %C = icmp ult i16 addrspace(1)* %X, %P
326 ; Smaller than the pointer size for a non-zero address space
327 define i1 @test18_as1_i32(i16 addrspace(1)* %P, i32 %I) {
328 ; CHECK-LABEL: @test18_as1_i32(
329 ; CHECK-NEXT: %1 = trunc i32 %I to i16
330 ; CHECK-NEXT: %C = icmp slt i16 %1, 0
331 ; CHECK-NEXT: ret i1 %C
332 %X = getelementptr inbounds i16, i16 addrspace(1)* %P, i32 %I
333 %C = icmp ult i16 addrspace(1)* %X, %P
337 ; Smaller than pointer size
338 define i1 @test18_i16(i16* %P, i16 %I) {
339 ; CHECK-LABEL: @test18_i16(
340 ; CHECK: %C = icmp slt i16 %I, 0
341 %X = getelementptr inbounds i16, i16* %P, i16 %I
342 %C = icmp ult i16* %X, %P
346 ; Same as pointer size
347 define i1 @test18_i64(i16* %P, i64 %I) {
348 ; CHECK-LABEL: @test18_i64(
349 ; CHECK: %C = icmp slt i64 %I, 0
350 %X = getelementptr inbounds i16, i16* %P, i64 %I
351 %C = icmp ult i16* %X, %P
355 ; Larger than the pointer size
356 define i1 @test18_i128(i16* %P, i128 %I) {
357 ; CHECK-LABEL: @test18_i128(
358 ; CHECK: %C = icmp slt i64 %1, 0
359 %X = getelementptr inbounds i16, i16* %P, i128 %I
360 %C = icmp ult i16* %X, %P
364 define i32 @test19(i32* %P, i32 %A, i32 %B) {
365 %tmp.4 = getelementptr inbounds i32, i32* %P, i32 %A
366 %tmp.9 = getelementptr inbounds i32, i32* %P, i32 %B
367 %tmp.10 = icmp eq i32* %tmp.4, %tmp.9
368 %tmp.11 = zext i1 %tmp.10 to i32
370 ; CHECK-LABEL: @test19(
371 ; CHECK: icmp eq i32 %A, %B
374 define i32 @test20(i32* %P, i32 %A, i32 %B) {
375 %tmp.4 = getelementptr inbounds i32, i32* %P, i32 %A
376 %tmp.6 = icmp eq i32* %tmp.4, %P
377 %tmp.7 = zext i1 %tmp.6 to i32
379 ; CHECK-LABEL: @test20(
380 ; CHECK: icmp eq i32 %A, 0
383 define i32 @test20_as1(i32 addrspace(1)* %P, i32 %A, i32 %B) {
384 %tmp.4 = getelementptr inbounds i32, i32 addrspace(1)* %P, i32 %A
385 %tmp.6 = icmp eq i32 addrspace(1)* %tmp.4, %P
386 %tmp.7 = zext i1 %tmp.6 to i32
388 ; CHECK-LABEL: @test20_as1(
389 ; CHECK: icmp eq i16 %1, 0
393 define i32 @test21() {
394 %pbob1 = alloca %intstruct
395 %pbob2 = getelementptr %intstruct, %intstruct* %pbob1
396 %pbobel = getelementptr %intstruct, %intstruct* %pbob2, i64 0, i32 0
397 %rval = load i32, i32* %pbobel
399 ; CHECK-LABEL: @test21(
400 ; CHECK: getelementptr inbounds %intstruct, %intstruct* %pbob1, i64 0, i32 0
404 @A = global i32 1 ; <i32*> [#uses=1]
405 @B = global i32 2 ; <i32*> [#uses=1]
407 define i1 @test22() {
408 %C = icmp ult i32* getelementptr (i32, i32* @A, i64 1),
409 getelementptr (i32, i32* @B, i64 2)
411 ; CHECK-LABEL: @test22(
412 ; CHECK: icmp ult (i32* getelementptr inbounds (i32, i32* @A, i64 1), i32* getelementptr (i32, i32* @B, i64 2))
416 %X = type { [10 x i32], float }
418 define i1 @test23() {
419 %A = getelementptr %X, %X* null, i64 0, i32 0, i64 0 ; <i32*> [#uses=1]
420 %B = icmp ne i32* %A, null ; <i1> [#uses=1]
422 ; CHECK-LABEL: @test23(
423 ; CHECK: ret i1 false
426 define void @test25() {
428 %tmp = getelementptr { i64, i64, i64, i64 }, { i64, i64, i64, i64 }* null, i32 0, i32 3 ; <i64*> [#uses=1]
429 %tmp.upgrd.1 = load i64, i64* %tmp ; <i64> [#uses=1]
430 %tmp8.ui = load i64, i64* null ; <i64> [#uses=1]
431 %tmp8 = bitcast i64 %tmp8.ui to i64 ; <i64> [#uses=1]
432 %tmp9 = and i64 %tmp8, %tmp.upgrd.1 ; <i64> [#uses=1]
433 %sext = trunc i64 %tmp9 to i32 ; <i32> [#uses=1]
434 %tmp27.i = sext i32 %sext to i64 ; <i64> [#uses=1]
435 tail call void @foo25( i32 0, i64 %tmp27.i )
437 ; CHECK-LABEL: @test25(
440 declare void @foo25(i32, i64)
444 define i1 @test26(i8* %arr) {
445 %X = getelementptr i8, i8* %arr, i32 1
446 %Y = getelementptr i8, i8* %arr, i32 1
447 %test = icmp uge i8* %X, %Y
449 ; CHECK-LABEL: @test26(
453 %struct.__large_struct = type { [100 x i64] }
454 %struct.compat_siginfo = type { i32, i32, i32, { [29 x i32] } }
455 %struct.siginfo_t = type { i32, i32, i32, { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] } }
456 %struct.sigval_t = type { i8* }
458 define i32 @test27(%struct.compat_siginfo* %to, %struct.siginfo_t* %from) {
460 %from_addr = alloca %struct.siginfo_t*
461 %tmp344 = load %struct.siginfo_t*, %struct.siginfo_t** %from_addr, align 8
462 %tmp345 = getelementptr %struct.siginfo_t, %struct.siginfo_t* %tmp344, i32 0, i32 3
463 %tmp346 = getelementptr { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] }, { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] }* %tmp345, i32 0, i32 0
464 %tmp346347 = bitcast { i32, i32, [0 x i8], %struct.sigval_t, i32 }* %tmp346 to { i32, i32, %struct.sigval_t }*
465 %tmp348 = getelementptr { i32, i32, %struct.sigval_t }, { i32, i32, %struct.sigval_t }* %tmp346347, i32 0, i32 2
466 %tmp349 = getelementptr %struct.sigval_t, %struct.sigval_t* %tmp348, i32 0, i32 0
467 %tmp349350 = bitcast i8** %tmp349 to i32*
468 %tmp351 = load i32, i32* %tmp349350, align 8
469 %tmp360 = call i32 asm sideeffect "...",
470 "=r,ir,*m,i,0,~{dirflag},~{fpsr},~{flags}"( i32 %tmp351,
471 %struct.__large_struct* null, i32 -14, i32 0 )
473 ; CHECK-LABEL: @test27(
477 %struct.x = type <{ i8 }>
478 @.str = internal constant [6 x i8] c"Main!\00"
479 @.str1 = internal constant [12 x i8] c"destroy %p\0A\00"
481 define i32 @test28() nounwind {
483 %orientations = alloca [1 x [1 x %struct.x]]
484 %tmp3 = call i32 @puts( i8* getelementptr ([6 x i8], [6 x i8]* @.str, i32 0, i32 0) ) nounwind
485 %tmp45 = getelementptr inbounds [1 x [1 x %struct.x]], [1 x [1 x %struct.x]]* %orientations, i32 1, i32 0, i32 0
486 %orientations62 = getelementptr [1 x [1 x %struct.x]], [1 x [1 x %struct.x]]* %orientations, i32 0, i32 0, i32 0
490 %indvar = phi i32 [ 0, %entry ], [ %indvar.next, %bb10 ]
491 %tmp.0.reg2mem.0.rec = mul i32 %indvar, -1
492 %tmp12.rec = add i32 %tmp.0.reg2mem.0.rec, -1
493 %tmp12 = getelementptr inbounds %struct.x, %struct.x* %tmp45, i32 %tmp12.rec
494 %tmp16 = call i32 (i8*, ...) @printf( i8* nonnull dereferenceable(1) getelementptr ([12 x i8], [12 x i8]* @.str1, i32 0, i32 0), %struct.x* %tmp12 ) nounwind
495 %tmp84 = icmp eq %struct.x* %tmp12, %orientations62
496 %indvar.next = add i32 %indvar, 1
497 br i1 %tmp84, label %bb17, label %bb10
501 ; CHECK-LABEL: @test28(
502 ; CHECK: icmp eq i32 %indvar, 0
505 declare i32 @puts(i8*)
507 declare i32 @printf(i8*, ...)
513 %T = type <{ i64, i64, i64 }>
514 define i32 @test29(i8* %start, i32 %X) nounwind {
516 %tmp3 = load i64, i64* null
517 %add.ptr = getelementptr i8, i8* %start, i64 %tmp3
518 %tmp158 = load i32, i32* null
519 %add.ptr159 = getelementptr %T, %T* null, i32 %tmp158
520 %add.ptr209 = getelementptr i8, i8* %start, i64 0
521 %add.ptr212 = getelementptr i8, i8* %add.ptr209, i32 %X
522 %cmp214 = icmp ugt i8* %add.ptr212, %add.ptr
523 br i1 %cmp214, label %if.then216, label %if.end363
530 ; CHECK-LABEL: @test29(
535 define i32 @test30(i32 %m, i32 %n) nounwind {
537 %0 = alloca i32, i32 %n, align 4
538 %1 = bitcast i32* %0 to [0 x i32]*
539 call void @test30f(i32* %0) nounwind
540 %2 = getelementptr [0 x i32], [0 x i32]* %1, i32 0, i32 %m
541 %3 = load i32, i32* %2, align 4
543 ; CHECK-LABEL: @test30(
544 ; CHECK: getelementptr i32
547 declare void @test30f(i32*)
551 define i1 @test31(i32* %A) {
552 %B = getelementptr i32, i32* %A, i32 1
553 %C = getelementptr i32, i32* %A, i64 1
554 %V = icmp eq i32* %B, %C
556 ; CHECK-LABEL: @test31(
562 define i8* @test32(i8* %v) {
563 %A = alloca [4 x i8*], align 16
564 %B = getelementptr [4 x i8*], [4 x i8*]* %A, i32 0, i32 0
565 store i8* null, i8** %B
566 %C = bitcast [4 x i8*]* %A to { [16 x i8] }*
567 %D = getelementptr { [16 x i8] }, { [16 x i8] }* %C, i32 0, i32 0, i32 8
568 %E = bitcast i8* %D to i8**
569 store i8* %v, i8** %E
570 %F = getelementptr [4 x i8*], [4 x i8*]* %A, i32 0, i32 2
571 %G = load i8*, i8** %F
573 ; CHECK-LABEL: @test32(
574 ; CHECK: %D = getelementptr inbounds [4 x i8*], [4 x i8*]* %A, i64 0, i64 1
575 ; CHECK: %F = getelementptr inbounds [4 x i8*], [4 x i8*]* %A, i64 0, i64 2
579 %struct.Key = type { { i32, i32 } }
580 %struct.anon = type <{ i8, [3 x i8], i32 }>
582 define i32* @test33(%struct.Key* %A) {
583 ; CHECK-LABEL: @test33(
584 ; CHECK: getelementptr %struct.Key, %struct.Key* %A, i64 0, i32 0, i32 1
585 %B = bitcast %struct.Key* %A to %struct.anon*
586 %C = getelementptr %struct.anon, %struct.anon* %B, i32 0, i32 2
590 define i32 addrspace(1)* @test33_as1(%struct.Key addrspace(1)* %A) {
591 ; CHECK-LABEL: @test33_as1(
592 ; CHECK: getelementptr %struct.Key, %struct.Key addrspace(1)* %A, i16 0, i32 0, i32 1
593 %B = bitcast %struct.Key addrspace(1)* %A to %struct.anon addrspace(1)*
594 %C = getelementptr %struct.anon, %struct.anon addrspace(1)* %B, i32 0, i32 2
595 ret i32 addrspace(1)* %C
598 define i32 addrspace(1)* @test33_array_as1([10 x i32] addrspace(1)* %A) {
599 ; CHECK-LABEL: @test33_array_as1(
600 ; CHECK: getelementptr [10 x i32], [10 x i32] addrspace(1)* %A, i16 0, i16 2
601 %B = bitcast [10 x i32] addrspace(1)* %A to [5 x i32] addrspace(1)*
602 %C = getelementptr [5 x i32], [5 x i32] addrspace(1)* %B, i32 0, i32 2
603 ret i32 addrspace(1)* %C
606 ; Make sure the GEP indices use the right pointer sized integer
607 define i32 addrspace(1)* @test33_array_struct_as1([10 x %struct.Key] addrspace(1)* %A) {
608 ; CHECK-LABEL: @test33_array_struct_as1(
609 ; CHECK: getelementptr [10 x %struct.Key], [10 x %struct.Key] addrspace(1)* %A, i16 0, i16 1, i32 0, i32 0
610 %B = bitcast [10 x %struct.Key] addrspace(1)* %A to [20 x i32] addrspace(1)*
611 %C = getelementptr [20 x i32], [20 x i32] addrspace(1)* %B, i32 0, i32 2
612 ret i32 addrspace(1)* %C
615 define i32 addrspace(1)* @test33_addrspacecast(%struct.Key* %A) {
616 ; CHECK-LABEL: @test33_addrspacecast(
617 ; CHECK: %C = getelementptr %struct.Key, %struct.Key* %A, i64 0, i32 0, i32 1
618 ; CHECK-NEXT: addrspacecast i32* %C to i32 addrspace(1)*
620 %B = addrspacecast %struct.Key* %A to %struct.anon addrspace(1)*
621 %C = getelementptr %struct.anon, %struct.anon addrspace(1)* %B, i32 0, i32 2
622 ret i32 addrspace(1)* %C
625 %T2 = type { i8*, i8 }
626 define i8* @test34(i8* %Val, i64 %V) nounwind {
628 %A = alloca %T2, align 8
629 %mrv_gep = bitcast %T2* %A to i64*
630 %B = getelementptr %T2, %T2* %A, i64 0, i32 0
632 store i64 %V, i64* %mrv_gep
633 %C = load i8*, i8** %B, align 8
635 ; CHECK-LABEL: @test34(
636 ; CHECK: %[[C:.*]] = inttoptr i64 %V to i8*
637 ; CHECK: ret i8* %[[C]]
640 %t0 = type { i8*, [19 x i8] }
641 %t1 = type { i8*, [0 x i8] }
643 @array = external global [11 x i8]
645 @s = external global %t0
646 @"\01LC8" = external constant [17 x i8]
648 ; Instcombine should be able to fold this getelementptr.
650 define i32 @test35() nounwind {
651 call i32 (i8*, ...) @printf(i8* getelementptr ([17 x i8], [17 x i8]* @"\01LC8", i32 0, i32 0),
652 i8* getelementptr (%t1, %t1* bitcast (%t0* @s to %t1*), i32 0, i32 1, i32 0)) nounwind
654 ; CHECK-LABEL: @test35(
655 ; CHECK: call i32 (i8*, ...) @printf(i8* nonnull dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @"\01LC8", i64 0, i64 0), i8* getelementptr inbounds (%t0, %t0* @s, i64 0, i32 1, i64 0)) [[$NUW:#[0-9]+]]
658 ; Don't treat signed offsets as unsigned.
659 define i8* @test36() nounwind {
660 ret i8* getelementptr ([11 x i8], [11 x i8]* @array, i32 0, i64 -1)
661 ; CHECK-LABEL: @test36(
662 ; CHECK: ret i8* getelementptr ([11 x i8], [11 x i8]* @array, i64 0, i64 -1)
665 ; Instcombine shouldn't assume that gep(A,0,1) != gep(A,1,0).
666 @A37 = external constant [1 x i8]
667 define i1 @test37() nounwind {
668 ; CHECK-LABEL: @test37(
670 %t = icmp eq i8* getelementptr ([1 x i8], [1 x i8]* @A37, i64 0, i64 1),
671 getelementptr ([1 x i8], [1 x i8]* @A37, i64 1, i64 0)
675 ; Test index promotion
676 define i32* @test38(i32* %I, i32 %n) {
677 %A = getelementptr i32, i32* %I, i32 %n
679 ; CHECK-LABEL: @test38(
680 ; CHECK: = sext i32 %n to i64
681 ; CHECK: %A = getelementptr i32, i32* %I, i64 %
684 ; Test that we don't duplicate work when the second gep is a "bitcast".
685 %pr10322_t = type { i8* }
686 declare void @pr10322_f2(%pr10322_t*)
687 declare void @pr10322_f3(i8**)
688 define void @pr10322_f1(%pr10322_t* %foo) {
690 %arrayidx8 = getelementptr inbounds %pr10322_t, %pr10322_t* %foo, i64 2
691 call void @pr10322_f2(%pr10322_t* %arrayidx8) nounwind
692 %tmp2 = getelementptr inbounds %pr10322_t, %pr10322_t* %arrayidx8, i64 0, i32 0
693 call void @pr10322_f3(i8** %tmp2) nounwind
696 ; CHECK-LABEL: @pr10322_f1(
697 ; CHECK: %tmp2 = getelementptr inbounds %pr10322_t, %pr10322_t* %arrayidx8, i64 0, i32 0
700 ; Test that we combine the last two geps in this sequence, before we
701 ; would wait for gep1 and gep2 to be combined and never combine 2 and 3.
702 %three_gep_t = type {i32}
703 %three_gep_t2 = type {%three_gep_t}
705 define void @three_gep_f(%three_gep_t2* %x) {
706 %gep1 = getelementptr %three_gep_t2, %three_gep_t2* %x, i64 2
707 call void @three_gep_h(%three_gep_t2* %gep1)
708 %gep2 = getelementptr %three_gep_t2, %three_gep_t2* %gep1, i64 0, i32 0
709 %gep3 = getelementptr %three_gep_t, %three_gep_t* %gep2, i64 0, i32 0
710 call void @three_gep_g(i32* %gep3)
712 ; CHECK-LABEL: @three_gep_f(
713 ; CHECK: %gep3 = getelementptr %three_gep_t2, %three_gep_t2* %gep1, i64 0, i32 0, i32 0
717 declare void @three_gep_g(i32*)
718 declare void @three_gep_h(%three_gep_t2*)
720 %struct.ham = type { i32, %struct.zot*, %struct.zot*, %struct.zot* }
721 %struct.zot = type { i64, i8 }
723 define void @test39(%struct.ham* %arg, i8 %arg1) nounwind {
724 %tmp = getelementptr inbounds %struct.ham, %struct.ham* %arg, i64 0, i32 2
725 %tmp2 = load %struct.zot*, %struct.zot** %tmp, align 8
726 %tmp3 = bitcast %struct.zot* %tmp2 to i8*
727 %tmp4 = getelementptr inbounds i8, i8* %tmp3, i64 -8
728 store i8 %arg1, i8* %tmp4, align 8
731 ; CHECK-LABEL: @test39(
732 ; CHECK: getelementptr inbounds %struct.ham, %struct.ham* %arg, i64 0, i32 2
733 ; CHECK: getelementptr inbounds i8, i8* %{{.+}}, i64 -8
736 define i1 @pr16483([1 x i8]* %a, [1 x i8]* %b) {
737 %c = getelementptr [1 x i8], [1 x i8]* %a, i32 0, i32 0
738 %d = getelementptr [1 x i8], [1 x i8]* %b, i32 0, i32 0
739 %cmp = icmp ult i8* %c, %d
742 ; CHECK-LABEL: @pr16483(
743 ; CHECK-NEXT: icmp ult [1 x i8]* %a, %b
746 define i8 @test_gep_bitcast_as1(i32 addrspace(1)* %arr, i16 %N) {
747 ; CHECK-LABEL: @test_gep_bitcast_as1(
748 ; CHECK: getelementptr i32, i32 addrspace(1)* %arr, i16 %N
750 %cast = bitcast i32 addrspace(1)* %arr to i8 addrspace(1)*
752 %t = getelementptr i8, i8 addrspace(1)* %cast, i16 %V
753 %x = load i8, i8 addrspace(1)* %t
757 ; The element size of the array matches the element size of the pointer
758 define i64 @test_gep_bitcast_array_same_size_element([100 x double]* %arr, i64 %N) {
759 ; CHECK-LABEL: @test_gep_bitcast_array_same_size_element(
760 ; CHECK: getelementptr [100 x double], [100 x double]* %arr, i64 0, i64 %V
762 %cast = bitcast [100 x double]* %arr to i64*
764 %t = getelementptr i64, i64* %cast, i64 %V
765 %x = load i64, i64* %t
769 ; gep should be done in the original address space.
770 define i64 @test_gep_bitcast_array_same_size_element_addrspacecast([100 x double]* %arr, i64 %N) {
771 ; CHECK-LABEL: @test_gep_bitcast_array_same_size_element_addrspacecast(
772 ; CHECK: getelementptr [100 x double], [100 x double]* %arr, i64 0, i64 %V
773 ; CHECK-NEXT: bitcast double*
774 ; CHECK-NEXT: %t = addrspacecast i64*
775 ; CHECK: load i64, i64 addrspace(3)* %t
776 %cast = addrspacecast [100 x double]* %arr to i64 addrspace(3)*
778 %t = getelementptr i64, i64 addrspace(3)* %cast, i64 %V
779 %x = load i64, i64 addrspace(3)* %t
783 ; The element size of the array is different the element size of the pointer
784 define i8 @test_gep_bitcast_array_different_size_element([100 x double]* %arr, i64 %N) {
785 ; CHECK-LABEL: @test_gep_bitcast_array_different_size_element(
786 ; CHECK: getelementptr [100 x double], [100 x double]* %arr, i64 0, i64 %N
788 %cast = bitcast [100 x double]* %arr to i8*
790 %t = getelementptr i8, i8* %cast, i64 %V
795 define i64 @test_gep_bitcast_array_same_size_element_as1([100 x double] addrspace(1)* %arr, i16 %N) {
796 ; CHECK-LABEL: @test_gep_bitcast_array_same_size_element_as1(
797 ; CHECK: getelementptr [100 x double], [100 x double] addrspace(1)* %arr, i16 0, i16 %V
799 %cast = bitcast [100 x double] addrspace(1)* %arr to i64 addrspace(1)*
801 %t = getelementptr i64, i64 addrspace(1)* %cast, i16 %V
802 %x = load i64, i64 addrspace(1)* %t
806 define i8 @test_gep_bitcast_array_different_size_element_as1([100 x double] addrspace(1)* %arr, i16 %N) {
807 ; CHECK-LABEL: @test_gep_bitcast_array_different_size_element_as1(
808 ; CHECK: getelementptr [100 x double], [100 x double] addrspace(1)* %arr, i16 0, i16 %N
810 %cast = bitcast [100 x double] addrspace(1)* %arr to i8 addrspace(1)*
812 %t = getelementptr i8, i8 addrspace(1)* %cast, i16 %V
813 %x = load i8, i8 addrspace(1)* %t
817 define i64 @test40() {
818 %array = alloca [3 x i32], align 4
819 %gep = getelementptr inbounds [3 x i32], [3 x i32]* %array, i64 0, i64 2
820 %gepi8 = bitcast i32* %gep to i8*
821 %p = ptrtoint [3 x i32]* %array to i64
823 %gep2 = getelementptr i8, i8* %gepi8, i64 %np
824 %ret = ptrtoint i8* %gep2 to i64
827 ; CHECK-LABEL: @test40
828 ; CHECK-NEXT: ret i64 8
831 define i16 @test41([3 x i32] addrspace(1)* %array) {
832 %gep = getelementptr inbounds [3 x i32], [3 x i32] addrspace(1)* %array, i16 0, i16 2
833 %gepi8 = bitcast i32 addrspace(1)* %gep to i8 addrspace(1)*
834 %p = ptrtoint [3 x i32] addrspace(1)* %array to i16
836 %gep2 = getelementptr i8, i8 addrspace(1)* %gepi8, i16 %np
837 %ret = ptrtoint i8 addrspace(1)* %gep2 to i16
840 ; CHECK-LABEL: @test41(
841 ; CHECK-NEXT: ret i16 8
844 define i8* @test42(i8* %c1, i8* %c2) {
845 %ptrtoint = ptrtoint i8* %c1 to i64
846 %sub = sub i64 0, %ptrtoint
847 %gep = getelementptr inbounds i8, i8* %c2, i64 %sub
850 ; CHECK-LABEL: @test42(
851 ; CHECK-NEXT: [[PTRTOINT1:%.*]] = ptrtoint i8* %c1 to i64
852 ; CHECK-NEXT: [[PTRTOINT2:%.*]] = ptrtoint i8* %c2 to i64
853 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]]
854 ; CHECK-NEXT: [[INTTOPTR:%.*]] = inttoptr i64 [[SUB]] to i8*
855 ; CHECK-NEXT: ret i8* [[INTTOPTR]]
858 define i16* @test43(i16* %c1, i16* %c2) {
859 %ptrtoint = ptrtoint i16* %c1 to i64
860 %sub = sub i64 0, %ptrtoint
861 %shr = ashr i64 %sub, 1
862 %gep = getelementptr inbounds i16, i16* %c2, i64 %shr
865 ; CHECK-LABEL: @test43(
866 ; CHECK-NEXT: [[PTRTOINT1:%.*]] = ptrtoint i16* %c1 to i64
867 ; CHECK-NEXT: [[PTRTOINT2:%.*]] = ptrtoint i16* %c2 to i64
868 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]]
869 ; CHECK-NEXT: [[INTTOPTR:%.*]] = inttoptr i64 [[SUB]] to i16*
870 ; CHECK-NEXT: ret i16* [[INTTOPTR]]
873 define %struct.C* @test44(%struct.C* %c1, %struct.C* %c2) {
874 %ptrtoint = ptrtoint %struct.C* %c1 to i64
875 %sub = sub i64 0, %ptrtoint
876 %shr = sdiv i64 %sub, 7
877 %gep = getelementptr inbounds %struct.C, %struct.C* %c2, i64 %shr
880 ; CHECK-LABEL: @test44(
881 ; CHECK-NEXT: [[PTRTOINT1:%.*]] = ptrtoint %struct.C* %c1 to i64
882 ; CHECK-NEXT: [[PTRTOINT2:%.*]] = ptrtoint %struct.C* %c2 to i64
883 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]]
884 ; CHECK-NEXT: [[INTTOPTR:%.*]] = inttoptr i64 [[SUB]] to %struct.C*
885 ; CHECK-NEXT: ret %struct.C* [[INTTOPTR]]
888 define %struct.C* @test45(%struct.C* %c1, %struct.C** %c2) {
889 %ptrtoint1 = ptrtoint %struct.C* %c1 to i64
890 %ptrtoint2 = ptrtoint %struct.C** %c2 to i64
891 %sub = sub i64 %ptrtoint2, %ptrtoint1 ; C2 - C1
892 %shr = sdiv i64 %sub, 7
893 %gep = getelementptr inbounds %struct.C, %struct.C* %c1, i64 %shr ; C1 + (C2 - C1)
896 ; CHECK-LABEL: @test45(
897 ; CHECK-NEXT: [[BITCAST:%.*]] = bitcast %struct.C** %c2 to %struct.C*
898 ; CHECK-NEXT: ret %struct.C* [[BITCAST]]
901 define %struct.C* @test46(%struct.C* %c1, %struct.C* %c2, i64 %N) {
902 %ptrtoint = ptrtoint %struct.C* %c1 to i64
903 %sub = sub i64 0, %ptrtoint
904 %sdiv = sdiv i64 %sub, %N
905 %gep = getelementptr inbounds %struct.C, %struct.C* %c2, i64 %sdiv
908 ; CHECK-LABEL: @test46(
909 ; CHECK-NEXT: [[PTRTOINT:%.*]] = ptrtoint %struct.C* %c1 to i64
910 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[PTRTOINT]]
911 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv i64 [[SUB]], %N
912 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds %struct.C, %struct.C* %c2, i64 %sdiv
913 ; CHECK-NEXT: ret %struct.C* [[GEP]]
916 define i32* @test47(i32* %I, i64 %C, i64 %D) {
917 %sub = sub i64 %D, %C
918 %A = getelementptr i32, i32* %I, i64 %C
919 %B = getelementptr i32, i32* %A, i64 %sub
921 ; CHECK-LABEL: @test47(
922 ; CHECK-NEXT: %B = getelementptr i32, i32* %I, i64 %D
925 define i32* @test48(i32* %I, i64 %C, i64 %D) {
926 %sub = sub i64 %D, %C
927 %A = getelementptr i32, i32* %I, i64 %sub
928 %B = getelementptr i32, i32* %A, i64 %C
930 ; CHECK-LABEL: @test48(
931 ; CHECK-NEXT: %B = getelementptr i32, i32* %I, i64 %D
934 define i32* @test49(i32* %I, i64 %C) {
935 %notC = xor i64 -1, %C
936 %A = getelementptr i32, i32* %I, i64 %C
937 %B = getelementptr i32, i32* %A, i64 %notC
939 ; CHECK-LABEL: @test49(
940 ; CHECK-NEXT: %B = getelementptr i32, i32* %I, i64 -1
943 define i32 addrspace(1)* @ascast_0_gep(i32* %p) nounwind {
944 ; CHECK-LABEL: @ascast_0_gep(
945 ; CHECK-NOT: getelementptr
947 %gep = getelementptr i32, i32* %p, i32 0
948 %x = addrspacecast i32* %gep to i32 addrspace(1)*
949 ret i32 addrspace(1)* %x
952 ; Do not merge the GEP and the addrspacecast, because it would undo the
953 ; addrspacecast canonicalization.
954 define i32 addrspace(1)* @ascast_0_0_gep([128 x i32]* %p) nounwind {
955 ; CHECK-LABEL: @ascast_0_0_gep(
956 ; CHECK-NEXT: getelementptr [128 x i32]
957 ; CHECK-NEXT: addrspacecast i32*
958 ; CHECK-NEXT: ret i32 addrspace(1)*
959 %gep = getelementptr [128 x i32], [128 x i32]* %p, i32 0, i32 0
960 %x = addrspacecast i32* %gep to i32 addrspace(1)*
961 ret i32 addrspace(1)* %x
964 define <2 x i32*> @PR32414(i32** %ptr) {
965 ; CHECK-LABEL: @PR32414(
966 ; CHECK-NEXT: [[TMP0:%.*]] = bitcast i32** %ptr to i32*
967 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i32, i32* [[TMP0]], <2 x i64> <i64 0, i64 1>
968 ; CHECK-NEXT: ret <2 x i32*> [[TMP1]]
970 %tmp0 = bitcast i32** %ptr to i32*
971 %tmp1 = getelementptr inbounds i32, i32* %tmp0, <2 x i64> <i64 0, i64 1>
975 ; CHECK: attributes [[$NUW]] = { nounwind }