This is an archive of the discontinued LLVM Phabricator instance.

[AArch64][GlobalISel] Fold G_ADD into the cset for G_ICMP
ClosedPublic

Authored by paquette on Feb 9 2021, 5:14 PM.

Details

Summary

When we have a G_ADD which is fed by a G_ICMP on one side, we can fold it into the cset for the G_ICMP.

e.g. Given

%cmp = G_ICMP ... %x, %y
%add = G_ADD %cmp, %z

We would normally emit a cmp, cset, and add.

However, %add is either %z or %z + 1. So, we can just use %z as the source of the cset rather than wzr, saving an instruction.

This would probably be cleaner in AArch64PostLegalizerLowering, but we'd need to change the way we represent G_ICMP to do that, I think. For now, it's easiest to implement in selection.

This is a 0.1% code size improvement on CTMark/pairlocalalign at -Os.

Example: https://godbolt.org/z/7KdrP8

Diff Detail

Event Timeline

paquette created this revision.Feb 9 2021, 5:14 PM
paquette requested review of this revision.Feb 9 2021, 5:14 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 9 2021, 5:14 PM
aemerson accepted this revision.Feb 10 2021, 10:32 AM
aemerson added inline comments.
llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp
2183

What if the type is s64? emitCSetForICMP seems to only work for s32.

This revision is now accepted and ready to land.Feb 10 2021, 10:32 AM
aemerson requested changes to this revision.Feb 10 2021, 10:33 AM
This revision now requires changes to proceed.Feb 10 2021, 10:33 AM
paquette added inline comments.Feb 10 2021, 10:42 AM
llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp
2183

I don't think that should happen, because G_ICMP can only output a s32. So, any G_ADD fed by it should take in + produce a s32.

That being said, it's probably better to early exit if we don't have a s32 anyway.

(And maybe emitCSetForICMP should be changed to a general purpose emitCSet function.)

paquette updated this revision to Diff 322750.Feb 10 2021, 10:53 AM

Check the G_ADD type before performing the optimization, and add a vector test.

aemerson accepted this revision.Feb 10 2021, 1:12 PM
This revision is now accepted and ready to land.Feb 10 2021, 1:12 PM