This is an archive of the discontinued LLVM Phabricator instance.

[X86][SSE] Handle basic inversion of PTEST/TESTP operands (PR38522)
ClosedPublic

Authored by RKSimon on Mar 28 2020, 5:32 AM.

Details

Summary

PTEST/TESTP sets EFLAGS as:
TESTZ: ZF = (Op0 & Op1) == 0
TESTC: CF = (~Op0 & Op1) == 0
TESTNZC: ZF == 0 && CF == 0

If we are inverting the 0'th operand of a PTEST/TESTP instruction we can adjust the comparisons to correct handle the inversion implicitly.

Additionally, for "TESTZ" (ZF) cases, the allones case, PTEST(X,-1) can be simplified to PTEST(X,X).

We can expand this for the TEST(X,~Y) pattern and also handle KTEST/KORTEST as well but I wanted to get some initial feedback first.

Diff Detail

Event Timeline

RKSimon created this revision.Mar 28 2020, 5:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 28 2020, 5:32 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
RKSimon planned changes to this revision.Mar 28 2020, 5:37 AM

sorry, git stash screwed up - fixed version shortly.

RKSimon updated this revision to Diff 253332.Mar 28 2020, 5:41 AM

fixed merge

This feels subject to DAG combine ordering not folding the cmov+setcc before this combine gets a chance to run. Would this be simpler if we just did it at the intrinsic lowering stage?

This feels subject to DAG combine ordering not folding the cmov+setcc before this combine gets a chance to run. Would this be simpler if we just did it at the intrinsic lowering stage?

The problem we're going to hit is that most of the xor(X,-1) inversions cases are coming from comparison lowering - PR38522 in particular hits this. What about if I extend the CondCode cases handled? I was thinking about adding a thorough 'X86::GetComplementCondCode' helper, and possibly one that checks which flag bits are read.

Is this something we can handle in combineSetCCEFLAGS?

RKSimon updated this revision to Diff 253892.Mar 31 2020, 8:19 AM

Move combine inside combineSetCCEFLAGS

craig.topper added inline comments.Mar 31 2020, 12:22 PM
llvm/lib/Target/X86/X86ISelLowering.cpp
39858

Can we also handle COND_BE? Not sure if its possible for COND_A to get inverted before we get here

RKSimon updated this revision to Diff 254009.Mar 31 2020, 2:49 PM

Rebased tests and added COND_BE testnzc case.

RKSimon updated this revision to Diff 254010.Mar 31 2020, 2:52 PM

Tweaked new ptestnzc_256_invert0_commute test for X86::COND_BE case

This revision is now accepted and ready to land.Mar 31 2020, 3:00 PM
This revision was automatically updated to reflect the committed changes.