[PATCH] D137716: [AArch64][SVE] Fix bad PTEST(PG, OP(PG, ...)) optimization

Cullen Rhodes via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 9 07:42:19 PST 2022


c-rhodes created this revision.
c-rhodes added reviewers: paulwalker-arm, peterwaller-arm.
Herald added subscribers: psnobl, hiraditya, kristof.beyls, tschuett.
Herald added a reviewer: efriedma.
Herald added a project: All.
c-rhodes requested review of this revision.
Herald added a project: LLVM.

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

When the PTEST and the predicate-generating operation use the same mask
the PTEST is currently removed. This is incorrect since it doesn't
consider element size. PTEST operates on 8-bit predicates, but for
instructions like compare that also support 16/32/64-bit predicates, the
implicit PTEST performed by the instruction will consider fewer lanes
for these element sizes and could set different first or last active
flags.

For example, consider the following instruction sequence

    ptrue p0.b			; P0=1111-1111-1111-1111
    index z0.s, #0, #1		; Z0=<0,1,2,3>
    index z1.s, #1, #1		; Z1=<1,2,3,4>
    cmphi p1.s, p0/z, z1.s, z0.s  ; P1=0001-0001-0001-0001
  				;       ^ last active
    ptest p0, p1.b		; P1=0001-0001-0001-0001
  				;     ^ last active

where the compare generates a canonical all active 32-bit predicate (equivalent
to 'ptrue p1.s, all'). The implicit PTEST sets the last active flag, whereas
the PTEST instruction with the same mask doesn't.

This patch restricts the optimization to instructions operating on 8-bit
predicates. One caveat is the optimization is safe regardless of element
size for any active, this will be addressed in a later patch.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137716

Files:
  llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
  llvm/test/CodeGen/AArch64/sve-ptest-removal-cmpeq.ll
  llvm/test/CodeGen/AArch64/sve-ptest-removal-cmpeq.mir
  llvm/test/CodeGen/AArch64/sve-ptest-removal-cmpge.ll
  llvm/test/CodeGen/AArch64/sve-ptest-removal-cmpgt.ll
  llvm/test/CodeGen/AArch64/sve-ptest-removal-cmphi.ll
  llvm/test/CodeGen/AArch64/sve-ptest-removal-cmphs.ll
  llvm/test/CodeGen/AArch64/sve-ptest-removal-cmple.ll
  llvm/test/CodeGen/AArch64/sve-ptest-removal-cmplo.ll
  llvm/test/CodeGen/AArch64/sve-ptest-removal-cmpls.ll
  llvm/test/CodeGen/AArch64/sve-ptest-removal-cmplt.ll
  llvm/test/CodeGen/AArch64/sve-ptest-removal-cmpne.ll
  llvm/test/CodeGen/AArch64/sve-ptest-removal-pfirst-pnext.ll
  llvm/test/CodeGen/AArch64/sve-setcc.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137716.474271.patch
Type: text/x-patch
Size: 21983 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221109/2db2af6b/attachment.bin>


More information about the llvm-commits mailing list