https://gcc.gnu.org/g:01f482bf9819d591c6d1cdbecef2fa30512b8ab4

commit r17-6465-g01f482bf9819d591c6d1cdbecef2fa30512b8ab4
Author: Uros Bizjak <[email protected]>
Date:   Thu Nov 19 10:34:48 2026 +0100

    i386: Avoid false dependency for BSF [PR128662]
    
    BSF, like BSR, leaves its destination unmodified when the source is
    zero, so the hardware treats the destination as an input.  The new
    X86_TUNE_AVOID_FALSE_DEP_FOR_BSR tuning already clears the destination
    before BSR, but BSF was not handled:
    
     - *bsf<mode>_1 (used by the ffs expansion) was never split.
     - The ctz splitters were restricted to TARGET_BMI or generic tuning,
       so a non-generic tuning without TZCNT, which prints plain bsf, never
       got the clear.
    
    Use TARGET_AVOID_FALSE_DEP_FOR_BSR for the BSF forms when TARGET_BMI is
    not set, and keep the BMI and TZCNT tunings for the tzcnt form.  The
    falsedep insns can now be reached with plain bsf, so print it instead
    of hitting gcc_unreachable.  Generic tuning without BMI is unchanged,
    since it is in both tuning masks.
    
    Assisted-by: Claude Sonnet 5.5
    
            PR target/128662
    
    gcc/ChangeLog:
    
            * config/i386/i386.md (*bsf<mode>_1): Convert to
            define_insn_and_split.  Split to *bsf<mode>_1_falsedep when
            TARGET_AVOID_FALSE_DEP_FOR_BSR is set and the destination is not
            mentioned in the source.
            (*bsf<mode>_1_falsedep): New insn.
            (ctz<mode>2): Use TARGET_AVOID_FALSE_DEP_FOR_BSR in the split
            condition when TARGET_BMI is not set.
            (*ctzsidi2_<s>ext): Ditto.
            (*ctz<mode>2_falsedep): Output plain bsf instead of
            gcc_unreachable.  Update prefix_rep attribute.
            (*ctzsidi2_<s>ext_falsedep): Ditto.
            * config/i386/x86-tune.def (X86_TUNE_AVOID_FALSE_DEP_FOR_BSR):
            Update comment.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/i386/pr128662.c: Rename to...
            * gcc.target/i386/pr128662-1.c: ...this.
            * gcc.target/i386/pr128662-2.c: New test.

Diff:
---
 gcc/config/i386/i386.md                            | 68 +++++++++++++++++-----
 gcc/config/i386/x86-tune.def                       |  4 +-
 .../gcc.target/i386/{pr128662.c => pr128662-1.c}   |  0
 gcc/testsuite/gcc.target/i386/pr128662-2.c         | 18 ++++++
 4 files changed, 75 insertions(+), 15 deletions(-)

diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md
index dc1a06b67e0f..048998566aed 100644
--- a/gcc/config/i386/i386.md
+++ b/gcc/config/i386/i386.md
@@ -22285,7 +22285,7 @@
    (set_attr "c86_decode" "double")
    (set_attr "mode" "<MODE>")])
 
-(define_insn "*bsf<mode>_1"
+(define_insn_and_split "*bsf<mode>_1"
   [(set (reg:CCZ FLAGS_REG)
        (compare:CCZ (match_operand:SWI48 1 "nonimmediate_operand" "rm")
                     (const_int 0)))
@@ -22293,6 +22293,34 @@
        (ctz:SWI48 (match_dup 1)))]
   ""
   "bsf{<imodesuffix>}\t{%1, %0|%0, %1}"
+  "TARGET_AVOID_FALSE_DEP_FOR_BSR
+   && epilogue_completed
+   && optimize_function_for_speed_p (cfun)
+   && !reg_mentioned_p (operands[0], operands[1])"
+  [(parallel
+    [(set (reg:CCZ FLAGS_REG)
+         (compare:CCZ (match_dup 1) (const_int 0)))
+     (set (match_dup 0)
+         (ctz:SWI48 (match_dup 1)))
+     (unspec [(match_dup 0)] UNSPEC_INSN_FALSE_DEP)])]
+  "ix86_expand_clear (operands[0]);"
+  [(set_attr "type" "alu1")
+   (set_attr "prefix_0f" "1")
+   (set_attr "btver2_decode" "double")
+   (set_attr "znver1_decode" "vector")
+   (set_attr "c86_decode" "vector")
+   (set_attr "mode" "<MODE>")])
+
+(define_insn "*bsf<mode>_1_falsedep"
+  [(set (reg:CCZ FLAGS_REG)
+       (compare:CCZ (match_operand:SWI48 1 "nonimmediate_operand" "rm")
+                    (const_int 0)))
+   (set (match_operand:SWI48 0 "register_operand" "=r")
+       (ctz:SWI48 (match_dup 1)))
+   (unspec [(match_operand:SWI48 2 "register_operand" "0")]
+          UNSPEC_INSN_FALSE_DEP)]
+  ""
+  "bsf{<imodesuffix>}\t{%1, %0|%0, %1}"
   [(set_attr "type" "alu1")
    (set_attr "prefix_0f" "1")
    (set_attr "btver2_decode" "double")
@@ -22317,8 +22345,9 @@
 
   return "bsf{<imodesuffix>}\t{%1, %0|%0, %1}";
 }
-  "(TARGET_BMI || TARGET_CPU_P (GENERIC))
-   && (TARGET_AVOID_FALSE_DEP_FOR_BMI || TARGET_AVOID_FALSE_DEP_FOR_TZCNT)
+  "(TARGET_BMI
+    ? (TARGET_AVOID_FALSE_DEP_FOR_BMI || TARGET_AVOID_FALSE_DEP_FOR_TZCNT)
+    : TARGET_AVOID_FALSE_DEP_FOR_BSR)
    && epilogue_completed
    && optimize_function_for_speed_p (cfun)
    && !reg_mentioned_p (operands[0], operands[1])"
@@ -22356,13 +22385,18 @@
   else if (TARGET_CPU_P (GENERIC))
     /* tzcnt expands to 'rep bsf' and we can use it even if !TARGET_BMI.  */
     return "rep%; bsf{<imodesuffix>}\t{%1, %0|%0, %1}";
-  else
-    gcc_unreachable ();
+
+  return "bsf{<imodesuffix>}\t{%1, %0|%0, %1}";
 }
   [(set_attr "type" "alu1")
    (set_attr "c86_decode" "double")
    (set_attr "prefix_0f" "1")
-   (set_attr "prefix_rep" "1")
+   (set (attr "prefix_rep")
+     (if_then_else
+       (ior (match_test "TARGET_BMI")
+           (match_test "TARGET_CPU_P (GENERIC)"))
+       (const_string "1")
+       (const_string "0")))
    (set_attr "mode" "<MODE>")])
 
 (define_insn_and_split "*ctzsi2_zext"
@@ -22422,14 +22456,17 @@
 {
   if (TARGET_BMI)
     return "tzcnt{l}\t{%1, %k0|%k0, %1}";
-  else if (TARGET_CPU_P (GENERIC)
-          && !optimize_function_for_size_p (cfun))
+  else if (optimize_function_for_size_p (cfun))
+    ;
+  else if (TARGET_CPU_P (GENERIC))
     /* tzcnt expands to 'rep bsf' and we can use it even if !TARGET_BMI.  */
     return "rep%; bsf{l}\t{%1, %k0|%k0, %1}";
+
   return "bsf{l}\t{%1, %k0|%k0, %1}";
 }
-  "(TARGET_BMI || TARGET_CPU_P (GENERIC))
-   && (TARGET_AVOID_FALSE_DEP_FOR_BMI || TARGET_AVOID_FALSE_DEP_FOR_TZCNT)
+  "(TARGET_BMI
+    ? (TARGET_AVOID_FALSE_DEP_FOR_BMI || TARGET_AVOID_FALSE_DEP_FOR_TZCNT)
+    : TARGET_AVOID_FALSE_DEP_FOR_BSR)
    && epilogue_completed
    && optimize_function_for_speed_p (cfun)
    && !reg_mentioned_p (operands[0], operands[1])"
@@ -22465,12 +22502,17 @@
   else if (TARGET_CPU_P (GENERIC))
     /* tzcnt expands to 'rep bsf' and we can use it even if !TARGET_BMI.  */
     return "rep%; bsf{l}\t{%1, %k0|%k0, %1}";
-  else
-    gcc_unreachable ();
+
+  return "bsf{l}\t{%1, %k0|%k0, %1}";
 }
   [(set_attr "type" "alu1")
    (set_attr "prefix_0f" "1")
-   (set_attr "prefix_rep" "1")
+   (set (attr "prefix_rep")
+     (if_then_else
+       (ior (match_test "TARGET_BMI")
+           (match_test "TARGET_CPU_P (GENERIC)"))
+       (const_string "1")
+       (const_string "0")))
    (set_attr "mode" "SI")])
 
 (define_insn_and_split "bsr_rex64"
diff --git a/gcc/config/i386/x86-tune.def b/gcc/config/i386/x86-tune.def
index a219a688e7c1..59825a700068 100644
--- a/gcc/config/i386/x86-tune.def
+++ b/gcc/config/i386/x86-tune.def
@@ -362,8 +362,8 @@ DEF_TUNE (X86_TUNE_USE_BT, "use_bt",
          | m_AMD_MULTIPLE | m_ZHAOXIN | m_GOLDMONT | m_GOLDMONT_PLUS
          | m_TREMONT | m_CORE_HYBRID | m_CORE_ATOM | m_C86_4G | m_GENERIC)
 
-/* X86_TUNE_AVOID_FALSE_DEP_FOR_BSR: BSR leaves its destination unmodified
-   for a zero source, so the hardware treats it as an input.  */
+/* X86_TUNE_AVOID_FALSE_DEP_FOR_BSR: Avoid false dependency for bsr
+   (and bsf when tzcnt is not used) instructions.  */
 DEF_TUNE (X86_TUNE_AVOID_FALSE_DEP_FOR_BSR, "avoid_false_dep_for_bsr",
          m_NEHALEM | m_SANDYBRIDGE | m_CORE_AVX2 | m_TREMONT | m_CORE_HYBRID
          | m_CORE_ATOM | m_ZNVER4 | m_ZNVER5 | m_ZNVER6 | m_GENERIC)
diff --git a/gcc/testsuite/gcc.target/i386/pr128662.c 
b/gcc/testsuite/gcc.target/i386/pr128662-1.c
similarity index 100%
rename from gcc/testsuite/gcc.target/i386/pr128662.c
rename to gcc/testsuite/gcc.target/i386/pr128662-1.c
diff --git a/gcc/testsuite/gcc.target/i386/pr128662-2.c 
b/gcc/testsuite/gcc.target/i386/pr128662-2.c
new file mode 100644
index 000000000000..7c79091d4aae
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr128662-2.c
@@ -0,0 +1,18 @@
+/* PR target/128662 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -mno-bmi -masm=att -mtune=nehalem 
-mtune-ctrl=avoid_false_dep_for_bsr" } */
+
+unsigned long
+f1 (unsigned long x)
+{
+  return __builtin_ctzl (x);
+}
+
+/* Zero-extending variant (*ctzsidi2_<s>ext on 64-bit targets).  */
+unsigned long
+f2 (unsigned int x)
+{
+  return __builtin_ctz (x);
+}
+
+/* { dg-final { scan-assembler-times {xorl\t(%\w+), \1\n\tbsf[lq]\t} 2 } } */

Reply via email to