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

Chen Liang liach at openjdk.org
Mon Jun 23 23:13:31 UTC 2025


On Mon, 23 Jun 2025 07:56:48 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 incrementally with one additional commit since the last revision:
> 
>   Narrow type bound

src/hotspot/share/opto/countbitsnode.cpp line 61:

> 59:                          ti->_widen);
> 60:   }
> 61:   return TypeInt::INT;

Just curious, when would this fallback path be used?

test/hotspot/jtreg/compiler/c2/irTests/TestCountBitsRange.java line 47:

> 45:     @IR(failOn = IRNode.COUNT_LEADING_ZEROS_I)
> 46:     public boolean clzCompareInt() {
> 47:         return Integer.numberOfLeadingZeros(i) < 0 || Integer.numberOfLeadingZeros(i) > 32;

You limited the type to check the ones/zeroes of the input value, `count_leading_zeros_int(~ti->_bits._zeros), count_leading_zeros_int(ti->_bits._ones),`. I think we need test cases to cover those too, given they are more complex and thus more error prone.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/25928#discussion_r2162669197
PR Review Comment: https://git.openjdk.org/jdk/pull/25928#discussion_r2162666011


More information about the hotspot-compiler-dev mailing list