[llvm] [X86] Fix CCMP miscompile for flag-source-folded SETCC leaves (PR #217682)

Evgenii Kudriashov via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 21 08:57:43 PDT 2026


================
@@ -25518,16 +25518,36 @@ static bool canEmitConjunctionForCCMP(SelectionDAG &DAG, SDValue Val,
     return false;
   unsigned Opcode = Val.getOpcode();
   if (Opcode == ISD::SETCC) {
-    EVT VT = Val.getOperand(0).getValueType();
+    SDValue LHS = Val.getOperand(0), RHS = Val.getOperand(1);
+    EVT VT = LHS.getValueType();
     if (!VT.isInteger())
       return false;
     CanNegate = true;
     MustBeFirst = false;
+    // A SETCC leaf whose flag source will be folded by EmitTest onto an
+    // OR/ADD/XOR node cannot appear in a non-root slot of a CCMP chain:
+    // CCMP is a subtract and CTEST is an AND, so neither can reproduce this
+    // leaf's SF/ZF. Force such a leaf to the chain root, where its flag
+    // source becomes a plain flag-setting instruction and its EFLAGS feed
+    // the next CCMP. This happens for icmp-vs-0 (and the icmp-vs-{-1,1}
+    // forms TranslateX86CC rewrites into icmp-vs-0) whose LHS is an
+    // OR/ADD/XOR the DAG will re-use for flags.
----------------
e-kud wrote:

Agree. Fixing miscompilation first. Bailing out breaks some of the tests from ccmp/ctest generation. Thus avoid using EmitCmp completely but adding some cases from EmitCmp that could be lowered into ccmp/ctest.

https://github.com/llvm/llvm-project/pull/217682


More information about the llvm-commits mailing list