[clang] cbaf477 - [X86][APX] Enable CF and emit register-form CFCMOV by default (#242389)

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 21:23:18 PST 2026


Author: Feng Zou
Date: 2026-11-25T13:23:10+08:00
New Revision: cbaf47740712b2df43d3f2f73e654039a5122861

URL: https://github.com/llvm/llvm-project/commit/cbaf47740712b2df43d3f2f73e654039a5122861
DIFF: https://github.com/llvm/llvm-project/commit/cbaf47740712b2df43d3f2f73e654039a5122861.diff

LOG: [X86][APX] Enable CF and emit register-form CFCMOV by default (#242389)

Add CF to the DMR and NVL feature lists and to -mapxf/-mno-apxf, and
require it for __APX_F__.

Register-form CFCMOV (select with zero) is selected whenever CF is
available. Memory-form CFCMOV (conditional faulting load/store) is gated
on the new tuning feature prefer-cf-mem-operand, which no CPU sets. It
controls X86TTIImpl::hasConditionalLoadStoreForType, the
store(cmov(load)) -> CSTORE combine and the CLOAD/CSTORE isel patterns.

Assisted-by: Claude Opus 5.5 <noreply at anthropic.com>

Added: 
    llvm/test/CodeGen/X86/apx/cf-operand-gating.ll

Modified: 
    clang/include/clang/Options/Options.td
    clang/lib/Basic/Targets/X86.cpp
    clang/test/CodeGen/attr-target-x86.c
    clang/test/Driver/cl-x86-flags.c
    clang/test/Driver/x86-target-features.c
    clang/test/Preprocessor/predefined-arch-macros.c
    clang/test/Preprocessor/x86_target_features.c
    llvm/lib/Target/X86/X86.td
    llvm/lib/Target/X86/X86ISelLowering.cpp
    llvm/lib/Target/X86/X86InstrCMovSetCC.td
    llvm/lib/Target/X86/X86InstrPredicates.td
    llvm/lib/Target/X86/X86TargetTransformInfo.cpp
    llvm/lib/TargetParser/X86TargetParser.cpp
    llvm/test/CodeGen/X86/apx/cf.ll
    llvm/test/CodeGen/X86/apx/cfcmov.ll
    llvm/test/Transforms/PhaseOrdering/X86/masked-memory-ops-with-cf.ll
    llvm/test/Transforms/SimplifyCFG/X86/hoist-loads-stores-with-cf.ll

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 6abe104c8acb05..f23958cdd548d7 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -7764,10 +7764,10 @@ def mapx_features_EQ : CommaJoined<["-"], "mapx-features=">, Group<m_x86_Feature
 def mno_apx_features_EQ : CommaJoined<["-"], "mno-apx-features=">, Group<m_x86_Features_Group>,
     HelpText<"Disable features of APX">, Values<"egpr,push2pop2,ppx,ndd,ccmp,nf,cf,zu,jmpabs">, Visibility<[ClangOption, CLOption, FlangOption]>;
 def mapxf : Flag<["-"], "mapxf">, Alias<mapx_features_EQ>,
-            AliasArgs<["egpr","push2pop2","ppx","ndd","ccmp","nf","zu","jmpabs"]>,
+            AliasArgs<["egpr","push2pop2","ppx","ndd","ccmp","nf","cf","zu","jmpabs"]>,
             Group<m_x86_Features_Group>;
 def mno_apxf : Flag<["-"], "mno-apxf">, Alias<mno_apx_features_EQ>,
-               AliasArgs<["egpr","push2pop2","ppx","ndd","ccmp","nf","zu","jmpabs"]>,
+               AliasArgs<["egpr","push2pop2","ppx","ndd","ccmp","nf","cf","zu","jmpabs"]>,
                Group<m_x86_Features_Group>;
 def mapx_inline_asm_use_gpr32 : Flag<["-"], "mapx-inline-asm-use-gpr32">, Group<m_Group>,
                                 HelpText<"Enable use of GPR32 in inline assembly for APX">;

diff  --git a/clang/lib/Basic/Targets/X86.cpp b/clang/lib/Basic/Targets/X86.cpp
index 824f7b99ef9326..7067877d41f512 100644
--- a/clang/lib/Basic/Targets/X86.cpp
+++ b/clang/lib/Basic/Targets/X86.cpp
@@ -176,8 +176,8 @@ bool X86TargetInfo::initFeatureMap(
 
     if (Feature == "+apxf" || Feature == "-apxf") {
       char Sign = Feature[0];
-      for (const char *Sub :
-           {"egpr", "push2pop2", "ppx", "ndd", "ccmp", "nf", "zu", "jmpabs"})
+      for (const char *Sub : {"egpr", "push2pop2", "ppx", "ndd", "ccmp", "nf",
+                              "cf", "zu", "jmpabs"})
         UpdatedFeaturesVec.push_back(Sign + std::string(Sub));
       continue;
     }
@@ -1003,7 +1003,7 @@ void X86TargetInfo::getTargetDefines(const LangOptions &Opts,
   if (HasJMPABS)
     Builder.defineMacro("__JMPABS__");
   if (HasEGPR && HasPush2Pop2 && HasPPX && HasNDD && HasCCMP && HasNF &&
-      HasZU && HasJMPABS)
+      HasCF && HasZU && HasJMPABS)
     Builder.defineMacro("__APX_F__");
   if (HasEGPR && HasInlineAsmUseGPR32)
     Builder.defineMacro("__APX_INLINE_ASM_USE_GPR32__");

diff  --git a/clang/test/CodeGen/attr-target-x86.c b/clang/test/CodeGen/attr-target-x86.c
index d8fd922a06ebd9..e74df11ecc5aa3 100644
--- a/clang/test/CodeGen/attr-target-x86.c
+++ b/clang/test/CodeGen/attr-target-x86.c
@@ -227,11 +227,11 @@ void f_prefer_256_bit(void) {}
 __attribute__((target("no-prefer-256-bit")))
 void f_no_prefer_256_bit(void) {}
 
-// CHECK: [[f_apxf]] = {{.*}}"target-features"="{{.*}}+ccmp{{.*}}+egpr{{.*}}+jmpabs{{.*}}+ndd{{.*}}+nf{{.*}}+ppx{{.*}}+push2pop2{{.*}}+zu
+// CHECK: [[f_apxf]] = {{.*}}"target-features"="{{.*}}+ccmp{{.*}}+cf{{.*}}+egpr{{.*}}+jmpabs{{.*}}+ndd{{.*}}+nf{{.*}}+ppx{{.*}}+push2pop2{{.*}}+zu
 __attribute__((target("apxf")))
 void f_apxf(void) {}
 
-// CHECK: [[f_no_apxf]] = {{.*}}"target-features"="{{.*}}-ccmp{{.*}}-egpr{{.*}}-jmpabs{{.*}}-ndd{{.*}}-nf{{.*}}-ppx{{.*}}-push2pop2{{.*}}-zu
+// CHECK: [[f_no_apxf]] = {{.*}}"target-features"="{{.*}}-ccmp{{.*}}-cf{{.*}}-egpr{{.*}}-jmpabs{{.*}}-ndd{{.*}}-nf{{.*}}-ppx{{.*}}-push2pop2{{.*}}-zu
 __attribute__((target("no-apxf")))
 void f_no_apxf(void) {}
 

diff  --git a/clang/test/Driver/cl-x86-flags.c b/clang/test/Driver/cl-x86-flags.c
index 6d7191fdd24da3..eaa9333e8c1194 100644
--- a/clang/test/Driver/cl-x86-flags.c
+++ b/clang/test/Driver/cl-x86-flags.c
@@ -224,6 +224,6 @@ void f(void) {
 // RUN: %clang_cl --target=x86_64-pc-windows -mapxf -### -- 2>&1 %s | FileCheck -check-prefix=APXF %s
 // RUN: %clang_cl --target=x86_64-pc-windows -mapxf -mno-apxf -### -- 2>&1 %s | FileCheck -check-prefix=NO-APXF %s
 // RUN: %clang_cl --target=x86_64-pc-windows -mapx-features=egpr,push2pop2,ppx,ndd,ccmp,nf,cf,zu,jmpabs -### -- 2>&1 %s | FileCheck -check-prefix=APXALL %s
-// APXF: "-target-feature" "+egpr" "-target-feature" "+push2pop2" "-target-feature" "+ppx" "-target-feature" "+ndd" "-target-feature" "+ccmp" "-target-feature" "+nf" "-target-feature" "+zu" "-target-feature" "+jmpabs"
-// NO-APXF: "-target-feature" "-egpr" "-target-feature" "-push2pop2" "-target-feature" "-ppx" "-target-feature" "-ndd" "-target-feature" "-ccmp" "-target-feature" "-nf" "-target-feature" "-zu" "-target-feature" "-jmpabs"
+// APXF: "-target-feature" "+egpr" "-target-feature" "+push2pop2" "-target-feature" "+ppx" "-target-feature" "+ndd" "-target-feature" "+ccmp" "-target-feature" "+nf" "-target-feature" "+cf" "-target-feature" "+zu" "-target-feature" "+jmpabs"
+// NO-APXF: "-target-feature" "-egpr" "-target-feature" "-push2pop2" "-target-feature" "-ppx" "-target-feature" "-ndd" "-target-feature" "-ccmp" "-target-feature" "-nf" "-target-feature" "-cf" "-target-feature" "-zu" "-target-feature" "-jmpabs"
 // APXALL: "-target-feature" "+egpr" "-target-feature" "+push2pop2" "-target-feature" "+ppx" "-target-feature" "+ndd" "-target-feature" "+ccmp" "-target-feature" "+nf" "-target-feature" "+cf" "-target-feature" "+zu" "-target-feature" "+jmpabs"

diff  --git a/clang/test/Driver/x86-target-features.c b/clang/test/Driver/x86-target-features.c
index d0c0fdd1e97105..110ff5a4f7174a 100644
--- a/clang/test/Driver/x86-target-features.c
+++ b/clang/test/Driver/x86-target-features.c
@@ -446,8 +446,8 @@
 // RUN: %clang --target=x86_64-unknown-linux-gnu -mno-apxf -mapxf %s -### -o %t.o 2>&1 | FileCheck -check-prefix=APXF %s
 // RUN: %clang --target=x86_64-unknown-linux-gnu -mapxf -mno-apxf %s -### -o %t.o 2>&1 | FileCheck -check-prefix=NO-APXF %s
 //
-// APXF: "-target-feature" "+egpr" "-target-feature" "+push2pop2" "-target-feature" "+ppx" "-target-feature" "+ndd" "-target-feature" "+ccmp" "-target-feature" "+nf" "-target-feature" "+zu" "-target-feature" "+jmpabs"
-// NO-APXF: "-target-feature" "-egpr" "-target-feature" "-push2pop2" "-target-feature" "-ppx" "-target-feature" "-ndd" "-target-feature" "-ccmp" "-target-feature" "-nf" "-target-feature" "-zu" "-target-feature" "-jmpabs"
+// APXF: "-target-feature" "+egpr" "-target-feature" "+push2pop2" "-target-feature" "+ppx" "-target-feature" "+ndd" "-target-feature" "+ccmp" "-target-feature" "+nf" "-target-feature" "+cf" "-target-feature" "+zu" "-target-feature" "+jmpabs"
+// NO-APXF: "-target-feature" "-egpr" "-target-feature" "-push2pop2" "-target-feature" "-ppx" "-target-feature" "-ndd" "-target-feature" "-ccmp" "-target-feature" "-nf" "-target-feature" "-cf" "-target-feature" "-zu" "-target-feature" "-jmpabs"
 
 // RUN: %clang --target=x86_64-unknown-linux-gnu -mapx-features=egpr %s -### -o %t.o 2>&1 | FileCheck -check-prefix=EGPR %s
 // RUN: %clang --target=x86_64-unknown-linux-gnu -mapx-features=push2pop2 %s -### -o %t.o 2>&1 | FileCheck -check-prefix=PUSH2POP2 %s

diff  --git a/clang/test/Preprocessor/predefined-arch-macros.c b/clang/test/Preprocessor/predefined-arch-macros.c
index 29c18eae19ba91..294bbafa5a95e6 100644
--- a/clang/test/Preprocessor/predefined-arch-macros.c
+++ b/clang/test/Preprocessor/predefined-arch-macros.c
@@ -1840,6 +1840,7 @@
 // CHECK_GNR_M32: #define __AMX_INT8__ 1
 // CHECK_DMR_M32: #define __AMX_MOVRS__ 1
 // CHECK_GNR_M32: #define __AMX_TILE__ 1
+// CHECK_DMR_M32: #define __APX_F__ 1
 // CHECK_DMR_M32: #define __AVX10_2_512__ 1
 // CHECK_DMR_M32: #define __AVX10_2__ 1
 // CHECK_GNR_M32: #define __AVX2__ 1
@@ -1865,6 +1866,7 @@
 // CHECK_GNR_M32: #define __BMI2__ 1
 // CHECK_GNR_M32: #define __BMI__ 1
 // CHECK_DMR_M32: #define __CCMP__ 1
+// CHECK_DMR_M32: #define __CF__ 1
 // CHECK_GNR_M32: #define __CLDEMOTE__ 1
 // CHECK_GNR_M32: #define __CLFLUSHOPT__ 1
 // CHECK_GNR_M32: #define __CLWB__ 1
@@ -1945,6 +1947,7 @@
 // CHECK_GNR_M64: #define __AMX_INT8__ 1
 // CHECK_DMR_M64: #define __AMX_MOVRS__ 1
 // CHECK_GNR_M64: #define __AMX_TILE__ 1
+// CHECK_DMR_M64: #define __APX_F__ 1
 // CHECK_DMR_M64: #define __AVX10_2_512__ 1
 // CHECK_DMR_M64: #define __AVX10_2__ 1
 // CHECK_GNR_M64: #define __AVX2__ 1
@@ -1970,6 +1973,7 @@
 // CHECK_GNR_M64: #define __BMI2__ 1
 // CHECK_GNR_M64: #define __BMI__ 1
 // CHECK_DMR_M64: #define __CCMP__ 1
+// CHECK_DMR_M64: #define __CF__ 1
 // CHECK_GNR_M64: #define __CLDEMOTE__ 1
 // CHECK_GNR_M64: #define __CLFLUSHOPT__ 1
 // CHECK_GNR_M64: #define __CLWB__ 1
@@ -2533,6 +2537,7 @@
 // RUN:   | FileCheck -match-full-lines %s -check-prefixes=CHECK_ARL_M32,CHECK_ARLS_M32,CHECK_CWF_M32,CHECK_NVL_M32,CHECK_NKL_M32
 // CHECK_ARL_M32: #define __ADX__ 1
 // CHECK_ARL_M32: #define __AES__ 1
+// CHECK_NVL_M32: #define __APX_F__ 1
 // CHECK_NVL_M32: #define __AVX10_1__ 1
 // CHECK_NVL_M32: #define __AVX10_2__ 1
 // CHECK_ARL_M32: #define __AVX2__ 1
@@ -2562,6 +2567,7 @@
 // CHECK_ARL_M32: #define __BMI2__ 1
 // CHECK_ARL_M32: #define __BMI__ 1
 // CHECK_NVL_M32: #define __CCMP__ 1
+// CHECK_NVL_M32: #define __CF__ 1
 // CHECK_ARLS_M32-NOT: __CLDEMOTE__
 // CHECK_SRF_M32: #define __CLDEMOTE__ 1
 // CHECK_ARL_M32: #define __CLFLUSHOPT__ 1
@@ -2669,6 +2675,7 @@
 // RUN:   | FileCheck -match-full-lines %s -check-prefixes=CHECK_ARL_M64,CHECK_ARLS_M64,CHECK_CWF_M64,CHECK_NVL_M64,CHECK_NKL_M64
 // CHECK_ARL_M64: #define __ADX__ 1
 // CHECK_ARL_M64: #define __AES__ 1
+// CHECK_NVL_M64: #define __APX_F__ 1
 // CHECK_NVL_M64: #define __AVX10_1__ 1
 // CHECK_NVL_M64: #define __AVX10_2__ 1
 // CHECK_ARL_M64: #define __AVX2__ 1
@@ -2696,6 +2703,7 @@
 // CHECK_ARL_M64: #define __BMI2__ 1
 // CHECK_ARL_M64: #define __BMI__ 1
 // CHECK_NVL_M64: #define __CCMP__ 1
+// CHECK_NVL_M64: #define __CF__ 1
 // CHECK_ARLS_M64-NOT: __CLDEMOTE__
 // CHECK_SRF_M64: #define __CLDEMOTE__ 1
 // CHECK_ARL_M64: #define __CLFLUSHOPT__ 1

diff  --git a/clang/test/Preprocessor/x86_target_features.c b/clang/test/Preprocessor/x86_target_features.c
index 90b785d3c0694e..f75e9ee9a5d9c6 100644
--- a/clang/test/Preprocessor/x86_target_features.c
+++ b/clang/test/Preprocessor/x86_target_features.c
@@ -746,7 +746,7 @@
 // RUN: %clang -target x86_64-unknown-unknown -march=x86-64 -mapx-features=cf -x c -E -dM -o - %s | FileCheck --check-prefix=CF %s
 // RUN: %clang -target x86_64-unknown-unknown -march=x86-64 -mapx-features=zu -x c -E -dM -o - %s | FileCheck --check-prefix=ZU %s
 // RUN: %clang -target x86_64-unknown-unknown -march=x86-64 -mapx-features=jmpabs -x c -E -dM -o - %s | FileCheck --check-prefix=JMPABS %s
-// RUN: %clang -target x86_64-unknown-unknown -march=x86-64 -mapxf -x c -E -dM -o - %s | FileCheck --check-prefixes=EGPR,PUSH2POP2,PPX,NDD,CCMP,NF,ZU,JMPABS,APXF %s
+// RUN: %clang -target x86_64-unknown-unknown -march=x86-64 -mapxf -x c -E -dM -o - %s | FileCheck --check-prefixes=EGPR,PUSH2POP2,PPX,NDD,CCMP,NF,CF,ZU,JMPABS,APXF %s
 // APXF: #define __APX_F__ 1
 // CCMP: #define __CCMP__ 1
 // CF: #define __CF__ 1

diff  --git a/llvm/lib/Target/X86/X86.td b/llvm/lib/Target/X86/X86.td
index b24cdd737024be..8470ee3150027c 100644
--- a/llvm/lib/Target/X86/X86.td
+++ b/llvm/lib/Target/X86/X86.td
@@ -375,9 +375,8 @@ def FeatureCCMP : SubtargetFeature<"ccmp", "HasCCMP", "true",
                                    "Support conditional cmp & test instructions">;
 def FeatureNF : SubtargetFeature<"nf", "HasNF", "true",
                                  "Support status flags update suppression">;
-// FeatureCF is not enabled by default for APXF and targets that support APXF
-// due to performance reason, though it is part of APXF. Users need to enable it
-// manually.
+// Memory-operand CFCMOV (conditional faulting load/store) is only used with
+// TuningPreferCFMemOperand.
 def FeatureCF : SubtargetFeature<"cf", "HasCF", "true",
                                  "Support conditional faulting">;
 def FeatureZU : SubtargetFeature<"zu", "HasZU", "true",
@@ -908,6 +907,12 @@ def TuningPreferLegacySetCC
     : SubtargetFeature<"prefer-legacy-setcc", "PreferLegacySetCC", "true",
                        "Prefer to emit legacy SetCC.", [], InlineIgnore>;
 
+def TuningPreferCFMemOperand
+    : SubtargetFeature<"prefer-cf-mem-operand", "PreferCFMemOperand", "true",
+                       "Use CFCMOV with memory operands (conditional "
+                       "faulting load/store).",
+                       [], InlineIgnore>;
+
 //===----------------------------------------------------------------------===//
 // X86 CPU Families
 // TODO: Remove these - use general tuning features to determine codegen.
@@ -1309,6 +1314,7 @@ def ProcessorFeatures {
                                                   FeaturePPX,
                                                   FeatureNDD,
                                                   FeatureNF,
+                                                  FeatureCF,
                                                   FeatureJMPABS,
                                                   FeatureMOVRS,
                                                   FeatureAMXMOVRS,
@@ -1482,6 +1488,7 @@ def ProcessorFeatures {
                                                   FeaturePush2Pop2,
                                                   FeaturePPX,
                                                   FeatureNF,
+                                                  FeatureCF,
                                                   FeatureNDD,
                                                   FeatureZU,
                                                   FeatureJMPABS,

diff  --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 9367a29b19e084..c6ec7428fd0f94 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -56066,7 +56066,7 @@ static SDValue combineStore(SDNode *N, SelectionDAG &DAG,
   // Convert store(cmov(load(p), x, CC), p) to cstore(x, p, CC)
   //         store(cmov(x, load(p), CC), p) to cstore(x, p, InvertCC)
   if ((VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64) &&
-      Subtarget.hasCF() && St->isSimple()) {
+      Subtarget.hasCF() && Subtarget.preferCFMemOperand() && St->isSimple()) {
     SDValue Cmov;
     if (StoredVal.getOpcode() == X86ISD::CMOV)
       Cmov = StoredVal;

diff  --git a/llvm/lib/Target/X86/X86InstrCMovSetCC.td b/llvm/lib/Target/X86/X86InstrCMovSetCC.td
index 39663049b92bf7..e6c9020e0d5d64 100644
--- a/llvm/lib/Target/X86/X86InstrCMovSetCC.td
+++ b/llvm/lib/Target/X86/X86InstrCMovSetCC.td
@@ -113,7 +113,10 @@ let Predicates = [HasCMOV, HasCF] in {
             (CFCMOV32rr GR32:$src1, (inv_cond_XFORM timm:$cond))>;
   def : Pat<(X86cmov GR64:$src1, 0, timm:$cond, EFLAGS),
             (CFCMOV64rr GR64:$src1, (inv_cond_XFORM timm:$cond))>;
+}
 
+// Conditional faulting load/store, only used with prefer-cf-mem-operand.
+let Predicates = [HasCMOV, HasCF, UseCFMemOperand] in {
   def : Pat<(X86cload addr:$src1, 0, timm:$cond, EFLAGS),
             (CFCMOV16rm addr:$src1, timm:$cond)>;
   def : Pat<(X86cload addr:$src1, 0, timm:$cond, EFLAGS),

diff  --git a/llvm/lib/Target/X86/X86InstrPredicates.td b/llvm/lib/Target/X86/X86InstrPredicates.td
index 112118b7725f5a..1287e32c058a94 100644
--- a/llvm/lib/Target/X86/X86InstrPredicates.td
+++ b/llvm/lib/Target/X86/X86InstrPredicates.td
@@ -51,6 +51,7 @@ def PreferLegacySetCC  : Predicate<"!Subtarget->hasZU() || "
 def PreferNoLegacySetCC : Predicate<"Subtarget->hasZU() && "
                                     "!Subtarget->preferLegacySetCC()">;
 def HasCF        : Predicate<"Subtarget->hasCF()">;
+def UseCFMemOperand : Predicate<"Subtarget->preferCFMemOperand()">;
 def HasJMPABS    : Predicate<"Subtarget->hasJMPABS()">;
 def HasCMOV      : Predicate<"Subtarget->canUseCMOV()">;
 def NoCMOV       : Predicate<"!Subtarget->canUseCMOV()">;

diff  --git a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
index 76a11d7c77cc9e..83ec3427be0abd 100644
--- a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
+++ b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
@@ -155,7 +155,8 @@ unsigned X86TTIImpl::getNumberOfRegisters(unsigned ClassID) const {
 }
 
 bool X86TTIImpl::hasConditionalLoadStoreForType(Type *Ty, bool IsStore) const {
-  if (!ST->hasCF())
+  // Conditional faulting load/store requires prefer-cf-mem-operand.
+  if (!ST->hasCF() || !ST->preferCFMemOperand())
     return false;
   if (!Ty)
     return true;

diff  --git a/llvm/lib/TargetParser/X86TargetParser.cpp b/llvm/lib/TargetParser/X86TargetParser.cpp
index 384687702c7c25..b2427e03f9aee1 100644
--- a/llvm/lib/TargetParser/X86TargetParser.cpp
+++ b/llvm/lib/TargetParser/X86TargetParser.cpp
@@ -130,8 +130,8 @@ constexpr FeatureBitset FeaturesDiamondRapids =
     FeatureCMPCCXADD | FeatureAVXIFMA | FeatureAVXNECONVERT |
     FeatureAVXVNNIINT8 | FeatureAVXVNNIINT16 | FeatureSHA512 | FeatureSM3 |
     FeatureSM4 | FeatureEGPR | FeatureZU | FeatureCCMP | FeaturePush2Pop2 |
-    FeaturePPX | FeatureNDD | FeatureNF | FeatureJMPABS | FeatureMOVRS |
-    FeatureAMX_MOVRS | FeatureAMX_AVX512 | FeatureAMX_FP8;
+    FeaturePPX | FeatureNDD | FeatureNF | FeatureCF | FeatureJMPABS |
+    FeatureMOVRS | FeatureAMX_MOVRS | FeatureAMX_AVX512 | FeatureAMX_FP8;
 
 // Intel Atom processors.
 // Bonnell has feature parity with Core2 and adds MOVBE.
@@ -166,7 +166,7 @@ constexpr FeatureBitset FeaturesPantherlake =
 constexpr FeatureBitset FeaturesNovalake =
     FeaturesPantherlake | FeaturePREFETCHI | FeatureAVX10_2 | FeatureMOVRS |
     FeatureEGPR | FeatureZU | FeatureCCMP | FeaturePush2Pop2 | FeaturePPX |
-    FeatureNDD | FeatureNF | FeatureJMPABS;
+    FeatureNDD | FeatureNF | FeatureCF | FeatureJMPABS;
 constexpr FeatureBitset FeaturesClearwaterforest =
     (FeaturesSierraforest ^ FeatureWIDEKL) | FeatureAVXVNNIINT16 |
     FeatureSHA512 | FeatureSM3 | FeatureSM4 | FeaturePREFETCHI | FeatureUSERMSR;

diff  --git a/llvm/test/CodeGen/X86/apx/cf-operand-gating.ll b/llvm/test/CodeGen/X86/apx/cf-operand-gating.ll
new file mode 100644
index 00000000000000..8e2774e1c5c8a8
--- /dev/null
+++ b/llvm/test/CodeGen/X86/apx/cf-operand-gating.ll
@@ -0,0 +1,71 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -mtriple=x86_64 -mattr=+cf -verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,NOMEM
+; RUN: llc < %s -mtriple=x86_64 -mattr=+cf,+prefer-cf-mem-operand -verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,MEM
+; RUN: llc < %s -mtriple=x86_64 -mcpu=diamondrapids -verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,CPU
+; RUN: llc < %s -mtriple=x86_64 -mcpu=novalake -verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,CPU
+
+;; +cf emits register-form CFCMOV. Memory forms (conditional faulting
+;; load/store) need +prefer-cf-mem-operand. DMR and NVL enable CF by default.
+
+define i64 @reg_form(i64 %a, i64 %x) {
+; CHECK-LABEL: reg_form:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    testq %rdi, %rdi
+; CHECK-NEXT:    cfcmoveq %rsi, %rax
+; CHECK-NEXT:    retq
+  %c = icmp eq i64 %a, 0
+  %r = select i1 %c, i64 %x, i64 0
+  ret i64 %r
+}
+
+define void @masked_v1(i32 %a, ptr %b, ptr %p) {
+; NOMEM-LABEL: masked_v1:
+; NOMEM:       # %bb.0:
+; NOMEM-NEXT:    testl %edi, %edi
+; NOMEM-NEXT:    sete %cl
+; NOMEM-NEXT:    # implicit-def: $eax
+; NOMEM-NEXT:    je .LBB1_1
+; NOMEM-NEXT:  # %bb.2: # %else
+; NOMEM-NEXT:    testb %cl, %cl
+; NOMEM-NEXT:    jne .LBB1_3
+; NOMEM-NEXT:  .LBB1_4: # %else1
+; NOMEM-NEXT:    retq
+; NOMEM-NEXT:  .LBB1_1: # %cond.load
+; NOMEM-NEXT:    movl (%rsi), %eax
+; NOMEM-NEXT:    testb %cl, %cl
+; NOMEM-NEXT:    je .LBB1_4
+; NOMEM-NEXT:  .LBB1_3: # %cond.store
+; NOMEM-NEXT:    movl %eax, (%rdx)
+; NOMEM-NEXT:    retq
+;
+; MEM-LABEL: masked_v1:
+; MEM:       # %bb.0:
+; MEM-NEXT:    testl %edi, %edi
+; MEM-NEXT:    cfcmovel (%rsi), %eax
+; MEM-NEXT:    cfcmovel %eax, (%rdx)
+; MEM-NEXT:    retq
+;
+; CPU-LABEL: masked_v1:
+; CPU:       # %bb.0:
+; CPU-NEXT:    # implicit-def: $eax
+; CPU-NEXT:    testl %edi, %edi
+; CPU-NEXT:    sete %cl
+; CPU-NEXT:    je .LBB1_1
+; CPU-NEXT:  # %bb.2: # %else
+; CPU-NEXT:    testb $1, %cl
+; CPU-NEXT:    jne .LBB1_3
+; CPU-NEXT:  .LBB1_4: # %else1
+; CPU-NEXT:    retq
+; CPU-NEXT:  .LBB1_1: # %cond.load
+; CPU-NEXT:    movl (%rsi), %eax
+; CPU-NEXT:    testb $1, %cl
+; CPU-NEXT:    je .LBB1_4
+; CPU-NEXT:  .LBB1_3: # %cond.store
+; CPU-NEXT:    movl %eax, (%rdx)
+; CPU-NEXT:    retq
+  %cond = icmp eq i32 %a, 0
+  %m = bitcast i1 %cond to <1 x i1>
+  %v = call <1 x i32> @llvm.masked.load.v1i32.p0(ptr %b, i32 4, <1 x i1> %m, <1 x i32> poison)
+  call void @llvm.masked.store.v1i32.p0(<1 x i32> %v, ptr %p, i32 4, <1 x i1> %m)
+  ret void
+}

diff  --git a/llvm/test/CodeGen/X86/apx/cf.ll b/llvm/test/CodeGen/X86/apx/cf.ll
index 75a1253b36e257..b7c8643297221d 100644
--- a/llvm/test/CodeGen/X86/apx/cf.ll
+++ b/llvm/test/CodeGen/X86/apx/cf.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
-; RUN: llc < %s -mtriple=x86_64 -mattr=+cf,+nf,+avx512f -verify-machineinstrs | FileCheck %s
+; RUN: llc < %s -mtriple=x86_64 -mattr=+cf,+nf,+avx512f,+prefer-cf-mem-operand -verify-machineinstrs | FileCheck %s
 
 define void @basic(i32 %a, ptr %b, ptr %p, ptr %q) {
 ; CHECK-LABEL: basic:

diff  --git a/llvm/test/CodeGen/X86/apx/cfcmov.ll b/llvm/test/CodeGen/X86/apx/cfcmov.ll
index 37ba3d451c2b16..0e3465fbb8fca5 100644
--- a/llvm/test/CodeGen/X86/apx/cfcmov.ll
+++ b/llvm/test/CodeGen/X86/apx/cfcmov.ll
@@ -1,6 +1,7 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 2
-; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+cf -verify-machineinstrs | FileCheck %s
-; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+cf -x86-cmov-converter=false -verify-machineinstrs | FileCheck %s
+; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+cf -verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,NOMEM
+; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+cf -x86-cmov-converter=false -verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,NOMEM
+; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+cf,+prefer-cf-mem-operand -verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,MEM
 
 define i8 @cfcmov8rr(i8 %0) {
 ; CHECK-LABEL: cfcmov8rr:
@@ -95,12 +96,20 @@ define i64 @cfcmov64rr_inv(i64 %0) {
 }
 
 define void @cfcmov16mr(ptr %p, i16 %0) {
-; CHECK-LABEL: cfcmov16mr:
-; CHECK:       # %bb.0:
-; CHECK-NEXT:    movzwl (%rdi), %eax
-; CHECK-NEXT:    cmpw %ax, %si
-; CHECK-NEXT:    cfcmovlew %si, (%rdi)
-; CHECK-NEXT:    retq
+; NOMEM-LABEL: cfcmov16mr:
+; NOMEM:       # %bb.0:
+; NOMEM-NEXT:    movzwl (%rdi), %eax
+; NOMEM-NEXT:    cmpw %ax, %si
+; NOMEM-NEXT:    cmovgl %eax, %esi
+; NOMEM-NEXT:    movw %si, (%rdi)
+; NOMEM-NEXT:    retq
+;
+; MEM-LABEL: cfcmov16mr:
+; MEM:       # %bb.0:
+; MEM-NEXT:    movzwl (%rdi), %eax
+; MEM-NEXT:    cmpw %ax, %si
+; MEM-NEXT:    cfcmovlew %si, (%rdi)
+; MEM-NEXT:    retq
   %2 = load i16, ptr %p, align 2
   %3 = icmp sgt i16 %0, %2
   %4 = select i1 %3, i16 %2, i16 %0
@@ -109,11 +118,19 @@ define void @cfcmov16mr(ptr %p, i16 %0) {
 }
 
 define void @cfcmov32mr(ptr %p, i32 %0) {
-; CHECK-LABEL: cfcmov32mr:
-; CHECK:       # %bb.0:
-; CHECK-NEXT:    cmpl (%rdi), %esi
-; CHECK-NEXT:    cfcmovgl %esi, (%rdi)
-; CHECK-NEXT:    retq
+; NOMEM-LABEL: cfcmov32mr:
+; NOMEM:       # %bb.0:
+; NOMEM-NEXT:    movl (%rdi), %eax
+; NOMEM-NEXT:    cmpl %eax, %esi
+; NOMEM-NEXT:    cmovgl %esi, %eax
+; NOMEM-NEXT:    movl %eax, (%rdi)
+; NOMEM-NEXT:    retq
+;
+; MEM-LABEL: cfcmov32mr:
+; MEM:       # %bb.0:
+; MEM-NEXT:    cmpl (%rdi), %esi
+; MEM-NEXT:    cfcmovgl %esi, (%rdi)
+; MEM-NEXT:    retq
   %2 = load i32, ptr %p, align 4
   %3 = call i32 @llvm.smax.i32(i32 %0, i32 %2)
   store i32 %3, ptr %p, align 4
@@ -121,11 +138,19 @@ define void @cfcmov32mr(ptr %p, i32 %0) {
 }
 
 define void @cfcmov64mr(ptr %p, i64 %0) {
-; CHECK-LABEL: cfcmov64mr:
-; CHECK:       # %bb.0:
-; CHECK-NEXT:    cmpq (%rdi), %rsi
-; CHECK-NEXT:    cfcmovgq %rsi, (%rdi)
-; CHECK-NEXT:    retq
+; NOMEM-LABEL: cfcmov64mr:
+; NOMEM:       # %bb.0:
+; NOMEM-NEXT:    movq (%rdi), %rax
+; NOMEM-NEXT:    cmpq %rax, %rsi
+; NOMEM-NEXT:    cmovgq %rsi, %rax
+; NOMEM-NEXT:    movq %rax, (%rdi)
+; NOMEM-NEXT:    retq
+;
+; MEM-LABEL: cfcmov64mr:
+; MEM:       # %bb.0:
+; MEM-NEXT:    cmpq (%rdi), %rsi
+; MEM-NEXT:    cfcmovgq %rsi, (%rdi)
+; MEM-NEXT:    retq
   %2 = load i64, ptr %p, align 8
   %3 = icmp sgt i64 %0, %2
   %4 = select i1 %3, i64 %0, i64 %2

diff  --git a/llvm/test/Transforms/PhaseOrdering/X86/masked-memory-ops-with-cf.ll b/llvm/test/Transforms/PhaseOrdering/X86/masked-memory-ops-with-cf.ll
index dca864e596664b..cc2b60f68e1e5a 100644
--- a/llvm/test/Transforms/PhaseOrdering/X86/masked-memory-ops-with-cf.ll
+++ b/llvm/test/Transforms/PhaseOrdering/X86/masked-memory-ops-with-cf.ll
@@ -1,24 +1,40 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -mtriple=x86_64 -mattr=+cf -O1 -S | FileCheck %s
+; RUN: opt < %s -mtriple=x86_64 -mattr=+cf,+prefer-cf-mem-operand -O1 -S | FileCheck %s --check-prefix=MEM
+; RUN: opt < %s -mtriple=x86_64 -mattr=+cf -O1 -S | FileCheck %s --check-prefix=NOMEM
 
-;; Test masked.load/store.v1* is generated in simplifycfg and not falls back to branch+load/store in following passes.
+;; With +prefer-cf-mem-operand, simplifycfg forms masked.load/store.v1* and
+;; later passes keep them; otherwise the branch is kept.
 define void @basic(i1 %cond, ptr %b, ptr %p, ptr %q) {
-; CHECK-LABEL: @basic(
-; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[TMP0:%.*]] = bitcast i1 [[COND:%.*]] to <1 x i1>
-; CHECK-NEXT:    [[TMP1:%.*]] = call <1 x i16> @llvm.masked.load.v1i16.p0(ptr align 2 [[P:%.*]], <1 x i1> [[TMP0]], <1 x i16> poison)
-; CHECK-NEXT:    [[TMP2:%.*]] = bitcast <1 x i16> [[TMP1]] to i16
-; CHECK-NEXT:    [[TMP3:%.*]] = call <1 x i32> @llvm.masked.load.v1i32.p0(ptr align 4 [[Q:%.*]], <1 x i1> [[TMP0]], <1 x i32> poison)
-; CHECK-NEXT:    [[TMP4:%.*]] = bitcast <1 x i32> [[TMP3]] to i32
-; CHECK-NEXT:    [[TMP5:%.*]] = call <1 x i64> @llvm.masked.load.v1i64.p0(ptr align 8 [[B:%.*]], <1 x i1> [[TMP0]], <1 x i64> poison)
-; CHECK-NEXT:    [[TMP6:%.*]] = bitcast <1 x i64> [[TMP5]] to i64
-; CHECK-NEXT:    [[TMP7:%.*]] = bitcast i16 [[TMP2]] to <1 x i16>
-; CHECK-NEXT:    call void @llvm.masked.store.v1i16.p0(<1 x i16> [[TMP7]], ptr align 8 [[B]], <1 x i1> [[TMP0]])
-; CHECK-NEXT:    [[TMP8:%.*]] = bitcast i32 [[TMP4]] to <1 x i32>
-; CHECK-NEXT:    call void @llvm.masked.store.v1i32.p0(<1 x i32> [[TMP8]], ptr align 4 [[P]], <1 x i1> [[TMP0]])
-; CHECK-NEXT:    [[TMP9:%.*]] = bitcast i64 [[TMP6]] to <1 x i64>
-; CHECK-NEXT:    call void @llvm.masked.store.v1i64.p0(<1 x i64> [[TMP9]], ptr align 8 [[Q]], <1 x i1> [[TMP0]])
-; CHECK-NEXT:    ret void
+; MEM-LABEL: @basic(
+; MEM-NEXT:  entry:
+; MEM-NEXT:    [[TMP0:%.*]] = bitcast i1 [[COND:%.*]] to <1 x i1>
+; MEM-NEXT:    [[TMP1:%.*]] = call <1 x i16> @llvm.masked.load.v1i16.p0(ptr align 2 [[P:%.*]], <1 x i1> [[TMP0]], <1 x i16> poison)
+; MEM-NEXT:    [[TMP2:%.*]] = bitcast <1 x i16> [[TMP1]] to i16
+; MEM-NEXT:    [[TMP3:%.*]] = call <1 x i32> @llvm.masked.load.v1i32.p0(ptr align 4 [[Q:%.*]], <1 x i1> [[TMP0]], <1 x i32> poison)
+; MEM-NEXT:    [[TMP4:%.*]] = bitcast <1 x i32> [[TMP3]] to i32
+; MEM-NEXT:    [[TMP5:%.*]] = call <1 x i64> @llvm.masked.load.v1i64.p0(ptr align 8 [[B:%.*]], <1 x i1> [[TMP0]], <1 x i64> poison)
+; MEM-NEXT:    [[TMP6:%.*]] = bitcast <1 x i64> [[TMP5]] to i64
+; MEM-NEXT:    [[TMP7:%.*]] = bitcast i16 [[TMP2]] to <1 x i16>
+; MEM-NEXT:    call void @llvm.masked.store.v1i16.p0(<1 x i16> [[TMP7]], ptr align 8 [[B]], <1 x i1> [[TMP0]])
+; MEM-NEXT:    [[TMP8:%.*]] = bitcast i32 [[TMP4]] to <1 x i32>
+; MEM-NEXT:    call void @llvm.masked.store.v1i32.p0(<1 x i32> [[TMP8]], ptr align 4 [[P]], <1 x i1> [[TMP0]])
+; MEM-NEXT:    [[TMP9:%.*]] = bitcast i64 [[TMP6]] to <1 x i64>
+; MEM-NEXT:    call void @llvm.masked.store.v1i64.p0(<1 x i64> [[TMP9]], ptr align 8 [[Q]], <1 x i1> [[TMP0]])
+; MEM-NEXT:    ret void
+;
+; NOMEM-LABEL: @basic(
+; NOMEM-NEXT:  entry:
+; NOMEM-NEXT:    br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_END:%.*]]
+; NOMEM:       if.true:
+; NOMEM-NEXT:    [[PV:%.*]] = load i16, ptr [[P:%.*]], align 2
+; NOMEM-NEXT:    [[QV:%.*]] = load i32, ptr [[Q:%.*]], align 4
+; NOMEM-NEXT:    [[BV:%.*]] = load i64, ptr [[B:%.*]], align 8
+; NOMEM-NEXT:    store i16 [[PV]], ptr [[B]], align 8
+; NOMEM-NEXT:    store i32 [[QV]], ptr [[P]], align 4
+; NOMEM-NEXT:    store i64 [[BV]], ptr [[Q]], align 8
+; NOMEM-NEXT:    br label [[IF_END]]
+; NOMEM:       if.end:
+; NOMEM-NEXT:    ret void
 ;
 entry:
   br i1 %cond, label %if.true, label %if.false

diff  --git a/llvm/test/Transforms/SimplifyCFG/X86/hoist-loads-stores-with-cf.ll b/llvm/test/Transforms/SimplifyCFG/X86/hoist-loads-stores-with-cf.ll
index 11abc0ed50841c..4ffd23b20b0996 100644
--- a/llvm/test/Transforms/SimplifyCFG/X86/hoist-loads-stores-with-cf.ll
+++ b/llvm/test/Transforms/SimplifyCFG/X86/hoist-loads-stores-with-cf.ll
@@ -1,8 +1,8 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -mtriple=x86_64 -mattr=+cf -passes='simplifycfg<hoist-loads-stores-with-cond-faulting>' -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s --check-prefixes=CHECK,LOADSTORE
-; RUN: opt < %s -mtriple=x86_64 -mattr=+cf -passes='simplifycfg<hoist-loads-stores-with-cond-faulting>' -simplifycfg-require-and-preserve-domtree=1 -simplifycfg-hoist-loads-with-cond-faulting=false -S | FileCheck %s --check-prefixes=CHECK,NONE,STOREONLY
-; RUN: opt < %s -mtriple=x86_64 -mattr=+cf -passes='simplifycfg<hoist-loads-stores-with-cond-faulting>' -simplifycfg-require-and-preserve-domtree=1 -simplifycfg-hoist-stores-with-cond-faulting=false -S | FileCheck %s --check-prefixes=CHECK,NONE,LOADONLY
-; RUN: opt < %s -mtriple=x86_64 -mattr=+cf -passes='simplifycfg<hoist-loads-stores-with-cond-faulting>' -simplifycfg-require-and-preserve-domtree=1 -simplifycfg-hoist-stores-with-cond-faulting=false -simplifycfg-hoist-loads-with-cond-faulting=false -S | FileCheck %s --check-prefixes=CHECK,NONE,NONEONLY
+; RUN: opt < %s -mtriple=x86_64 -mattr=+cf,+prefer-cf-mem-operand -passes='simplifycfg<hoist-loads-stores-with-cond-faulting>' -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s --check-prefixes=CHECK,LOADSTORE
+; RUN: opt < %s -mtriple=x86_64 -mattr=+cf,+prefer-cf-mem-operand -passes='simplifycfg<hoist-loads-stores-with-cond-faulting>' -simplifycfg-require-and-preserve-domtree=1 -simplifycfg-hoist-loads-with-cond-faulting=false -S | FileCheck %s --check-prefixes=CHECK,NONE,STOREONLY
+; RUN: opt < %s -mtriple=x86_64 -mattr=+cf,+prefer-cf-mem-operand -passes='simplifycfg<hoist-loads-stores-with-cond-faulting>' -simplifycfg-require-and-preserve-domtree=1 -simplifycfg-hoist-stores-with-cond-faulting=false -S | FileCheck %s --check-prefixes=CHECK,NONE,LOADONLY
+; RUN: opt < %s -mtriple=x86_64 -mattr=+cf,+prefer-cf-mem-operand -passes='simplifycfg<hoist-loads-stores-with-cond-faulting>' -simplifycfg-require-and-preserve-domtree=1 -simplifycfg-hoist-stores-with-cond-faulting=false -simplifycfg-hoist-loads-with-cond-faulting=false -S | FileCheck %s --check-prefixes=CHECK,NONE,NONEONLY
 
 ;; Basic case: check masked.load/store is generated for i16/i32/i64.
 define void @basic(i1 %cond, ptr %b, ptr %p, ptr %q) {


        


More information about the cfe-commits mailing list