[gcc r14-7284] AArch64: Reassociate CONST in address expressions

Wilco Dijkstra wilco@gcc.gnu.org
Tue Jan 16 17:12:56 GMT 2024


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

commit r14-7284-gdb4e496aadf1d7ab1c5af24410394d1551ddd3f0
Author: Wilco Dijkstra <wilco.dijkstra@arm.com>
Date:   Tue Jan 16 16:27:02 2024 +0000

    AArch64: Reassociate CONST in address expressions
    
    GCC tends to optimistically create CONST of globals with an immediate offset.
    However it is almost always better to CSE addresses of globals and add immediate
    offsets separately (the offset could be merged later in single-use cases).
    Splitting CONST expressions with an index in aarch64_legitimize_address fixes
    part of PR112573.
    
    gcc/ChangeLog:
            PR target/112573
            * config/aarch64/aarch64.cc (aarch64_legitimize_address): Reassociate
            badly formed CONST expressions.
    
    gcc/testsuite/ChangeLog:
            PR target/112573
            * gcc.target/aarch64/pr112573.c: Add new test.

Diff:
---
 gcc/config/aarch64/aarch64.cc               | 11 +++++++++++
 gcc/testsuite/gcc.target/aarch64/pr112573.c | 15 +++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
index 7d1f8c65ce4..e6bd3fd0bb4 100644
--- a/gcc/config/aarch64/aarch64.cc
+++ b/gcc/config/aarch64/aarch64.cc
@@ -12617,6 +12617,17 @@ aarch64_legitimize_address (rtx x, rtx /* orig_x  */, machine_mode mode)
      not to split a CONST for some forms of address expression, otherwise
      it will generate sub-optimal code.  */
 
+  /* First split X + CONST (base, offset) into (base + X) + offset.  */
+  if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST)
+    {
+      poly_int64 offset;
+      rtx base = strip_offset (XEXP (x, 1), &offset);
+
+      base = expand_binop (Pmode, add_optab, base, XEXP (x, 0),
+			   NULL_RTX, true, OPTAB_DIRECT);
+      x = plus_constant (Pmode, base, offset);
+    }
+
   if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
     {
       rtx base = XEXP (x, 0);
diff --git a/gcc/testsuite/gcc.target/aarch64/pr112573.c b/gcc/testsuite/gcc.target/aarch64/pr112573.c
new file mode 100644
index 00000000000..be04c0ca86a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/pr112573.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-section-anchors" } */
+
+char a[100];
+
+void f1 (int x, int y)
+{
+  *((a + y) + 3) = x;
+  *((a + y) + 2) = x;
+  *((a + y) + 1) = x;
+  *((a + y) + 0) = x;
+}
+
+/* { dg-final { scan-assembler-times "strb" 4 } } */
+/* { dg-final { scan-assembler-times "adrp" 1 } } */


More information about the Gcc-cvs mailing list