• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; RUN: opt < %s -instcombine -S | FileCheck %s
2
3target datalayout = "e-p:64:64-p1:16:16-p2:32:32:32-p3:64:64:64"
4
5%intstruct = type { i32 }
6%pair = type { i32, i32 }
7%struct.B = type { double }
8%struct.A = type { %struct.B, i32, i32 }
9
10
11@Global = constant [10 x i8] c"helloworld"
12@Global_as1 = addrspace(1) constant [10 x i8] c"helloworld"
13
14; Test noop elimination
15define i32* @test1(i32* %I) {
16        %A = getelementptr i32* %I, i64 0
17        ret i32* %A
18; CHECK-LABEL: @test1(
19; CHECK: ret i32* %I
20}
21
22define i32 addrspace(1)* @test1_as1(i32 addrspace(1)* %I) {
23  %A = getelementptr i32 addrspace(1)* %I, i64 0
24  ret i32 addrspace(1)* %A
25; CHECK-LABEL: @test1_as1(
26; CHECK: ret i32 addrspace(1)* %I
27}
28
29; Test noop elimination
30define i32* @test2(i32* %I) {
31        %A = getelementptr i32* %I
32        ret i32* %A
33; CHECK-LABEL: @test2(
34; CHECK: ret i32* %I
35}
36
37; Test that two array indexing geps fold
38define i32* @test3(i32* %I) {
39        %A = getelementptr i32* %I, i64 17
40        %B = getelementptr i32* %A, i64 4
41        ret i32* %B
42; CHECK-LABEL: @test3(
43; CHECK: getelementptr i32* %I, i64 21
44}
45
46; Test that two getelementptr insts fold
47define i32* @test4({ i32 }* %I) {
48        %A = getelementptr { i32 }* %I, i64 1
49        %B = getelementptr { i32 }* %A, i64 0, i32 0
50        ret i32* %B
51; CHECK-LABEL: @test4(
52; CHECK: getelementptr { i32 }* %I, i64 1, i32 0
53}
54
55define void @test5(i8 %B) {
56        ; This should be turned into a constexpr instead of being an instruction
57        %A = getelementptr [10 x i8]* @Global, i64 0, i64 4
58        store i8 %B, i8* %A
59        ret void
60; CHECK-LABEL: @test5(
61; CHECK: store i8 %B, i8* getelementptr inbounds ([10 x i8]* @Global, i64 0, i64 4)
62}
63
64define void @test5_as1(i8 %B) {
65        ; This should be turned into a constexpr instead of being an instruction
66        %A = getelementptr [10 x i8] addrspace(1)* @Global_as1, i16 0, i16 4
67        store i8 %B, i8 addrspace(1)* %A
68        ret void
69; CHECK-LABEL: @test5_as1(
70; CHECK: store i8 %B, i8 addrspace(1)* getelementptr inbounds ([10 x i8] addrspace(1)* @Global_as1, i16 0, i16 4)
71}
72
73%as1_ptr_struct = type { i32 addrspace(1)* }
74%as2_ptr_struct = type { i32 addrspace(2)* }
75
76@global_as2 = addrspace(2) global i32 zeroinitializer
77@global_as1_as2_ptr = addrspace(1) global %as2_ptr_struct { i32 addrspace(2)* @global_as2 }
78
79; This should be turned into a constexpr instead of being an instruction
80define void @test_evaluate_gep_nested_as_ptrs(i32 addrspace(2)* %B) {
81; CHECK-LABEL: @test_evaluate_gep_nested_as_ptrs(
82; CHECK-NEXT: store i32 addrspace(2)* %B, i32 addrspace(2)* addrspace(1)* getelementptr inbounds (%as2_ptr_struct addrspace(1)* @global_as1_as2_ptr, i16 0, i32 0), align 8
83; CHECK-NEXT: ret void
84  %A = getelementptr %as2_ptr_struct addrspace(1)* @global_as1_as2_ptr, i16 0, i32 0
85  store i32 addrspace(2)* %B, i32 addrspace(2)* addrspace(1)* %A
86  ret void
87}
88
89@arst = addrspace(1) global [4 x i8 addrspace(2)*] zeroinitializer
90
91define void @test_evaluate_gep_as_ptrs_array(i8 addrspace(2)* %B) {
92; CHECK-LABEL: @test_evaluate_gep_as_ptrs_array(
93; CHECK-NEXT: store i8 addrspace(2)* %B, i8 addrspace(2)* addrspace(1)* getelementptr inbounds ([4 x i8 addrspace(2)*] addrspace(1)* @arst, i16 0, i16 2), align 4
94
95; CHECK-NEXT: ret void
96  %A = getelementptr [4 x i8 addrspace(2)*] addrspace(1)* @arst, i16 0, i16 2
97  store i8 addrspace(2)* %B, i8 addrspace(2)* addrspace(1)* %A
98  ret void
99}
100
101define i32* @test7(i32* %I, i64 %C, i64 %D) {
102        %A = getelementptr i32* %I, i64 %C
103        %B = getelementptr i32* %A, i64 %D
104        ret i32* %B
105; CHECK-LABEL: @test7(
106; CHECK: %A.sum = add i64 %C, %D
107; CHECK: getelementptr i32* %I, i64 %A.sum
108}
109
110define i8* @test8([10 x i32]* %X) {
111        ;; Fold into the cast.
112        %A = getelementptr [10 x i32]* %X, i64 0, i64 0
113        %B = bitcast i32* %A to i8*
114        ret i8* %B
115; CHECK-LABEL: @test8(
116; CHECK: bitcast [10 x i32]* %X to i8*
117}
118
119define i32 @test9() {
120        %A = getelementptr { i32, double }* null, i32 0, i32 1
121        %B = ptrtoint double* %A to i32
122        ret i32 %B
123; CHECK-LABEL: @test9(
124; CHECK: ret i32 8
125}
126
127define i1 @test10({ i32, i32 }* %x, { i32, i32 }* %y) {
128        %tmp.1 = getelementptr { i32, i32 }* %x, i32 0, i32 1
129        %tmp.3 = getelementptr { i32, i32 }* %y, i32 0, i32 1
130        ;; seteq x, y
131        %tmp.4 = icmp eq i32* %tmp.1, %tmp.3
132        ret i1 %tmp.4
133; CHECK-LABEL: @test10(
134; CHECK: icmp eq { i32, i32 }* %x, %y
135}
136
137define i1 @test11({ i32, i32 }* %X) {
138        %P = getelementptr { i32, i32 }* %X, i32 0, i32 0
139        %Q = icmp eq i32* %P, null
140        ret i1 %Q
141; CHECK-LABEL: @test11(
142; CHECK: icmp eq { i32, i32 }* %X, null
143}
144
145
146; PR4748
147define i32 @test12(%struct.A* %a) {
148entry:
149  %g3 = getelementptr %struct.A* %a, i32 0, i32 1
150  store i32 10, i32* %g3, align 4
151
152  %g4 = getelementptr %struct.A* %a, i32 0, i32 0
153
154  %new_a = bitcast %struct.B* %g4 to %struct.A*
155
156  %g5 = getelementptr %struct.A* %new_a, i32 0, i32 1
157  %a_a = load i32* %g5, align 4
158  ret i32 %a_a
159; CHECK-LABEL:      @test12(
160; CHECK:      getelementptr %struct.A* %a, i64 0, i32 1
161; CHECK-NEXT: store i32 10, i32* %g3
162; CHECK-NEXT: ret i32 10
163}
164
165
166; PR2235
167%S = type { i32, [ 100 x i32] }
168define i1 @test13(i64 %X, %S* %P) {
169        %A = getelementptr inbounds %S* %P, i32 0, i32 1, i64 %X
170        %B = getelementptr inbounds %S* %P, i32 0, i32 0
171	%C = icmp eq i32* %A, %B
172	ret i1 %C
173; CHECK-LABEL: @test13(
174; CHECK:    %C = icmp eq i64 %X, -1
175}
176
177define <2 x i1> @test13_vector(<2 x i64> %X, <2 x %S*> %P) nounwind {
178; CHECK-LABEL: @test13_vector(
179; CHECK-NEXT: shl nuw <2 x i64> %X, <i64 2, i64 2>
180; CHECK-NEXT: add <2 x i64> %A.idx, <i64 4, i64 4>
181; CHECK-NEXT: icmp eq <2 x i64> %A.offs, zeroinitializer
182  %A = getelementptr inbounds <2 x %S*> %P, <2 x i64> zeroinitializer, <2 x i32> <i32 1, i32 1>, <2 x i64> %X
183  %B = getelementptr inbounds <2 x %S*> %P, <2 x i64> <i64 0, i64 0>, <2 x i32> <i32 0, i32 0>
184  %C = icmp eq <2 x i32*> %A, %B
185  ret <2 x i1> %C
186}
187
188define i1 @test13_as1(i16 %X, %S addrspace(1)* %P) {
189; CHECK-LABEL: @test13_as1(
190; CHECK-NEXT:  %C = icmp eq i16 %X, -1
191; CHECK-NEXT: ret i1 %C
192  %A = getelementptr inbounds %S addrspace(1)* %P, i16 0, i32 1, i16 %X
193  %B = getelementptr inbounds %S addrspace(1)* %P, i16 0, i32 0
194  %C = icmp eq i32 addrspace(1)* %A, %B
195  ret i1 %C
196}
197
198define <2 x i1> @test13_vector_as1(<2 x i16> %X, <2 x %S addrspace(1)*> %P) {
199; CHECK-LABEL: @test13_vector_as1(
200; CHECK-NEXT: shl nuw <2 x i16> %X, <i16 2, i16 2>
201; CHECK-NEXT: add <2 x i16> %A.idx, <i16 4, i16 4>
202; CHECK-NEXT: icmp eq <2 x i16> %A.offs, zeroinitializer
203; CHECK-NEXT: ret <2 x i1>
204  %A = getelementptr inbounds <2 x %S addrspace(1)*> %P, <2 x i16> <i16 0, i16 0>, <2 x i32> <i32 1, i32 1>, <2 x i16> %X
205  %B = getelementptr inbounds <2 x %S addrspace(1)*> %P, <2 x i16> <i16 0, i16 0>, <2 x i32> <i32 0, i32 0>
206  %C = icmp eq <2 x i32 addrspace(1)*> %A, %B
207  ret <2 x i1> %C
208}
209
210define i1 @test13_i32(i32 %X, %S* %P) {
211; CHECK-LABEL: @test13_i32(
212; CHECK: %C = icmp eq i32 %X, -1
213  %A = getelementptr inbounds %S* %P, i32 0, i32 1, i32 %X
214  %B = getelementptr inbounds %S* %P, i32 0, i32 0
215  %C = icmp eq i32* %A, %B
216  ret i1 %C
217}
218
219define i1 @test13_i16(i16 %X, %S* %P) {
220; CHECK-LABEL: @test13_i16(
221; CHECK: %C = icmp eq i16 %X, -1
222  %A = getelementptr inbounds %S* %P, i16 0, i32 1, i16 %X
223  %B = getelementptr inbounds %S* %P, i16 0, i32 0
224  %C = icmp eq i32* %A, %B
225  ret i1 %C
226}
227
228define i1 @test13_i128(i128 %X, %S* %P) {
229; CHECK-LABEL: @test13_i128(
230; CHECK: %C = icmp eq i64 %1, -1
231  %A = getelementptr inbounds %S* %P, i128 0, i32 1, i128 %X
232  %B = getelementptr inbounds %S* %P, i128 0, i32 0
233  %C = icmp eq i32* %A, %B
234  ret i1 %C
235}
236
237
238@G = external global [3 x i8]
239define i8* @test14(i32 %Idx) {
240        %idx = zext i32 %Idx to i64
241        %tmp = getelementptr i8* getelementptr ([3 x i8]* @G, i32 0, i32 0), i64 %idx
242        ret i8* %tmp
243; CHECK-LABEL: @test14(
244; CHECK: getelementptr [3 x i8]* @G, i64 0, i64 %idx
245}
246
247
248; Test folding of constantexpr geps into normal geps.
249@Array = external global [40 x i32]
250define i32 *@test15(i64 %X) {
251        %A = getelementptr i32* getelementptr ([40 x i32]* @Array, i64 0, i64 0), i64 %X
252        ret i32* %A
253; CHECK-LABEL: @test15(
254; CHECK: getelementptr [40 x i32]* @Array, i64 0, i64 %X
255}
256
257
258define i32* @test16(i32* %X, i32 %Idx) {
259        %R = getelementptr i32* %X, i32 %Idx
260        ret i32* %R
261; CHECK-LABEL: @test16(
262; CHECK: sext i32 %Idx to i64
263}
264
265
266define i1 @test17(i16* %P, i32 %I, i32 %J) {
267        %X = getelementptr inbounds i16* %P, i32 %I
268        %Y = getelementptr inbounds i16* %P, i32 %J
269        %C = icmp ult i16* %X, %Y
270        ret i1 %C
271; CHECK-LABEL: @test17(
272; CHECK: %C = icmp slt i32 %I, %J
273}
274
275define i1 @test18(i16* %P, i32 %I) {
276        %X = getelementptr inbounds i16* %P, i32 %I
277        %C = icmp ult i16* %X, %P
278        ret i1 %C
279; CHECK-LABEL: @test18(
280; CHECK: %C = icmp slt i32 %I, 0
281}
282
283; Larger than the pointer size for a non-zero address space
284define i1 @test18_as1(i16 addrspace(1)* %P, i32 %I) {
285; CHECK-LABEL: @test18_as1(
286; CHECK-NEXT: %1 = trunc i32 %I to i16
287; CHECK-NEXT: %C = icmp slt i16 %1, 0
288; CHECK-NEXT: ret i1 %C
289  %X = getelementptr inbounds i16 addrspace(1)* %P, i32 %I
290  %C = icmp ult i16 addrspace(1)* %X, %P
291  ret i1 %C
292}
293
294; Smaller than the pointer size for a non-zero address space
295define i1 @test18_as1_i32(i16 addrspace(1)* %P, i32 %I) {
296; CHECK-LABEL: @test18_as1_i32(
297; CHECK-NEXT: %1 = trunc i32 %I to i16
298; CHECK-NEXT: %C = icmp slt i16 %1, 0
299; CHECK-NEXT: ret i1 %C
300  %X = getelementptr inbounds i16 addrspace(1)* %P, i32 %I
301  %C = icmp ult i16 addrspace(1)* %X, %P
302  ret i1 %C
303}
304
305; Smaller than pointer size
306define i1 @test18_i16(i16* %P, i16 %I) {
307; CHECK-LABEL: @test18_i16(
308; CHECK: %C = icmp slt i16 %I, 0
309  %X = getelementptr inbounds i16* %P, i16 %I
310  %C = icmp ult i16* %X, %P
311  ret i1 %C
312}
313
314; Same as pointer size
315define i1 @test18_i64(i16* %P, i64 %I) {
316; CHECK-LABEL: @test18_i64(
317; CHECK: %C = icmp slt i64 %I, 0
318  %X = getelementptr inbounds i16* %P, i64 %I
319  %C = icmp ult i16* %X, %P
320  ret i1 %C
321}
322
323; Larger than the pointer size
324define i1 @test18_i128(i16* %P, i128 %I) {
325; CHECK-LABEL: @test18_i128(
326; CHECK: %C = icmp slt i64 %1, 0
327  %X = getelementptr inbounds i16* %P, i128 %I
328  %C = icmp ult i16* %X, %P
329  ret i1 %C
330}
331
332define i32 @test19(i32* %P, i32 %A, i32 %B) {
333        %tmp.4 = getelementptr inbounds i32* %P, i32 %A
334        %tmp.9 = getelementptr inbounds i32* %P, i32 %B
335        %tmp.10 = icmp eq i32* %tmp.4, %tmp.9
336        %tmp.11 = zext i1 %tmp.10 to i32
337        ret i32 %tmp.11
338; CHECK-LABEL: @test19(
339; CHECK: icmp eq i32 %A, %B
340}
341
342define i32 @test20(i32* %P, i32 %A, i32 %B) {
343        %tmp.4 = getelementptr inbounds i32* %P, i32 %A
344        %tmp.6 = icmp eq i32* %tmp.4, %P
345        %tmp.7 = zext i1 %tmp.6 to i32
346        ret i32 %tmp.7
347; CHECK-LABEL: @test20(
348; CHECK: icmp eq i32 %A, 0
349}
350
351define i32 @test20_as1(i32 addrspace(1)* %P, i32 %A, i32 %B) {
352  %tmp.4 = getelementptr inbounds i32 addrspace(1)* %P, i32 %A
353  %tmp.6 = icmp eq i32 addrspace(1)* %tmp.4, %P
354  %tmp.7 = zext i1 %tmp.6 to i32
355  ret i32 %tmp.7
356; CHECK-LABEL: @test20_as1(
357; CHECK: icmp eq i16 %1, 0
358}
359
360
361define i32 @test21() {
362        %pbob1 = alloca %intstruct
363        %pbob2 = getelementptr %intstruct* %pbob1
364        %pbobel = getelementptr %intstruct* %pbob2, i64 0, i32 0
365        %rval = load i32* %pbobel
366        ret i32 %rval
367; CHECK-LABEL: @test21(
368; CHECK: getelementptr %intstruct* %pbob1, i64 0, i32 0
369}
370
371
372@A = global i32 1               ; <i32*> [#uses=1]
373@B = global i32 2               ; <i32*> [#uses=1]
374
375define i1 @test22() {
376        %C = icmp ult i32* getelementptr (i32* @A, i64 1),
377                           getelementptr (i32* @B, i64 2)
378        ret i1 %C
379; CHECK-LABEL: @test22(
380; CHECK: icmp ult (i32* getelementptr inbounds (i32* @A, i64 1), i32* getelementptr (i32* @B, i64 2))
381}
382
383
384%X = type { [10 x i32], float }
385
386define i1 @test23() {
387        %A = getelementptr %X* null, i64 0, i32 0, i64 0                ; <i32*> [#uses=1]
388        %B = icmp ne i32* %A, null              ; <i1> [#uses=1]
389        ret i1 %B
390; CHECK-LABEL: @test23(
391; CHECK: ret i1 false
392}
393
394define void @test25() {
395entry:
396        %tmp = getelementptr { i64, i64, i64, i64 }* null, i32 0, i32 3         ; <i64*> [#uses=1]
397        %tmp.upgrd.1 = load i64* %tmp           ; <i64> [#uses=1]
398        %tmp8.ui = load i64* null               ; <i64> [#uses=1]
399        %tmp8 = bitcast i64 %tmp8.ui to i64             ; <i64> [#uses=1]
400        %tmp9 = and i64 %tmp8, %tmp.upgrd.1             ; <i64> [#uses=1]
401        %sext = trunc i64 %tmp9 to i32          ; <i32> [#uses=1]
402        %tmp27.i = sext i32 %sext to i64                ; <i64> [#uses=1]
403        tail call void @foo25( i32 0, i64 %tmp27.i )
404        unreachable
405; CHECK-LABEL: @test25(
406}
407
408declare void @foo25(i32, i64)
409
410
411; PR1637
412define i1 @test26(i8* %arr) {
413        %X = getelementptr i8* %arr, i32 1
414        %Y = getelementptr i8* %arr, i32 1
415        %test = icmp uge i8* %X, %Y
416        ret i1 %test
417; CHECK-LABEL: @test26(
418; CHECK: ret i1 true
419}
420
421	%struct.__large_struct = type { [100 x i64] }
422	%struct.compat_siginfo = type { i32, i32, i32, { [29 x i32] } }
423	%struct.siginfo_t = type { i32, i32, i32, { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] } }
424	%struct.sigval_t = type { i8* }
425
426define i32 @test27(%struct.compat_siginfo* %to, %struct.siginfo_t* %from) {
427entry:
428	%from_addr = alloca %struct.siginfo_t*
429	%tmp344 = load %struct.siginfo_t** %from_addr, align 8
430	%tmp345 = getelementptr %struct.siginfo_t* %tmp344, i32 0, i32 3
431	%tmp346 = getelementptr { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] }* %tmp345, i32 0, i32 0
432	%tmp346347 = bitcast { i32, i32, [0 x i8], %struct.sigval_t, i32 }* %tmp346 to { i32, i32, %struct.sigval_t }*
433	%tmp348 = getelementptr { i32, i32, %struct.sigval_t }* %tmp346347, i32 0, i32 2
434	%tmp349 = getelementptr %struct.sigval_t* %tmp348, i32 0, i32 0
435	%tmp349350 = bitcast i8** %tmp349 to i32*
436	%tmp351 = load i32* %tmp349350, align 8
437	%tmp360 = call i32 asm sideeffect "...",
438        "=r,ir,*m,i,0,~{dirflag},~{fpsr},~{flags}"( i32 %tmp351,
439         %struct.__large_struct* null, i32 -14, i32 0 )
440	unreachable
441; CHECK-LABEL: @test27(
442}
443
444; PR1978
445	%struct.x = type <{ i8 }>
446@.str = internal constant [6 x i8] c"Main!\00"
447@.str1 = internal constant [12 x i8] c"destroy %p\0A\00"
448
449define i32 @test28() nounwind  {
450entry:
451	%orientations = alloca [1 x [1 x %struct.x]]
452	%tmp3 = call i32 @puts( i8* getelementptr ([6 x i8]* @.str, i32 0, i32 0) ) nounwind
453	%tmp45 = getelementptr inbounds [1 x [1 x %struct.x]]* %orientations, i32 1, i32 0, i32 0
454	%orientations62 = getelementptr [1 x [1 x %struct.x]]* %orientations, i32 0, i32 0, i32 0
455	br label %bb10
456
457bb10:
458	%indvar = phi i32 [ 0, %entry ], [ %indvar.next, %bb10 ]
459	%tmp.0.reg2mem.0.rec = mul i32 %indvar, -1
460	%tmp12.rec = add i32 %tmp.0.reg2mem.0.rec, -1
461	%tmp12 = getelementptr inbounds %struct.x* %tmp45, i32 %tmp12.rec
462	%tmp16 = call i32 (i8*, ...)* @printf( i8* getelementptr ([12 x i8]* @.str1, i32 0, i32 0), %struct.x* %tmp12 ) nounwind
463	%tmp84 = icmp eq %struct.x* %tmp12, %orientations62
464	%indvar.next = add i32 %indvar, 1
465	br i1 %tmp84, label %bb17, label %bb10
466
467bb17:
468	ret i32 0
469; CHECK-LABEL: @test28(
470; CHECK: icmp eq i32 %indvar, 0
471}
472
473declare i32 @puts(i8*)
474
475declare i32 @printf(i8*, ...)
476
477
478
479
480; rdar://6762290
481	%T = type <{ i64, i64, i64 }>
482define i32 @test29(i8* %start, i32 %X) nounwind {
483entry:
484	%tmp3 = load i64* null
485	%add.ptr = getelementptr i8* %start, i64 %tmp3
486	%tmp158 = load i32* null
487	%add.ptr159 = getelementptr %T* null, i32 %tmp158
488	%add.ptr209 = getelementptr i8* %start, i64 0
489	%add.ptr212 = getelementptr i8* %add.ptr209, i32 %X
490	%cmp214 = icmp ugt i8* %add.ptr212, %add.ptr
491	br i1 %cmp214, label %if.then216, label %if.end363
492
493if.then216:
494	ret i32 1
495
496if.end363:
497	ret i32 0
498; CHECK-LABEL: @test29(
499}
500
501
502; PR3694
503define i32 @test30(i32 %m, i32 %n) nounwind {
504entry:
505	%0 = alloca i32, i32 %n, align 4
506	%1 = bitcast i32* %0 to [0 x i32]*
507	call void @test30f(i32* %0) nounwind
508	%2 = getelementptr [0 x i32]* %1, i32 0, i32 %m
509	%3 = load i32* %2, align 4
510	ret i32 %3
511; CHECK-LABEL: @test30(
512; CHECK: getelementptr i32
513}
514
515declare void @test30f(i32*)
516
517
518
519define i1 @test31(i32* %A) {
520        %B = getelementptr i32* %A, i32 1
521        %C = getelementptr i32* %A, i64 1
522        %V = icmp eq i32* %B, %C
523        ret i1 %V
524; CHECK-LABEL: @test31(
525; CHECK: ret i1 true
526}
527
528
529; PR1345
530define i8* @test32(i8* %v) {
531	%A = alloca [4 x i8*], align 16
532	%B = getelementptr [4 x i8*]* %A, i32 0, i32 0
533	store i8* null, i8** %B
534	%C = bitcast [4 x i8*]* %A to { [16 x i8] }*
535	%D = getelementptr { [16 x i8] }* %C, i32 0, i32 0, i32 8
536	%E = bitcast i8* %D to i8**
537	store i8* %v, i8** %E
538	%F = getelementptr [4 x i8*]* %A, i32 0, i32 2
539	%G = load i8** %F
540	ret i8* %G
541; CHECK-LABEL: @test32(
542; CHECK: %D = getelementptr [4 x i8*]* %A, i64 0, i64 1
543; CHECK: %F = getelementptr [4 x i8*]* %A, i64 0, i64 2
544}
545
546; PR3290
547%struct.Key = type { { i32, i32 } }
548%struct.anon = type <{ i8, [3 x i8], i32 }>
549
550define i32* @test33(%struct.Key* %A) {
551; CHECK-LABEL: @test33(
552; CHECK: getelementptr %struct.Key* %A, i64 0, i32 0, i32 1
553  %B = bitcast %struct.Key* %A to %struct.anon*
554  %C = getelementptr %struct.anon* %B, i32 0, i32 2
555  ret i32* %C
556}
557
558define i32 addrspace(1)* @test33_as1(%struct.Key addrspace(1)* %A) {
559; CHECK-LABEL: @test33_as1(
560; CHECK: getelementptr %struct.Key addrspace(1)* %A, i16 0, i32 0, i32 1
561  %B = bitcast %struct.Key addrspace(1)* %A to %struct.anon addrspace(1)*
562  %C = getelementptr %struct.anon addrspace(1)* %B, i32 0, i32 2
563  ret i32 addrspace(1)* %C
564}
565
566define i32 addrspace(1)* @test33_array_as1([10 x i32] addrspace(1)* %A) {
567; CHECK-LABEL: @test33_array_as1(
568; CHECK: getelementptr [10 x i32] addrspace(1)* %A, i16 0, i16 2
569  %B = bitcast [10 x i32] addrspace(1)* %A to [5 x i32] addrspace(1)*
570  %C = getelementptr [5 x i32] addrspace(1)* %B, i32 0, i32 2
571  ret i32 addrspace(1)* %C
572}
573
574; Make sure the GEP indices use the right pointer sized integer
575define i32 addrspace(1)* @test33_array_struct_as1([10 x %struct.Key] addrspace(1)* %A) {
576; CHECK-LABEL: @test33_array_struct_as1(
577; CHECK: getelementptr [10 x %struct.Key] addrspace(1)* %A, i16 0, i16 1, i32 0, i32 0
578  %B = bitcast [10 x %struct.Key] addrspace(1)* %A to [20 x i32] addrspace(1)*
579  %C = getelementptr [20 x i32] addrspace(1)* %B, i32 0, i32 2
580  ret i32 addrspace(1)* %C
581}
582
583	%T2 = type { i8*, i8 }
584define i8* @test34(i8* %Val, i64 %V) nounwind {
585entry:
586	%A = alloca %T2, align 8
587	%mrv_gep = bitcast %T2* %A to i64*
588	%B = getelementptr %T2* %A, i64 0, i32 0
589
590      	store i64 %V, i64* %mrv_gep
591	%C = load i8** %B, align 8
592	ret i8* %C
593; CHECK-LABEL: @test34(
594; CHECK: %V.c = inttoptr i64 %V to i8*
595; CHECK: ret i8* %V.c
596}
597
598%t0 = type { i8*, [19 x i8] }
599%t1 = type { i8*, [0 x i8] }
600
601@array = external global [11 x i8]
602
603@s = external global %t0
604@"\01LC8" = external constant [17 x i8]
605
606; Instcombine should be able to fold this getelementptr.
607
608define i32 @test35() nounwind {
609  call i32 (i8*, ...)* @printf(i8* getelementptr ([17 x i8]* @"\01LC8", i32 0, i32 0),
610             i8* getelementptr (%t1* bitcast (%t0* @s to %t1*), i32 0, i32 1, i32 0)) nounwind
611  ret i32 0
612; CHECK-LABEL: @test35(
613; CHECK: call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([17 x i8]* @"\01LC8", i64 0, i64 0), i8* getelementptr inbounds (%t0* @s, i64 0, i32 1, i64 0)) [[NUW:#[0-9]+]]
614}
615
616; Instcombine should constant-fold the GEP so that indices that have
617; static array extents are within bounds of those array extents.
618; In the below, -1 is not in the range [0,11). After the transformation,
619; the same address is computed, but 3 is in the range of [0,11).
620
621define i8* @test36() nounwind {
622  ret i8* getelementptr ([11 x i8]* @array, i32 0, i64 -1)
623; CHECK-LABEL: @test36(
624; CHECK: ret i8* getelementptr ([11 x i8]* @array, i64 1676976733973595601, i64 4)
625}
626
627; Instcombine shouldn't assume that gep(A,0,1) != gep(A,1,0).
628@A37 = external constant [1 x i8]
629define i1 @test37() nounwind {
630; CHECK-LABEL: @test37(
631; CHECK: ret i1 true
632  %t = icmp eq i8* getelementptr ([1 x i8]* @A37, i64 0, i64 1),
633                   getelementptr ([1 x i8]* @A37, i64 1, i64 0)
634  ret i1 %t
635}
636
637; Test index promotion
638define i32* @test38(i32* %I, i32 %n) {
639        %A = getelementptr i32* %I, i32 %n
640        ret i32* %A
641; CHECK-LABEL: @test38(
642; CHECK: = sext i32 %n to i64
643; CHECK: %A = getelementptr i32* %I, i64 %
644}
645
646; Test that we don't duplicate work when the second gep is a "bitcast".
647%pr10322_t = type { i8* }
648declare void @pr10322_f2(%pr10322_t*)
649declare void @pr10322_f3(i8**)
650define void @pr10322_f1(%pr10322_t* %foo) {
651entry:
652  %arrayidx8 = getelementptr inbounds %pr10322_t* %foo, i64 2
653  call void @pr10322_f2(%pr10322_t* %arrayidx8) nounwind
654  %tmp2 = getelementptr inbounds %pr10322_t* %arrayidx8, i64 0, i32 0
655  call void @pr10322_f3(i8** %tmp2) nounwind
656  ret void
657
658; CHECK-LABEL: @pr10322_f1(
659; CHECK: %tmp2 = getelementptr inbounds %pr10322_t* %arrayidx8, i64 0, i32 0
660}
661
662; Test that we combine the last two geps in this sequence, before we
663; would wait for gep1 and gep2 to be combined and never combine 2 and 3.
664%three_gep_t = type {i32}
665%three_gep_t2 = type {%three_gep_t}
666
667define void @three_gep_f(%three_gep_t2* %x) {
668  %gep1 = getelementptr %three_gep_t2* %x, i64 2
669  call void @three_gep_h(%three_gep_t2* %gep1)
670  %gep2 = getelementptr %three_gep_t2* %gep1, i64 0, i32 0
671  %gep3 = getelementptr %three_gep_t* %gep2, i64 0, i32 0
672  call void @three_gep_g(i32* %gep3)
673
674; CHECK-LABEL: @three_gep_f(
675; CHECK: %gep3 = getelementptr %three_gep_t2* %gep1, i64 0, i32 0, i32 0
676  ret void
677}
678
679declare void @three_gep_g(i32*)
680declare void @three_gep_h(%three_gep_t2*)
681
682%struct.ham = type { i32, %struct.zot*, %struct.zot*, %struct.zot* }
683%struct.zot = type { i64, i8 }
684
685define void @test39(%struct.ham* %arg, i8 %arg1) nounwind {
686  %tmp = getelementptr inbounds %struct.ham* %arg, i64 0, i32 2
687  %tmp2 = load %struct.zot** %tmp, align 8
688  %tmp3 = bitcast %struct.zot* %tmp2 to i8*
689  %tmp4 = getelementptr inbounds i8* %tmp3, i64 -8
690  store i8 %arg1, i8* %tmp4, align 8
691  ret void
692
693; CHECK-LABEL: @test39(
694; CHECK: getelementptr inbounds %struct.ham* %arg, i64 0, i32 2
695; CHECK: getelementptr inbounds i8* %tmp3, i64 -8
696}
697
698define i1 @pr16483([1 x i8]* %a, [1 x i8]* %b) {
699  %c = getelementptr [1 x i8]* %a, i32 0, i32 0
700  %d = getelementptr [1 x i8]* %b, i32 0, i32 0
701  %cmp = icmp ult i8* %c, %d
702  ret i1 %cmp
703
704; CHECK-LABEL: @pr16483(
705; CHECK-NEXT: icmp ult  [1 x i8]* %a, %b
706}
707
708define i8 @test_gep_bitcast_as1(i32 addrspace(1)* %arr, i16 %N) {
709; CHECK-LABEL: @test_gep_bitcast_as1(
710; CHECK: getelementptr i32 addrspace(1)* %arr, i16 %N
711; CHECK: bitcast
712  %cast = bitcast i32 addrspace(1)* %arr to i8 addrspace(1)*
713  %V = mul i16 %N, 4
714  %t = getelementptr i8 addrspace(1)* %cast, i16 %V
715  %x = load i8 addrspace(1)* %t
716  ret i8 %x
717}
718
719; The element size of the array matches the element size of the pointer
720define i64 @test_gep_bitcast_array_same_size_element([100 x double]* %arr, i64 %N) {
721; CHECK-LABEL: @test_gep_bitcast_array_same_size_element(
722; CHECK: getelementptr [100 x double]* %arr, i64 0, i64 %V
723; CHECK: bitcast
724  %cast = bitcast [100 x double]* %arr to i64*
725  %V = mul i64 %N, 8
726  %t = getelementptr i64* %cast, i64 %V
727  %x = load i64* %t
728  ret i64 %x
729}
730
731; gep should be done in the original address space.
732define i64 @test_gep_bitcast_array_same_size_element_addrspacecast([100 x double]* %arr, i64 %N) {
733; CHECK-LABEL: @test_gep_bitcast_array_same_size_element_addrspacecast(
734; CHECK: getelementptr [100 x double]* %arr, i64 0, i64 %V
735; CHECK-NEXT: bitcast double*
736; CHECK-NEXT: %t = addrspacecast i64*
737; CHECK: load i64 addrspace(3)* %t
738  %cast = addrspacecast [100 x double]* %arr to i64 addrspace(3)*
739  %V = mul i64 %N, 8
740  %t = getelementptr i64 addrspace(3)* %cast, i64 %V
741  %x = load i64 addrspace(3)* %t
742  ret i64 %x
743}
744
745; The element size of the array is different the element size of the pointer
746define i8 @test_gep_bitcast_array_different_size_element([100 x double]* %arr, i64 %N) {
747; CHECK-LABEL: @test_gep_bitcast_array_different_size_element(
748; CHECK: getelementptr [100 x double]* %arr, i64 0, i64 %N
749; CHECK: bitcast
750  %cast = bitcast [100 x double]* %arr to i8*
751  %V = mul i64 %N, 8
752  %t = getelementptr i8* %cast, i64 %V
753  %x = load i8* %t
754  ret i8 %x
755}
756
757define i64 @test_gep_bitcast_array_same_size_element_as1([100 x double] addrspace(1)* %arr, i16 %N) {
758; CHECK-LABEL: @test_gep_bitcast_array_same_size_element_as1(
759; CHECK: getelementptr [100 x double] addrspace(1)* %arr, i16 0, i16 %V
760; CHECK: bitcast
761  %cast = bitcast [100 x double] addrspace(1)* %arr to i64 addrspace(1)*
762  %V = mul i16 %N, 8
763  %t = getelementptr i64 addrspace(1)* %cast, i16 %V
764  %x = load i64 addrspace(1)* %t
765  ret i64 %x
766}
767
768define i8 @test_gep_bitcast_array_different_size_element_as1([100 x double] addrspace(1)* %arr, i16 %N) {
769; CHECK-LABEL: @test_gep_bitcast_array_different_size_element_as1(
770; CHECK: getelementptr [100 x double] addrspace(1)* %arr, i16 0, i16 %N
771; CHECK: bitcast
772  %cast = bitcast [100 x double] addrspace(1)* %arr to i8 addrspace(1)*
773  %V = mul i16 %N, 8
774  %t = getelementptr i8 addrspace(1)* %cast, i16 %V
775  %x = load i8 addrspace(1)* %t
776  ret i8 %x
777}
778
779define i64 @test40() {
780  %array = alloca [3 x i32], align 4
781  %gep = getelementptr inbounds [3 x i32]* %array, i64 0, i64 2
782  %gepi8 = bitcast i32* %gep to i8*
783  %p = ptrtoint [3 x i32]* %array to i64
784  %np = sub i64 0, %p
785  %gep2 = getelementptr i8* %gepi8, i64 %np
786  %ret = ptrtoint i8* %gep2 to i64
787  ret i64 %ret
788
789; CHECK-LABEL: @test40
790; CHECK-NEXT: ret i64 8
791}
792
793define i16 @test41([3 x i32] addrspace(1)* %array) {
794  %gep = getelementptr inbounds [3 x i32] addrspace(1)* %array, i16 0, i16 2
795  %gepi8 = bitcast i32 addrspace(1)* %gep to i8 addrspace(1)*
796  %p = ptrtoint [3 x i32] addrspace(1)* %array to i16
797  %np = sub i16 0, %p
798  %gep2 = getelementptr i8 addrspace(1)* %gepi8, i16 %np
799  %ret = ptrtoint i8 addrspace(1)* %gep2 to i16
800  ret i16 %ret
801
802; CHECK-LABEL: @test41(
803; CHECK-NEXT: ret i16 8
804}
805
806define i32 addrspace(1)* @ascast_0_gep(i32* %p) nounwind {
807; CHECK-LABEL: @ascast_0_gep(
808; CHECK-NOT: getelementptr
809; CHECK: ret
810  %gep = getelementptr i32* %p, i32 0
811  %x = addrspacecast i32* %gep to i32 addrspace(1)*
812  ret i32 addrspace(1)* %x
813}
814
815; Do not merge the GEP and the addrspacecast, because it would undo the
816; addrspacecast canonicalization.
817define i32 addrspace(1)* @ascast_0_0_gep([128 x i32]* %p) nounwind {
818; CHECK-LABEL: @ascast_0_0_gep(
819; CHECK-NEXT: getelementptr [128 x i32]
820; CHECK-NEXT: addrspacecast i32*
821; CHECK-NEXT: ret i32 addrspace(1)*
822  %gep = getelementptr [128 x i32]* %p, i32 0, i32 0
823  %x = addrspacecast i32* %gep to i32 addrspace(1)*
824  ret i32 addrspace(1)* %x
825}
826
827; CHECK: attributes [[NUW]] = { nounwind }
828