1 ; RUN: llvm-as < %s | opt -instcombine | llvm-dis | FileCheck %s
3 target datalayout = "e-p:64:64"
5 %pair = type { i32, i32 }
6 %struct.B = type { double }
7 %struct.A = type { %struct.B, i32, i32 }
10 @Global = constant [10 x i8] c"helloworld"
12 ; Test noop elimination
13 define i32* @test1(i32* %I) {
14 %A = getelementptr i32* %I, i64 0
20 ; Test noop elimination
21 define i32* @test2(i32* %I) {
22 %A = getelementptr i32* %I
28 ; Test that two array indexing geps fold
29 define i32* @test3(i32* %I) {
30 %A = getelementptr i32* %I, i64 17
31 %B = getelementptr i32* %A, i64 4
34 ; CHECK: getelementptr i32* %I, i64 21
37 ; Test that two getelementptr insts fold
38 define i32* @test4({ i32 }* %I) {
39 %A = getelementptr { i32 }* %I, i64 1
40 %B = getelementptr { i32 }* %A, i64 0, i32 0
43 ; CHECK: getelementptr %0* %I, i64 1, i32 0
46 define void @test5(i8 %B) {
47 ; This should be turned into a constexpr instead of being an instruction
48 %A = getelementptr [10 x i8]* @Global, i64 0, i64 4
52 ; CHECK: store i8 %B, i8* getelementptr inbounds ([10 x i8]* @Global, i64 0, i64 4)
55 define i32* @test6() {
57 %A = getelementptr [4 x i32]* %M, i64 0, i64 0
58 %B = getelementptr i32* %A, i64 2
61 ; CHECK: getelementptr [4 x i32]* %M, i64 0, i64 2
64 define i32* @test7(i32* %I, i64 %C, i64 %D) {
65 %A = getelementptr i32* %I, i64 %C ; <i32*> [#uses=1]
66 %B = getelementptr i32* %A, i64 %D ; <i32*> [#uses=1]
69 ; CHECK: %A.sum = add i64 %C, %D
70 ; CHECK: getelementptr i32* %I, i64 %A.sum
73 define i8* @test8([10 x i32]* %X) {
74 ;; Fold into the cast.
75 %A = getelementptr [10 x i32]* %X, i64 0, i64 0 ; <i32*> [#uses=1]
76 %B = bitcast i32* %A to i8* ; <i8*> [#uses=1]
79 ; CHECK: bitcast [10 x i32]* %X to i8*
83 %A = getelementptr { i32, double }* null, i32 0, i32 1 ; <double*> [#uses=1]
84 %B = ptrtoint double* %A to i32 ; <i32> [#uses=1]
90 define i1 @test10({ i32, i32 }* %x, { i32, i32 }* %y) {
91 %tmp.1 = getelementptr { i32, i32 }* %x, i32 0, i32 1 ; <i32*> [#uses=1]
92 %tmp.3 = getelementptr { i32, i32 }* %y, i32 0, i32 1 ; <i32*> [#uses=1]
94 %tmp.4 = icmp eq i32* %tmp.1, %tmp.3 ; <i1> [#uses=1]
97 ; CHECK: icmp eq %pair* %x, %y
100 define i1 @test11({ i32, i32 }* %X) {
101 %P = getelementptr { i32, i32 }* %X, i32 0, i32 0 ; <i32*> [#uses=1]
102 %Q = icmp eq i32* %P, null ; <i1> [#uses=1]
105 ; CHECK: icmp eq %pair* %X, null
110 define i32 @test12(%struct.A* %a) {
112 %g3 = getelementptr %struct.A* %a, i32 0, i32 1
113 store i32 10, i32* %g3, align 4
115 %g4 = getelementptr %struct.A* %a, i32 0, i32 0
117 %new_a = bitcast %struct.B* %g4 to %struct.A*
119 %g5 = getelementptr %struct.A* %new_a, i32 0, i32 1
120 %a_a = load i32* %g5, align 4
123 ; CHECK: getelementptr %struct.A* %a, i64 0, i32 1
124 ; CHECK-NEXT: store i32 10, i32* %g3
125 ; CHECK-NEXT: ret i32 10
130 %S = type { i32, [ 100 x i32] }
131 define i1 @test13(i64 %X, %S* %P) {
132 %A = getelementptr inbounds %S* %P, i32 0, i32 1, i64 %X
133 %B = getelementptr inbounds %S* %P, i32 0, i32 0
134 %C = icmp eq i32* %A, %B
137 ; CHECK: %C = icmp eq i64 %X, -1
141 @G = external global [3 x i8] ; <[3 x i8]*> [#uses=1]
142 define i8* @test14(i32 %Idx) {
143 %idx = zext i32 %Idx to i64 ; <i64> [#uses=1]
144 %tmp = getelementptr i8* getelementptr ([3 x i8]* @G, i32 0, i32 0), i64 %idx
147 ; CHECK: getelementptr [3 x i8]* @G, i64 0, i64 %idx
151 ; Test folding of constantexpr geps into normal geps.
152 @Array = external global [40 x i32] ; <[40 x i32]*> [#uses=2]
153 define i32 *@test15(i64 %X) {
154 %A = getelementptr i32* getelementptr ([40 x i32]* @Array, i64 0, i64 0), i64 %X
157 ; CHECK: getelementptr [40 x i32]* @Array, i64 0, i64 %X
161 define i32* @test16(i32* %X, i32 %Idx) {
162 %R = getelementptr i32* %X, i32 %Idx
165 ; CHECK: sext i32 %Idx to i64
169 define i1 @test17(i16* %P, i32 %I, i32 %J) {
170 %X = getelementptr inbounds i16* %P, i32 %I
171 %Y = getelementptr inbounds i16* %P, i32 %J
172 %C = icmp ult i16* %X, %Y
175 ; CHECK: %C = icmp slt i32 %I, %J
178 define i1 @test18(i16* %P, i32 %I) {
179 %X = getelementptr inbounds i16* %P, i32 %I
180 %C = icmp ult i16* %X, %P
183 ; CHECK: %C = icmp slt i32 %I, 0
186 define i32 @test19(i32* %P, i32 %A, i32 %B) {
187 %tmp.4 = getelementptr inbounds i32* %P, i32 %A
188 %tmp.9 = getelementptr inbounds i32* %P, i32 %B
189 %tmp.10 = icmp eq i32* %tmp.4, %tmp.9
190 %tmp.11 = zext i1 %tmp.10 to i32
193 ; CHECK: icmp eq i32 %A, %B
196 define i32 @test20(i32* %P, i32 %A, i32 %B) {
197 %tmp.4 = getelementptr inbounds i32* %P, i32 %A
198 %tmp.6 = icmp eq i32* %tmp.4, %P
199 %tmp.7 = zext i1 %tmp.6 to i32
202 ; CHECK: icmp eq i32 %A, 0