RFR: 8360192: C2: Make the type of count leading/trailing zero nodes more precise [v5]

Jasmine Karthikeyan jkarthikeyan at openjdk.org
Mon Aug 4 16:54:09 UTC 2025


On Wed, 23 Jul 2025 09:31:18 GMT, Qizheng Xing <qxing at openjdk.org> wrote:

>> The result of count leading/trailing zeros is always non-negative, and the maximum value is integer type's size in bits. In previous versions, when C2 can not know the operand value of a CLZ/CTZ node at compile time, it will generate a full-width integer type for its result. This can significantly affect the efficiency of code in some cases.
>> 
>> This patch makes the type of CLZ/CTZ nodes more precise, to make C2 generate better code. For example, the following implementation runs ~115% faster on x86-64 with this patch:
>> 
>> 
>> public static int numberOfNibbles(int i) {
>>   int mag = Integer.SIZE - Integer.numberOfLeadingZeros(i);
>>   return Math.max((mag + 3) / 4, 1);
>> }
>> 
>> 
>> Testing: tier1, IR test
>
> Qizheng Xing has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains six additional commits since the last revision:
> 
>  - Merge branch 'master' into enhance-clz-type
>  - Move `TestCountBitsRange` to `compiler.c2.gvn`
>  - Fix null checks
>  - Narrow type bound
>  - Use `BitsPerX` constant instead of `sizeof`
>  - Make the type of count leading/trailing zero nodes more precise

This is nice! I just have a comment on the unit test.

test/hotspot/jtreg/compiler/c2/gvn/TestCountBitsRange.java line 43:

> 41:     static int i = RunInfo.getRandom().nextInt();
> 42:     static long l = RunInfo.getRandom().nextLong();
> 43: 

It would be nice to also check the return values of the functions with a non-compiled version, so that we can make sure that the constant folding results are correct as well.

-------------

PR Review: https://git.openjdk.org/jdk/pull/25928#pullrequestreview-3085007943
PR Review Comment: https://git.openjdk.org/jdk/pull/25928#discussion_r2252040764


More information about the hotspot-compiler-dev mailing list