[PATCH] D154791: [InstCombine] Transform bitwise (A << X, zext(icmp)) -> zext (bitwise(A < 0, icmp)) fold.

Hongyu Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 9 07:32:26 PDT 2023


XChy created this revision.
XChy added reviewers: nikic, goldstein.w.n, k-arrows, spatel.
XChy added projects: LLVM, All.
Herald added subscribers: StephenFan, hiraditya.
XChy requested review of this revision.
Herald added a subscriber: llvm-commits.

This extends **foldCastedBitwiseLogic** to handle the similar cases.
I have recently submitted a patch to implement a single fold like:

  (A > 0) | (A < 0) -> zext (A != 0) 

But it is not general enough, and some problems like a < b & a >= b - 1 <https://github.com/llvm/llvm-project/issues/62586> happen again.

So I generalize this fold by matching the pattern `bitwise(A << X, zext(icmp))`, and replace `A << X` with `zext(A < 0)` here.
(X is the scalar size bits of the type of A))
Then we get `bitwise(zext(A < 0), zext(icmp))`, this will be folded by original code in **foldCastedBitwiseLogic**, into `zext(bitwise(A < 0, icmp))`.
And finally, any related icmp fold will be automatically implemented because `bitwise(icmp,icmp)` had been implemented.

The proof of the correctness is obvious, because the folds below were previously proved and implemented.
`A << X -> zext(A < 0)`
`bitwise(zext(A), zext(B)) -> zext(bitwise(A, B))`
And the fold of this patch is the combination of folds above.

Related issue:
a < b | a >b <https://github.com/llvm/llvm-project/issues/62586>
a < b & a >= b - 1 <https://github.com/llvm/llvm-project/issues/62586>
Related patch:
D154126 <https://reviews.llvm.org/D154126>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154791

Files:
  llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
  llvm/test/Transforms/InstCombine/and-or-icmps.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154791.538433.patch
Type: text/x-patch
Size: 8261 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230709/c557c1e6/attachment.bin>


More information about the llvm-commits mailing list