https://gcc.gnu.org/g:bb273fcdc0945f3f07a03ba90a39443494563f06

commit r17-6340-gbb273fcdc0945f3f07a03ba90a39443494563f06
Author: Kyrylo Tkachov <[email protected]>
Date:   Wed Nov 11 05:12:38 2026 -0800

    aarch64: Add memory clobber to GCS stack switch patterns [PR128284]
    
    The GCSSS1 and GCSSS2 instructions validate and consume the cap token
    at the new GCS address and write a return token to the old GCS.  The
    aarch64_gcsss1 and aarch64_gcsss2 patterns are plain unspec_volatile
    with no memory effects, so RTL optimisers treat memory as unchanged
    across them.  For:
    
      uint64_t f (uint64_t *p)
      {
        uint64_t a = *p;
        __gcsss (p);
        return a ^ *p;
      }
    
    cse1 reuses the load before the switch for the load after it and the
    function folds to returning 0.
    
    Add (clobber (mem:BLK (scratch))) to both patterns, as done for other
    instructions with arbitrary memory side effects.  GCSPOPM only reads
    memory and changes no memory, so it is left unchanged.
    
    Before:
            sys     #3, c7, c7, #2, x0 // gcsss1
            mov     w0, 0
            sysl    x0, #3, c7, c7, #3 // gcsss2
            mov     w0, 0
            ret
    After:
            ldr     x2, [x0]
            sys     #3, c7, c7, #2, x0 // gcsss1
            mov     w1, 0
            sysl    x1, #3, c7, c7, #3 // gcsss2
            ldr     x0, [x0]
            eor     x0, x2, x0
            ret
    
    Bootstrapped and tested on aarch64-none-linux-gnu.
    
    gcc/ChangeLog:
    
            PR target/128284
            * config/aarch64/aarch64.md (aarch64_gcsss1): Add a memory clobber.
            (aarch64_gcsss2): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            PR target/128284
            * gcc.target/aarch64/pr128284.c: New test.
    
    Signed-off-by: Kyrylo Tkachov <[email protected]>

Diff:
---
 gcc/config/aarch64/aarch64.md               |  8 ++++++--
 gcc/testsuite/gcc.target/aarch64/pr128284.c | 16 ++++++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/gcc/config/aarch64/aarch64.md b/gcc/config/aarch64/aarch64.md
index 0fb3bc7c0e4d..9c8c24f4a709 100644
--- a/gcc/config/aarch64/aarch64.md
+++ b/gcc/config/aarch64/aarch64.md
@@ -9059,14 +9059,18 @@
 )
 
 (define_insn "aarch64_gcsss1"
-  [(unspec_volatile [(match_operand:DI 0 "register_operand" "r")] 
UNSPECV_GCSSS1)]
+  [(unspec_volatile [(match_operand:DI 0 "register_operand" "r")]
+                   UNSPECV_GCSSS1)
+   (clobber (mem:BLK (scratch)))]
   ""
   "sys\\t#3, c7, c7, #2, %0 // gcsss1"
 )
 
 (define_insn "aarch64_gcsss2"
   [(set (match_operand:DI 0 "register_operand" "=r")
-       (unspec_volatile:DI [(match_operand:DI 1 "register_operand" "0")] 
UNSPECV_GCSSS2))]
+       (unspec_volatile:DI [(match_operand:DI 1 "register_operand" "0")]
+                           UNSPECV_GCSSS2))
+   (clobber (mem:BLK (scratch)))]
   ""
   "sysl\\t%0, #3, c7, c7, #3 // gcsss2"
 )
diff --git a/gcc/testsuite/gcc.target/aarch64/pr128284.c 
b/gcc/testsuite/gcc.target/aarch64/pr128284.c
new file mode 100644
index 000000000000..61fb330d66c1
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/pr128284.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mbranch-protection=none" } */
+
+/* The GCS stack switch reads and writes memory, so the second load of *P
+   must not be CSEd with the first.  */
+
+unsigned long
+f (unsigned long *p)
+{
+  unsigned long a = *p;
+  __builtin_aarch64_gcsss (p);
+  return a ^ *p;
+}
+
+/* { dg-final { scan-assembler-times {\tldr\tx[0-9]+, \[x[0-9]+\]} 2 } } */
+/* { dg-final { scan-assembler-not {\tmov\tw0, 0} } } */

Reply via email to