This is an archive of the discontinued LLVM Phabricator instance.

[DAG] Fold setcc eq with ashr to compare to zero.
ClosedPublic

Authored by dmgreen on Sep 3 2021, 1:00 AM.

Details

Summary

Pulled out of D109149, this folds set_cc seteq (ashr X, BW-1), -1 -> set_cc setlt X, 0 to prevent some regressions later on when folding select_cc setgt X, -1, C, ~C -> xor (ashr X, BW-1), C

Diff Detail

Event Timeline

dmgreen created this revision.Sep 3 2021, 1:00 AM
dmgreen requested review of this revision.Sep 3 2021, 1:00 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 3 2021, 1:00 AM
Herald added subscribers: MaskRay, wdng. · View Herald Transcript
foad added a subscriber: foad.Sep 3 2021, 1:58 AM

Looks reasonable to me, but doesn't instcombine pick this up at the IR level? Or don't the .ll test cases run instcombine?

This is part of D109149, it prevents that from causing larger code when this pattern comes up from other DAG Combines.

spatel accepted this revision.Sep 3 2021, 5:28 AM

LGTM.
The motivating transform in D109149 only works on scalars if I'm seeing it correctly. This part could apply to vectors without much effort, but it's already in a FIXME block, so nothing more to do at the moment.

This revision is now accepted and ready to land.Sep 3 2021, 5:28 AM
This revision was landed with ongoing or failed builds.Sep 5 2021, 6:06 AM
This revision was automatically updated to reflect the committed changes.
uabelho added a subscriber: uabelho.Sep 8 2021, 3:51 AM
uabelho added inline comments.
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
3921

The comment at line 3914 says we should use "setge" for the "setne" case, but the code actually use "SETGT"?

I'm seeing a miscompile with this patch for my out of tree target and I'm not sure what the problem actually is, but the difference between the comment and the code caught my eye.

foad added inline comments.Sep 8 2021, 3:54 AM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
3921

Yes it should definitely be SETGE here!

uabelho added inline comments.Sep 8 2021, 3:57 AM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
3921

When it goes wrong for me the above code rewrites

  t22: i32 = sra t20, Constant:i16<31>, foo.c:15:24
t29: i16 = setcc t22, Constant:i32<-1>, setne:ch, foo.c:15:22

into

t36: i16 = setcc t20, Constant:i32<0>, setgt:ch, foo.c:15:22

t20 is 0, so t22 is also 0 and t29 is 1.
But t36 is 0. With "setge" instead of "setgt" t36 would be 1.

dmgreen added inline comments.Sep 8 2021, 3:58 AM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
3921

Oh yeah. Does changing it to SETGE fix the miscompile?

dmgreen added inline comments.Sep 8 2021, 4:01 AM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
3921

OK. I'll switch that to the correct thing. Thanks for the report.

uabelho added inline comments.Sep 8 2021, 4:04 AM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
3921

Yep it does

uabelho added inline comments.Sep 8 2021, 4:05 AM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
3921

Sounds good! Thanks!

d8d24c64fe21 hopefully fixes the issue. Let me know if not.

d8d24c64fe21 hopefully fixes the issue. Let me know if not.

Yes, it works now. Thanks!