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

Quan Anh Mai qamai at openjdk.org
Wed Aug 6 12:42:12 UTC 2025


On Wed, 6 Aug 2025 08:24:45 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 nine additional commits since the last revision:
> 
>  - Merge branch 'master' into enhance-clz-type
>  - Add checks for results of all test methods
>  - Replace `isa_*` with `is_*` and add checks for `Type::BOTTOM`
>  - 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

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

> 48: //------------------------------Value------------------------------------------
> 49: const Type* CountLeadingZerosINode::Value(PhaseGVN* phase) const {
> 50:   // If the input is TOP, the result is also TOP.

IMO this comment is unnecessary, it is trivial to infer from the code below.

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

> 55: 
> 56:   // If the input is BOTTOM, the result is the local BOTTOM.
> 57:   if (t == Type::BOTTOM) {

`t` should not be `Type::BOTTOM`, please remove this case.

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

> 60: 
> 61:   const TypeInt* ti = t->is_int();
> 62:   if (ti->is_con()) {

This is unnecessary, if `ti` is a constant then `~ti->_bits._zeros == ti->_bits._ones` and the below case will return a constant anyway.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/25928#discussion_r2257016904
PR Review Comment: https://git.openjdk.org/jdk/pull/25928#discussion_r2257017845
PR Review Comment: https://git.openjdk.org/jdk/pull/25928#discussion_r2257019909


More information about the hotspot-compiler-dev mailing list