[PATCH] D154805: [DAGCombiner] Fold IEEE `fmul`/`fdiv` by Pow2 to `add`/`sub` of exp

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 30 18:49:57 PDT 2023


goldstein.w.n added a comment.

In D154805#4544765 <https://reviews.llvm.org/D154805#4544765>, @RKSimon wrote:

> Have you looked at using the existing DAG::isKnownToBeAPowerOfTwo and DAGCombiner::BuildLogBase2 methods?

Just did. That would work in a sense. The rationale of keeping seperate is that `takeLog2` as is, is basically guranteed to return an expression thats as or less expensive as the `pow2` op. I.e
if we have `(min a, b)`, we might return `(min log2_a, log2_b)` but wouldn't return that if we didn't already find a min.

Think that highlights this might be more re-appropriately named `findInexpensiveLog2` (changed).

Looking at our current combine ability we also seem to not optimize trivial log2 cases of `BuildLogBase2` i.e:

  declare i32 @llvm.ctlz.i32(i32, i1)
  
  define i32 @trivial_log2(i32 %x) {
    %s = shl i32 1, %x
    %r = call i32 @llvm.ctlz.i32(i32 %s, i1 true)
    ret i32 %r
  }

Doesn't optimize out the `shl/ctlz`.

I can follow up with patches to integrate `takeLog2` into `buildLog2Base`


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154805/new/

https://reviews.llvm.org/D154805



More information about the llvm-commits mailing list