This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Generalize foldAndOrOfICmpEqZeroAndICmp
ClosedPublic

Authored by 0xdc03 on Jul 11 2023, 3:05 AM.

Details

Summary

This patch generalizes the fold implemented by foldAndOrOfICmpEqZeroAndICmp,
which are:

(icmp eq X, 0) | (icmp ult Other, X) -> (icmp ule Other, X-1)
(icmp ne X, 0) & (icmp uge Other, X) -> (icmp ugt Other, X-1)

to the following:

(icmp eq X, C) | (icmp ult Other, (X - C)) -> (icmp ule Other, (X - (C + 1)))
(icmp ne X, C) & (icmp uge Other, (X - C)) -> (icmp ugt Other, (X - (C + 1)))

The function foldAndOrOfICmpEqZeroAndICmp is also renamed to
foldAndOrOfICmpEqConstantAndICmp to reflect the changes.

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

Fixes #63749.

Diff Detail

Event Timeline

0xdc03 created this revision.Jul 11 2023, 3:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 11 2023, 3:05 AM
0xdc03 requested review of this revision.Jul 11 2023, 3:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 11 2023, 3:05 AM
0xdc03 updated this revision to Diff 538994.Jul 11 2023, 3:11 AM
0xdc03 edited the summary of this revision. (Show Details)Jul 11 2023, 3:12 AM
nikic added inline comments.Jul 11 2023, 3:32 AM
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
3002

Move this lower, not needed here yet.

3022

Create the constant directly from *CInt + 1, no need for CreateAdd.

llvm/test/Transforms/InstCombine/and-or-icmp-const-icmp.ll
27–28

Write this in canonical form (add -5) or drop the test if redundant with the next.

27–28

Not actually commuted. Grep for thwart complexity-based canonicalization in tests.

27–28

No need for separate commuted vector tests.

0xdc03 updated this revision to Diff 539019.Jul 11 2023, 4:12 AM
  • Address reviewer comments
    • Remove unneeded variable C
    • Clean up tests
0xdc03 marked 4 inline comments as done.Jul 11 2023, 4:14 AM
0xdc03 added inline comments.
llvm/test/Transforms/InstCombine/and-or-icmp-const-icmp.ll
27–28

I was aiming for the signs to be opposite in them, dunno if that is necessary.

nikic accepted this revision.Jul 11 2023, 6:33 AM

LGTM

This revision is now accepted and ready to land.Jul 11 2023, 6:33 AM
0xdc03 updated this revision to Diff 539084.Jul 11 2023, 7:13 AM
  • Fix failing test
  • Rebase on top of updated tests
nikic added inline comments.Jul 11 2023, 7:28 AM
llvm/test/Transforms/InstCombine/and-or-icmp-const-icmp.ll
160

As you're using m_APIntAllowUndef now, I'd suggest to also add a "splat with undef" vector test like <i8 5, i8 undef> here.

0xdc03 updated this revision to Diff 539099.Jul 11 2023, 7:45 AM
  • Address reviewer comment: Add an undef splat vector test
0xdc03 marked 2 inline comments as done.Jul 11 2023, 7:46 AM
nikic added inline comments.Jul 11 2023, 7:48 AM
llvm/test/Transforms/InstCombine/and-or-icmp-const-icmp.ll
235

We should also have the variant with the undef on the add. Handling that one would need m_SpecificIntAllowUndef() as well.

0xdc03 updated this revision to Diff 539171.Jul 11 2023, 9:55 AM
  • Address reviewer comment: Match undef on add as well
0xdc03 marked an inline comment as done.Jul 11 2023, 9:56 AM
0xdc03 added inline comments.
llvm/test/Transforms/InstCombine/and-or-icmp-const-icmp.ll
235

Done, though I'm just appending the test to this patch.

nikic accepted this revision.Jul 11 2023, 10:08 AM

LGTM

This revision was landed with ongoing or failed builds.Jul 11 2023, 10:44 PM
This revision was automatically updated to reflect the committed changes.