1; RUN: opt -S -codegenprepare -mtriple=amdgcn-unknown-unknown -mcpu=bonaire < %s | FileCheck -check-prefix=OPT -check-prefix=OPT-CI %s
2; RUN: opt -S -codegenprepare -mtriple=amdgcn-unknown-unknown -mcpu=tonga < %s | FileCheck -check-prefix=OPT -check-prefix=OPT-VI %s
3; RUN: llc -march=amdgcn -mcpu=bonaire -mattr=-promote-alloca < %s | FileCheck -check-prefix=GCN -check-prefix=CI %s
4; RUN: llc -march=amdgcn -mcpu=tonga -mattr=-promote-alloca < %s | FileCheck -check-prefix=GCN -check-prefix=VI %s
5
6; OPT-LABEL: @test_no_sink_flat_small_offset_i32(
7; OPT: getelementptr i32, i32 addrspace(4)* %in
8; OPT: br i1
9; OPT-NOT: ptrtoint
10
11; GCN-LABEL: {{^}}test_no_sink_flat_small_offset_i32:
12; GCN: flat_load_dword
13; GCN: {{^}}BB0_2:
14define void @test_no_sink_flat_small_offset_i32(i32 addrspace(4)* %out, i32 addrspace(4)* %in, i32 %cond) {
15entry:
16  %out.gep = getelementptr i32, i32 addrspace(4)* %out, i64 999999
17  %in.gep = getelementptr i32, i32 addrspace(4)* %in, i64 7
18  %tmp0 = icmp eq i32 %cond, 0
19  br i1 %tmp0, label %endif, label %if
20
21if:
22  %tmp1 = load i32, i32 addrspace(4)* %in.gep
23  br label %endif
24
25endif:
26  %x = phi i32 [ %tmp1, %if ], [ 0, %entry ]
27  store i32 %x, i32 addrspace(4)* %out.gep
28  br label %done
29
30done:
31  ret void
32}
33
34; OPT-LABEL: @test_sink_noop_addrspacecast_flat_to_global_i32(
35; OPT: getelementptr i32, i32 addrspace(4)* %out,
36; OPT-CI-NOT: getelementptr
37; OPT: br i1
38
39; OPT-CI: ptrtoint
40; OPT-CI: add
41; OPT-CI: inttoptr
42; OPT: br label
43
44; GCN-LABEL: {{^}}test_sink_noop_addrspacecast_flat_to_global_i32:
45; CI: buffer_load_dword {{v[0-9]+}}, off, s{{\[[0-9]+:[0-9]+\]}}, 0 offset:28
46define void @test_sink_noop_addrspacecast_flat_to_global_i32(i32 addrspace(4)* %out, i32 addrspace(4)* %in, i32 %cond) {
47entry:
48  %out.gep = getelementptr i32, i32 addrspace(4)* %out, i64 999999
49  %in.gep = getelementptr i32, i32 addrspace(4)* %in, i64 7
50  %cast = addrspacecast i32 addrspace(4)* %in.gep to i32 addrspace(1)*
51  %tmp0 = icmp eq i32 %cond, 0
52  br i1 %tmp0, label %endif, label %if
53
54if:
55  %tmp1 = load i32, i32 addrspace(1)* %cast
56  br label %endif
57
58endif:
59  %x = phi i32 [ %tmp1, %if ], [ 0, %entry ]
60  store i32 %x, i32 addrspace(4)* %out.gep
61  br label %done
62
63done:
64  ret void
65}
66
67; OPT-LABEL: @test_sink_noop_addrspacecast_flat_to_constant_i32(
68; OPT: getelementptr i32, i32 addrspace(4)* %out,
69; OPT-CI-NOT: getelementptr
70; OPT: br i1
71
72; OPT-CI: ptrtoint
73; OPT-CI: add
74; OPT-CI: inttoptr
75; OPT: br label
76
77; GCN-LABEL: {{^}}test_sink_noop_addrspacecast_flat_to_constant_i32:
78; CI: s_load_dword {{s[0-9]+}}, s{{\[[0-9]+:[0-9]+\]}}, 0xd
79define void @test_sink_noop_addrspacecast_flat_to_constant_i32(i32 addrspace(4)* %out, i32 addrspace(4)* %in, i32 %cond) {
80entry:
81  %out.gep = getelementptr i32, i32 addrspace(4)* %out, i64 999999
82  %in.gep = getelementptr i32, i32 addrspace(4)* %in, i64 7
83  %cast = addrspacecast i32 addrspace(4)* %in.gep to i32 addrspace(2)*
84  %tmp0 = icmp eq i32 %cond, 0
85  br i1 %tmp0, label %endif, label %if
86
87if:
88  %tmp1 = load i32, i32 addrspace(2)* %cast
89  br label %endif
90
91endif:
92  %x = phi i32 [ %tmp1, %if ], [ 0, %entry ]
93  store i32 %x, i32 addrspace(4)* %out.gep
94  br label %done
95
96done:
97  ret void
98}
99