Thanks, pushed with wording change.
Uros Bizjak <[email protected]> 于2026年11月9日周一 15:15写道: > > On Mon, Nov 9, 2026 at 7:17 AM Hongyu Wang <[email protected]> wrote: > > > > Hi, > > > > On Intel cores xchg reg,reg has a false dependency. Both results wait > > for both inputs, so a swap delays the first half that was ready. Add a > > tune for Core and hybrid tunings that swaps with three moves through a > > free scratch register. > > > > Bootstrapped & regtested on x86_64-pc-linux-gnu{-m32,}. No impact on > > spec26/spec17. > > > > Ok for master? > > > > 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. > > OK, with a comment improvement. “false dependency” is slightly loose > here. Each swap result only truly depends on one input, so the issue > is that the xchg uop structure makes both outputs wait for both > inputs. > > Thanks, > Uros. > > > --- > > gcc/config/i386/i386.h | 2 ++ > > gcc/config/i386/i386.md | 13 +++++++++++++ > > gcc/config/i386/x86-tune.def | 6 ++++++ > > 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, 64 insertions(+), 1 deletion(-) > > create mode 100644 gcc/testsuite/gcc.target/i386/pr128445-1.c > > create mode 100644 gcc/testsuite/gcc.target/i386/pr128445-2.c > > > > diff --git a/gcc/config/i386/i386.h b/gcc/config/i386/i386.h > > index 35563a6144a..a26f3c6ee32 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 bc225020890..14ef6465a75 100644 > > --- a/gcc/config/i386/i386.md > > +++ b/gcc/config/i386/i386.md > > @@ -3507,6 +3507,19 @@ (define_peephole2 > > && peep2_reg_dead_p (1, operands[1])" > > [(set (match_dup 0) (match_dup 1))]) > > > > +;; Convert xchg reg,reg to 3 movs to avoid false dependency. > > ;; 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 d4c27351ad7..98da4f61fcd 100644 > > --- a/gcc/config/i386/x86-tune.def > > +++ b/gcc/config/i386/x86-tune.def > > @@ -379,6 +379,12 @@ 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 false dependency for > > + xchg reg,reg. Both results wait for both inputs, so swap with three > > + moves through a free register instead. */ > > /* 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 00000000000..99243348d4f > > --- /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 00000000000..b2cb82dd5ab > > --- /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 eec05f06c97..e93ce68a5ab 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(); > > > > -- > > 2.31.1 > >
