This is an archive of the discontinued LLVM Phabricator instance.

[X86] Use GF2P8AFFINEQB to implement vector bitreverse.

Authored by craig.topper on Nov 15 2020, 9:05 PM.



We can use GF2P8AFFINEQB to reverse bits in a byte. Shuffles are needed to reverse the bytes in elements larger than i8. LegalizeVectorOps takes care of inserting the shuffle for the larger element size.

We already have Custom lowering for v16i8 with SSSE3, v32i8 with AVX, and v64i8 with AVX512BW. So we only need to mark v16i8 with SSE2+GFNI as Custom.

I think we might be able to use this for scalars too by moving into a vector and back. But I'll save that for a follow up as its a little more involved.

Diff Detail

Event Timeline

craig.topper created this revision.Nov 15 2020, 9:05 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 15 2020, 9:05 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
craig.topper requested review of this revision.Nov 15 2020, 9:05 PM
craig.topper edited the summary of this revision. (Show Details)Nov 15 2020, 9:11 PM

Cheers for looking at this!

Please can you add cost model coverage as well?

FYI I raised a bug about using GFNI for counting leading/trailing bits here

The InstLatX64 guys found a load of other potential uses for GFNI at


We found it useful for XOP to handle scalar cases on the vector unit - would it be useful for GFNI as well?

Update cost model.

Not doing scalar yet. Making scalar Custom requires us to make vXi16/32/64 Custom as well as the LegalizeVectorOps Expand will scalarize if scalar is legal or custom.

RKSimon accepted this revision.Nov 17 2020, 4:04 AM

LGTM - I'm not sure whether its worth supporting this for pre-SSSE3 but technically its possible (and we already do this a lot for recent AVX512 features on nonVLX fictional hardware).

21 ↗(On Diff #305668)

These can all probably have a GFNI 'common' prefix to reduce some duplication

This revision is now accepted and ready to land.Nov 17 2020, 4:04 AM
pengfei accepted this revision.Nov 17 2020, 8:11 PM


This revision was landed with ongoing or failed builds.Nov 17 2020, 11:50 PM
This revision was automatically updated to reflect the committed changes.