<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/65720>65720</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[LICM] Loop-invariant load instruction is not move to preheader of loop
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mrxia1216
</td>
</tr>
</table>
<pre>
**Base llvm amd-stg-open, execute this command:**
opt -filter-print-funcs=_ZN6hipcub10DeviceSpmv11CsrMVKernelIiEEvNS0_10SpmvParamsIT_iEE -S -passes="licm" benchmark_device_spmv-hip-amdgcn-amd-amdhsa-gfx906.ll (rename benchmark_device_spmv-hip-amdgcn-amd-amdhsa-gfx906.log to benchmark_device_spmv-hip-amdgcn-amd-amdhsa-gfx906.ll)
**IR output:**
define protected amdgpu_kernel void @_ZN6hipcub10DeviceSpmv11CsrMVKernelIiEEvNS0_10SpmvParamsIT_iEE(ptr addrspace(4) nocapture readonly byref(%"struct.hipcub::DeviceSpmv::SpmvParams") align 8 %0) local_unnamed_addr #4 comdat align 2 {
.......
.lr.ph: ; preds = %21
%30 = add nuw nsw i64 %22, %5
%31 = sext i32 %2 to i64
%32 = getelementptr inbounds i32, ptr %.sroa.2.0.copyload, i64 %31
%33 = addrspacecast ptr %32 to ptr addrspace(1)
%34 = getelementptr inbounds i32, ptr %.sroa.6.0.copyload, i64 %31
%35 = addrspacecast ptr %34 to ptr addrspace(1)
br label %36
._crit_edge.loopexit: ; preds = %60
br label %._crit_edge
._crit_edge: ; preds = %._crit_edge.loopexit, %21
ret void
36: ; preds = %60, %.lr.ph
%37 = phi i64 [ 0, %.lr.ph ], [ %62, %60 ]
%38 = mul nuw nsw i64 %37, %28
%39 = add nuw nsw i64 %30, %38
%40 = trunc i64 %39 to i32
**%41 = load i32, ptr addrspace(1) %33, align 4, !tbaa !19**
%42 = icmp sgt i32 %41, %40
br i1 %42, label %43, label %60
43: ; preds = %36
%sext39 = shl i64 %39, 32
%44 = ashr exact i64 %sext39, 32
%45 = getelementptr inbounds i32, ptr %.sroa.0.0.copyload, i64 %44
%46 = addrspacecast ptr %45 to ptr addrspace(1)
%47 = load i32, ptr addrspace(1) %46, align 4, !tbaa !19
%48 = mul nsw i32 %47, %.sroa.932.0.copyload
%49 = getelementptr inbounds i32, ptr %.sroa.4.0.copyload, i64 %44
%50 = addrspacecast ptr %49 to ptr addrspace(1)
%51 = load i32, ptr addrspace(1) %50, align 4, !tbaa !19
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, ptr %.sroa.5.0.copyload, i64 %52
%54 = addrspacecast ptr %53 to ptr addrspace(1)
%55 = load i32, ptr addrspace(1) %54, align 4, !tbaa !19
%56 = mul nsw i32 %48, %55
%57 = atomicrmw add ptr addrspace(3) @_ZZN6hipcub10DeviceSpmv11CsrMVKernelIiEEvNS0_10SpmvParamsIT_iEEE7partial, i32 %56 syncscope("agent-one-as") monotonic, align 4
fence syncscope("workgroup") release
tail call void @llvm.amdgcn.s.barrier()
fence syncscope("workgroup") acquire
br i1 %6, label %58, label %60
58: ; preds = %43
%59 = load i32, ptr addrspace(3) @_ZZN6hipcub10DeviceSpmv11CsrMVKernelIiEEvNS0_10SpmvParamsIT_iEEE7partial, align 4, !tbaa !19
store i32 %59, ptr addrspace(1) %35, align 4, !tbaa !19
br label %60
60: ; preds = %58, %43, %36
%61 = add nuw nsw i64 %37, 1
%62 = and i64 %61, 4294967295
%63 = icmp ugt i64 %29, %62
br i1 %63, label %36, label %._crit_edge.loopexit, !llvm.loop !22
}
**question:**
**The instruction(%41 = load i32, ptr addrspace(1) %33, align 4, !tbaa !19) is loop-invariant, but it is not optimized to move to block .lr.ph**
[benchmark_device_spmv-hip-amdgcn-amd-amdhsa-gfx906.log](https://github.com/llvm/llvm-project/files/12554567/benchmark_device_spmv-hip-amdgcn-amd-amdhsa-gfx906.log)
</pre>
<img width="1px" height="1px" alt="" src="http://email.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