1 ; RUN: opt -S -instcombine %s -o - | FileCheck %s
2 target datalayout = "e-p:32:32:32-p1:64:64:64-p2:8:8:8-p3:16:16:16-p4:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:32"
4 @g = addrspace(3) global i32 89
6 @const_zero_i8_as1 = addrspace(1) constant i8 0
7 @const_zero_i32_as1 = addrspace(1) constant i32 0
9 @const_zero_i8_as2 = addrspace(2) constant i8 0
10 @const_zero_i32_as2 = addrspace(2) constant i32 0
12 @const_zero_i8_as3 = addrspace(3) constant i8 0
13 @const_zero_i32_as3 = addrspace(3) constant i32 0
15 ; Test constant folding of inttoptr (ptrtoint constantexpr)
16 ; The intermediate integer size is the same as the pointer size
17 define i32 addrspace(3)* @test_constant_fold_inttoptr_as_pointer_same_size() {
18 ; CHECK-LABEL: @test_constant_fold_inttoptr_as_pointer_same_size(
19 ; CHECK-NEXT: ret i32 addrspace(3)* @const_zero_i32_as3
20 %x = ptrtoint i32 addrspace(3)* @const_zero_i32_as3 to i32
21 %y = inttoptr i32 %x to i32 addrspace(3)*
22 ret i32 addrspace(3)* %y
25 ; The intermediate integer size is larger than the pointer size
26 define i32 addrspace(2)* @test_constant_fold_inttoptr_as_pointer_smaller() {
27 ; CHECK-LABEL: @test_constant_fold_inttoptr_as_pointer_smaller(
28 ; CHECK-NEXT: ret i32 addrspace(2)* @const_zero_i32_as2
29 %x = ptrtoint i32 addrspace(2)* @const_zero_i32_as2 to i16
30 %y = inttoptr i16 %x to i32 addrspace(2)*
31 ret i32 addrspace(2)* %y
34 ; Different address spaces that are the same size, but they are
35 ; different so there should be a bitcast.
36 define i32 addrspace(4)* @test_constant_fold_inttoptr_as_pointer_smaller_different_as() {
37 ; CHECK-LABEL: @test_constant_fold_inttoptr_as_pointer_smaller_different_as(
38 ; CHECK-NEXT: ret i32 addrspace(4)* bitcast (i32 addrspace(3)* @const_zero_i32_as3 to i32 addrspace(4)*)
39 %x = ptrtoint i32 addrspace(3)* @const_zero_i32_as3 to i16
40 %y = inttoptr i16 %x to i32 addrspace(4)*
41 ret i32 addrspace(4)* %y
44 ; Make sure we don't introduce a bitcast between different sized
45 ; address spaces when folding this
46 define i32 addrspace(2)* @test_constant_fold_inttoptr_as_pointer_smaller_different_size_as() {
47 ; CHECK-LABEL: @test_constant_fold_inttoptr_as_pointer_smaller_different_size_as(
48 ; CHECK-NEXT: ret i32 addrspace(2)* inttoptr (i32 ptrtoint (i32 addrspace(3)* @const_zero_i32_as3 to i32) to i32 addrspace(2)*)
49 %x = ptrtoint i32 addrspace(3)* @const_zero_i32_as3 to i32
50 %y = inttoptr i32 %x to i32 addrspace(2)*
51 ret i32 addrspace(2)* %y
54 ; The intermediate integer size is too small, nothing should happen
55 define i32 addrspace(3)* @test_constant_fold_inttoptr_as_pointer_larger() {
56 ; CHECK-LABEL: @test_constant_fold_inttoptr_as_pointer_larger(
57 ; CHECK-NEXT: ret i32 addrspace(3)* inttoptr (i8 ptrtoint (i32 addrspace(3)* @const_zero_i32_as3 to i8) to i32 addrspace(3)*)
58 %x = ptrtoint i32 addrspace(3)* @const_zero_i32_as3 to i8
59 %y = inttoptr i8 %x to i32 addrspace(3)*
60 ret i32 addrspace(3)* %y
63 define i8 @const_fold_ptrtoint() {
64 ; CHECK-LABEL: @const_fold_ptrtoint(
65 ; CHECK-NEXT: ret i8 4
66 ret i8 ptrtoint (i32 addrspace(2)* inttoptr (i4 4 to i32 addrspace(2)*) to i8)
69 ; Test that mask happens when the destination pointer is smaller than
71 define i8 @const_fold_ptrtoint_mask() {
72 ; CHECK-LABEL: @const_fold_ptrtoint_mask(
73 ; CHECK-NEXT: ret i8 1
74 ret i8 ptrtoint (i32 addrspace(3)* inttoptr (i32 257 to i32 addrspace(3)*) to i8)
77 define i32 addrspace(3)* @const_inttoptr() {
78 ; CHECK-LABEL: @const_inttoptr(
79 ; CHECK-NEXT: ret i32 addrspace(3)* inttoptr (i16 4 to i32 addrspace(3)*)
80 %p = inttoptr i16 4 to i32 addrspace(3)*
81 ret i32 addrspace(3)* %p
84 define i16 @const_ptrtoint() {
85 ; CHECK-LABEL: @const_ptrtoint(
86 ; CHECK-NEXT: ret i16 ptrtoint (i32 addrspace(3)* @g to i16)
87 %i = ptrtoint i32 addrspace(3)* @g to i16
91 define i16 @const_inttoptr_ptrtoint() {
92 ; CHECK-LABEL: @const_inttoptr_ptrtoint(
93 ; CHECK-NEXT: ret i16 9
94 ret i16 ptrtoint (i32 addrspace(3)* inttoptr (i16 9 to i32 addrspace(3)*) to i16)
97 define i1 @constant_fold_cmp_constantexpr_inttoptr() {
98 ; CHECK-LABEL: @constant_fold_cmp_constantexpr_inttoptr(
99 ; CHECK-NEXT: ret i1 true
100 %x = icmp eq i32 addrspace(3)* inttoptr (i16 0 to i32 addrspace(3)*), null
104 define i1 @constant_fold_inttoptr_null(i16 %i) {
105 ; CHECK-LABEL: @constant_fold_inttoptr_null(
106 ; CHECK-NEXT: ret i1 false
107 %x = icmp eq i32 addrspace(3)* inttoptr (i16 99 to i32 addrspace(3)*), inttoptr (i16 0 to i32 addrspace(3)*)
111 define i1 @constant_fold_ptrtoint_null() {
112 ; CHECK-LABEL: @constant_fold_ptrtoint_null(
113 ; CHECK-NEXT: ret i1 false
114 %x = icmp eq i16 ptrtoint (i32 addrspace(3)* @g to i16), ptrtoint (i32 addrspace(3)* null to i16)
118 define i1 @constant_fold_ptrtoint_null_2() {
119 ; CHECK-LABEL: @constant_fold_ptrtoint_null_2(
120 ; CHECK-NEXT: ret i1 false
121 %x = icmp eq i16 ptrtoint (i32 addrspace(3)* null to i16), ptrtoint (i32 addrspace(3)* @g to i16)
125 define i1 @constant_fold_ptrtoint() {
126 ; CHECK-LABEL: @constant_fold_ptrtoint(
127 ; CHECK-NEXT: ret i1 true
128 %x = icmp eq i16 ptrtoint (i32 addrspace(3)* @g to i16), ptrtoint (i32 addrspace(3)* @g to i16)
132 define i1 @constant_fold_inttoptr() {
133 ; CHECK-LABEL: @constant_fold_inttoptr(
134 ; CHECK-NEXT: ret i1 false
135 %x = icmp eq i32 addrspace(3)* inttoptr (i16 99 to i32 addrspace(3)*), inttoptr (i16 27 to i32 addrspace(3)*)
139 @g_float_as3 = addrspace(3) global float zeroinitializer
140 @g_v4f_as3 = addrspace(3) global <4 x float> zeroinitializer
142 define float @constant_fold_bitcast_ftoi_load() {
143 ; CHECK-LABEL: @constant_fold_bitcast_ftoi_load(
144 ; CHECK: load float addrspace(3)* bitcast (i32 addrspace(3)* @g to float addrspace(3)*), align 4
145 %a = load float addrspace(3)* bitcast (i32 addrspace(3)* @g to float addrspace(3)*), align 4
149 define i32 @constant_fold_bitcast_itof_load() {
150 ; CHECK-LABEL: @constant_fold_bitcast_itof_load(
151 ; CHECK: load i32 addrspace(3)* bitcast (float addrspace(3)* @g_float_as3 to i32 addrspace(3)*), align 4
152 %a = load i32 addrspace(3)* bitcast (float addrspace(3)* @g_float_as3 to i32 addrspace(3)*), align 4
156 define <4 x i32> @constant_fold_bitcast_vector_as() {
157 ; CHECK-LABEL: @constant_fold_bitcast_vector_as(
158 ; CHECK: load <4 x float> addrspace(3)* @g_v4f_as3, align 16
159 ; CHECK: bitcast <4 x float> %1 to <4 x i32>
160 %a = load <4 x i32> addrspace(3)* bitcast (<4 x float> addrspace(3)* @g_v4f_as3 to <4 x i32> addrspace(3)*), align 4
164 @i32_array_as3 = addrspace(3) global [10 x i32] zeroinitializer
166 define i32 @test_cast_gep_small_indices_as() {
167 ; CHECK-LABEL: @test_cast_gep_small_indices_as(
168 ; CHECK: load i32 addrspace(3)* getelementptr inbounds ([10 x i32] addrspace(3)* @i32_array_as3, i16 0, i16 0), align 16
169 %p = getelementptr [10 x i32] addrspace(3)* @i32_array_as3, i7 0, i7 0
170 %x = load i32 addrspace(3)* %p, align 4
174 %struct.foo = type { float, float, [4 x i32], i32 addrspace(3)* }
176 @constant_fold_global_ptr = addrspace(3) global %struct.foo {
179 [4 x i32] zeroinitializer,
180 i32 addrspace(3)* getelementptr ([10 x i32] addrspace(3)* @i32_array_as3, i64 0, i64 0)
183 define i32 @test_cast_gep_large_indices_as() {
184 ; CHECK-LABEL: @test_cast_gep_large_indices_as(
185 ; CHECK: load i32 addrspace(3)* getelementptr inbounds ([10 x i32] addrspace(3)* @i32_array_as3, i16 0, i16 0), align 16
186 %p = getelementptr [10 x i32] addrspace(3)* @i32_array_as3, i64 0, i64 0
187 %x = load i32 addrspace(3)* %p, align 4
191 define i32 @test_constant_cast_gep_struct_indices_as() {
192 ; CHECK-LABEL: @test_constant_cast_gep_struct_indices_as(
193 ; CHECK: load i32 addrspace(3)* getelementptr inbounds (%struct.foo addrspace(3)* @constant_fold_global_ptr, i16 0, i32 2, i16 2), align 8
194 %x = getelementptr %struct.foo addrspace(3)* @constant_fold_global_ptr, i18 0, i32 2, i12 2
195 %y = load i32 addrspace(3)* %x, align 4
199 @constant_data_as3 = addrspace(3) constant [5 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5]
201 define i32 @test_read_data_from_global_as3() {
202 ; CHECK-LABEL: @test_read_data_from_global_as3(
203 ; CHECK-NEXT: ret i32 2
204 %x = getelementptr [5 x i32] addrspace(3)* @constant_data_as3, i32 0, i32 1
205 %y = load i32 addrspace(3)* %x, align 4
209 @a = addrspace(1) constant i32 9
210 @b = addrspace(1) constant i32 23
211 @c = addrspace(1) constant i32 34
212 @d = addrspace(1) constant i32 99
214 @ptr_array = addrspace(2) constant [4 x i32 addrspace(1)*] [ i32 addrspace(1)* @a, i32 addrspace(1)* @b, i32 addrspace(1)* @c, i32 addrspace(1)* @d]
215 @indirect = addrspace(0) constant i32 addrspace(1)* addrspace(2)* getelementptr inbounds ([4 x i32 addrspace(1)*] addrspace(2)* @ptr_array, i1 0, i32 2)
217 define i32 @constant_through_array_as_ptrs() {
218 ; CHECK-LABEL: @constant_through_array_as_ptrs(
219 ; CHECK-NEXT: ret i32 34
220 %p = load i32 addrspace(1)* addrspace(2)* addrspace(0)* @indirect, align 4
221 %a = load i32 addrspace(1)* addrspace(2)* %p, align 4
222 %b = load i32 addrspace(1)* %a, align 4