[llvm] [SDAG] Allow S/Zext in select-abd pattern (PR #191207)

via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 18 12:23:35 PDT 2026


================
@@ -1463,25 +1464,33 @@ inline auto m_IntrinsicWOChain(const OpndPreds &...Opnds) {
 
 struct SpecificNeg_match {
   SDValue V;
+  bool AllowTypeMismatch;
 
-  explicit SpecificNeg_match(SDValue V) : V(V) {}
+  explicit SpecificNeg_match(SDValue V, bool AllowTypeMismatch = false)
+      : V(V), AllowTypeMismatch(AllowTypeMismatch) {}
 
   template <typename MatchContext>
   bool match(const MatchContext &Ctx, SDValue N) {
     if (sd_context_match(N, Ctx, m_Neg(m_Specific(V))))
       return true;
 
     return ISD::matchBinaryPredicate(
-        V, N, [](ConstantSDNode *LHS, ConstantSDNode *RHS) {
-          return LHS->getAPIntValue() == -RHS->getAPIntValue();
-        });
+        V, N,
+        [](ConstantSDNode *LHS, ConstantSDNode *RHS) {
+          APInt RHSInt = RHS->getAPIntValue();
+          APInt LHSInt = LHS->getAPIntValue();
+          unsigned Width = std::max(RHSInt.getBitWidth(), LHSInt.getBitWidth());
+          return LHSInt.sext(Width) == -RHSInt.sext(Width);
+        },
+        /*AllowUndefs*/ false, AllowTypeMismatch);
   }
 };
 
 /// Match a negation of a specific value V, either as sub(0, V) or as
 /// constant(s) that are the negation of V's constant(s).
-inline SpecificNeg_match m_SpecificNeg(SDValue V) {
-  return SpecificNeg_match(V);
+inline SpecificNeg_match m_SpecificNeg(SDValue V,
+                                       bool AllowTypeMismatch = false) {
----------------
DaKnig wrote:

it appears that DAGCombiner does not fold `sub a, const:b` into `add a, -b` when b is a bv of const, only when b is a Constant. this is odd.

Tried to bypass it by manually performing this in llvm-ir, encountered another issue: we need `m_Specific` to also match "same value, different type" for constants.

I do not know how to proceed with constants. I will remove that from the patch for now. Please advice on how to do that.

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


More information about the llvm-commits mailing list