This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] recognizeBSwapOrBitReverseIdiom - support for 'partial' bswap patterns (PR47191)
ClosedPublic

Authored by RKSimon on Sep 30 2020, 8:25 AM.

Details

Summary

If we're bswap'ing some bytes and zero'ing the remainder we can perform this as a bswap+mask which helps us match 'partial' bswaps as a first step towards folding into a more complex bswap pattern.

Diff Detail

Event Timeline

RKSimon created this revision.Sep 30 2020, 8:25 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 30 2020, 8:25 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
RKSimon requested review of this revision.Sep 30 2020, 8:25 AM
RKSimon added inline comments.
llvm/test/Transforms/InstCombine/bswap.ll
607

We would have reduced this to bswap+mask but InstCombinerImpl::matchBSwap is too harsh at filtering out or(A,B) patterns where A and B have different opcodes.

spatel added inline comments.Oct 1 2020, 10:26 AM
llvm/test/Transforms/InstCombine/bswap.ll
607

Add test(s) that already have a partial bswap, so we can see the most basic transform enabled by this patch?

Something like this:

define i32 @partial_bswap(i32 %x) {
  %x3 = shl i32 %x, 24
  %a2 = shl i32 %x, 8
  %x2 = and i32 %a2, 16711680
  %x32 = or i32 %x3, %x2
  %t1 = and i32 %x, -65536
  %t2 = call i32 @llvm.bswap.i32(i32 %t1)
  %r = or i32 %x32, %t2
  ret i32 %r
}
spatel added inline comments.Oct 1 2020, 10:41 AM
llvm/lib/Transforms/Utils/Local.cpp
2953–2954

I misread that the first time because I wasn't expecting the +=8 for something named ByteIndex. Coder pref, but might be better to multiply by 8 in the index calc and use normal increment in the for-loop.

RKSimon updated this revision to Diff 295641.Oct 1 2020, 11:57 AM

Addressed @spatel's comments, I've relaxed the requirements in InstCombinerImpl::matchBSwap as I'd mentioned it was necessary for an existing test and also the new partial_bswap test

Are there tests for the patterns like https://godbolt.org/z/Pzd8sf ?

Are there tests for the patterns like https://godbolt.org/z/Pzd8sf ?

Those are load combines so its unlikely we'll ever get them to work pre-DAG.

spatel accepted this revision.Oct 2 2020, 8:01 AM

LGTM

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2048–2049

The comments are now more specific than the logic now. Adjust to be more like the OrWithOrs?

// (A >> B) | C or A | (B >> C)
// (A & B) | C or A | (B & C)

Presumably, recognizeBSwapOrBitReverseIdiom() can deal with the more general patterns.

This revision is now accepted and ready to land.Oct 2 2020, 8:01 AM
RKSimon reopened this revision.Oct 2 2020, 10:27 AM
RKSimon added inline comments.
llvm/lib/Transforms/Utils/Local.cpp
3086

some clangs stage2 builds fail in this call when building lib/Object/CMakeFiles/LLVMObject.dir/ELF.cpp.o for some reason

This revision is now accepted and ready to land.Oct 2 2020, 10:27 AM