This is an archive of the discontinued LLVM Phabricator instance.

[TargetLowering][RISCV] Make expandCTLZ work for non-power of 2 types.
ClosedPublic

Authored by craig.topper on Jul 9 2022, 4:49 PM.

Details

Summary

To convert CTLZ to popcount we do

x = x | (x >> 1);
x = x | (x >> 2);
...
x = x | (x >>16);
x = x | (x >>32); // for 64-bit input
return popcount(~x);

This smears the most significant set bit across all of the bits
below it then inverts the remaining 0s and does a population count.

To support non-power of 2 types, the last shift amount must be
more than half of the size of the type. For i15, the last shift
was previously a shift by 4, with this patch we add another shift
of 8.

Fixes PR56457.

Diff Detail

Event Timeline

craig.topper created this revision.Jul 9 2022, 4:49 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 9 2022, 4:49 PM
craig.topper requested review of this revision.Jul 9 2022, 4:49 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 9 2022, 4:49 PM
reames accepted this revision.Jul 12 2022, 10:03 AM

LGTM

This revision is now accepted and ready to land.Jul 12 2022, 10:03 AM
This revision was landed with ongoing or failed builds.Jul 12 2022, 11:37 AM
This revision was automatically updated to reflect the committed changes.