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

[InstCombine] Optimize ceil division idiom #95652

Closed
nikic opened this issue Jun 15, 2024 · 2 comments · May be fixed by #100977
Closed

[InstCombine] Optimize ceil division idiom #95652

nikic opened this issue Jun 15, 2024 · 2 comments · May be fixed by #100977
Assignees
Labels
llvm:instcombine missed-optimization wontfix Issue is real, but we can't or won't fix it. Not invalid

Comments

@nikic
Copy link
Contributor

nikic commented Jun 15, 2024

https://alive2.llvm.org/ce/z/Lo75y7

define i8 @src(i8 %x, i8 %y) {
  %wo = call {i8, i1} @llvm.uadd.with.overflow(i8 %x, i8 %y)
  %ov = extractvalue {i8, i1} %wo, 1
  %ov.not = xor i1 %ov, true
  call void @llvm.assume(i1 %ov.not)

  %nonzero = icmp ne i8 %x, 0
  %bias = zext i1 %nonzero to i8
  %sub = sub i8 %x, %bias
  %div = udiv i8 %sub, %y
  %add = add i8 %div, %bias
  ret i8 %add
}

define i8 @tgt(i8 %x, i8 %y) {
  %y.minus.1 = add i8 %y, -1
  %add = add i8 %x, %y.minus.1
  %div = udiv i8 %add, %y
  ret i8 %div
}

We could optimize Bias = X != 0; (X - Bias) / Y + Bias to the more efficient (X + Y - 1) / Y if we know that the addition cannot overflow.

Probably the more important case is where the udiv is a lshr instead, as that's where the relative overhead is higher.

@jayfoad
Copy link
Contributor

jayfoad commented Jun 17, 2024

We could optimize Bias = Y != 0; (X - Bias) / Y + 1

It's Bias = X != 0; (X - Bias) / Y + Bias

antoniofrighetto added a commit to antoniofrighetto/llvm-project that referenced this issue Jul 29, 2024
The expression `add (udiv (sub A, Bias), B), Bias` can be
folded to `udiv (add A, B - 1), B)` when the sum between
`A` and `B` is known not to overflow, and `Bias = A != 0`.

Fixes: llvm#95652.

Proof: https://alive2.llvm.org/ce/z/hiWHQA.
antoniofrighetto added a commit to antoniofrighetto/llvm-project that referenced this issue Jul 29, 2024
The expression `add (udiv (sub A, Bias), B), Bias` can be
folded to `udiv (add A, B - 1), B)` when the sum between
`A` and `B` is known not to overflow, and `Bias = A != 0`.

Fixes: llvm#95652.

Proof: https://alive2.llvm.org/ce/z/hiWHQA.
@nikic
Copy link
Contributor Author

nikic commented Aug 30, 2024

Based on discussion in #100977, it looks like in practice we don't really have enough information to prove the necessary pre-condition, so I'll go ahead and close this.

@nikic nikic closed this as not planned Won't fix, can't repro, duplicate, stale Aug 30, 2024
@EugeneZelenko EugeneZelenko added the wontfix Issue is real, but we can't or won't fix it. Not invalid label Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:instcombine missed-optimization wontfix Issue is real, but we can't or won't fix it. Not invalid
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants