https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128136

liu zhenyu <lzy20000419 at outlook dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lzy20000419 at outlook dot com

--- Comment #2 from liu zhenyu <lzy20000419 at outlook dot com> ---
The load is replaced in RTL DSE (dse1), by the read-forwarding path in
check_mem_read_rtx:

  gcc/dse.cc (14.2: ~line 2378; same on trunk), the cselib-based read path:
      if (!used_in_call
          && store_info->rhs
          && store_info->group_id == -1
          && store_info->cse_base == base
          && known_subrange_p (offset, width, store_info->offset,
store_info->width)
          && all_positions_needed_p (...)
          && replace_read (store_info, i_ptr, read_info, insn_info, loc))

canon_address () derives the cselib base from the address alone, so %fs:(p) and
%gs:(p) get the same base. The PR102733 fix
(r14-1508) added store_info::addrspace, but only compares it in record_store
(store kills store), not here. -fdump-rtl-dse1-details shows
"processing cselib load mem:(mem:SI (reg/v:DI 101 [ p ]) [... AS2])" ...
"replaced the loaded MEM with (reg 104)".

Adding the missing check fixes the testcase at -O1/-O2/-O3/-Os (tested on a
14.2 build; the same code is on trunk):

--- a/gcc/dse.cc
+++ b/gcc/dse.cc
@@ check_mem_read_rtx
              && store_info->group_id == -1
              && store_info->cse_base == base
+             && store_info->addrspace == MEM_ADDR_SPACE (mem)
              && known_subrange_p (offset, width, store_info->offset,

The same path also gives these related results (all fixed by the line above):
  int g (unsigned long p) { *(int __seg_fs *)p = 1; *(int __seg_gs *)p = 2;
return *(int __seg_fs *)p; }   // returns 2, not the fs value
  int d (unsigned long p) { *(int __seg_gs *)p = 1; return *(int *)p; }        
                          // generic load forwarded from a gs store
  int i (unsigned long p) { *(long __seg_fs *)p = 0x100000002L; return *(int
__seg_gs *)p; }              // forwarded via lowpart
  int a (void) { *(int __seg_fs *)16 = 1; return *(int __seg_gs *)16; }        
                           // constant address: not a const group
                                                                               
                           // because the AS is non-generic
(A run-time harness with arch_prctl(ARCH_SET_GS), like the one in comment 0,
shows wrong values at -O1..-Os and correct ones with -fno-dse;
godbolt trunk agrees.)

Disclaimer: This test case was generated by a fuzzer, and the report was
written with LLM assistance.

Reply via email to