[llvm] [X86] combinePTESTCC - canonicalize constants to the RHS if the PTEST/TESTP node just uses the ZF flag (PR #165601)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 29 10:29:14 PDT 2025


https://github.com/RKSimon created https://github.com/llvm/llvm-project/pull/165601

If we're just comparing against zero then move the constant to the RHS to reduce duplicated folds.

Noticed while triaging #156233

>From 96e5e6d1e315d04ffaf5495505273d78499b8819 Mon Sep 17 00:00:00 2001
From: Simon Pilgrim <llvm-dev at redking.me.uk>
Date: Wed, 29 Oct 2025 17:28:28 +0000
Subject: [PATCH] [X86] combinePTESTCC - canonicalize constants to the RHS if
 the PTEST/TESTP node just uses the ZF flag

If we're just comparing against zero then move the constant to the RHS to reduce duplicated folds.

Noticed while triaging #156233
---
 llvm/lib/Target/X86/X86ISelLowering.cpp | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 89b42da9a40f0..624cff24ddf03 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -48787,6 +48787,11 @@ static SDValue combinePTESTCC(SDValue EFLAGS, X86::CondCode &CC,
   }
 
   if (CC == X86::COND_E || CC == X86::COND_NE) {
+    // Canonicalize constant to RHS if we're just using ZF.
+    if (Op0 != Op1 && DAG.isConstantIntBuildVectorOrConstantInt(Op0) &&
+        !DAG.isConstantIntBuildVectorOrConstantInt(Op1))
+      return DAG.getNode(EFLAGS.getOpcode(), SDLoc(EFLAGS), VT, Op1, Op0);
+
     // TESTZ(X,~Y) == TESTC(Y,X)
     if (SDValue NotOp1 = IsNOT(Op1, DAG)) {
       CC = (CC == X86::COND_E ? X86::COND_B : X86::COND_AE);
@@ -48850,10 +48855,6 @@ static SDValue combinePTESTCC(SDValue EFLAGS, X86::CondCode &CC,
       }
     }
 
-    // TESTZ(-1,X) == TESTZ(X,X)
-    if (ISD::isBuildVectorAllOnes(Op0.getNode()))
-      return DAG.getNode(EFLAGS.getOpcode(), SDLoc(EFLAGS), VT, Op1, Op1);
-
     // TESTZ(X,-1) == TESTZ(X,X)
     if (ISD::isBuildVectorAllOnes(Op1.getNode()))
       return DAG.getNode(EFLAGS.getOpcode(), SDLoc(EFLAGS), VT, Op0, Op0);



More information about the llvm-commits mailing list