RFR: 8336464: C2: Force CastX2P to be a two-address instruction

Dean Long dlong at openjdk.org
Mon Aug 5 19:30:30 UTC 2024


On Fri, 12 Jul 2024 13:59:23 GMT, Fei Gao <fgao at openjdk.org> wrote:

> This patch forces `CastX2P` to be a two-address instruction, so that C2 could allocate the same register for `dst` and `src`. Then we can remove the instruction completely in the assembly.
> 
> The motivation comes from some cast operations like `castPP`. The difference for ADLC between `castPP` and `CastX2P` lies in that `CastX2P` always has different types for `dst` and `src`. We can force ADLC to generate an extra `two_adr()` for `CastX2P` like it does automatically for `castPP`, which could tell register allocator that the instruction needs the same register for `dst` and `src`.
> 
> However, sometimes, RA and GCM in C2 can't work as we expected.
> 
> For example, we have Assembly on the existing code:
> 
>   ldp    x10, x11, [x17,#136]
>   add    x10, x10, x15
>   add    x11, x11, x10
>   ldr    x12, [x17,#152]
>   str    x16, [x10]
>   add    x10, x12, x15
>   str    x16, [x11]
>   str    x16, [x10]
> 
> 
> After applying the patch independently, the assembly is:
> 
>   ldr    x10, [x16,#136]  <--- 1
>   add    x10, x10, x15
>   ldr    x11, [x16,#144]  <--- 2
>   mov    x13, x10         <--- 3
>   str    x17, [x13]
>   ldr    x12, [x16,#152]
>   add    x10, x11, x10
>   str    x17, [x10]
>   add    x10, x12, x15
>   str    x17, [x10]
> 
> 
> C2 generates a totally extra `mov`, see 3, and we even lost the chance to merge load pair, see 1 and 2. That's terrible.
> 
> Although this scenario would disappear after combining with https://github.com/openjdk/jdk/pull/20157, I'm still not sure if this patch is worthwhile.

I thought reusing one of the inputs for the destination is the default, and we have to add TEMP to rules to prevent this from happening.  So I don't understand why sometimes the register allocator doesn't reuse the register when there are no other uses.  There is a concept of "chain rule" in ADLC that I don't quite understand, but I suspect that it is related.

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

PR Comment: https://git.openjdk.org/jdk/pull/20159#issuecomment-2269762174


More information about the hotspot-compiler-dev mailing list