Issue 76559
Summary Missed optimization for `fdiv` followed by `fmul`
Labels missed-optimization, llvm
Assignees
Reporter rikhuijzer
    I wonder whether rewriting `@src` to `@trg` would be a valid optimization?
```ll
define float @src(float %0) {
  %2 = fdiv float %0, 6.000000e+00
  %3 = fmul float %2, 3.000000e+00
  ret float %3
}

define float @trg(float %0) {
  %2 = fdiv float %0, 2.000000e+00
  ret float %2
}
```
since $\frac{x}{6.0} * 3.0 = \frac{x}{2.0}$.

I've tried to verify this via `alive-tv`, but it times out due to the floating point numbers. I've tried locally with `--smt-to=3600000` (1 hour timeout).

Intuitively, I would say that the optimization is valid because it should make the results **more** accurate.

For example, the result for 7664.71 is more accurate via `f2` (with the suggested optimization) than `f1` (without the suggested optimization) in Julia:

```julia
julia> f1(x) = x / 6.0 * 3.0;

julia> f1(0.2)
0.1

julia> f2(x) = x / 2.0;

julia> f2(0.2)
0.1

julia> @code_llvm dump_module=false debuginfo=:none f1(0.2)
define double @julia_f1_1438(double %0) #0 {
top:
  %1 = fdiv double %0, 6.000000e+00
  %2 = fmul double %1, 3.000000e+00
  ret double %2
}

julia> @code_llvm dump_module=false debuginfo=:none f2(0.2)
define double @julia_f2_1440(double %0) #0 {
top:
  %1 = fmul double %0, 5.000000e-01
  ret double %1
}

julia> f1(7664.71) * 2
7664.709999999999

julia> f2(7664.71) * 2
7664.71
```

So would an optimization for $\frac{x}{a} * b$ be valid for certain `a` and `b`?
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to