[llvm] [AArch64] Eliminate Common SUBS by Reassociating Non-Constants (PR #123344)

Marius Kamp via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 19 04:44:08 PST 2025


================
@@ -24899,16 +24899,36 @@ static SDValue reassociateCSELOperandsForCSE(SDNode *N, SelectionDAG &DAG) {
   SDValue SubsNode = N->getOperand(3);
   if (SubsNode.getOpcode() != AArch64ISD::SUBS || !SubsNode.hasOneUse())
     return SDValue();
-  auto *CmpOpConst = dyn_cast<ConstantSDNode>(SubsNode.getOperand(1));
-  if (!CmpOpConst)
-    return SDValue();
 
+  SDValue CmpOpToMatch = SubsNode.getOperand(1);
   SDValue CmpOpOther = SubsNode.getOperand(0);
   EVT VT = N->getValueType(0);
 
+  unsigned ExpectedOpcode;
+  std::function<bool(SDValue)> CheckOp;
+  std::function<SDValue()> BuildSubsOp;
+  auto *CmpOpConst = dyn_cast<ConstantSDNode>(CmpOpToMatch);
+  if (CmpOpConst) {
+    ExpectedOpcode = ISD::ADD;
+    CheckOp = [&](SDValue Op) {
+      auto *AddOpConst = dyn_cast<ConstantSDNode>(Op);
+      return AddOpConst &&
+             AddOpConst->getAPIntValue() == -CmpOpConst->getAPIntValue();
+    };
+    BuildSubsOp = [&] {
+      return DAG.getConstant(CmpOpConst->getAPIntValue(), SDLoc(CmpOpConst),
----------------
mskamp wrote:

Good. In this way, we can get rid of at least one `std::function`.

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


More information about the llvm-commits mailing list