Skip to content

[InstCombine] Optimize ceil division idiom #95652

@nikic

Description

@nikic

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.

Metadata

Metadata

Labels

llvm:instcombineCovers the InstCombine, InstSimplify and AggressiveInstCombine passesmissed-optimizationwontfixIssue is real, but we can't or won't fix it. Not invalid

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions