llvm-project
69780be1 - [AggressiveInstCombine] Recognize table based log2 and replace with ctlz+sub. (#185160)

Commit
45 days ago
[AggressiveInstCombine] Recognize table based log2 and replace with ctlz+sub. (#185160) Recognize table based log2 implementations like ``` unsigned log2(unsigned v) { static const unsigned char table[] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 }; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; return table[(unsigned)(v * 0x07C4ACDDU) >> 27]; } ``` and replaces with 31 - llvm.ctlz(v). Similar for i64 log2. Other sizes can be supported with correct multiply constant and table values, but I have not found examples yet. This code is based on the existing tryToRecognizeTableBasedCttz. Like that function, we support any combination of multiply constant and table values that produce the correct result. It handles the same pattern as #177110, but does not match the outer subtract from that patch. It is assumed that InstCombine or other optimizations can combine (sub 31 (sub 31, cttz V)) later. I have limited this to targets that have a fast ctlz. The backend does not yet have a table based lowering for ctlz so this reduces the chance of regressions.
Author
Parents
Loading