This is an archive of the discontinued LLVM Phabricator instance.

[AArch64] Convert CMP/SELECT sign patterns to OR & ASR.
ClosedPublic

Authored by fhahn on Feb 12 2021, 5:47 AM.

Details

Summary

ICMP & SELECT patterns extracting the sign of a value can be simplified
to OR & ASR (see https://alive2.llvm.org/ce/z/Xx4iZ0).

This does not save any instructions in IR, but it is profitable on
AArch64, because we need at least 2 extra instructions to materialize 1
and -1 for the SELECT.

The improvements result in ~5% speedups on loops of the form

static int sign_of(int x) {
  if (x < 0) return -1;
  return 1;
}

void foo(const int *x, int *res, int cnt) {
  for (int i=0;i<cnt;i++)
    res[i] = sign_of(x[i]);
}

Diff Detail

Event Timeline

fhahn created this revision.Feb 12 2021, 5:47 AM
fhahn requested review of this revision.Feb 12 2021, 5:47 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 12 2021, 5:47 AM
dmgreen added inline comments.Feb 14 2021, 2:09 AM
llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
6719

auto -> ValueType

6720

auto -> SDValue

6722

I don't think this needs getFixedValue()

6723

auto -> SDValue (or just return directly)

14990–14991

SetCC-> -> SetCC.

15004

VT.getSimpleVT().SimpleTy, then I think you can drop all the extra MVT()'s.

15007

These is a ISD::isConstantSplatVectorAllOnes, but I don't know if there is one for "IsOne"

fhahn updated this revision to Diff 323758.Feb 15 2021, 7:54 AM

Addressed comments, thanks!

fhahn updated this revision to Diff 323762.Feb 15 2021, 8:07 AM
fhahn marked 6 inline comments as done.

Updated to use isConstantSplatVector

fhahn marked an inline comment as done.Feb 15 2021, 8:07 AM
fhahn added inline comments.
llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
15007

I updated the code to use isConstantSplatVectorAllOnes and isConstantSplatVector for the OneValue cause.

dmgreen accepted this revision.Feb 16 2021, 8:51 AM

Thanks

This revision is now accepted and ready to land.Feb 16 2021, 8:51 AM
This revision was automatically updated to reflect the committed changes.
fhahn marked an inline comment as done.