[GlobalISel] udiv x, (shl pow2, y) is not folded to a right shift
When the divisor is a power-of-two constant shifted left by a variable amount, the udiv is a right shift by log2(c) + y. DAGCombiner::visitUDIVLike does this fold. GlobalISel has no equivalent, so the full division expansion is used.
Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/udiv.i32.ll, v_udiv_i32_pow2_shl_denom):
define i32 @v_udiv_i32_pow2_shl_denom(i32 %x, i32 %y) {
%shl.y = shl i32 4096, %y
%r = udiv i32 %x, %shl.y
ret i32 %r
}
llc -global-isel -amdgpu-codegenprepare-disable-idiv-expansion=1 -mtriple=amdgpu6.00-amd-amdpal test.ll -o -
Current GlobalISel output: 22 instructions (full 32-bit udiv expansion).
Desired output (same as SelectionDAG):
s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
v_add_i32_e32 v1, vcc, 12, v1
v_lshrrev_b32_e32 v0, v1, v0
s_setpc_b64 s[30:31]
Same for vectors and i64 (udiv.i32.ll, udiv.i64.ll).
Possible fix: a generic combine (G_UDIV x, (G_SHL C, y)) -> (G_LSHR x, (G_ADD y, log2(C))) for a power-of-two constant C.
@llvm/issue-subscribers-backend-amdgpu
Author: Petar Avramovic (petar-avramovic)
Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/udiv.i32.ll, v_udiv_i32_pow2_shl_denom):
define i32 @v_udiv_i32_pow2_shl_denom(i32 %x, i32 %y) { %shl.y = shl i32 4096, %y %r = udiv i32 %x, %shl.y ret i32 %r }
llc -global-isel -amdgpu-codegenprepare-disable-idiv-expansion=1 -mtriple=amdgpu6.00-amd-amdpal test.ll -o -
Current GlobalISel output: 22 instructions (full 32-bit udiv expansion).
Desired output (same as SelectionDAG):
s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
v_add_i32_e32 v1, vcc, 12, v1
v_lshrrev_b32_e32 v0, v1, v0
s_setpc_b64 s[30:31]
Same for vectors and i64 (udiv.i32.ll, udiv.i64.ll).
Possible fix: a generic combine (G_UDIV x, (G_SHL C, y)) -> (G_LSHR x, (G_ADD y, log2(C))) for a power-of-two constant C.
Hi!
This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:
- Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
- Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
- Fix the issue locally. Be aware that LLVM's AI policy forbids the use of AI tools to fix good first issues since it squanders a learning opportunity. Using LLMs to search and learn is permitted, since the goal is learning.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.
If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.
@llvm/issue-subscribers-good-first-issue
Author: Petar Avramovic (petar-avramovic)
Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/udiv.i32.ll, v_udiv_i32_pow2_shl_denom):
define i32 @v_udiv_i32_pow2_shl_denom(i32 %x, i32 %y) { %shl.y = shl i32 4096, %y %r = udiv i32 %x, %shl.y ret i32 %r }
llc -global-isel -amdgpu-codegenprepare-disable-idiv-expansion=1 -mtriple=amdgpu6.00-amd-amdpal test.ll -o -
Current GlobalISel output: 22 instructions (full 32-bit udiv expansion).
Desired output (same as SelectionDAG):
s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
v_add_i32_e32 v1, vcc, 12, v1
v_lshrrev_b32_e32 v0, v1, v0
s_setpc_b64 s[30:31]
Same for vectors and i64 (udiv.i32.ll, udiv.i64.ll).
Possible fix: a generic combine (G_UDIV x, (G_SHL C, y)) -> (G_LSHR x, (G_ADD y, log2(C))) for a power-of-two constant C.
Hi, I'd like to work on this. Could it be assigned to me? :)
It seems like someone else is already working on this in PR #236601. Should i leave it to them?
7. It seems like someone else is already working on this in PR #236601. Should i leave it to them?
oh ok, it happens, you can pick up something else if you want