1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 target datalayout = "e-m:m-p:40:64:64:32-i32:32-i16:16-i8:8-n32"
6 %struct.B = type { double }
7 %struct.A = type { %struct.B, i32, i32 }
8 %struct.C = type { [7 x i8] }
11 @Global = external global [10 x i8]
13 ; Test that two array indexing geps fold
14 define i32* @test1(i32* %I) {
15 ; CHECK-LABEL: @test1(
16 ; CHECK-NEXT: [[B:%.*]] = getelementptr i32, i32* [[I:%.*]], i32 21
17 ; CHECK-NEXT: ret i32* [[B]]
19 %A = getelementptr i32, i32* %I, i8 17
20 %B = getelementptr i32, i32* %A, i16 4
24 ; Test that two getelementptr insts fold
25 define 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]]
30 %A = getelementptr { i32 }, { i32 }* %I, i32 1
31 %B = getelementptr { i32 }, { i32 }* %A, i32 0, i32 0
35 define 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
41 %A = getelementptr [10 x i8], [10 x i8]* @Global, i32 0, i32 4
46 %as1_ptr_struct = type { i32 addrspace(1)* }
47 %as2_ptr_struct = type { i32 addrspace(2)* }
49 @global_as2 = addrspace(2) global i32 zeroinitializer
50 @global_as1_as2_ptr = addrspace(1) global %as2_ptr_struct { i32 addrspace(2)* @global_as2 }
52 ; This should be turned into a constexpr instead of being an instruction
53 define 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
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
63 @arst = addrspace(1) global [4 x i8 addrspace(2)*] zeroinitializer
65 define 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
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
76 define 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]]
82 %A = getelementptr i32, i32* %I, i32 %C
83 %B = getelementptr i32, i32* %A, i32 %D
88 define 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]]
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
96 %tmp.4 = icmp eq i32* %tmp.1, %tmp.3
100 %S = type { i32, [ 100 x i32] }
102 define <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 1073741823, i32 1073741823>
105 ; CHECK-NEXT: ret <2 x i1> [[C]]
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
113 @G = external global [3 x i8]
114 define i8* @test7(i16 %Idx) {
115 ; CHECK-LABEL: @test7(
116 ; CHECK-NEXT: [[ZE_IDX:%.*]] = zext i16 [[IDX:%.*]] to i32
117 ; CHECK-NEXT: [[TMP:%.*]] = getelementptr [3 x i8], [3 x i8]* @G, i32 0, i32 [[ZE_IDX]]
118 ; CHECK-NEXT: ret i8* [[TMP]]
120 %ZE_Idx = zext i16 %Idx to i32
121 %tmp = getelementptr i8, i8* getelementptr ([3 x i8], [3 x i8]* @G, i32 0, i32 0), i32 %ZE_Idx
126 ; Test folding of constantexpr geps into normal geps.
127 @Array = external global [40 x i32]
128 define i32 *@test8(i32 %X) {
129 ; CHECK-LABEL: @test8(
130 ; CHECK-NEXT: [[A:%.*]] = getelementptr [40 x i32], [40 x i32]* @Array, i32 0, i32 [[X:%.*]]
131 ; CHECK-NEXT: ret i32* [[A]]
133 %A = getelementptr i32, i32* getelementptr ([40 x i32], [40 x i32]* @Array, i32 0, i32 0), i32 %X
137 define i32 *@test9(i32 *%base, i8 %ind) {
138 ; CHECK-LABEL: @test9(
139 ; CHECK-NEXT: [[TMP1:%.*]] = sext i8 [[IND:%.*]] to i32
140 ; CHECK-NEXT: [[RES:%.*]] = getelementptr i32, i32* [[BASE:%.*]], i32 [[TMP1]]
141 ; CHECK-NEXT: ret i32* [[RES]]
143 %res = getelementptr i32, i32 *%base, i8 %ind
147 define i32 @test10() {
148 ; CHECK-LABEL: @test10(
149 ; CHECK-NEXT: ret i32 8
151 %A = getelementptr { i32, double }, { i32, double }* null, i32 0, i32 1
152 %B = ptrtoint double* %A to i32