1 # RUN: llc -march=amdgcn -verify-machineinstrs -run-pass si-load-store-opt -o - %s | FileCheck %s
3 # Check that SILoadStoreOptimizer honors memory dependencies between moved
6 # The following IR snippet would usually be optimized by the peephole optimizer.
7 # However, an equivalent situation can occur with buffer instructions as well.
9 # CHECK-LABEL: name: mem_dependency
10 # CHECK: DS_READ2_B32 %0, 0, 1,
11 # CHECK: DS_WRITE_B32 %0, killed %1, 64,
12 # CHECK: DS_READ2_B32 %0, 16, 17,
13 # CHECK: DS_WRITE_B32 killed %0, %5, 0
16 define amdgpu_kernel void @mem_dependency(i32 addrspace(3)* %ptr.0) nounwind {
17 %ptr.4 = getelementptr i32, i32 addrspace(3)* %ptr.0, i32 1
18 %ptr.64 = getelementptr i32, i32 addrspace(3)* %ptr.0, i32 16
19 %1 = load i32, i32 addrspace(3)* %ptr.0
20 store i32 %1, i32 addrspace(3)* %ptr.64
21 %2 = load i32, i32 addrspace(3)* %ptr.64
22 %3 = load i32, i32 addrspace(3)* %ptr.4
24 store i32 %4, i32 addrspace(3)* %ptr.0
28 @lds0 = external dso_local unnamed_addr addrspace(3) global [256 x i32], align 4
29 @lds1 = external dso_local unnamed_addr addrspace(3) global [256 x i32], align 4
30 @lds2 = external dso_local unnamed_addr addrspace(3) global [256 x i32], align 4
31 @lds3 = external dso_local unnamed_addr addrspace(3) global [256 x i32], align 4
33 define void @asm_defines_address() #0 {
35 %tmp1 = load i32, i32 addrspace(3)* getelementptr inbounds ([256 x i32], [256 x i32] addrspace(3)* @lds0, i32 0, i32 0), align 4
36 %0 = and i32 %tmp1, 255
37 %tmp3 = load i32, i32 addrspace(3)* getelementptr ([256 x i32], [256 x i32] addrspace(3)* @lds1, i32 0, i32 undef), align 4
38 %tmp6 = load i32, i32 addrspace(3)* getelementptr ([256 x i32], [256 x i32] addrspace(3)* @lds3, i32 0, i32 undef), align 4
39 %tmp7 = tail call i32 asm "v_or_b32 $0, 0, $1", "=v,v"(i32 %tmp6) #1
40 %tmp10 = lshr i32 %tmp7, 16
41 %tmp11 = and i32 %tmp10, 255
42 %tmp12 = getelementptr inbounds [256 x i32], [256 x i32] addrspace(3)* @lds1, i32 0, i32 %tmp11
43 %tmp13 = load i32, i32 addrspace(3)* %tmp12, align 4
44 %tmp14 = xor i32 %tmp3, %tmp13
45 %tmp15 = lshr i32 %tmp14, 8
46 %tmp16 = and i32 %tmp15, 16711680
47 %tmp19 = lshr i32 %tmp16, 16
48 %tmp20 = and i32 %tmp19, 255
49 %tmp21 = getelementptr inbounds [256 x i32], [256 x i32] addrspace(3)* @lds1, i32 0, i32 %tmp20
50 %tmp22 = load i32, i32 addrspace(3)* %tmp21, align 4
51 %tmp24 = load i32, i32 addrspace(3)* getelementptr ([256 x i32], [256 x i32] addrspace(3)* @lds2, i32 0, i32 undef), align 4
52 %tmp25 = xor i32 %tmp22, %tmp24
53 %tmp26 = and i32 %tmp25, -16777216
54 %tmp28 = or i32 %0, %tmp26
55 store volatile i32 %tmp28, i32 addrspace(1)* undef
59 attributes #0 = { convergent nounwind }
60 attributes #1 = { convergent nounwind readnone }
62 define amdgpu_kernel void @move_waw_hazards() #0 {
66 attributes #0 = { convergent nounwind }
68 define amdgpu_kernel void @merge_mmos(i32 addrspace(1)* %ptr_addr1) { ret void }
74 exposesReturnsTwice: false
76 regBankSelected: false
78 tracksRegLiveness: true
80 - { reg: '$vgpr0', virtual-reg: '%1' }
82 isFrameAddressTaken: false
83 isReturnAddressTaken: false
92 hasOpaqueSPAdjustment: false
94 hasMustTailInVarArgFunc: false
99 %1:vgpr_32 = COPY $vgpr0
101 %2:vgpr_32 = DS_READ_B32 %1, 0, 0, implicit $m0, implicit $exec :: (load 4 from %ir.ptr.0)
102 DS_WRITE_B32 %1, killed %2, 64, 0, implicit $m0, implicit $exec :: (store 4 into %ir.ptr.64)
104 ; Make this load unmergeable, to tempt SILoadStoreOptimizer into merging the
106 %6:vreg_64 = DS_READ2_B32 %1, 16, 17, 0, implicit $m0, implicit $exec :: (load 8 from %ir.ptr.64, align 4)
107 %3:vgpr_32 = COPY %6.sub0
108 %4:vgpr_32 = DS_READ_B32 %1, 4, 0, implicit $m0, implicit $exec :: (load 4 from %ir.ptr.4)
109 %5:vgpr_32 = V_ADD_I32_e32 killed %3, killed %4, implicit-def $vcc, implicit $exec
110 DS_WRITE_B32 killed %1, %5, 0, 0, implicit killed $m0, implicit $exec :: (store 4 into %ir.ptr.0)
115 # Make sure the asm def isn't moved after the point where it's used for
117 # CHECK-LABEL: name: asm_defines_address
118 # CHECK: DS_READ2ST64_B32
119 # CHECK: DS_READ2ST64_B32
123 name: asm_defines_address
124 tracksRegLiveness: true
126 - { id: 0, class: vgpr_32, preferred-register: '' }
129 %1:vgpr_32 = V_MOV_B32_e32 0, implicit $exec
130 %2:vgpr_32 = DS_READ_B32 %1, 3072, 0, implicit $m0, implicit $exec :: (dereferenceable load 4 from `i32 addrspace(3)* getelementptr inbounds ([256 x i32], [256 x i32] addrspace(3)* @lds0, i32 0, i32 0)`, addrspace 3)
131 %3:vgpr_32 = DS_READ_B32 %1, 2048, 0, implicit $m0, implicit $exec :: (load 4 from `i32 addrspace(3)* getelementptr ([256 x i32], [256 x i32] addrspace(3)* @lds1, i32 0, i32 undef)`, addrspace 3)
132 %4:vgpr_32 = DS_READ_B32 %1, 1024, 0, implicit $m0, implicit $exec :: (load 4 from `i32 addrspace(3)* getelementptr ([256 x i32], [256 x i32] addrspace(3)* @lds3, i32 0, i32 undef)`, addrspace 3)
133 INLINEASM &"v_or_b32 $0, 0, $1", 32, 327690, def %0, 327689, %4
134 %5:vgpr_32 = DS_READ_B32 %0, 2048, 0, implicit $m0, implicit $exec :: (load 4 from %ir.tmp12, addrspace 3)
135 %6:vgpr_32 = DS_READ_B32 %5, 2048, 0, implicit $m0, implicit $exec :: (load 4 from %ir.tmp21, addrspace 3)
136 %7:vgpr_32 = DS_READ_B32 %1, 0, 0, implicit $m0, implicit $exec :: (load 4 from `i32 addrspace(3)* getelementptr ([256 x i32], [256 x i32] addrspace(3)* @lds2, i32 0, i32 undef)`, addrspace 3)
137 S_SETPC_B64_return undef $sgpr30_sgpr31, implicit %6, implicit %7
141 # Make sure Write-after-Write hazards are correctly detected and the
142 # instructions moved accordingly.
144 # CHECK-LABEL: name: move_waw_hazards
146 # CHECK: S_CMP_EQ_U32
147 name: move_waw_hazards
148 tracksRegLiveness: true
151 liveins: $sgpr0_sgpr1
153 %3:sgpr_64 = COPY $sgpr0_sgpr1
154 %6:sreg_32_xm0_xexec = S_MOV_B32 0
155 %7:sreg_32_xm0 = S_MOV_B32 0
156 %8:sreg_64_xexec = REG_SEQUENCE killed %6, %subreg.sub0, %7, %subreg.sub1
157 %9:sreg_128 = S_LOAD_DWORDX4_IMM killed %8, 0, 0, 0 :: (invariant load 16, addrspace 6)
158 %31:sreg_64_xexec = S_BUFFER_LOAD_DWORDX2_IMM %9, 0, 0, 0 :: (dereferenceable invariant load 4)
159 %10:sreg_32_xm0_xexec = COPY %31.sub0
160 %11:sreg_32_xm0_xexec = COPY killed %31.sub1
161 %12:sreg_32_xm0_xexec = S_BUFFER_LOAD_DWORD_IMM %9, 2, 0, 0 :: (dereferenceable invariant load 4)
162 %13:sreg_64 = V_CMP_NE_U32_e64 killed %11, 0, implicit $exec
163 %15:sreg_64 = V_CMP_NE_U32_e64 killed %12, 0, implicit $exec
164 %17:sreg_64_xexec = S_AND_B64 killed %13, killed %15, implicit-def dead $scc
165 S_CMP_EQ_U32 killed %10, 0, implicit-def $scc
166 %18:sreg_32_xm0_xexec = S_BUFFER_LOAD_DWORD_IMM %9, 3, 0, 0 :: (dereferenceable invariant load 4)
170 # CHECK-LABEL: merge_mmos
171 # CHECK: S_BUFFER_LOAD_DWORDX2_IMM %0, 0, 0, 0 :: (dereferenceable invariant load 8, align 4)
172 # CHECK: BUFFER_LOAD_DWORDX2_OFFSET %0, 0, 0, 0, 0, 0, 0, implicit $exec :: (dereferenceable load 8, align 4)
173 # CHECK: BUFFER_STORE_DWORDX2_OFFSET_exact killed %{{[0-9]+}}, %0, 0, 0, 0, 0, 0, 0, implicit $exec :: (dereferenceable store 8, align 4)
174 # CHECK: BUFFER_LOAD_DWORDX2_OFFSET %0, 0, 64, 0, 0, 0, 0, implicit $exec :: (dereferenceable load 8 from %ir.ptr_addr1 + 64, align 4
175 # CHECK: BUFFER_STORE_DWORDX2_OFFSET_exact killed %{{[0-9]+}}, %0, 0, 64, 0, 0, 0, 0, implicit $exec :: (dereferenceable store 8 into %ir.ptr_addr1 + 64, align 4
177 tracksRegLiveness: true
180 liveins: $sgpr0_sgpr1_sgpr2_sgpr3
182 %0:sreg_128 = COPY $sgpr0_sgpr1_sgpr2_sgpr3
183 %1:sreg_32_xm0_xexec = S_BUFFER_LOAD_DWORD_IMM %0, 0, 0, 0 :: (dereferenceable invariant load 4)
184 %2:sreg_32_xm0_xexec = S_BUFFER_LOAD_DWORD_IMM %0, 1, 0, 0 :: (dereferenceable invariant load 4)
185 %3:vgpr_32 = BUFFER_LOAD_DWORD_OFFSET %0, 0, 0, 0, 0, 0, 0, implicit $exec :: (dereferenceable load 4)
186 %4:vgpr_32 = BUFFER_LOAD_DWORD_OFFSET %0, 0, 4, 0, 0, 0, 0, implicit $exec :: (dereferenceable load 4)
187 BUFFER_STORE_DWORD_OFFSET_exact %3, %0, 0, 0, 0, 0, 0, 0, implicit $exec :: (dereferenceable store 4)
188 BUFFER_STORE_DWORD_OFFSET_exact %4, %0, 0, 4, 0, 0, 0, 0, implicit $exec :: (dereferenceable store 4)
189 %5:vgpr_32 = BUFFER_LOAD_DWORD_OFFSET %0, 0, 64, 0, 0, 0, 0, implicit $exec :: (dereferenceable load 4 from %ir.ptr_addr1 + 64)
190 %6:vgpr_32 = BUFFER_LOAD_DWORD_OFFSET %0, 0, 68, 0, 0, 0, 0, implicit $exec :: (dereferenceable load 4 from %ir.ptr_addr1 + 68)
191 BUFFER_STORE_DWORD_OFFSET_exact %5, %0, 0, 64, 0, 0, 0, 0, implicit $exec :: (dereferenceable store 4 into %ir.ptr_addr1 + 64)
192 BUFFER_STORE_DWORD_OFFSET_exact %6, %0, 0, 68, 0, 0, 0, 0, implicit $exec :: (dereferenceable store 4 into %ir.ptr_addr1 + 68)