[llvm] [DAGCombiner, NVPTX] Port 'rem' custom combine from NVPTX to generic combiner (PR #167147)

via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 27 04:08:50 PST 2025


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp -- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp --diff_from_common_commit
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 867f30985..899ae1453 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -900,40 +900,43 @@ namespace {
                          ISD::NodeType ExtType);
   };
 
-/// Generic remainder optimization : Folds a remainder operation (A % B) by reusing the computed quotient (A / B).
-static SDValue PerformREMCombineGeneric(SDNode *N, DAGCombiner &DC,
-                                        CodeGenOptLevel OptLevel) {
-  assert(N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM);
+  /// Generic remainder optimization : Folds a remainder operation (A % B) by
+  /// reusing the computed quotient (A / B).
+  static SDValue PerformREMCombineGeneric(SDNode *N, DAGCombiner &DC,
+                                          CodeGenOptLevel OptLevel) {
+    assert(N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM);
+
+    // Don't do anything at less than -O2.
+    if (OptLevel < CodeGenOptLevel::Default)
+      return SDValue();
 
-  // Don't do anything at less than -O2.
-  if (OptLevel < CodeGenOptLevel::Default)
-    return SDValue();
+    SelectionDAG &DAG = DC.getDAG();
+    SDLoc DL(N);
+    EVT VT = N->getValueType(0);
+    bool IsSigned = N->getOpcode() == ISD::SREM;
+    unsigned DivOpc = IsSigned ? ISD::SDIV : ISD::UDIV;
 
-  SelectionDAG &DAG = DC.getDAG();
-  SDLoc DL(N);
-  EVT VT = N->getValueType(0);
-  bool IsSigned = N->getOpcode() == ISD::SREM;
-  unsigned DivOpc = IsSigned ? ISD::SDIV : ISD::UDIV;
+    const SDValue &Num = N->getOperand(0);
+    const SDValue &Den = N->getOperand(1);
 
-  const SDValue &Num = N->getOperand(0);
-  const SDValue &Den = N->getOperand(1);
-  
-  AttributeList Attr = DC.getDAG().getMachineFunction().getFunction().getAttributes();
-  if (DC.getDAG().getTargetLoweringInfo().isIntDivCheap(N->getValueType(0), Attr))
-    return SDValue();
+    AttributeList Attr =
+        DC.getDAG().getMachineFunction().getFunction().getAttributes();
+    if (DC.getDAG().getTargetLoweringInfo().isIntDivCheap(N->getValueType(0),
+                                                          Attr))
+      return SDValue();
 
-  for (const SDNode *U : Num->users()) {
-    if (U->getOpcode() == DivOpc && U->getOperand(0) == Num &&
-        U->getOperand(1) == Den) {
-      // Num % Den -> Num - (Num / Den) * Den
-      return DAG.getNode(ISD::SUB, DL, VT, Num,
-                         DAG.getNode(ISD::MUL, DL, VT,
-                                     DAG.getNode(DivOpc, DL, VT, Num, Den),
-                                     Den));
+    for (const SDNode *U : Num->users()) {
+      if (U->getOpcode() == DivOpc && U->getOperand(0) == Num &&
+          U->getOperand(1) == Den) {
+        // Num % Den -> Num - (Num / Den) * Den
+        return DAG.getNode(ISD::SUB, DL, VT, Num,
+                           DAG.getNode(ISD::MUL, DL, VT,
+                                       DAG.getNode(DivOpc, DL, VT, Num, Den),
+                                       Den));
+      }
     }
+    return SDValue();
   }
-  return SDValue();
-}
 
 /// This class is a DAGUpdateListener that removes any deleted
 /// nodes from the worklist.
@@ -5437,7 +5440,7 @@ SDValue DAGCombiner::visitREM(SDNode *N) {
 
   if (SDValue V = PerformREMCombineGeneric(N, *this, OptLevel))
     return V;
-  
+
   if (isSigned) {
     // If we know the sign bits of both operands are zero, strength reduce to a
     // urem instead.  Handles (X & 0x0FFFFFFF) %s 16 -> X&15

``````````

</details>


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


More information about the llvm-commits mailing list