This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] (icmp eq A, -1) & (icmp eq B, -1) --> (icmp eq (A&B), -1)
ClosedPublic

Authored by xgupta on May 29 2023, 1:09 PM.

Details

Summary

This patch add another icmp fold for -1 case.

This fixes https://github.com/llvm/llvm-project/issues/62311,
where we want instcombine to merge all compare intructions together so
later passes like simplifycfg and slpvectorize can better optimize this
chained comparison.

Diff Detail

Event Timeline

xgupta created this revision.May 29 2023, 1:09 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 29 2023, 1:09 PM
xgupta requested review of this revision.May 29 2023, 1:09 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 29 2023, 1:09 PM
xgupta updated this revision to Diff 526471.May 29 2023, 1:15 PM

Add C++ version test case in llvm lit test case.

xgupta updated this revision to Diff 526474.May 29 2023, 1:18 PM

corrected diff

goldstein.w.n added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2955

m_ConstantInt<-1>() -> m_AllOnes()

llvm/test/Transforms/InstCombine/pr62311.ll
75 ↗(On Diff #526474)

This single test isn't really sufficient.

Can you add a few more simple tests covering the following cases:

  1. (x == -1 && y == -1)
  2. (x != -1) || (y != -1)
  3. (x == -1) || (y == -1) -- failure case
  4. (x == -1) && (y != -1) -- failure case

maybe a few more failure cases using other constants / mismatched constants.
Something in the style of icmp-add.ll should be fine.

Can you also split the patch with the tests so we can more easily see the diff generated by this patch.

I.e

Patch1: Test Cases
Patch2: Implementation

Then match patch2 the parent of patch1.

xgupta updated this revision to Diff 526531.May 30 2023, 12:03 AM

address comment for m_ConstantInt<-1>() -> m_AllOnes()

xgupta marked an inline comment as done.May 30 2023, 12:03 AM
hnrklssn added inline comments.May 30 2023, 1:08 AM
llvm/test/Transforms/InstCombine/pr62311.ll
75 ↗(On Diff #526474)

For the success cases, could you also add a second run line to show that this transformation is enough to do the vectorisation asked for in https://github.com/llvm/llvm-project/issues/62311?
Something like:

; RUN: opt < %s -passes=instcombine -S | FileCheck %s
; RUN: opt < %s -passes=instcombine,simplify-cfg,<some-vectoriser-pass> -S | FileCheck --check-prefix INTEGRATED %s
fhahn added a subscriber: fhahn.May 30 2023, 1:44 AM
fhahn added inline comments.
llvm/test/Transforms/InstCombine/pr62311.ll
75 ↗(On Diff #526474)

To check the end-to-end result it would probably be better to add a test in PhaseOrdering that just runs -passes='default<O2>' (or something) rather than having the InstCombine tests depend on other passes

xgupta marked 3 inline comments as done.May 30 2023, 2:00 AM
xgupta added inline comments.
llvm/test/Transforms/InstCombine/pr62311.ll
75 ↗(On Diff #526474)

Thanks, Updated the test case RUN line here - https://reviews.llvm.org/D151694.

goldstein.w.n added inline comments.May 30 2023, 9:58 AM
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2952

Is there why this patch adds this new functionality and doesn't juist update foldLogOpOfMaskedICmps to handle undefs? Is it alot of work?

2958

ConstantInt::get(LHS0->getType(), -1) -> Constant::getAllOnesValue(LHS0->getType())

xgupta updated this revision to Diff 526889.May 30 2023, 9:11 PM

Address comment

xgupta marked an inline comment as done.May 30 2023, 9:14 PM
xgupta added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2952

I have not thought about that, I will try to explore that.
I wrote this as similar to a few line above for (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)"

2958

Thanks, updated.

goldstein.w.n added inline comments.May 30 2023, 9:48 PM
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2952

well I'm just wondering what the todo is about.

xgupta marked an inline comment as done.May 31 2023, 6:56 AM
xgupta added a subscriber: nikic.
xgupta added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2952
xgupta added inline comments.Jun 1 2023, 8:39 PM
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2952

@nikic shall we keep TODO or remove it or do you have any suggestion how to make foldLogOpOfMaskedICmps handle undefs.

goldstein.w.n added inline comments.Jun 1 2023, 8:48 PM
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2952

I don't understanding, the TODO is new with your code? If you are just copy pasting the comment please drop it.

xgupta updated this revision to Diff 527727.Jun 1 2023, 9:46 PM

Remove TODO

xgupta marked an inline comment as done.Jun 1 2023, 9:47 PM
xgupta added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2952

Dropped it.

We should see diffs in the tests caused by this patch.

xgupta updated this revision to Diff 529345.Jun 7 2023, 9:33 AM
xgupta marked an inline comment as done.

Add updated test case

xgupta updated this revision to Diff 529441.Jun 7 2023, 2:43 PM

Updated test cases

goldstein.w.n accepted this revision.Jun 7 2023, 3:58 PM

LGTM.

Do you need me to push this for you? If so can you please reply with your name/email?

This revision is now accepted and ready to land.Jun 7 2023, 3:58 PM
xgupta added a comment.Jun 7 2023, 8:35 PM

LGTM.

Do you need me to push this for you? If so can you please reply with your name/email?

Thank you for the review. I do have commit access. Will push it.