This is an archive of the discontinued LLVM Phabricator instance.

[X86] Use carry flag from add for (seteq (add X, -1), -1).
ClosedPublic

Authored by craig.topper on Dec 30 2019, 2:53 PM.

Details

Summary

If we just subtracted 1 and are checking if the result is -1. We can use the carry flag from the ADD instead of an explicit CMP. I'm using the same checks for the add users as EmitTest.

Fixes PR44412

Diff Detail

Event Timeline

craig.topper created this revision.Dec 30 2019, 2:53 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 30 2019, 2:53 PM
Herald added a subscriber: hiraditya. · View Herald Transcript

Invert the condition. Despite what the PR said, it should be jb to the top of the loop not jae. The original code condition was jne on a compare with -1. If the add produce a value != -1, then the carry flag is set since -1 plus anything but 0 will have unsigned overflow and produce a value other than -1. Only 0+-1 produces -1 and it does it without overflowing.

spatel added inline comments.Dec 31 2019, 7:00 AM
llvm/lib/Target/X86/X86ISelLowering.cpp
21703

Make this a static helper, so we're not duplicating the code in EmitTest()?
Also, can use a for-range loop?

for (SDNode *User : Op->uses())
llvm/test/CodeGen/X86/pr44412.ll
29–30

Add the sibling test to produce 'jae' by inverting the icmp or the branch labels?

Address review comments

spatel accepted this revision.Dec 31 2019, 1:52 PM

LGTM

This revision is now accepted and ready to land.Dec 31 2019, 1:52 PM
xbolva00 accepted this revision.Dec 31 2019, 1:59 PM

lg too

This revision was automatically updated to reflect the committed changes.