[PATCH] D148609: [InstCombine] Fix buggy `(mul X, Y)` -> `(shl X, Log2(Y))` transform PR62175

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 18 00:34:04 PDT 2023


goldstein.w.n created this revision.
goldstein.w.n added reviewers: nikic, spatel, majnemer, junaire, zhendongsu.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
goldstein.w.n requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Bug was because we recognized patterns like `(shl 4, Z)` as a power of
2 we could take Log2 of (`2 + Z`), but doing `(shl X, (2 + Z))` can
cause a poison shift.

  https://alive2.llvm.org/ce/z/yuJm_k

The fix is to verify that `Log2(Y)` will be a non-poisonous shift
amount. We can do this with:

  `nsw` flag:
      - https://alive2.llvm.org/ce/z/yyyJBr
      - https://alive2.llvm.org/ce/z/YgubD_
  `nuw` flag:
      - https://alive2.llvm.org/ce/z/-4mpyV
      - https://alive2.llvm.org/ce/z/a6ik6r
  Prove `Y != 0`:
      - https://alive2.llvm.org/ce/z/ced4su
      - https://alive2.llvm.org/ce/z/X-JJHb


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D148609

Files:
  llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
  llvm/test/Transforms/InstCombine/mul-pow2.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148609.514539.patch
Type: text/x-patch
Size: 6968 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230418/aed7bb69/attachment.bin>


More information about the llvm-commits mailing list