On 10/7/26 7:21 AM, [email protected] wrote:
From: Kyrylo Tkachov <[email protected]>

cselib_lookup_mem looks for a value of the mode of X on the addr_list of
the value of the address of X.  It accepts a value if one of its
locations is a MEM in the address space of X, but it does not check that
this MEM is at the address of X.  A store in another address space can
put a value on the addr_list, and the value can also have a location at
another address in the address space of X.  For example, on x86:

   static const long tab[4] = { 1, 2, 3, 4 };

   long
   f (int i, long __seg_gs *p)
   {
     long *q = (long *) (__UINTPTR_TYPE__) p;
     *p = tab[i];
     return *q;
   }

The %gs store puts the value of TAB[I] on the addr_list of P.  The
generic load from Q then finds it through its location in TAB, and
postreload replaces the load with a register.  With -O2 -fno-dse
(without -fno-dse, a similar bug in DSE removes the load earlier):

        movslq  %edi, %rdi
        movq    tab(,%rdi,8), %rax
        movq    %rax, %gs:(%rsi)
        ret

Require the MEM location to be at the address of X too.

Bootstrapped and tested on aarch64-none-linux-gnu and x86_64-linux.
Ok for trunk?
Thanks,
Kyrill

Cc: Jeff Law <[email protected]>

gcc/ChangeLog:

        * cselib.cc (cselib_has_mem_loc_p): New function.
        (cselib_lookup_mem): Use it to also check the address of the
        memory location.

gcc/testsuite/ChangeLog:

        * gcc.target/i386/addr-space-9.c: New test.

Signed-off-by: Kyrylo Tkachov <[email protected]>
---
New in v2.  Patch 2/4 uses the same check for its new query.  v1 of
that patch had the same problem.
I found this  explanation confusing.

If I'm understanding things correctly, we can have multiple instances of the same address on the addr_list.  It's just an address, not a full MEM, so we don't have address space information.  Right?

Once we've found the right addr_list, we have to walk the values list since that's the one that will carry the actual MEM expression where we've got access to the address space?  For that second case we need to check both the underlying address as well as the address space?

Can you reword the cover letter to be a bit clearer, assuming I'm right?  Assuming I'm right, I think the implementation is OK (thought one might legitimately ask if we should have distinct addr_lists for different address spaces).

jeff


Reply via email to