RFR: 8376220: C2: Refactor the logic to in MemNode::find_previous_store [v4]

Quan Anh Mai qamai at openjdk.org
Sat Jan 24 18:53:53 UTC 2026


On Sat, 24 Jan 2026 18:43:15 GMT, Quan Anh Mai <qamai at openjdk.org> wrote:

>> Hi,
>> 
>> This patch refactors the logic in `MemNode::find_previous_store` and makes a small improvement to `MemNode::detect_ptr_independence`. An IR test accompanies the improvement.
>> 
>> Please take a look and share your thoughts, thanks a lot.
>
> Quan Anh Mai has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Test description

src/hotspot/share/opto/memnode.cpp line 1236:

> 1234:       // LoadVector/StoreVector needs additional check to ensure the types match.
> 1235:       if (st->is_StoreVector()) {
> 1236:         // Some kind of masked access or gather/scatter

This condition is insufficient to determine if `this` inspects the same memory as `st`. Luckily, `LoadVectorMasked`, `LoadVectorGather`, and `LoadVectorGatherMasked` all have `store_Opcode()` being `-1`, preventing any folding with them. On the other hand, `LoadVector` has `store_Opcode()` being `Op_StoreVector`, so the only case here turns out the be correct. However, it is better to be precise here.

src/hotspot/share/opto/memnode.cpp line 3565:

> 3563:       val->in(MemNode::Memory )->eqv_uncast(mem) &&
> 3564:       val->as_Load()->store_Opcode() == Opcode()) {
> 3565:     if (!is_StoreVector()) {

This condition here is also insufficient. But again, similar to above, only `LoadVector` has a valid `store_Opcode()`, and it is `Op_StoreVector`. Furthermore, we mistakenly check `mem->is_LoadVector()`, which is always `false`, so there is no chance of mis-optimization.

src/hotspot/share/opto/memnode.cpp line 3567:

> 3565:     if (!is_StoreVector()) {
> 3566:       result = mem;
> 3567:     } else if (Opcode() == Op_StoreVector && val->Opcode() == Op_LoadVector &&

It should be possible to merge the check here and the check below. However, `LoadVectorNode` does not expose `indices` and `mask` the same way `StoreVectorNode` does. In addition, we need to verify the correctness thoroughly when changing `store_Opcode()` of `LoadVectorMask`, `LoadVectorGather`, and `LoadVectorGatherMasked` so as not to introduce any mis-optimization. As a result, I think it should be left to another PR.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/29390#discussion_r2724484199
PR Review Comment: https://git.openjdk.org/jdk/pull/29390#discussion_r2724485057
PR Review Comment: https://git.openjdk.org/jdk/pull/29390#discussion_r2724491193


More information about the hotspot-compiler-dev mailing list