[PATCH] D132002: [RISCV] Refactor performSUBCombine to prepare for D132000.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 16 16:38:16 PDT 2022


craig.topper created this revision.
craig.topper added reviewers: reames, asb, luismarques.
Herald added subscribers: sunshaoce, VincentWu, luke957, StephenFan, vkmr, frasercrmck, evandro, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, shiva0217, kito-cheng, niosHD, sabuasal, simoncook, johnrusso, rbar, hiraditya, arichardson.
Herald added a project: All.
craig.topper requested review of this revision.
Herald added subscribers: pcwang-thead, eopXD, MaskRay.
Herald added a project: LLVM.

This refactors the code into a separate function with early returns.
D132000 <https://reviews.llvm.org/D132000> adds an additional operation to the if/else that selects
NewLHS, but can otherwise share the rest of the code.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132002

Files:
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp


Index: llvm/lib/Target/RISCV/RISCVISelLowering.cpp
===================================================================
--- llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -8279,35 +8279,50 @@
   return combineSelectAndUseCommutative(N, DAG, /*AllOnes*/ false);
 }
 
-static SDValue performSUBCombine(SDNode *N, SelectionDAG &DAG) {
+// Try to turn a sub boolean RHS and constant LHS into an addi.
+static SDValue combineSubOfBoolean(SDNode *N, SelectionDAG &DAG) {
   SDValue N0 = N->getOperand(0);
   SDValue N1 = N->getOperand(1);
+  EVT VT = N->getValueType(0);
+  SDLoc DL(N);
 
-  // Prefer to make this 'add 0/1' rather than 'sub 0/1'
-  // sub constant, 0/1 -> add constant - 1, 1/0
-  // (sub constant, (setcc x, y, eq/neq)) ->
-  // (add (setcc x, y, neq/eq), constant - 1)
+  // Require a constant LHS.
   auto *N0C = dyn_cast<ConstantSDNode>(N0);
-  if (N0C && N1.getOpcode() == ISD::SETCC && N1.hasOneUse()) {
+  if (!N0C)
+    return SDValue();
+
+  // All our optimizations involve subtracting 1 from the immediate and forming
+  // an ADDI. Make sure the new immediate is valid for an ADDI.
+  APInt ImmValMinus1 = N0C->getAPIntValue() - 1;
+  if (!ImmValMinus1.isSignedIntN(12))
+    return SDValue();
+
+  SDValue NewLHS;
+  if (N1.getOpcode() == ISD::SETCC && N1.hasOneUse()) {
+    // (sub constant, (setcc x, y, eq/neq)) ->
+    // (add (setcc x, y, neq/eq), constant - 1)
     ISD::CondCode CCVal = cast<CondCodeSDNode>(N1.getOperand(2))->get();
     EVT SetCCOpVT = N1.getOperand(0).getValueType();
-    if (SetCCOpVT.isInteger() && isIntEqualitySetCC(CCVal)) {
-      EVT VT = N->getValueType(0);
-      APInt ImmValMinus1 = N0C->getAPIntValue() - 1;
-      // If this doesn't form ADDI, the transform won't save any instructions
-      // and may increase the number of constants we need.
-      if (ImmValMinus1.isSignedIntN(12)) {
-        CCVal = ISD::getSetCCInverse(CCVal, SetCCOpVT);
-        SDValue NewN0 = DAG.getSetCC(SDLoc(N1), VT, N1.getOperand(0),
-                                     N1.getOperand(1), CCVal);
-        SDValue NewN1 = DAG.getConstant(ImmValMinus1, SDLoc(N), VT);
-        return DAG.getNode(ISD::ADD, SDLoc(N), VT, NewN0, NewN1);
-      }
-    }
-  }
+    if (!isIntEqualitySetCC(CCVal) || !SetCCOpVT.isInteger())
+      return SDValue();
+    CCVal = ISD::getSetCCInverse(CCVal, SetCCOpVT);
+    NewLHS =
+        DAG.getSetCC(SDLoc(N1), VT, N1.getOperand(0), N1.getOperand(1), CCVal);
+  } else
+    return SDValue();
+
+  SDValue NewRHS = DAG.getConstant(ImmValMinus1, DL, VT);
+  return DAG.getNode(ISD::ADD, DL, VT, NewLHS, NewRHS);
+}
+
+static SDValue performSUBCombine(SDNode *N, SelectionDAG &DAG) {
+  if (SDValue V = combineSubOfBoolean(N, DAG))
+    return V;
 
   // fold (sub x, (select lhs, rhs, cc, 0, y)) ->
   //      (select lhs, rhs, cc, x, (sub x, y))
+  SDValue N0 = N->getOperand(0);
+  SDValue N1 = N->getOperand(1);
   return combineSelectAndUse(N, N1, N0, DAG, /*AllOnes*/ false);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132002.453163.patch
Type: text/x-patch
Size: 3015 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220816/705a6cd2/attachment.bin>


More information about the llvm-commits mailing list