Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select not sunk into branch on same condition #63756

Closed
nikic opened this issue Jul 8, 2023 · 0 comments
Closed

Select not sunk into branch on same condition #63756

nikic opened this issue Jul 8, 2023 · 0 comments

Comments

@nikic
Copy link
Contributor

nikic commented Jul 8, 2023

https://llvm.godbolt.org/z/s61x8M7Tf

declare void @use(i32)
declare void @dummy()

define void @test(i1 %c, i32 %a, i32 %b) {
  %s = select i1 %c, i32 %a, i32 %b
  br i1 %c, label %if, label %else

if:
  call void @dummy()
  call void @use(i32 %s)
  ret void

else:
  call void @use(i32 %s)
  call void @dummy()
  ret void
}

We have a branch on the same condition as the select, so we should sink the select arms into the branches. It's pointless to compute the select value in the common code path:

declare void @use(i32)
declare void @dummy()

define void @test(i1 %c, i32 %a, i32 %b) {
  br i1 %c, label %if, label %else

if:
  call void @dummy()
  call void @use(i32 %a)
  ret void

else:
  call void @use(i32 %b)
  call void @dummy()
  ret void
}
@nikic nikic self-assigned this Jul 8, 2023
@nikic nikic closed this as completed in 103f1ac Jul 10, 2023
veselypeta pushed a commit to veselypeta/cherillvm that referenced this issue Sep 4, 2024
We may be able to replace the select with one of its select arms
at some but not all uses.

For uses in phi nodes this was already handled in the getValueOnEdge()
fold (which we can't drop entirely because it also handles an additional
case).

Fixes llvm/llvm-project#63756.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant