[llvm] [AArch64] Fold swapped sub/SUBS conditions (PR #121412)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 6 10:20:08 PST 2025


================
@@ -24854,6 +24854,30 @@ static SDValue performCSELCombine(SDNode *N,
   if (SDValue Folded = foldCSELofCTTZ(N, DAG))
 		return Folded;
 
+  // CSEL a, b, cc, SUBS(x, y) -> CSEL a, b, swapped(cc), SUBS(y, x)
+  // if SUB(y, x) already exists and we can produce a swapped predicate for cc.
+  SDValue Cond = N->getOperand(3);
+  if (DCI.isAfterLegalizeDAG() && Cond.getOpcode() == AArch64ISD::SUBS &&
+      Cond.hasOneUse() && Cond->hasNUsesOfValue(0, 0) &&
----------------
david-arm wrote:

I must admit I'm a bit confused about how this leads to better code because we still have to generate a brand new SUBS and the `doesNotExist` code is looking for a standard `ISD::SUB` rather than a AArch64ISD::SUBS. Is this relying upon some behaviour where we end up lowering ISD::SUB to AArch64ISD::SUBS and CSE then spots your new SUBS and the old SUBS are the same and reuses the original SUBS? If so, what if the pre-existing SUB DAG node gets optimised away or converted into a different node? I suppose the argument may be that it's at least no worse than before.

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


More information about the llvm-commits mailing list