cse: Check volatile memory in cselib_redundant_set_p

For h8300-elf, gcc.dg/pr114768.c fails when compiled with -O2 -msx since
cselib_redundant_set_p returns true when called with

(gdb) call debug (set)
(set (mem:HI (reg/f:SI 0 r0 [orig:21 p ] [21]) [1 *p_3(D)+0 S2 A16])
    (mem/v:HI (reg/f:SI 0 r0 [orig:21 p ] [21]) [1 MEM[(volatile int *)p_3(D)]+0 S2 A16]))
(gdb)

from reload_cse_regs.  Update cselib_redundant_set_p to return false for
volatile memory source or destination.

gcc/

	PR target/122343
	* cselib.cc (cselib_redundant_set_p): Return false for volatile
	memory source or destination.

gcc/testsuite/

	PR target/122343
	* gcc.dg/pr122343-1.c: New test.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
This commit is contained in:
H.J. Lu
2025-12-17 12:18:15 +08:00
parent 278eb0a081
commit 4fbe8f4438
2 changed files with 15 additions and 2 deletions

View File

@@ -1194,7 +1194,9 @@ cselib_redundant_set_p (rtx set)
if (cselib_reg_set_mode (dest) != GET_MODE (dest))
return false;
if (!rtx_equal_for_cselib_p (dest, SET_SRC (set)))
rtx src = SET_SRC (set);
if ((MEM_P (src) && MEM_VOLATILE_P (src))
|| !rtx_equal_for_cselib_p (dest, src))
return false;
while (GET_CODE (dest) == SUBREG
@@ -1205,6 +1207,9 @@ cselib_redundant_set_p (rtx set)
if (!flag_strict_aliasing || !MEM_P (dest))
return true;
if (MEM_VOLATILE_P (dest))
return false;
/* For a store we need to check that suppressing it will not change
the effective alias set. */
rtx dest_addr = XEXP (dest, 0);
@@ -1242,7 +1247,6 @@ cselib_redundant_set_p (rtx set)
/* We failed to find a recorded value in the cselib history, so try
the source of this set; this catches cases such as *p = *q when p
and q have the same value. */
rtx src = SET_SRC (set);
while (GET_CODE (src) == SUBREG)
src = XEXP (src, 0);

View File

@@ -0,0 +1,9 @@
/* { dg-do compile } */
/* { dg-options "-O2 -fdump-rtl-final" } */
/* { dg-final { scan-rtl-dump "\\\(mem/v:" "final" } } */
void
foo (int *p)
{
*(volatile int *) p = *p;
}