1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -instcombine %s -o - | FileCheck %s
3 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"
6 define i32 @test_as0(i32 addrspace(0)* %a) {
7 ; CHECK-LABEL: @test_as0(
8 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr i32, i32* [[A:%.*]], i32 1
9 ; CHECK-NEXT: [[Y:%.*]] = load i32, i32* [[ARRAYIDX]], align 4
10 ; CHECK-NEXT: ret i32 [[Y]]
12 %arrayidx = getelementptr i32, i32 addrspace(0)* %a, i64 1
13 %y = load i32, i32 addrspace(0)* %arrayidx, align 4
17 define i32 @test_as1(i32 addrspace(1)* %a) {
18 ; CHECK-LABEL: @test_as1(
19 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr i32, i32 addrspace(1)* [[A:%.*]], i64 1
20 ; CHECK-NEXT: [[Y:%.*]] = load i32, i32 addrspace(1)* [[ARRAYIDX]], align 4
21 ; CHECK-NEXT: ret i32 [[Y]]
23 %arrayidx = getelementptr i32, i32 addrspace(1)* %a, i32 1
24 %y = load i32, i32 addrspace(1)* %arrayidx, align 4
28 define i32 @test_as2(i32 addrspace(2)* %a) {
29 ; CHECK-LABEL: @test_as2(
30 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr i32, i32 addrspace(2)* [[A:%.*]], i8 1
31 ; CHECK-NEXT: [[Y:%.*]] = load i32, i32 addrspace(2)* [[ARRAYIDX]], align 4
32 ; CHECK-NEXT: ret i32 [[Y]]
34 %arrayidx = getelementptr i32, i32 addrspace(2)* %a, i32 1
35 %y = load i32, i32 addrspace(2)* %arrayidx, align 4
39 define i32 @test_as3(i32 addrspace(3)* %a) {
40 ; CHECK-LABEL: @test_as3(
41 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr i32, i32 addrspace(3)* [[A:%.*]], i16 1
42 ; CHECK-NEXT: [[Y:%.*]] = load i32, i32 addrspace(3)* [[ARRAYIDX]], align 4
43 ; CHECK-NEXT: ret i32 [[Y]]
45 %arrayidx = getelementptr i32, i32 addrspace(3)* %a, i32 1
46 %y = load i32, i32 addrspace(3)* %arrayidx, align 4
50 define i32 @test_combine_ptrtoint(i32 addrspace(2)* %a) {
51 ; CHECK-LABEL: @test_combine_ptrtoint(
52 ; CHECK-NEXT: [[Y:%.*]] = load i32, i32 addrspace(2)* [[A:%.*]], align 4
53 ; CHECK-NEXT: ret i32 [[Y]]
55 %cast = ptrtoint i32 addrspace(2)* %a to i8
56 %castback = inttoptr i8 %cast to i32 addrspace(2)*
57 %y = load i32, i32 addrspace(2)* %castback, align 4
61 define i8 @test_combine_inttoptr(i8 %a) {
62 ; CHECK-LABEL: @test_combine_inttoptr(
63 ; CHECK-NEXT: ret i8 [[A:%.*]]
65 %cast = inttoptr i8 %a to i32 addrspace(2)*
66 %castback = ptrtoint i32 addrspace(2)* %cast to i8
70 define i32 @test_combine_vector_ptrtoint(<2 x i32 addrspace(2)*> %a) {
71 ; CHECK-LABEL: @test_combine_vector_ptrtoint(
72 ; CHECK-NEXT: [[P:%.*]] = extractelement <2 x i32 addrspace(2)*> [[A:%.*]], i32 0
73 ; CHECK-NEXT: [[Y:%.*]] = load i32, i32 addrspace(2)* [[P]], align 4
74 ; CHECK-NEXT: ret i32 [[Y]]
76 %cast = ptrtoint <2 x i32 addrspace(2)*> %a to <2 x i8>
77 %castback = inttoptr <2 x i8> %cast to <2 x i32 addrspace(2)*>
78 %p = extractelement <2 x i32 addrspace(2)*> %castback, i32 0
79 %y = load i32, i32 addrspace(2)* %p, align 4
83 define <2 x i8> @test_combine_vector_inttoptr(<2 x i8> %a) {
84 ; CHECK-LABEL: @test_combine_vector_inttoptr(
85 ; CHECK-NEXT: ret <2 x i8> [[A:%.*]]
87 %cast = inttoptr <2 x i8> %a to <2 x i32 addrspace(2)*>
88 %castback = ptrtoint <2 x i32 addrspace(2)*> %cast to <2 x i8>
89 ret <2 x i8> %castback
92 ; Check that the GEP index is changed to the address space integer type (i64 -> i8)
93 define i32 addrspace(2)* @shrink_gep_constant_index_64_as2(i32 addrspace(2)* %p) {
94 ; CHECK-LABEL: @shrink_gep_constant_index_64_as2(
95 ; CHECK-NEXT: [[RET:%.*]] = getelementptr i32, i32 addrspace(2)* [[P:%.*]], i8 1
96 ; CHECK-NEXT: ret i32 addrspace(2)* [[RET]]
98 %ret = getelementptr i32, i32 addrspace(2)* %p, i64 1
99 ret i32 addrspace(2)* %ret
102 define i32 addrspace(2)* @shrink_gep_constant_index_32_as2(i32 addrspace(2)* %p) {
103 ; CHECK-LABEL: @shrink_gep_constant_index_32_as2(
104 ; CHECK-NEXT: [[RET:%.*]] = getelementptr i32, i32 addrspace(2)* [[P:%.*]], i8 1
105 ; CHECK-NEXT: ret i32 addrspace(2)* [[RET]]
107 %ret = getelementptr i32, i32 addrspace(2)* %p, i32 1
108 ret i32 addrspace(2)* %ret
111 define i32 addrspace(3)* @shrink_gep_constant_index_64_as3(i32 addrspace(3)* %p) {
112 ; CHECK-LABEL: @shrink_gep_constant_index_64_as3(
113 ; CHECK-NEXT: [[RET:%.*]] = getelementptr i32, i32 addrspace(3)* [[P:%.*]], i16 1
114 ; CHECK-NEXT: ret i32 addrspace(3)* [[RET]]
116 %ret = getelementptr i32, i32 addrspace(3)* %p, i64 1
117 ret i32 addrspace(3)* %ret
120 define i32 addrspace(2)* @shrink_gep_variable_index_64_as2(i32 addrspace(2)* %p, i64 %idx) {
121 ; CHECK-LABEL: @shrink_gep_variable_index_64_as2(
122 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[IDX:%.*]] to i8
123 ; CHECK-NEXT: [[RET:%.*]] = getelementptr i32, i32 addrspace(2)* [[P:%.*]], i8 [[TMP1]]
124 ; CHECK-NEXT: ret i32 addrspace(2)* [[RET]]
126 %ret = getelementptr i32, i32 addrspace(2)* %p, i64 %idx
127 ret i32 addrspace(2)* %ret
130 define i32 addrspace(1)* @grow_gep_variable_index_8_as1(i32 addrspace(1)* %p, i8 %idx) {
131 ; CHECK-LABEL: @grow_gep_variable_index_8_as1(
132 ; CHECK-NEXT: [[TMP1:%.*]] = sext i8 [[IDX:%.*]] to i64
133 ; CHECK-NEXT: [[RET:%.*]] = getelementptr i32, i32 addrspace(1)* [[P:%.*]], i64 [[TMP1]]
134 ; CHECK-NEXT: ret i32 addrspace(1)* [[RET]]
136 %ret = getelementptr i32, i32 addrspace(1)* %p, i8 %idx
137 ret i32 addrspace(1)* %ret