This is an archive of the discontinued LLVM Phabricator instance.

[AArch64] Rewrite (add, csel) to cinc
ClosedPublic

Authored by SjoerdMeijer on Mar 16 2021, 6:13 AM.

Details

Summary

Don't rewrite an add instruction with 2 SET_CC operands into a csel instruction. The total instruction sequence uses an extra instruction and register. Preventing this, allows us to match a (add, csel) pattern and rewrite this into a cinc, see the new test case in arm64-csel.ll.

Diff Detail

Event Timeline

SjoerdMeijer created this revision.Mar 16 2021, 6:13 AM
SjoerdMeijer requested review of this revision.Mar 16 2021, 6:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 16 2021, 6:13 AM
SjoerdMeijer edited the summary of this revision. (Show Details)Mar 16 2021, 6:14 AM

Should we be generating:

cmp
cset
cmp
csinc

That lets the csinc do the add, if it's only going to be adding 0/1. Maybe that's possible with an extra isel pattern from add(cset(..))?

Thanks for the suggestion Dave. Just did the pen and paper exercise and agree that:

cmp   w8, #3
cset  w8, hi
cmp   w9, #33
csinc w0, w8, w8, <=

is even better. :-) Also agreed that a match pattern would be nice, so looking into that now.

SjoerdMeijer retitled this revision from [AArch64] Don't perform SetccAddFolding with 2 SET_CC operands to [AArch64] Rewrite (add, csel) to cinc.
SjoerdMeijer edited the summary of this revision. (Show Details)

This now generates cinc, which is even better.

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
13193

I had to keep this, otherwise this lowering will happen first and the pattern, see below, doesn't match,.

llvm/lib/Target/AArch64/AArch64InstrInfo.td
2167

I had to add an zext for this pattern, that's because the way setcc is legalised from an i1 to i32 first, then zext'ed to i64.

dmgreen accepted this revision.Mar 17 2021, 10:47 AM

Thanks. LGTM

The lowering/optimization of CSEL/CSINC/etc feels a bit weak to me, so any improvements are good to see. Tablegen improvements especially as (as far as I understand) they can help in both SelectionDag and GlobalISel.

This revision is now accepted and ready to land.Mar 17 2021, 10:47 AM

Yeah, thanks Dave, I think I will be looking a bit more in this area, but this is a start...

This revision was landed with ongoing or failed builds.Mar 18 2021, 1:49 AM
This revision was automatically updated to reflect the committed changes.