This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Identify partial bitreverses
Needs ReviewPublic

Authored by jmolloy on Dec 14 2015, 7:45 AM.

Details

Summary

It could be beneficial to emit a bitreverse intrinsic even when some of the result of that intrinsic need to be masked out. For example, assume we have a bitreverse but just without some bits:

int4_t b = 0;
if (a & 1) b |= 8;
if (a & 2) b |= 4;
// (a & 4) -> (b |= 2) is missing
if (a & 8) b |= 1;

Here, we can simply perform the bitreverse and mask: (res &= ~2).

This patch provides support for this. Obviously, doing this unconditionally would not be a good idea. A single bit move that happens to be correct for a bitreversal will cause a bitreverse (which may not be cheap on the target) and mask to be emitted. Therefore in this patch we heuristically say that if a bitreverse would require more than half its bits masked out not to bother. Perhaps this should be a target hook, depending on whether bitreverse is cheap or not?

Diff Detail

Repository
rL LLVM

Event Timeline

jmolloy updated this revision to Diff 42720.Dec 14 2015, 7:45 AM
jmolloy retitled this revision from to [InstCombine] Identify partial bitreverses.
jmolloy updated this object.
jmolloy added reviewers: hfinkel, sbaranga, sanjoy.
jmolloy set the repository for this revision to rL LLVM.
jmolloy added subscribers: majnemer, llvm-commits.

Optimistic Christmas Season Ping!

sanjoy edited edge metadata.Dec 22 2015, 1:08 AM

I have a couple of (possibly naive) questions.

I'm not familiar with this code, so while I can help review it, I think the final LGTM should come from someone else more familiar with this code.

lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
1584

Should this be "false if not"?

1765

I don't understand the logic here. If BitProvenance[i] == i then I suppose that means the ith bit of the output is the ith bit of V. How does that bit end up in the instruction you return?

1782

Will teaching CollectBitParts about the bswap and bitreverse intrinsics have the same effect. If so, that sounds slightly better.

1790–1799

Might want to use Builder->CreateCall here, so that the call instruction gets enqueued in the worklist.

jmolloy marked an inline comment as done.Jan 4 2016, 9:38 AM

Hi Sanjoy,

Thanks for the review! New version coming up.

James

lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
1584

I honestly don't know. The problem is that would be a double negative ("if not unsuccessful").

The best thing to do here is for me to rewrite this function to be sane and return true on success and false on failure. I'll do this in a dedicated followup.

1765

In a partial bitreverse/bswap, some of the bits are going to be the same as the original value. We're essentially saying that the result is a OR-ed mask of a reversed value and the original value:

uint8_t a;
uint8_t b = bitreverse(a);
uint8_t c = a & M | b & ~M

This can come from something like:

uint8_t dest = src; // In a pure bitreverse, dest would be initialized to zero.
if (src & 1<<0) dest |= 1<<7;
if (src & 1<<1) dest |= 1<<6;
...
1782

Indeed, that sounds much better. My new patch does this.

jmolloy updated this revision to Diff 43893.Jan 4 2016, 9:38 AM
jmolloy edited edge metadata.
sanjoy added inline comments.Jan 17 2016, 1:25 PM
lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
1765

In a partial bitreverse/bswap, some of the bits are going to be the same as the original value. We're essentially saying that the result is a OR-ed mask of a reversed value and the original value:

Right, and I don't see where you're creating that OR -- all I see is the bitreverse(a) & ~M part. If I understand the scheme correctly and am not misreading the code, you'll have to keep two masks -- IgnoreMask (set if BitProvenance[i] == -1) and IdentityMask (set if BitProvenance[i] == i), and then return bitreverse(a) & ~IgnoreMask | a & IdentityMask.

(I assume BitProvenance[i] == -1 means "bit i is always 0 in the output"?)

sanjoy requested changes to this revision.Feb 15 2016, 4:26 PM
sanjoy edited edge metadata.

Getting this out of my queue for now. If my last comment was incorrect (and I'm misreading the code or something), please let me know.

This revision now requires changes to proceed.Feb 15 2016, 4:26 PM
sanjoy resigned from this revision.Jan 29 2022, 5:25 PM
This revision now requires review to proceed.Jan 29 2022, 5:25 PM