This is an archive of the discontinued LLVM Phabricator instance.

[TLI] SimplifySetCC - Fold ~X >/< ~Y --> Y >/< X
ClosedPublic

Authored by junaire on Mar 21 2023, 4:20 AM.

Details

Summary

Fixes: https://github.com/llvm/llvm-project/issues/61120

Signed-off-by: Jun Zhang <jun@junz.org>

Diff Detail

Event Timeline

junaire created this revision.Mar 21 2023, 4:20 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 21 2023, 4:20 AM
junaire requested review of this revision.Mar 21 2023, 4:20 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 21 2023, 4:20 AM

Looks like the patch doesn't work for vectors, do you have any insights about it? @RKSimon

Thanks for taking a look at this!

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
4979

Take a look at https://github.com/llvm/llvm-project/blob/3a8f161a3401edeb58e018e2d389dd2413a6417f/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp#L6501

This shows its safe to handle all icmp predicates - and handles cases where one operand is a constant which can be constant folded

4984

You should be able to use llvm::isBitwiseNot to detect this

RKSimon retitled this revision from [TLI] Fold ~X s>/u< ~Y --> Y s>/u< X to [TLI] SimplifySetCC - Fold ~X s>/u< ~Y --> Y s>/u< X.Mar 21 2023, 4:39 AM
junaire updated this revision to Diff 506926.Mar 21 2023, 5:26 AM
  1. Use llvm::isBitwiseNot to simplify code.
  2. Support X > ~C --> X < ~C
junaire marked an inline comment as done.Mar 21 2023, 5:32 AM
junaire added inline comments.
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
4979

Thanks for the review! However, I'm unsure what is the right way to say 'all icmp predicates' and I'm worried about why vectors don't fold correctly.

If I run llc -mcpu=btver2 test.ll to:

define <2 x i64> @cmp_sgt_not(<2 x i64> %a, <2 x i64> %b) {                                                                                                                                                         %na = xor <2 x i64> %a, <i64 -1, i64 -1>                                                                                                                                                                          %nb = xor <2 x i64> %b, <i64 -1, i64 -1>                                                                                                                                                                          %c = icmp sgt <2 x i64> %na, %nb                                                                                                                                                                                  %r = sext <2 x i1> %c to <2 x i64>                                                                                                                                                                                ret <2 x i64> %r                                                                                                                                                                                                }

It does fold to:

vpcmpgtq        %xmm0, %xmm1, %xmm0                                                                                                                                                                               retq

But for ule it doesn't work as well.

Let me know if you think it's necessary to add more tests for the remaining preds!

What regressions are you seeing for ULE vector cases with this fold? Manual optimization looks OK - https://llvm.godbolt.org/z/n9ezae5En

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
4982

N0.getValueType().isInteger() should be implied from the CodeCode - I don't think you need it but make it an assert if you're worried

junaire updated this revision to Diff 507265.Mar 22 2023, 1:32 AM

Make N0.getValueType().isInteger() an assert.

What regressions are you seeing for ULE vector cases with this fold? Manual optimization looks OK - https://llvm.godbolt.org/z/n9ezae5En

I may misunderstand something. I was expected it could folded like https://llvm.godbolt.org/z/ebfj4MoKr

junaire retitled this revision from [TLI] SimplifySetCC - Fold ~X s>/u< ~Y --> Y s>/u< X to [TLI] SimplifySetCC - Fold ~X >/< ~Y --> Y >/< X.Mar 22 2023, 1:38 AM
junaire updated this revision to Diff 507266.Mar 22 2023, 1:50 AM

Revert 'Make N0.getValueType().isInteger() an assert.' since it causes a lot of crashes in the tests.

junaire updated this revision to Diff 507267.Mar 22 2023, 1:51 AM

Make clang-format happy

junaire added inline comments.Mar 22 2023, 1:54 AM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
4982

Looks like it's a must, without the check I saw lots of crashes in the tests. For example:

llc: llvm-project/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp:4985: llvm::SDValue llvm::TargetLowering::SimplifySetCC(llvm::EVT, llvm::SDValue, llvm::SDValue, ISD::CondCode, bool, llvm::TargetLowering::DAGCombinerInfo &, const llvm::SDLoc &) const: Assertion `N0.getValueType().isInteger() && "N0 is not an ineger type"' failed.
... Crashes ....
llvm-project/llvm/test/CodeGen/X86/machine-combiner.ll:694:14: error: SSE-LABEL: expected string not found in input
...
RKSimon added inline comments.Mar 22 2023, 4:55 AM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
4987

Can you replace this with llvm::isConstantIntBuildVectorOrConstantInt(N1) to support vectors as well?

junaire updated this revision to Diff 507364.Mar 22 2023, 7:52 AM

Use SelectionDAG::isConstantIntBuildVectorOrConstantInt to support vector constant.

junaire marked an inline comment as done.Mar 22 2023, 7:53 AM
RKSimon accepted this revision.Mar 22 2023, 9:12 AM

LGTM - cheers!

This revision is now accepted and ready to land.Mar 22 2023, 9:12 AM
This revision was landed with ongoing or failed builds.Mar 22 2023, 9:49 PM
This revision was automatically updated to reflect the committed changes.

LGTM - cheers!

Thanks for the review!