1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -instcombine -S | FileCheck %s
3
4target datalayout = "e-m:m-p:40:64:64:32-i32:32-i16:16-i8:8-n32"
5
6%struct.B = type { double }
7%struct.A = type { %struct.B, i32, i32 }
8%struct.C = type { [7 x i8] }
9
10
11@Global = external global [10 x i8]
12
13; Test that two array indexing geps fold
14define i32* @test1(i32* %I) {
15; CHECK-LABEL: @test1(
16; CHECK-NEXT:    [[B:%.*]] = getelementptr i32, i32* [[I:%.*]], i32 21
17; CHECK-NEXT:    ret i32* [[B]]
18;
19  %A = getelementptr i32, i32* %I, i8 17
20  %B = getelementptr i32, i32* %A, i16 4
21  ret i32* %B
22}
23
24; Test that two getelementptr insts fold
25define i32* @test2({ i32 }* %I) {
26; CHECK-LABEL: @test2(
27; CHECK-NEXT:    [[B:%.*]] = getelementptr { i32 }, { i32 }* [[I:%.*]], i32 1, i32 0
28; CHECK-NEXT:    ret i32* [[B]]
29;
30  %A = getelementptr { i32 }, { i32 }* %I, i32 1
31  %B = getelementptr { i32 }, { i32 }* %A, i32 0, i32 0
32  ret i32* %B
33}
34
35define void @test3(i8 %B) {
36; This should be turned into a constexpr instead of being an instruction
37; CHECK-LABEL: @test3(
38; CHECK-NEXT:    store i8 [[B:%.*]], i8* getelementptr inbounds ([10 x i8], [10 x i8]* @Global, i32 0, i32 4), align 1
39; CHECK-NEXT:    ret void
40;
41  %A = getelementptr [10 x i8], [10 x i8]* @Global, i32 0, i32 4
42  store i8 %B, i8* %A
43  ret void
44}
45
46%as1_ptr_struct = type { i32 addrspace(1)* }
47%as2_ptr_struct = type { i32 addrspace(2)* }
48
49@global_as2 = addrspace(2) global i32 zeroinitializer
50@global_as1_as2_ptr = addrspace(1) global %as2_ptr_struct { i32 addrspace(2)* @global_as2 }
51
52; This should be turned into a constexpr instead of being an instruction
53define void @test_evaluate_gep_nested_as_ptrs(i32 addrspace(2)* %B) {
54; CHECK-LABEL: @test_evaluate_gep_nested_as_ptrs(
55; 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, i32 0, i32 0), align 8
56; CHECK-NEXT:    ret void
57;
58  %A = getelementptr %as2_ptr_struct, %as2_ptr_struct addrspace(1)* @global_as1_as2_ptr, i32 0, i32 0
59  store i32 addrspace(2)* %B, i32 addrspace(2)* addrspace(1)* %A
60  ret void
61}
62
63@arst = addrspace(1) global [4 x i8 addrspace(2)*] zeroinitializer
64
65define void @test_evaluate_gep_as_ptrs_array(i8 addrspace(2)* %B) {
66; CHECK-LABEL: @test_evaluate_gep_as_ptrs_array(
67; 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, i32 0, i32 2), align 16
68; CHECK-NEXT:    ret void
69;
70
71  %A = getelementptr [4 x i8 addrspace(2)*], [4 x i8 addrspace(2)*] addrspace(1)* @arst, i16 0, i16 2
72  store i8 addrspace(2)* %B, i8 addrspace(2)* addrspace(1)* %A
73  ret void
74}
75
76define i32* @test4(i32* %I, i32 %C, i32 %D) {
77; CHECK-LABEL: @test4(
78; CHECK-NEXT:    [[A:%.*]] = getelementptr i32, i32* [[I:%.*]], i32 [[C:%.*]]
79; CHECK-NEXT:    [[B:%.*]] = getelementptr i32, i32* [[A]], i32 [[D:%.*]]
80; CHECK-NEXT:    ret i32* [[B]]
81;
82  %A = getelementptr i32, i32* %I, i32 %C
83  %B = getelementptr i32, i32* %A, i32 %D
84  ret i32* %B
85}
86
87
88define i1 @test5({ i32, i32 }* %x, { i32, i32 }* %y) {
89; CHECK-LABEL: @test5(
90; CHECK-NEXT:    [[TMP_4:%.*]] = icmp eq { i32, i32 }* [[X:%.*]], [[Y:%.*]]
91; CHECK-NEXT:    ret i1 [[TMP_4]]
92;
93  %tmp.1 = getelementptr { i32, i32 }, { i32, i32 }* %x, i32 0, i32 1
94  %tmp.3 = getelementptr { i32, i32 }, { i32, i32 }* %y, i32 0, i32 1
95  ;; seteq x, y
96  %tmp.4 = icmp eq i32* %tmp.1, %tmp.3
97  ret i1 %tmp.4
98}
99
100%S = type { i32, [ 100 x i32] }
101
102define <2 x i1> @test6(<2 x i32> %X, <2 x %S*> %P) nounwind {
103; CHECK-LABEL: @test6(
104; CHECK-NEXT:    [[C:%.*]] = icmp eq <2 x i32> [[X:%.*]], <i32 -1, i32 -1>
105; CHECK-NEXT:    ret <2 x i1> [[C]]
106;
107  %A = getelementptr inbounds %S, <2 x %S*> %P, <2 x i32> zeroinitializer, <2 x i32> <i32 1, i32 1>, <2 x i32> %X
108  %B = getelementptr inbounds %S, <2 x %S*> %P, <2 x i32> <i32 0, i32 0>, <2 x i32> <i32 0, i32 0>
109  %C = icmp eq <2 x i32*> %A, %B
110  ret <2 x i1> %C
111}
112
113; Same as above, but indices scalarized.
114define <2 x i1> @test6b(<2 x i32> %X, <2 x %S*> %P) nounwind {
115; CHECK-LABEL: @test6b(
116; CHECK-NEXT:    [[C:%.*]] = icmp eq <2 x i32> [[X:%.*]], <i32 -1, i32 -1>
117; CHECK-NEXT:    ret <2 x i1> [[C]]
118;
119  %A = getelementptr inbounds %S, <2 x %S*> %P, i32 0, i32 1, <2 x i32> %X
120  %B = getelementptr inbounds %S, <2 x %S*> %P, i32 0, i32 0
121  %C = icmp eq <2 x i32*> %A, %B
122  ret <2 x i1> %C
123}
124
125@G = external global [3 x i8]
126define i8* @test7(i16 %Idx) {
127; CHECK-LABEL: @test7(
128; CHECK-NEXT:    [[ZE_IDX:%.*]] = zext i16 [[IDX:%.*]] to i32
129; CHECK-NEXT:    [[TMP:%.*]] = getelementptr [3 x i8], [3 x i8]* @G, i32 0, i32 [[ZE_IDX]]
130; CHECK-NEXT:    ret i8* [[TMP]]
131;
132  %ZE_Idx = zext i16 %Idx to i32
133  %tmp = getelementptr i8, i8* getelementptr ([3 x i8], [3 x i8]* @G, i32 0, i32 0), i32 %ZE_Idx
134  ret i8* %tmp
135}
136
137
138; Test folding of constantexpr geps into normal geps.
139@Array = external global [40 x i32]
140define i32 *@test8(i32 %X) {
141; CHECK-LABEL: @test8(
142; CHECK-NEXT:    [[A:%.*]] = getelementptr [40 x i32], [40 x i32]* @Array, i32 0, i32 [[X:%.*]]
143; CHECK-NEXT:    ret i32* [[A]]
144;
145  %A = getelementptr i32, i32* getelementptr ([40 x i32], [40 x i32]* @Array, i32 0, i32 0), i32 %X
146  ret i32* %A
147}
148
149define i32 *@test9(i32 *%base, i8 %ind) {
150; CHECK-LABEL: @test9(
151; CHECK-NEXT:    [[TMP1:%.*]] = sext i8 [[IND:%.*]] to i32
152; CHECK-NEXT:    [[RES:%.*]] = getelementptr i32, i32* [[BASE:%.*]], i32 [[TMP1]]
153; CHECK-NEXT:    ret i32* [[RES]]
154;
155  %res = getelementptr i32, i32 *%base, i8 %ind
156  ret i32* %res
157}
158
159define i32 @test10() {
160; CHECK-LABEL: @test10(
161; CHECK-NEXT:    ret i32 8
162;
163  %A = getelementptr { i32, double }, { i32, double }* null, i32 0, i32 1
164  %B = ptrtoint double* %A to i32
165  ret i32 %B
166}
167
168@X_as1 = addrspace(1) global [1000 x i8] zeroinitializer, align 16
169
170define i16 @constant_fold_custom_dl() {
171; CHECK-LABEL: @constant_fold_custom_dl(
172; CHECK-NEXT:  entry:
173; CHECK-NEXT:    ret i16 ptrtoint (i8 addrspace(1)* getelementptr (i8, i8 addrspace(1)* getelementptr inbounds ([1000 x i8], [1000 x i8] addrspace(1)* @X_as1, i32 1, i32 0), i32 sext (i16 sub (i16 0, i16 ptrtoint ([1000 x i8] addrspace(1)* @X_as1 to i16)) to i32)) to i16)
174;
175
176entry:
177  %A = bitcast i8 addrspace(1)* getelementptr inbounds ([1000 x i8], [1000 x i8] addrspace(1)* @X_as1, i64 1, i64 0) to i8 addrspace(1)*
178  %B = bitcast i8 addrspace(1)* getelementptr inbounds ([1000 x i8], [1000 x i8] addrspace(1)* @X_as1, i64 0, i64 0) to i8 addrspace(1)*
179
180  %B2 = ptrtoint i8 addrspace(1)* %B to i16
181  %C = sub i16 0, %B2
182  %D = getelementptr i8, i8 addrspace(1)* %A, i16 %C
183  %E = ptrtoint i8 addrspace(1)* %D to i16
184
185  ret i16 %E
186}
187
188