This is an archive of the discontinued LLVM Phabricator instance.

[AArch64][SVE] Fix bad PTEST(X, X) optimization
ClosedPublic

Authored by c-rhodes on Nov 9 2022, 7:51 AM.

Details

Summary

AArch64InstrInfo::optimizePTestInstr attempts to remove a PTEST of a
predicate generating operation that identically sets flags (implictly).

When the mask is the same as the input predicate the PTEST is currently
removed. This is incorrect since the mask for the implicit PTEST
performed by the flag-setting instruction differs from the mask
specified to the explicit PTEST and could set different flags.

For example, consider

PG=<1, 1, x, x>
Z0=<1, 2, x, x>
Z1=<2, 1, x, x>

X=CMPLE(PG, Z0, Z1)
 =<0, 1, x, x>       NZCV=0xxx
PTEST(X, X),         NZCV=1xxx

where the first active flag (bit 'N' in NZCV) is set by the explicit
PTEST, but not by the implicit PTEST as part of the compare. Given the
PTEST mask and source are the same however, first is equivalent to any,
so the PTEST could be removed if the condition is changed. The same
applies to last active. It is safe to remove the PTEST for any active,
but this information isn't available in the current optimization.

This patch fixes the bad optimization, a later patch will implement the
optimization proposed above and fix the any active case.

Diff Detail

Event Timeline

c-rhodes created this revision.Nov 9 2022, 7:51 AM
c-rhodes requested review of this revision.Nov 9 2022, 7:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 9 2022, 7:51 AM
Matt added a subscriber: Matt.Nov 11 2022, 7:28 PM
bsmith accepted this revision.Nov 14 2022, 3:04 AM
bsmith added a subscriber: bsmith.

LGTM, modulo future changes.

This revision is now accepted and ready to land.Nov 14 2022, 3:04 AM
This revision was landed with ongoing or failed builds.Nov 15 2022, 3:59 AM
This revision was automatically updated to reflect the committed changes.