This is an archive of the discontinued LLVM Phabricator instance.

[X86] Transform `(icmp eq/ne Abs(A), Pow2)` -> `(and/or (icmp eq/ne A,Pow2), (icmp eq/ne A,-Pow2))`
ClosedPublic

Authored by goldstein.w.n on Jan 23 2023, 3:24 AM.

Details

Summary

Only if Abs(A) has one use, in which case the `(and/or (icmp eq/ne
A,Pow2), (icmp eq/ne A,-Pow2))` can be optimized in
DAGCombiner::foldAndOrOfSETCC.

Alive Links:
EQ: https://alive2.llvm.org/ce/z/gTxSgV
NE: https://alive2.llvm.org/ce/z/MUf57Y

Diff Detail

Event Timeline

goldstein.w.n created this revision.Jan 23 2023, 3:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 23 2023, 3:24 AM
goldstein.w.n requested review of this revision.Jan 23 2023, 3:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 23 2023, 3:24 AM
RKSimon added inline comments.Jan 23 2023, 3:41 AM
llvm/lib/Target/X86/X86ISelLowering.cpp
53394

const APInt &CInt

goldstein.w.n marked an inline comment as done.Jan 23 2023, 12:03 PM

Use reference

Please can you add alive links?

llvm/lib/Target/X86/X86ISelLowering.cpp
53410

Add comment explaining the fold:

// Fold (icmp eq/ne Abs(A), Pow2)
//  --> (or/and (icmp eq/ne A,Pow2), (icmp eq/ne A,-Pow2))
53414

APInt::isPowerOf2() shouldn't require a isZero check?

Comment better + remove redundant iszero check

goldstein.w.n marked 2 inline comments as done.Jan 25 2023, 1:17 PM
goldstein.w.n added inline comments.
llvm/lib/Target/X86/X86ISelLowering.cpp
53414

APInt::isPowerOf2() shouldn't require a isZero check?

Is redundant, updated D142344 aswell.

goldstein.w.n edited the summary of this revision. (Show Details)Jan 25 2023, 1:17 PM
goldstein.w.n marked an inline comment as done.
goldstein.w.n edited the summary of this revision. (Show Details)

Rebase

pengfei added inline comments.Jan 26 2023, 3:25 AM
llvm/lib/Target/X86/X86ISelLowering.cpp
53411

Seems for 0 and INT_MIN, we can simply transform to icmp eq/ne A, C. Should we do it together given we already got the constant, or a follow?

goldstein.w.n added inline comments.Jan 26 2023, 9:30 AM
llvm/lib/Target/X86/X86ISelLowering.cpp
53411

We can although I don't that that IR ever really reaches the backend as the middle-end would clean up that pattern.
My thought it strange IR shouldn't cause broken transforms, but its fine for it to cause missed optimizations.

Although can add those two cases if you'd like, dont really have strong opinions on the matter.

nikic added a subscriber: nikic.Jan 26 2023, 1:47 PM
nikic added inline comments.
llvm/lib/Target/X86/X86ISelLowering.cpp
53411

The general rule of thumb is that DAGCombiner should only copy IR folds if the pattern appears as a result of legalization of canonical IR.

goldstein.w.n added inline comments.Jan 26 2023, 2:08 PM
llvm/lib/Target/X86/X86ISelLowering.cpp
53411

The general rule of thumb is that DAGCombiner should only copy IR folds if the pattern appears as a result of legalization of canonical IR.

@pengfei made: D142666 but agree with nikic its probably unnecessary. Didn't see any test changes and don't know of any cases where it would reasonably appear.

goldstein.w.n added inline comments.Jan 26 2023, 2:11 PM
llvm/lib/Target/X86/X86ISelLowering.cpp
53411

The general rule of thumb is that DAGCombiner should only copy IR folds if the pattern appears as a result of legalization of canonical IR.

@nikic do you think InstCombine should cannocicalize (or/and (icmp eq/ne X, C), (icmp eq/ne X, -C)) <--> (icmp eq/ne ABS(X), ABS(C))? I'm sure which was more useful if either.

pengfei added inline comments.Jan 27 2023, 2:22 AM
llvm/lib/Target/X86/X86ISelLowering.cpp
53411

Thanks @nikic for the inputs! My comment was inspired when reviewering test cases like eq_pow_or, abs_eq_pow2_zero etc.
I did have the same concern, but I'm not clear of it. This also makes me worrying whether the similar thing this patch does is necessary or not and what's important how we check for that? Any pointers would help.

llvm/test/CodeGen/X86/icmp-pow2-logic-npow2.ll
11–30

So we may remove these tests instead? We should avoid to add dud tests.

goldstein.w.n added inline comments.Jan 27 2023, 11:40 AM
llvm/lib/Target/X86/X86ISelLowering.cpp
53411

Thanks @nikic for the inputs! My comment was inspired when reviewering test cases like eq_pow_or, abs_eq_pow2_zero etc.
I did have the same concern, but I'm not clear of it. This also makes me worrying whether the similar thing this patch does is necessary or not and what's important how we check for that? Any pointers would help.

I can drop the checks, it still seems to be correct w.o them: https://alive2.llvm.org/ce/z/Q3PTMv

Guess my feeling is we know its the wrong thing to do, and if it does come up, we don't want to be
hiding it. Up to you.

goldstein.w.n added inline comments.Jan 27 2023, 11:43 AM
llvm/test/CodeGen/X86/icmp-pow2-logic-npow2.ll
11–30

So we may remove these tests instead? We should avoid to add dud tests.

This test is for D142344 where it is not a dud.
Do you mean abs_eq_pow2_zero and abs_ne_pow2_signed_min?

goldstein.w.n added inline comments.Jan 27 2023, 11:45 AM
llvm/lib/Target/X86/X86ISelLowering.cpp
53411

Thanks @nikic for the inputs! My comment was inspired when reviewering test cases like eq_pow_or, abs_eq_pow2_zero etc.
I did have the same concern, but I'm not clear of it. This also makes me worrying whether the similar thing this patch does is necessary or not and what's important how we check for that? Any pointers would help.

I can drop the checks, it still seems to be correct w.o them: https://alive2.llvm.org/ce/z/Q3PTMv

Guess my feeling is we know its the wrong thing to do, and if it does come up, we don't want to be
hiding it. Up to you.

Personal preference is:
leave checks in and ignore the case > handle the case with D142666 > remove the branches

pengfei added inline comments.Jan 27 2023, 6:26 PM
llvm/lib/Target/X86/X86ISelLowering.cpp
53411

It looks awkward to me that neither do I want to increase unnecessary code complexity nor can assert these cases never happen.
I don't have preference. I suggest to add comments to explain they are no worth to handle if you want to keep them.

Tips: Phabricator will set the link for it e.g., D142666 if you don't put `` around it.

llvm/test/CodeGen/X86/icmp-pow2-logic-npow2.ll
11–30

Yes. Sorry for the mistake.

goldstein.w.n marked an inline comment as done.Jan 27 2023, 8:34 PM
goldstein.w.n added inline comments.
llvm/test/CodeGen/X86/icmp-pow2-logic-npow2.ll
11–30

Yes. Sorry for the mistake.

11–30

So we may remove these tests instead? We should avoid to add dud tests.

Removed those, although did add a few other tests for negative cases that were missing.
Unlike the 0/intmin case, they can actually catch a bug, but lmk if I should drop them.

RKSimon accepted this revision.Jan 29 2023, 7:34 AM

LGTM

This revision is now accepted and ready to land.Jan 29 2023, 7:34 AM
goldstein.w.n marked an inline comment as done.

Rebase

This revision was landed with ongoing or failed builds.Feb 14 2023, 4:59 PM
This revision was automatically updated to reflect the committed changes.