llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Missed optimization: fold frexp(x).exp < C to fabs(x) < 2^(C-1)

Open dreamking60 opened this issue 8 months ago • 3 comments

define i1 @src(float %arg0) {
  %v0 = tail call { float, i32 } @llvm.frexp.f32.i32(float %arg0)
  %v1 = extractvalue { float, i32 } %v0, 1
  %v2 = icmp slt i32 %v1, 24
  ret i1 %v2
}

define i1 @tgt(float %arg0) {
  %v0 = call float @llvm.fabs.f32(float %arg0)
  %v1 = fcmp olt float %v0, 8.388608e+06
  ret i1 %v1
}

alive2: https://alive2.llvm.org/ce/z/T6_APp godbolt: https://godbolt.org/z/sxxran9zj

Pattern found in: https://github.com/dtcxzyw/llvm-opt-benchmark/blob/main/bench/arrow/optimized/decimal.ll

dreamking60 avatar Mar 14 '26 04:03 dreamking60

Hi @dreamking60, I think this optimization can be generalized in this way: IsFinite(x) ∧ (1 ≤s C ≤s 2^(E-1) + 1) |= icmp_slt(extract_exp(frexp_{E,P}(x)), C) ⇒ fcmp_olt(fabs(x), 2^(C-1))

hiandgodblessyou avatar Jun 08 '26 19:06 hiandgodblessyou

Hi @dreamking60, I think this optimization can be generalized in this way: IsFinite(x) ∧ (1 ≤s C ≤s 2^(E-1) + 1) |= icmp_slt(extract_exp(frexp_{E,P}(x)), C) ⇒ fcmp_olt(fabs(x), 2^(C-1))

That's looks good. Thanks for your advice.

dreamking60 avatar Jun 11 '26 02:06 dreamking60

I would like to work on this issue.

FathimaHaris avatar Jul 14 '26 06:07 FathimaHaris