[TargetLowering][RISCV] Make expandCTLZ work for non-power of 2 types.
authorCraig Topper <craig.topper@sifive.com>
Sat, 9 Jul 2022 23:40:52 +0000 (16:40 -0700)
committerCraig Topper <craig.topper@sifive.com>
Tue, 12 Jul 2022 18:36:37 +0000 (11:36 -0700)
commit8eaf00e04dba1b98acf8031b61d9488387a1066e
tree470d8e3bcee8c995467d0379ed362c7309cf2602
parent866be0aa8ae40913a9ad999d9b4c525d3f15a129
[TargetLowering][RISCV] Make expandCTLZ work for non-power of 2 types.

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.

Differential Revision: https://reviews.llvm.org/D129431
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
llvm/test/CodeGen/RISCV/pr56457.ll