[llvm] [AArch64] Support commuted operands in performFlagSettingCombine (PR #162496)

Paul Walker via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 8 08:42:10 PDT 2025


================
@@ -26193,9 +26193,21 @@ static SDValue performFlagSettingCombine(SDNode *N,
   }
 
   // Combine identical generic nodes into this node, re-using the result.
-  if (SDNode *Generic = DCI.DAG.getNodeIfExists(
-          GenericOpcode, DCI.DAG.getVTList(VT), {LHS, RHS}))
-    DCI.CombineTo(Generic, SDValue(N, 0));
+  auto CombineWithExistingGeneric = [&](SDValue Op0, SDValue Op1) {
+    if (SDNode *Generic = DCI.DAG.getNodeIfExists(
+            GenericOpcode, DCI.DAG.getVTList(VT), {Op0, Op1})) {
+      DCI.CombineTo(Generic, SDValue(N, 0));
+      return true;
+    }
+    return false;
+  };
+
+  if (CombineWithExistingGeneric(LHS, RHS))
+    return SDValue();
+
+  if (DCI.DAG.getTargetLoweringInfo().isCommutativeBinOp(GenericOpcode) &&
+      CombineWithExistingGeneric(RHS, LHS))
----------------
paulwalker-arm wrote:

Would it be better to extend `getNodeIfExists` to support commuting its operands? If you're worried some uses may expect the exact operand order you could add an `AllowCommute` or add a dedicated function with the ability.  Doing this will make it trivial to migrate other call sites where commuting operands has value.

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


More information about the llvm-commits mailing list