Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[InstCombine] Improve folds for and/or cmp, cmp #72965

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

goldsteinn
Copy link
Contributor

  • [InstCombine] Add tests for expanding foldICmpWithLowBitMaskedVal; NFC
  • [InstCombine] Improve mask detection in foldICmpWithLowBitMaskedVal
  • [InstCombine] Recognize (icmp eq/ne (and X, ~Mask), 0) pattern in foldICmpWithLowBitMaskedVal
  • [InstCombine] Make the (icmp eq/ne (and X, Y), X) canonicalization work for non-const operands
  • [InstCombine] Make getKnownSign a member function of InstCombiner; NFC
  • [InstCombine] Add transforms (icmp spred (and X, Y), X) if X or Y are known signed/unsigned
  • [InstCombine] Add tests for folding multiuse (icmp eq/ne (or X, Y), Y); NFC
  • [InstCombine] Folding multiuse (icmp eq/ne (or X, Y), Y) for 2 uses of Y
  • [InstCombine] Add tests for transforming (or/and (icmp eq/ne X,0),(icmp eq/ne X,Pow2OrZero)); NFC
  • [InstCombine] Add transforms for (or/and (icmp eq/ne X,0),(icmp eq/ne X,Pow2OrZero))

Make recursive matcher that is able to detect a lot more patterns.
Proofs for all supported patterns: https://alive2.llvm.org/ce/z/fSQ3nZ

Differential Revision: https://reviews.llvm.org/D159058
…foldICmpWithLowBitMaskedVal`

`(icmp eq/ne (and X, ~Mask), 0)` is equivilent to `(icmp eq/ne (and X,
Mask), X` and we sometimes generate the former pattern intentionally
to reduce number of uses of `X`.
Proof: https://alive2.llvm.org/ce/z/3u-usC

Differential Revision: https://reviews.llvm.org/D159329
…work for non-const operands

We currently do:
    `(icmp eq/ne (and X, Y), Y)` -> `(icmp eq/ne (and ~X, Y), 0)`
if `X` is constant. We can make this more general and do it if `X` is
freely invertable (i.e say `X = ~Z`).

As well, we can also do:
    `(icmp eq/ne (and X, Y), Y)` -> `(icmp eq/ne (or X, ~Y), -1)`
If `Y` is freely invertible.

Proofs: https://alive2.llvm.org/ce/z/yeWH3E

Differential Revision: https://reviews.llvm.org/D159059
This is prep for using `getKnownSign` outside of just InstCombineCalls.

Differential Revision: https://reviews.llvm.org/D159060
…Y` are known signed/unsigned

Several transforms:
    1) If known `Y < 0`:
        - slt -> ult: https://alive2.llvm.org/ce/z/unpsxu
        - sle -> ule: https://alive2.llvm.org/ce/z/a5cpmr
        - sgt -> ugt: https://alive2.llvm.org/ce/z/qgsgya
        - sge -> uge: https://alive2.llvm.org/ce/z/ek_3ls
    2) If known `Y > 0`:
        - `(X & PosY) s> X --> X s< 0`
            - https://alive2.llvm.org/ce/z/drewij
        - `(X & PosY) s> X --> X s< 0`
            - https://alive2.llvm.org/ce/z/9arL3D
    3) If known `X < 0`:
        - `(NegX & Y) s> NegX --> Y s>= 0`
            - https://alive2.llvm.org/ce/z/ApkaEh
        - `(NegX & Y) s<= NegX --> Y s< 0`
            - https://alive2.llvm.org/ce/z/oRnfHp

Differential Revision: https://reviews.llvm.org/D145426
… of `Y`

The fold will replace 2 uses of `Y` we should also do fold if `Y` has
2 uses (not only oneuse).

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D159062
…e X,Pow2OrZero))`

`(or (icmp eq X, 0), (icmp eq X, Pow2OrZero))`
    --> `(icmp eq (and X, Pow2OrZero), X)`

`(and (icmp ne X, 0), (icmp ne X, Pow2OrZero))`
    --> `(icmp ne (and X, Pow2OrZero), X)`

Proofs: https://alive2.llvm.org/ce/z/nPo2BN

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D157312
@goldsteinn goldsteinn changed the title goldsteinn/and canonicalization cont [InstCombine] Improve folds for and/or cmp, cmp Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant