https://github.com/fzou1 updated 
https://github.com/llvm/llvm-project/pull/242389

>From 61b5365adea120be27e665ff5f81b37e249c75c7 Mon Sep 17 00:00:00 2001
From: "Zou, Feng" <[email protected]>
Date: Fri, 13 Nov 2026 07:23:39 +0100
Subject: [PATCH 1/2] [X86][APX] Enable CF and emit register-form CFCMOV by
 default

Enable conditional faulting (CF) for APX targets. Register-operand CFCMOV is
emitted but memory-operand CFCMOV is disabled by default.

Assist-by: Claude Opus 5.5 <[email protected]>
---
 clang/include/clang/Options/Options.td        |   4 +-
 clang/lib/Basic/Targets/X86.cpp               |   6 +-
 clang/test/CodeGen/attr-target-x86.c          |   4 +-
 clang/test/Driver/cl-x86-flags.c              |   4 +-
 clang/test/Driver/x86-target-features.c       |   4 +-
 .../Preprocessor/predefined-arch-macros.c     |   8 +
 clang/test/Preprocessor/x86_target_features.c |   4 +-
 llvm/lib/Target/X86/X86.td                    |   7 +-
 llvm/lib/Target/X86/X86ISelLowering.cpp       |   3 +-
 llvm/lib/Target/X86/X86InstrCMovSetCC.td      |  18 +-
 llvm/lib/Target/X86/X86InstrPredicates.td     |   2 +
 llvm/lib/Target/X86/X86Options.td             |   6 +
 .../lib/Target/X86/X86TargetTransformInfo.cpp |   3 +-
 llvm/lib/TargetParser/X86TargetParser.cpp     |   6 +-
 .../test/CodeGen/X86/apx/cf-operand-gating.ll | 154 +++++++++++++
 llvm/test/CodeGen/X86/apx/cf.ll               |   2 +-
 llvm/test/CodeGen/X86/apx/cfcmov.ll           | 210 ++++++++++++------
 .../X86/masked-memory-ops-with-cf.ll          |  53 +++--
 .../X86/hoist-loads-stores-with-cf.ll         |   8 +-
 19 files changed, 397 insertions(+), 109 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/apx/cf-operand-gating.ll

diff --git a/clang/include/clang/Options/Options.td 
b/clang/include/clang/Options/Options.td
index 4fc924f848507e..14c2edd90d2d14 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -7686,10 +7686,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 670c85a769d71b..924672c7f699b6 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 c7323484500757..32ea346419a9b9 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..fe184b4e732e3f 100644
--- a/clang/test/Preprocessor/x86_target_features.c
+++ b/clang/test/Preprocessor/x86_target_features.c
@@ -746,7 +746,9 @@
 // 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
+// RUN: %clang -target x86_64-unknown-unknown -march=x86-64 -mapxf 
-mno-apx-features=cf -x c -E -dM -o - %s | FileCheck --check-prefix=APXF-NOCF %s
+// APXF-NOCF-NOT: #define __APX_F__
 // 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..b5e9a720cc1ce3 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.
+// CFCMOV selection is controlled by -x86-cf-reg-operand (default on) and
+// -x86-cf-mem-operand (default off).
 def FeatureCF : SubtargetFeature<"cf", "HasCF", "true",
                                  "Support conditional faulting">;
 def FeatureZU : SubtargetFeature<"zu", "HasZU", "true",
@@ -1309,6 +1308,7 @@ def ProcessorFeatures {
                                                   FeaturePPX,
                                                   FeatureNDD,
                                                   FeatureNF,
+                                                  FeatureCF,
                                                   FeatureJMPABS,
                                                   FeatureMOVRS,
                                                   FeatureAMXMOVRS,
@@ -1482,6 +1482,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 f6d13c236133e5..9be7108760d4dc 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -55948,7 +55948,8 @@ 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.getCLOpts().cf_mem_operand &&
+      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..d479d81b5abbe7 100644
--- a/llvm/lib/Target/X86/X86InstrCMovSetCC.td
+++ b/llvm/lib/Target/X86/X86InstrCMovSetCC.td
@@ -33,9 +33,7 @@ let isCommutable = 1, SchedRW = [WriteCMOV] in {
 let Predicates = [HasCMOV, HasCF, In64BitMode] in {
   def rr : ITy<0x40, MRMDestRegCC, t, (outs t.RegClass:$dst),
                (ins t.RegClass:$src1, ccode:$cond),
-               "cfcmov${cond}", unaryop_ndd_args,
-               [(set t.RegClass:$dst,
-                 (X86cmov 0, t.RegClass:$src1, timm:$cond, EFLAGS))]>, 
UseEFLAGS, NF;
+               "cfcmov${cond}", unaryop_ndd_args, []>, UseEFLAGS, NF;
   def rr_REV : ITy<0x40, MRMSrcRegCC, t, (outs t.RegClass:$dst),
                    (ins t.RegClass:$src1, ccode:$cond),
                    "cfcmov${cond}", unaryop_ndd_args,
@@ -106,14 +104,26 @@ let Predicates = [HasCMOV, HasNDD] in {
   def : Pat<(X86cmov (loadi64 ndd_addr:$src1), GR64:$src2, timm:$cond, EFLAGS),
             (CMOV64rm_ND GR64:$src2, ndd_addr:$src1, (inv_cond_XFORM 
timm:$cond))>;
 }
-let Predicates = [HasCMOV, HasCF] in {
+// Register-operand CFCMOV for select with zero. With
+// -x86-cf-reg-operand=false, XOR + CMOV is used instead.
+let Predicates = [HasCMOV, HasCF, UseCFRegOperand] in {
+  def : Pat<(X86cmov 0, GR16:$src1, timm:$cond, EFLAGS),
+            (CFCMOV16rr GR16:$src1, timm:$cond)>;
+  def : Pat<(X86cmov 0, GR32:$src1, timm:$cond, EFLAGS),
+            (CFCMOV32rr GR32:$src1, timm:$cond)>;
+  def : Pat<(X86cmov 0, GR64:$src1, timm:$cond, EFLAGS),
+            (CFCMOV64rr GR64:$src1, timm:$cond)>;
   def : Pat<(X86cmov GR16:$src1, 0, timm:$cond, EFLAGS),
             (CFCMOV16rr GR16:$src1, (inv_cond_XFORM timm:$cond))>;
   def : Pat<(X86cmov GR32:$src1, 0, timm:$cond, EFLAGS),
             (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))>;
+}
 
+// Memory-operand CFCMOV (conditional faulting load/store) is only selected
+// with -x86-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..2eba5e96515f44 100644
--- a/llvm/lib/Target/X86/X86InstrPredicates.td
+++ b/llvm/lib/Target/X86/X86InstrPredicates.td
@@ -51,6 +51,8 @@ def PreferLegacySetCC  : Predicate<"!Subtarget->hasZU() || "
 def PreferNoLegacySetCC : Predicate<"Subtarget->hasZU() && "
                                     "!Subtarget->preferLegacySetCC()">;
 def HasCF        : Predicate<"Subtarget->hasCF()">;
+def UseCFRegOperand : Predicate<"Subtarget->getCLOpts().cf_reg_operand">;
+def UseCFMemOperand : Predicate<"Subtarget->getCLOpts().cf_mem_operand">;
 def HasJMPABS    : Predicate<"Subtarget->hasJMPABS()">;
 def HasCMOV      : Predicate<"Subtarget->canUseCMOV()">;
 def NoCMOV       : Predicate<"!Subtarget->canUseCMOV()">;
diff --git a/llvm/lib/Target/X86/X86Options.td 
b/llvm/lib/Target/X86/X86Options.td
index 1b4d66efb44d68..921c3bce6b15c4 100644
--- a/llvm/lib/Target/X86/X86Options.td
+++ b/llvm/lib/Target/X86/X86Options.td
@@ -72,6 +72,12 @@ defm : BoolField<"x86-indirect-branch-tracking", "false",
 defm : BoolField<"x86-use-vzeroupper", "true",
   "Minimize AVX to SSE transition penalty">;
 
+// X86InstrCMovSetCC.td
+defm : BoolField<"x86-cf-reg-operand", "true",
+  "Enable CFCMOV with register operands (select with zero)">;
+defm : BoolField<"x86-cf-mem-operand", "false",
+  "Enable CFCMOV with memory operands (conditional faulting load/store)">;
+
 // X86InstrInfo.cpp
 defm : BoolField<"disable-spill-fusing", "false",
   "Disable fusing of spill code into instructions">;
diff --git a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp 
b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
index 95c7f1f18c5a1d..93dcd9918fd146 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())
+  // Memory-operand CFCMOV (conditional load/store) is opt-in.
+  if (!ST->hasCF() || !ST->getCLOpts().cf_mem_operand)
     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..da087b2f59f8c3
--- /dev/null
+++ b/llvm/test/CodeGen/X86/apx/cf-operand-gating.ll
@@ -0,0 +1,154 @@
+; 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 -x86-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=CPU,CPU-REG
+; RUN: llc < %s -mtriple=x86_64 -mcpu=novalake -verify-machineinstrs | 
FileCheck %s --check-prefixes=CPU,CPU-REG
+; RUN: llc < %s -mtriple=x86_64 -mcpu=novalake -x86-cf-reg-operand=false 
-verify-machineinstrs | FileCheck %s --check-prefixes=CPU,CPU-NOREG
+
+;; +cf emits register-form CFCMOV only; memory forms need -x86-cf-mem-operand.
+;; DMR and NVL enable CF by default. -x86-cf-reg-operand=false uses XOR + CMOV.
+
+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
+;
+; CPU-REG-LABEL: reg_form:
+; CPU-REG:       # %bb.0:
+; CPU-REG-NEXT:    testq %rdi, %rdi
+; CPU-REG-NEXT:    cfcmoveq %rsi, %rax
+; CPU-REG-NEXT:    retq
+;
+; CPU-NOREG-LABEL: reg_form:
+; CPU-NOREG:       # %bb.0:
+; CPU-NOREG-NEXT:    xorl %eax, %eax
+; CPU-NOREG-NEXT:    testq %rdi, %rdi
+; CPU-NOREG-NEXT:    cmoveq %rsi, %rax
+; CPU-NOREG-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
+}
+
+;; An i16 select of a foldable load is not promoted to i32. With NDD, the GR16
+;; CFCMOV patterns are picked over CMOV16rm_ND: (X86cmov 0, x) here and
+;; (X86cmov x, 0) below.
+define i16 @reg_form_i16_load(i1 %c, ptr %p) {
+; CHECK-LABEL: reg_form_i16_load:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    testb $1, %dil
+; CHECK-NEXT:    je .LBB2_1
+; CHECK-NEXT:  # %bb.2:
+; CHECK-NEXT:    movzwl (%rsi), %eax
+; CHECK-NEXT:    # kill: def $ax killed $ax killed $eax
+; CHECK-NEXT:    retq
+; CHECK-NEXT:  .LBB2_1:
+; CHECK-NEXT:    xorl %eax, %eax
+; CHECK-NEXT:    # kill: def $ax killed $ax killed $eax
+; CHECK-NEXT:    retq
+;
+; CPU-REG-LABEL: reg_form_i16_load:
+; CPU-REG:       # %bb.0:
+; CPU-REG-NEXT:    movzwl (%rsi), %eax
+; CPU-REG-NEXT:    testb $1, %dil
+; CPU-REG-NEXT:    cfcmovnew %ax, %ax
+; CPU-REG-NEXT:    retq
+;
+; CPU-NOREG-LABEL: reg_form_i16_load:
+; CPU-NOREG:       # %bb.0:
+; CPU-NOREG-NEXT:    xorl %eax, %eax
+; CPU-NOREG-NEXT:    testb $1, %dil
+; CPU-NOREG-NEXT:    cmovnew (%rsi), %ax
+; CPU-NOREG-NEXT:    # kill: def $ax killed $ax killed $eax
+; CPU-NOREG-NEXT:    retq
+  %l = load i16, ptr %p
+  %r = select i1 %c, i16 %l, i16 0
+  ret i16 %r
+}
+
+define i16 @reg_form_i16_load_inv(i1 %c, ptr %p) {
+; CHECK-LABEL: reg_form_i16_load_inv:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    testb $1, %dil
+; CHECK-NEXT:    jne .LBB3_1
+; CHECK-NEXT:  # %bb.2:
+; CHECK-NEXT:    movzwl (%rsi), %eax
+; CHECK-NEXT:    # kill: def $ax killed $ax killed $eax
+; CHECK-NEXT:    retq
+; CHECK-NEXT:  .LBB3_1:
+; CHECK-NEXT:    xorl %eax, %eax
+; CHECK-NEXT:    # kill: def $ax killed $ax killed $eax
+; CHECK-NEXT:    retq
+;
+; CPU-REG-LABEL: reg_form_i16_load_inv:
+; CPU-REG:       # %bb.0:
+; CPU-REG-NEXT:    movzwl (%rsi), %eax
+; CPU-REG-NEXT:    testb $1, %dil
+; CPU-REG-NEXT:    cfcmovew %ax, %ax
+; CPU-REG-NEXT:    retq
+;
+; CPU-NOREG-LABEL: reg_form_i16_load_inv:
+; CPU-NOREG:       # %bb.0:
+; CPU-NOREG-NEXT:    xorl %eax, %eax
+; CPU-NOREG-NEXT:    testb $1, %dil
+; CPU-NOREG-NEXT:    cmovew (%rsi), %ax
+; CPU-NOREG-NEXT:    # kill: def $ax killed $ax killed $eax
+; CPU-NOREG-NEXT:    retq
+  %l = load i16, ptr %p
+  %r = select i1 %c, i16 0, i16 %l
+  ret i16 %r
+}
diff --git a/llvm/test/CodeGen/X86/apx/cf.ll b/llvm/test/CodeGen/X86/apx/cf.ll
index 75a1253b36e257..acb59ce0bc9c2e 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 -x86-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..bd84a5d49d17d1 100644
--- a/llvm/test/CodeGen/X86/apx/cfcmov.ll
+++ b/llvm/test/CodeGen/X86/apx/cfcmov.ll
@@ -1,106 +1,176 @@
 ; 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,REG,NOMEM
+; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+cf -x86-cmov-converter=false 
-verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,REG,NOMEM
+; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+cf -x86-cf-mem-operand 
-verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,REG,MEM
+; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+cf -x86-cf-reg-operand=false 
-verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,NOMEM,NOREG
 
 define i8 @cfcmov8rr(i8 %0) {
-; CHECK-LABEL: cfcmov8rr:
-; CHECK:       # %bb.0:
-; CHECK-NEXT:    cmpb $1, %dil
-; CHECK-NEXT:    cfcmovel %edi, %eax
-; CHECK-NEXT:    # kill: def $al killed $al killed $eax
-; CHECK-NEXT:    retq
+; REG-LABEL: cfcmov8rr:
+; REG:       # %bb.0:
+; REG-NEXT:    cmpb $1, %dil
+; REG-NEXT:    cfcmovel %edi, %eax
+; REG-NEXT:    # kill: def $al killed $al killed $eax
+; REG-NEXT:    retq
+;
+; NOREG-LABEL: cfcmov8rr:
+; NOREG:       # %bb.0:
+; NOREG-NEXT:    xorl %eax, %eax
+; NOREG-NEXT:    cmpb $1, %dil
+; NOREG-NEXT:    cmovel %edi, %eax
+; NOREG-NEXT:    # kill: def $al killed $al killed $eax
+; NOREG-NEXT:    retq
   %2 = icmp eq i8 %0, 1
   %3 = select i1 %2, i8 %0, i8 0
   ret i8 %3
 }
 
 define i16 @cfcmov16rr(i16 %0) {
-; CHECK-LABEL: cfcmov16rr:
-; CHECK:       # %bb.0:
-; CHECK-NEXT:    cmpw $1, %di
-; CHECK-NEXT:    cfcmovnel %edi, %eax
-; CHECK-NEXT:    # kill: def $ax killed $ax killed $eax
-; CHECK-NEXT:    retq
+; REG-LABEL: cfcmov16rr:
+; REG:       # %bb.0:
+; REG-NEXT:    cmpw $1, %di
+; REG-NEXT:    cfcmovnel %edi, %eax
+; REG-NEXT:    # kill: def $ax killed $ax killed $eax
+; REG-NEXT:    retq
+;
+; NOREG-LABEL: cfcmov16rr:
+; NOREG:       # %bb.0:
+; NOREG-NEXT:    xorl %eax, %eax
+; NOREG-NEXT:    cmpw $1, %di
+; NOREG-NEXT:    cmovnel %edi, %eax
+; NOREG-NEXT:    # kill: def $ax killed $ax killed $eax
+; NOREG-NEXT:    retq
   %2 = icmp ne i16 %0, 1
   %3 = select i1 %2, i16 %0, i16 0
   ret i16 %3
 }
 
 define i32 @cfcmov32rr(i32 %0) {
-; CHECK-LABEL: cfcmov32rr:
-; CHECK:       # %bb.0:
-; CHECK-NEXT:    cmpl $2, %edi
-; CHECK-NEXT:    cfcmovael %edi, %eax
-; CHECK-NEXT:    retq
+; REG-LABEL: cfcmov32rr:
+; REG:       # %bb.0:
+; REG-NEXT:    cmpl $2, %edi
+; REG-NEXT:    cfcmovael %edi, %eax
+; REG-NEXT:    retq
+;
+; NOREG-LABEL: cfcmov32rr:
+; NOREG:       # %bb.0:
+; NOREG-NEXT:    xorl %eax, %eax
+; NOREG-NEXT:    cmpl $2, %edi
+; NOREG-NEXT:    cmovael %edi, %eax
+; NOREG-NEXT:    retq
   %2 = icmp ugt i32 %0, 1
   %3 = select i1 %2, i32 %0, i32 0
   ret i32 %3
 }
 
 define i64 @cfcmov64rr(i64 %0) {
-; CHECK-LABEL: cfcmov64rr:
-; CHECK:       # %bb.0:
-; CHECK-NEXT:    testq %rdi, %rdi
-; CHECK-NEXT:    cfcmoveq %rdi, %rax
-; CHECK-NEXT:    retq
+; REG-LABEL: cfcmov64rr:
+; REG:       # %bb.0:
+; REG-NEXT:    testq %rdi, %rdi
+; REG-NEXT:    cfcmoveq %rdi, %rax
+; REG-NEXT:    retq
+;
+; NOREG-LABEL: cfcmov64rr:
+; NOREG:       # %bb.0:
+; NOREG-NEXT:    xorl %eax, %eax
+; NOREG-NEXT:    testq %rdi, %rdi
+; NOREG-NEXT:    cmoveq %rdi, %rax
+; NOREG-NEXT:    retq
   %2 = icmp ult i64 %0, 1
   %3 = select i1 %2, i64 %0, i64 0
   ret i64 %3
 }
 
 define i8 @cfcmov8rr_inv(i8 %0) {
-; CHECK-LABEL: cfcmov8rr_inv:
-; CHECK:       # %bb.0:
-; CHECK-NEXT:    cmpb $1, %dil
-; CHECK-NEXT:    cfcmovnel %edi, %eax
-; CHECK-NEXT:    # kill: def $al killed $al killed $eax
-; CHECK-NEXT:    retq
+; REG-LABEL: cfcmov8rr_inv:
+; REG:       # %bb.0:
+; REG-NEXT:    cmpb $1, %dil
+; REG-NEXT:    cfcmovnel %edi, %eax
+; REG-NEXT:    # kill: def $al killed $al killed $eax
+; REG-NEXT:    retq
+;
+; NOREG-LABEL: cfcmov8rr_inv:
+; NOREG:       # %bb.0:
+; NOREG-NEXT:    xorl %eax, %eax
+; NOREG-NEXT:    cmpb $1, %dil
+; NOREG-NEXT:    cmovnel %edi, %eax
+; NOREG-NEXT:    # kill: def $al killed $al killed $eax
+; NOREG-NEXT:    retq
   %2 = icmp eq i8 %0, 1
   %3 = select i1 %2, i8 0, i8 %0
   ret i8 %3
 }
 
 define i16 @cfcmov16rr_inv(i16 %0) {
-; CHECK-LABEL: cfcmov16rr_inv:
-; CHECK:       # %bb.0:
-; CHECK-NEXT:    cmpw $1, %di
-; CHECK-NEXT:    cfcmovel %edi, %eax
-; CHECK-NEXT:    # kill: def $ax killed $ax killed $eax
-; CHECK-NEXT:    retq
+; REG-LABEL: cfcmov16rr_inv:
+; REG:       # %bb.0:
+; REG-NEXT:    cmpw $1, %di
+; REG-NEXT:    cfcmovel %edi, %eax
+; REG-NEXT:    # kill: def $ax killed $ax killed $eax
+; REG-NEXT:    retq
+;
+; NOREG-LABEL: cfcmov16rr_inv:
+; NOREG:       # %bb.0:
+; NOREG-NEXT:    xorl %eax, %eax
+; NOREG-NEXT:    cmpw $1, %di
+; NOREG-NEXT:    cmovel %edi, %eax
+; NOREG-NEXT:    # kill: def $ax killed $ax killed $eax
+; NOREG-NEXT:    retq
   %2 = icmp ne i16 %0, 1
   %3 = select i1 %2, i16 0, i16 %0
   ret i16 %3
 }
 
 define i32 @cfcmov32rr_inv(i32 %0) {
-; CHECK-LABEL: cfcmov32rr_inv:
-; CHECK:       # %bb.0:
-; CHECK-NEXT:    cmpl $2, %edi
-; CHECK-NEXT:    cfcmovbl %edi, %eax
-; CHECK-NEXT:    retq
+; REG-LABEL: cfcmov32rr_inv:
+; REG:       # %bb.0:
+; REG-NEXT:    cmpl $2, %edi
+; REG-NEXT:    cfcmovbl %edi, %eax
+; REG-NEXT:    retq
+;
+; NOREG-LABEL: cfcmov32rr_inv:
+; NOREG:       # %bb.0:
+; NOREG-NEXT:    xorl %eax, %eax
+; NOREG-NEXT:    cmpl $2, %edi
+; NOREG-NEXT:    cmovbl %edi, %eax
+; NOREG-NEXT:    retq
   %2 = icmp ugt i32 %0, 1
   %3 = select i1 %2, i32 0, i32 %0
   ret i32 %3
 }
 
 define i64 @cfcmov64rr_inv(i64 %0) {
-; CHECK-LABEL: cfcmov64rr_inv:
-; CHECK:       # %bb.0:
-; CHECK-NEXT:    cmpq $2, %rdi
-; CHECK-NEXT:    cfcmovaeq %rdi, %rax
-; CHECK-NEXT:    retq
+; REG-LABEL: cfcmov64rr_inv:
+; REG:       # %bb.0:
+; REG-NEXT:    cmpq $2, %rdi
+; REG-NEXT:    cfcmovaeq %rdi, %rax
+; REG-NEXT:    retq
+;
+; NOREG-LABEL: cfcmov64rr_inv:
+; NOREG:       # %bb.0:
+; NOREG-NEXT:    xorl %eax, %eax
+; NOREG-NEXT:    cmpq $2, %rdi
+; NOREG-NEXT:    cmovaeq %rdi, %rax
+; NOREG-NEXT:    retq
   %2 = icmp ule i64 %0, 1
   %3 = select i1 %2, i64 0, i64 %0
   ret i64 %3
 }
 
 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 +179,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 +199,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..2e861c2244cf26 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,41 @@
 ; 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 -x86-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 -x86-cf-mem-operand, masked.load/store.v1* is generated in simplifycfg
+;; and not expanded back to branch+load/store by later passes; without it 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..b11d02f5950f07 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 -x86-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 -x86-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 -x86-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 -x86-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) {

>From 2e53e81185d972aa624a6f372e853692b43ec49b Mon Sep 17 00:00:00 2001
From: "Zou, Feng" <[email protected]>
Date: Mon, 16 Nov 2026 03:50:43 +0100
Subject: [PATCH 2/2] [X86][APX] Make CFCMOV operand selection tuning features

- TuningPreferCFRegOperand ("prefer-cf-reg-operand") enables
  register-operand CFCMOV for select with zero. It is added to the DMR and
  NVL tuning lists.
- TuningPreferCFMemOperand ("prefer-cf-mem-operand") enables
  memory-operand CFCMOV (conditional faulting load/store). It is not set
  for any CPU.

Assisted-by: Claude Opus 5.5 <[email protected]>
---
 llvm/lib/Target/X86/X86.td                    |  21 ++-
 llvm/lib/Target/X86/X86ISelLowering.cpp       |   3 +-
 llvm/lib/Target/X86/X86InstrCMovSetCC.td      |   9 +-
 llvm/lib/Target/X86/X86InstrPredicates.td     |   3 +-
 llvm/lib/Target/X86/X86Options.td             |   6 -
 .../lib/Target/X86/X86TargetTransformInfo.cpp |   4 +-
 .../test/CodeGen/X86/apx/cf-operand-gating.ll | 152 +++++++++++++++---
 llvm/test/CodeGen/X86/apx/cf.ll               |   2 +-
 llvm/test/CodeGen/X86/apx/cfcmov.ll           |   8 +-
 .../X86/masked-memory-ops-with-cf.ll          |   7 +-
 .../X86/hoist-loads-stores-with-cf.ll         |   8 +-
 11 files changed, 164 insertions(+), 59 deletions(-)

diff --git a/llvm/lib/Target/X86/X86.td b/llvm/lib/Target/X86/X86.td
index b5e9a720cc1ce3..62923ca75bc94f 100644
--- a/llvm/lib/Target/X86/X86.td
+++ b/llvm/lib/Target/X86/X86.td
@@ -375,8 +375,8 @@ def FeatureCCMP : SubtargetFeature<"ccmp", "HasCCMP", 
"true",
                                    "Support conditional cmp & test 
instructions">;
 def FeatureNF : SubtargetFeature<"nf", "HasNF", "true",
                                  "Support status flags update suppression">;
-// CFCMOV selection is controlled by -x86-cf-reg-operand (default on) and
-// -x86-cf-mem-operand (default off).
+// CFCMOV selection is controlled by the tuning features 
TuningPreferCFRegOperand
+// and TuningPreferCFMemOperand.
 def FeatureCF : SubtargetFeature<"cf", "HasCF", "true",
                                  "Support conditional faulting">;
 def FeatureZU : SubtargetFeature<"zu", "HasZU", "true",
@@ -907,6 +907,17 @@ def TuningPreferLegacySetCC
     : SubtargetFeature<"prefer-legacy-setcc", "PreferLegacySetCC", "true",
                        "Prefer to emit legacy SetCC.", [], InlineIgnore>;
 
+def TuningPreferCFRegOperand
+    : SubtargetFeature<"prefer-cf-reg-operand", "PreferCFRegOperand", "true",
+                       "Prefer CFCMOV with register operands (select with 
zero)",
+                       [], InlineIgnore>;
+
+def TuningPreferCFMemOperand
+    : SubtargetFeature<"prefer-cf-mem-operand", "PreferCFMemOperand", "true",
+                       "Prefer CFCMOV with memory operands (conditional 
faulting "
+                       "load/store)",
+                       [], InlineIgnore>;
+
 
//===----------------------------------------------------------------------===//
 // X86 CPU Families
 // TODO: Remove these - use general tuning features to determine codegen.
@@ -1316,7 +1327,8 @@ def ProcessorFeatures {
                                                   FeatureAMXFP8];
   list<SubtargetFeature> DMRFeatures =
     !listconcat(GNRDFeatures, DMRAdditionalFeatures);
-  list<SubtargetFeature> DMRAdditionalTuning = [TuningPreferLegacySetCC];
+  list<SubtargetFeature> DMRAdditionalTuning = [TuningPreferLegacySetCC,
+                                                 TuningPreferCFRegOperand];
   list<SubtargetFeature> DMRTuning = !listconcat(GNRTuning, 
DMRAdditionalTuning);
 
   // Atom
@@ -1490,7 +1502,8 @@ def ProcessorFeatures {
                                                   FeaturePREFETCHI];
   list<SubtargetFeature> NVLFeatures =
       !listconcat(PTLFeatures, NVLAdditionalFeatures);
-  list<SubtargetFeature> NVLAdditionalTuning = [TuningPreferLegacySetCC];
+  list<SubtargetFeature> NVLAdditionalTuning = [TuningPreferLegacySetCC,
+                                                 TuningPreferCFRegOperand];
   list<SubtargetFeature> NVLTuning = !listconcat(ADLTuning, 
NVLAdditionalTuning);
 
   // Clearwaterforest
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp 
b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 9be7108760d4dc..3a31a6c128982d 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -55948,8 +55948,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() && Subtarget.getCLOpts().cf_mem_operand &&
-      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 d479d81b5abbe7..5845f12bde96af 100644
--- a/llvm/lib/Target/X86/X86InstrCMovSetCC.td
+++ b/llvm/lib/Target/X86/X86InstrCMovSetCC.td
@@ -104,8 +104,7 @@ let Predicates = [HasCMOV, HasNDD] in {
   def : Pat<(X86cmov (loadi64 ndd_addr:$src1), GR64:$src2, timm:$cond, EFLAGS),
             (CMOV64rm_ND GR64:$src2, ndd_addr:$src1, (inv_cond_XFORM 
timm:$cond))>;
 }
-// Register-operand CFCMOV for select with zero. With
-// -x86-cf-reg-operand=false, XOR + CMOV is used instead.
+// Select with zero. Without prefer-cf-reg-operand, XOR + CMOV is used.
 let Predicates = [HasCMOV, HasCF, UseCFRegOperand] in {
   def : Pat<(X86cmov 0, GR16:$src1, timm:$cond, EFLAGS),
             (CFCMOV16rr GR16:$src1, timm:$cond)>;
@@ -121,9 +120,9 @@ let Predicates = [HasCMOV, HasCF, UseCFRegOperand] in {
             (CFCMOV64rr GR64:$src1, (inv_cond_XFORM timm:$cond))>;
 }
 
-// Memory-operand CFCMOV (conditional faulting load/store) is only selected
-// with -x86-cf-mem-operand.
-let Predicates = [HasCMOV, HasCF, UseCFMemOperand] in {
+// Conditional faulting load/store. CLOAD/CSTORE are only formed with
+// prefer-cf-mem-operand.
+let Predicates = [HasCMOV, HasCF] 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 2eba5e96515f44..80fe8d93126561 100644
--- a/llvm/lib/Target/X86/X86InstrPredicates.td
+++ b/llvm/lib/Target/X86/X86InstrPredicates.td
@@ -51,8 +51,7 @@ def PreferLegacySetCC  : Predicate<"!Subtarget->hasZU() || "
 def PreferNoLegacySetCC : Predicate<"Subtarget->hasZU() && "
                                     "!Subtarget->preferLegacySetCC()">;
 def HasCF        : Predicate<"Subtarget->hasCF()">;
-def UseCFRegOperand : Predicate<"Subtarget->getCLOpts().cf_reg_operand">;
-def UseCFMemOperand : Predicate<"Subtarget->getCLOpts().cf_mem_operand">;
+def UseCFRegOperand : Predicate<"Subtarget->preferCFRegOperand()">;
 def HasJMPABS    : Predicate<"Subtarget->hasJMPABS()">;
 def HasCMOV      : Predicate<"Subtarget->canUseCMOV()">;
 def NoCMOV       : Predicate<"!Subtarget->canUseCMOV()">;
diff --git a/llvm/lib/Target/X86/X86Options.td 
b/llvm/lib/Target/X86/X86Options.td
index 921c3bce6b15c4..1b4d66efb44d68 100644
--- a/llvm/lib/Target/X86/X86Options.td
+++ b/llvm/lib/Target/X86/X86Options.td
@@ -72,12 +72,6 @@ defm : BoolField<"x86-indirect-branch-tracking", "false",
 defm : BoolField<"x86-use-vzeroupper", "true",
   "Minimize AVX to SSE transition penalty">;
 
-// X86InstrCMovSetCC.td
-defm : BoolField<"x86-cf-reg-operand", "true",
-  "Enable CFCMOV with register operands (select with zero)">;
-defm : BoolField<"x86-cf-mem-operand", "false",
-  "Enable CFCMOV with memory operands (conditional faulting load/store)">;
-
 // X86InstrInfo.cpp
 defm : BoolField<"disable-spill-fusing", "false",
   "Disable fusing of spill code into instructions">;
diff --git a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp 
b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
index 93dcd9918fd146..54cc9a9aeb3efe 100644
--- a/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
+++ b/llvm/lib/Target/X86/X86TargetTransformInfo.cpp
@@ -155,8 +155,8 @@ unsigned X86TTIImpl::getNumberOfRegisters(unsigned ClassID) 
const {
 }
 
 bool X86TTIImpl::hasConditionalLoadStoreForType(Type *Ty, bool IsStore) const {
-  // Memory-operand CFCMOV (conditional load/store) is opt-in.
-  if (!ST->hasCF() || !ST->getCLOpts().cf_mem_operand)
+  // Conditional faulting load/store requires prefer-cf-mem-operand.
+  if (!ST->hasCF() || !ST->preferCFMemOperand())
     return false;
   if (!Ty)
     return true;
diff --git a/llvm/test/CodeGen/X86/apx/cf-operand-gating.ll 
b/llvm/test/CodeGen/X86/apx/cf-operand-gating.ll
index da087b2f59f8c3..f9bd2eed74fce0 100644
--- a/llvm/test/CodeGen/X86/apx/cf-operand-gating.ll
+++ b/llvm/test/CodeGen/X86/apx/cf-operand-gating.ll
@@ -1,14 +1,25 @@
 ; 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 -x86-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=CPU,CPU-REG
-; RUN: llc < %s -mtriple=x86_64 -mcpu=novalake -verify-machineinstrs | 
FileCheck %s --check-prefixes=CPU,CPU-REG
-; RUN: llc < %s -mtriple=x86_64 -mcpu=novalake -x86-cf-reg-operand=false 
-verify-machineinstrs | FileCheck %s --check-prefixes=CPU,CPU-NOREG
+; RUN: llc < %s -mtriple=x86_64 -mattr=+cf -verify-machineinstrs | FileCheck 
%s --check-prefixes=NOTUNE
+; RUN: llc < %s -mtriple=x86_64 -mattr=+cf,+prefer-cf-reg-operand 
-verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,NOMEM
+; RUN: llc < %s -mtriple=x86_64 
-mattr=+cf,+prefer-cf-reg-operand,+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=CPU-REG
+; RUN: llc < %s -mtriple=x86_64 -mcpu=novalake -verify-machineinstrs | 
FileCheck %s --check-prefixes=CPU-REG
+; RUN: llc < %s -mtriple=x86_64 -mcpu=novalake -mattr=-prefer-cf-reg-operand 
-verify-machineinstrs | FileCheck %s --check-prefixes=CPU-NOREG
+; RUN: llc < %s -mtriple=x86_64 -mcpu=novalake 
-mattr=-cf,+prefer-cf-mem-operand -verify-machineinstrs | FileCheck %s 
--check-prefixes=CPU-NOREG
+; RUN: llc < %s -mtriple=x86_64 -mcpu=novalake -mattr=+prefer-cf-mem-operand 
-verify-machineinstrs | FileCheck %s --check-prefixes=CPU-MEM
 
-;; +cf emits register-form CFCMOV only; memory forms need -x86-cf-mem-operand.
-;; DMR and NVL enable CF by default. -x86-cf-reg-operand=false uses XOR + CMOV.
+;; +cf alone emits no CFCMOV. Register forms need +prefer-cf-reg-operand and
+;; memory forms need +prefer-cf-mem-operand. DMR and NVL enable CF and
+;; prefer-cf-reg-operand by default; -prefer-cf-reg-operand uses XOR + CMOV.
 
 define i64 @reg_form(i64 %a, i64 %x) {
+; NOTUNE-LABEL: reg_form:
+; NOTUNE:       # %bb.0:
+; NOTUNE-NEXT:    xorl %eax, %eax
+; NOTUNE-NEXT:    testq %rdi, %rdi
+; NOTUNE-NEXT:    cmoveq %rsi, %rax
+; NOTUNE-NEXT:    retq
+;
 ; CHECK-LABEL: reg_form:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    testq %rdi, %rdi
@@ -27,12 +38,37 @@ define i64 @reg_form(i64 %a, i64 %x) {
 ; CPU-NOREG-NEXT:    testq %rdi, %rdi
 ; CPU-NOREG-NEXT:    cmoveq %rsi, %rax
 ; CPU-NOREG-NEXT:    retq
+;
+; CPU-MEM-LABEL: reg_form:
+; CPU-MEM:       # %bb.0:
+; CPU-MEM-NEXT:    testq %rdi, %rdi
+; CPU-MEM-NEXT:    cfcmoveq %rsi, %rax
+; CPU-MEM-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) {
+; NOTUNE-LABEL: masked_v1:
+; NOTUNE:       # %bb.0:
+; NOTUNE-NEXT:    testl %edi, %edi
+; NOTUNE-NEXT:    sete %cl
+; NOTUNE-NEXT:    # implicit-def: $eax
+; NOTUNE-NEXT:    je .LBB1_1
+; NOTUNE-NEXT:  # %bb.2: # %else
+; NOTUNE-NEXT:    testb %cl, %cl
+; NOTUNE-NEXT:    jne .LBB1_3
+; NOTUNE-NEXT:  .LBB1_4: # %else1
+; NOTUNE-NEXT:    retq
+; NOTUNE-NEXT:  .LBB1_1: # %cond.load
+; NOTUNE-NEXT:    movl (%rsi), %eax
+; NOTUNE-NEXT:    testb %cl, %cl
+; NOTUNE-NEXT:    je .LBB1_4
+; NOTUNE-NEXT:  .LBB1_3: # %cond.store
+; NOTUNE-NEXT:    movl %eax, (%rdx)
+; NOTUNE-NEXT:    retq
+;
 ; NOMEM-LABEL: masked_v1:
 ; NOMEM:       # %bb.0:
 ; NOMEM-NEXT:    testl %edi, %edi
@@ -59,24 +95,50 @@ define void @masked_v1(i32 %a, ptr %b, ptr %p) {
 ; 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
+; CPU-REG-LABEL: masked_v1:
+; CPU-REG:       # %bb.0:
+; CPU-REG-NEXT:    # implicit-def: $eax
+; CPU-REG-NEXT:    testl %edi, %edi
+; CPU-REG-NEXT:    sete %cl
+; CPU-REG-NEXT:    je .LBB1_1
+; CPU-REG-NEXT:  # %bb.2: # %else
+; CPU-REG-NEXT:    testb $1, %cl
+; CPU-REG-NEXT:    jne .LBB1_3
+; CPU-REG-NEXT:  .LBB1_4: # %else1
+; CPU-REG-NEXT:    retq
+; CPU-REG-NEXT:  .LBB1_1: # %cond.load
+; CPU-REG-NEXT:    movl (%rsi), %eax
+; CPU-REG-NEXT:    testb $1, %cl
+; CPU-REG-NEXT:    je .LBB1_4
+; CPU-REG-NEXT:  .LBB1_3: # %cond.store
+; CPU-REG-NEXT:    movl %eax, (%rdx)
+; CPU-REG-NEXT:    retq
+;
+; CPU-NOREG-LABEL: masked_v1:
+; CPU-NOREG:       # %bb.0:
+; CPU-NOREG-NEXT:    # implicit-def: $eax
+; CPU-NOREG-NEXT:    testl %edi, %edi
+; CPU-NOREG-NEXT:    sete %cl
+; CPU-NOREG-NEXT:    je .LBB1_1
+; CPU-NOREG-NEXT:  # %bb.2: # %else
+; CPU-NOREG-NEXT:    testb $1, %cl
+; CPU-NOREG-NEXT:    jne .LBB1_3
+; CPU-NOREG-NEXT:  .LBB1_4: # %else1
+; CPU-NOREG-NEXT:    retq
+; CPU-NOREG-NEXT:  .LBB1_1: # %cond.load
+; CPU-NOREG-NEXT:    movl (%rsi), %eax
+; CPU-NOREG-NEXT:    testb $1, %cl
+; CPU-NOREG-NEXT:    je .LBB1_4
+; CPU-NOREG-NEXT:  .LBB1_3: # %cond.store
+; CPU-NOREG-NEXT:    movl %eax, (%rdx)
+; CPU-NOREG-NEXT:    retq
+;
+; CPU-MEM-LABEL: masked_v1:
+; CPU-MEM:       # %bb.0:
+; CPU-MEM-NEXT:    testl %edi, %edi
+; CPU-MEM-NEXT:    cfcmovel (%rsi), %eax
+; CPU-MEM-NEXT:    cfcmovel %eax, (%rdx)
+; CPU-MEM-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)
@@ -88,6 +150,19 @@ define void @masked_v1(i32 %a, ptr %b, ptr %p) {
 ;; CFCMOV patterns are picked over CMOV16rm_ND: (X86cmov 0, x) here and
 ;; (X86cmov x, 0) below.
 define i16 @reg_form_i16_load(i1 %c, ptr %p) {
+; NOTUNE-LABEL: reg_form_i16_load:
+; NOTUNE:       # %bb.0:
+; NOTUNE-NEXT:    testb $1, %dil
+; NOTUNE-NEXT:    je .LBB2_1
+; NOTUNE-NEXT:  # %bb.2:
+; NOTUNE-NEXT:    movzwl (%rsi), %eax
+; NOTUNE-NEXT:    # kill: def $ax killed $ax killed $eax
+; NOTUNE-NEXT:    retq
+; NOTUNE-NEXT:  .LBB2_1:
+; NOTUNE-NEXT:    xorl %eax, %eax
+; NOTUNE-NEXT:    # kill: def $ax killed $ax killed $eax
+; NOTUNE-NEXT:    retq
+;
 ; CHECK-LABEL: reg_form_i16_load:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    testb $1, %dil
@@ -115,12 +190,32 @@ define i16 @reg_form_i16_load(i1 %c, ptr %p) {
 ; CPU-NOREG-NEXT:    cmovnew (%rsi), %ax
 ; CPU-NOREG-NEXT:    # kill: def $ax killed $ax killed $eax
 ; CPU-NOREG-NEXT:    retq
+;
+; CPU-MEM-LABEL: reg_form_i16_load:
+; CPU-MEM:       # %bb.0:
+; CPU-MEM-NEXT:    movzwl (%rsi), %eax
+; CPU-MEM-NEXT:    testb $1, %dil
+; CPU-MEM-NEXT:    cfcmovnew %ax, %ax
+; CPU-MEM-NEXT:    retq
   %l = load i16, ptr %p
   %r = select i1 %c, i16 %l, i16 0
   ret i16 %r
 }
 
 define i16 @reg_form_i16_load_inv(i1 %c, ptr %p) {
+; NOTUNE-LABEL: reg_form_i16_load_inv:
+; NOTUNE:       # %bb.0:
+; NOTUNE-NEXT:    testb $1, %dil
+; NOTUNE-NEXT:    jne .LBB3_1
+; NOTUNE-NEXT:  # %bb.2:
+; NOTUNE-NEXT:    movzwl (%rsi), %eax
+; NOTUNE-NEXT:    # kill: def $ax killed $ax killed $eax
+; NOTUNE-NEXT:    retq
+; NOTUNE-NEXT:  .LBB3_1:
+; NOTUNE-NEXT:    xorl %eax, %eax
+; NOTUNE-NEXT:    # kill: def $ax killed $ax killed $eax
+; NOTUNE-NEXT:    retq
+;
 ; CHECK-LABEL: reg_form_i16_load_inv:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    testb $1, %dil
@@ -148,6 +243,13 @@ define i16 @reg_form_i16_load_inv(i1 %c, ptr %p) {
 ; CPU-NOREG-NEXT:    cmovew (%rsi), %ax
 ; CPU-NOREG-NEXT:    # kill: def $ax killed $ax killed $eax
 ; CPU-NOREG-NEXT:    retq
+;
+; CPU-MEM-LABEL: reg_form_i16_load_inv:
+; CPU-MEM:       # %bb.0:
+; CPU-MEM-NEXT:    movzwl (%rsi), %eax
+; CPU-MEM-NEXT:    testb $1, %dil
+; CPU-MEM-NEXT:    cfcmovew %ax, %ax
+; CPU-MEM-NEXT:    retq
   %l = load i16, ptr %p
   %r = select i1 %c, i16 0, i16 %l
   ret i16 %r
diff --git a/llvm/test/CodeGen/X86/apx/cf.ll b/llvm/test/CodeGen/X86/apx/cf.ll
index acb59ce0bc9c2e..52a28677a6db43 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 -x86-cf-mem-operand 
-verify-machineinstrs | FileCheck %s
+; RUN: llc < %s -mtriple=x86_64 
-mattr=+cf,+nf,+avx512f,+prefer-cf-reg-operand,+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 bd84a5d49d17d1..589bf0eaec8080 100644
--- a/llvm/test/CodeGen/X86/apx/cfcmov.ll
+++ b/llvm/test/CodeGen/X86/apx/cfcmov.ll
@@ -1,8 +1,8 @@
 ; 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 --check-prefixes=CHECK,REG,NOMEM
-; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+cf -x86-cmov-converter=false 
-verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,REG,NOMEM
-; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+cf -x86-cf-mem-operand 
-verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,REG,MEM
-; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+cf -x86-cf-reg-operand=false 
-verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,NOMEM,NOREG
+; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+cf,+prefer-cf-reg-operand 
-verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,REG,NOMEM
+; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+cf,+prefer-cf-reg-operand 
-x86-cmov-converter=false -verify-machineinstrs | FileCheck %s 
--check-prefixes=CHECK,REG,NOMEM
+; RUN: llc < %s -mtriple=x86_64-unknown 
-mattr=+cf,+prefer-cf-reg-operand,+prefer-cf-mem-operand -verify-machineinstrs 
| FileCheck %s --check-prefixes=CHECK,REG,MEM
+; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+cf -verify-machineinstrs | 
FileCheck %s --check-prefixes=CHECK,NOMEM,NOREG
 
 define i8 @cfcmov8rr(i8 %0) {
 ; REG-LABEL: cfcmov8rr:
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 2e861c2244cf26..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,10 +1,9 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -mtriple=x86_64 -mattr=+cf -x86-cf-mem-operand -O1 -S | 
FileCheck %s --check-prefix=MEM
+; 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
 
-;; With -x86-cf-mem-operand, masked.load/store.v1* is generated in simplifycfg
-;; and not expanded back to branch+load/store by later passes; without it the
-;; branch is kept.
+;; 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) {
 ; MEM-LABEL: @basic(
 ; MEM-NEXT:  entry:
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 b11d02f5950f07..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 -x86-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 -x86-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 -x86-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 -x86-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
+; 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) {

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to