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

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 6 20:25:55 PDT 2023


goldstein.w.n created this revision.
goldstein.w.n added reviewers: nikic, RKSimon, arsenm.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
goldstein.w.n requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

This implements:

  (fmul C, (uitofp Pow2))
      -> (bitcast_to_FP (add (bitcast_to_INT C), Log2(Pow2) << mantissa))
  (fdiv C, (uitofp Pow2))
      -> (bitcast_to_FP (sub (bitcast_to_INT C), Log2(Pow2) << mantissa))

The motivation is mostly `fdiv` where `2^(-p)` is a fairly common
expression.

The patch is intentionally conservative about the transform, only
doing so if we

1. have IEEE floats
2. `C` is normal
3. add/sub of `max(Log2(Pow2))` stays in the min/max exponent bounds.

Alive2 can't realistically prove this, but did test float16/float32
cases (within the bounds of the above rules) exhaustively.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154678

Files:
  llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
  llvm/test/Transforms/InstCombine/fmul-and-fdiv-by-int-pow2.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154678.537968.patch
Type: text/x-patch
Size: 11062 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230707/fdb6d25c/attachment.bin>


More information about the llvm-commits mailing list