[llvm] [X86] Fold some (setcc (sub (truncate (srl (add X, C1), C2)), C3), CC) patterns to (setcc (cmp (add (truncate (srl X, C2)), C1'), C3), CC) (PR #126448)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 12 01:55:04 PST 2025


=?utf-8?q?João?= Gouveia <jtalonegouveia at gmail.com>,
=?utf-8?q?João?= Gouveia <jtalonegouveia at gmail.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/126448 at github.com>


================
@@ -48472,6 +48472,55 @@ static SDValue combineSetCCMOVMSK(SDValue EFLAGS, X86::CondCode &CC,
   return SDValue();
 }
 
+// Attempt to fold some (setcc (sub (truncate (srl (add X, C1), C2)), C3), CC)
+// patterns to (setcc (cmp (add (truncate (srl X, C2)), C1'), C3), CC). C1' will
+// be smaller than C1 so we are able to avoid generating code with MOVABS and
+// large constants in certain cases.
+static SDValue combineSetCCTruncAdd(SDValue EFLAGS, X86::CondCode &CC,
+                                    SelectionDAG &DAG) {
+  using namespace llvm::SDPatternMatch;
+  if (!(CC == X86::COND_E || CC == X86::COND_NE || CC == X86::COND_AE ||
+        CC == X86::COND_B))
+    return SDValue();
+
+  SDValue AddLhs;
+  APInt AddConst, SrlConst, CmpConst;
+  if (!sd_match(EFLAGS,
+                m_AllOf(m_SpecificVT(MVT::i32),
+                        m_BinOp(X86ISD::SUB,
+                                m_Trunc(m_Srl(m_Add(m_Value(AddLhs),
+                                                    m_ConstInt(AddConst)),
+                                              m_ConstInt(SrlConst))),
+                                m_ConstInt(CmpConst)))))
+    return SDValue();
+
+  SDValue Srl;
+  if (!sd_match(EFLAGS.getOperand(0).getOperand(0),
+                m_AllOf(m_SpecificVT(MVT::i64), m_Value(Srl))))
+    return SDValue();
+
+  // Avoid changing the ADD if it is used elsewhere.
+  if (!Srl.getOperand(0).hasOneUse())
----------------
RKSimon wrote:

This can be replaced by a m_OneUse in the first pattern match 

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


More information about the llvm-commits mailing list