https://gcc.gnu.org/g:5852873096e16f9bacfc8372bec384f826a3a99a

commit r17-6045-g5852873096e16f9bacfc8372bec384f826a3a99a
Author: Hongyu Wang <[email protected]>
Date:   Fri Oct 30 11:18:46 2026 +0530

    i386: Avoid xchg reg,reg for register swaps on Intel targets [PR128445]
    
    Each result of a register swap depends on only one of the inputs, while
    on Intel cores since Sandy Bridge the xchg reg,reg uops make both results
    wait for both inputs, so a swap delays the half that was ready.
    Add a tune for Core and hybrid tunings that swaps with three moves
    through a scratch register instead.
    
    gcc/ChangeLog:
    
            PR target/128445
            * config/i386/x86-tune.def (X86_TUNE_AVOID_FALSE_DEP_FOR_XCHG): New 
tune.
            * config/i386/i386.h (TARGET_AVOID_FALSE_DEP_FOR_XCHG): New macro.
            * config/i386/i386.md (swap<mode> to three moves peephole2): New
            peephole2.
    
    gcc/testsuite/ChangeLog:
    
            PR target/128445
            * gcc.target/i386/pr128445-1.c: New test.
            * gcc.target/i386/pr128445-2.c: Likewise.
            * gcc.target/i386/xchg-3.c: Add -mtune=generic.

Diff:
---
 gcc/config/i386/i386.h                     |  2 ++
 gcc/config/i386/i386.md                    | 14 ++++++++++++++
 gcc/config/i386/x86-tune.def               |  8 ++++++++
 gcc/testsuite/gcc.target/i386/pr128445-1.c | 21 +++++++++++++++++++++
 gcc/testsuite/gcc.target/i386/pr128445-2.c | 21 +++++++++++++++++++++
 gcc/testsuite/gcc.target/i386/xchg-3.c     |  2 +-
 6 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/gcc/config/i386/i386.h b/gcc/config/i386/i386.h
index 24c555f893bf..ac2173012c37 100644
--- a/gcc/config/i386/i386.h
+++ b/gcc/config/i386/i386.h
@@ -496,6 +496,8 @@ extern unsigned char ix86_tune_features[X86_TUNE_LAST];
        ix86_tune_features[X86_TUNE_AVOID_FALSE_DEP_FOR_TZCNT]
 #define TARGET_AVOID_FALSE_DEP_FOR_BLS \
        ix86_tune_features[X86_TUNE_AVOID_FALSE_DEP_FOR_BLS]
+#define TARGET_AVOID_FALSE_DEP_FOR_XCHG \
+       ix86_tune_features[X86_TUNE_AVOID_FALSE_DEP_FOR_XCHG]
 #define TARGET_ONE_IF_CONV_INSN \
        ix86_tune_features[X86_TUNE_ONE_IF_CONV_INSN]
 #define TARGET_AVOID_MFENCE ix86_tune_features[X86_TUNE_AVOID_MFENCE]
diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md
index b1469babab6e..3bd20837e710 100644
--- a/gcc/config/i386/i386.md
+++ b/gcc/config/i386/i386.md
@@ -3512,6 +3512,20 @@
    && peep2_reg_dead_p (1, operands[1])"
   [(set (match_dup 0) (match_dup 1))])
 
+;; Convert xchg reg,reg to 3 movs through a scratch register
+;; to avoid the false dependency between the two results.
+(define_peephole2
+  [(match_scratch:SWI48 2 "r")
+   (parallel [(set (match_operand:SWI48 0 "general_reg_operand")
+                  (match_operand:SWI48 1 "general_reg_operand"))
+             (set (match_dup 1) (match_dup 0))])]
+  "TARGET_AVOID_FALSE_DEP_FOR_XCHG
+   && optimize_insn_for_speed_p ()
+   && REGNO (operands[0]) != REGNO (operands[1])"
+  [(set (match_dup 2) (match_dup 0))
+   (set (match_dup 0) (match_dup 1))
+   (set (match_dup 1) (match_dup 2))])
+
 ;; Convert moves to/from AX_REG into xchg with -Oz.
 (define_peephole2
   [(set (match_operand:SWI48 0 "general_reg_operand")
diff --git a/gcc/config/i386/x86-tune.def b/gcc/config/i386/x86-tune.def
index d4c27351ad78..116172202821 100644
--- a/gcc/config/i386/x86-tune.def
+++ b/gcc/config/i386/x86-tune.def
@@ -379,6 +379,14 @@ DEF_TUNE (X86_TUNE_AVOID_FALSE_DEP_FOR_TZCNT, 
"avoid_false_dep_for_tzcnt",
 DEF_TUNE (X86_TUNE_AVOID_FALSE_DEP_FOR_BLS, "avoid_false_dep_for_bls",
          m_ZNVER5)
 
+/* X86_TUNE_AVOID_FALSE_DEP_FOR_XCHG: Avoid xchg reg,reg.  Each result of
+   a register swap really depends on only one of the inputs, but xchg
+   reg,reg makes both results wait for both inputs, so a swap can delay
+   the half whose input was ready early.  Emit the swap as three moves
+   through a free scratch register instead.  */
+DEF_TUNE (X86_TUNE_AVOID_FALSE_DEP_FOR_XCHG, "avoid_false_dep_for_xchg",
+         m_CORE_ALL | m_CORE_HYBRID)
+
 /* X86_TUNE_ADJUST_UNROLL: This enables adjusting the unroll factor based
    on hardware capabilities. Bdver3 hardware has a loop buffer which makes
    unrolling small loop less important. For, such architectures we adjust
diff --git a/gcc/testsuite/gcc.target/i386/pr128445-1.c 
b/gcc/testsuite/gcc.target/i386/pr128445-1.c
new file mode 100644
index 000000000000..99243348d4fb
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr128445-1.c
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mtune=novalake" } */
+
+#ifdef __SIZEOF_INT128__
+typedef unsigned long long half;
+typedef unsigned __int128 wide;
+#else
+typedef unsigned int half;
+typedef unsigned long long wide;
+#endif
+
+#define N (sizeof (half) * 8)
+
+wide
+foo (half a, half b)
+{
+  wide p = (wide) a * b;
+  return (p << N) | (p >> N);
+}
+
+/* { dg-final { scan-assembler-not "\[ \t\]xchg" } } */
diff --git a/gcc/testsuite/gcc.target/i386/pr128445-2.c 
b/gcc/testsuite/gcc.target/i386/pr128445-2.c
new file mode 100644
index 000000000000..b2cb82dd5ab8
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr128445-2.c
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mtune=znver4" } */
+
+#ifdef __SIZEOF_INT128__
+typedef unsigned long long half;
+typedef unsigned __int128 wide;
+#else
+typedef unsigned int half;
+typedef unsigned long long wide;
+#endif
+
+#define N (sizeof (half) * 8)
+
+wide
+foo (half a, half b)
+{
+  wide p = (wide) a * b;
+  return (p << N) | (p >> N);
+}
+
+/* { dg-final { scan-assembler "\[ \t\]xchg\[lq\]" } } */
diff --git a/gcc/testsuite/gcc.target/i386/xchg-3.c 
b/gcc/testsuite/gcc.target/i386/xchg-3.c
index eec05f06c97b..e93ce68a5aba 100644
--- a/gcc/testsuite/gcc.target/i386/xchg-3.c
+++ b/gcc/testsuite/gcc.target/i386/xchg-3.c
@@ -1,5 +1,5 @@
 /* { dg-do compile { target ia32 } } */
-/* { dg-options "-O2" } */
+/* { dg-options "-O2 -mtune=generic" } */
 
 unsigned long long bar();

Reply via email to