[llvm] [DAG] Fold urem(urem(A, BCst), Op1Cst) -> urem(A, Op1Cst) (PR #159517)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 18 03:16:22 PDT 2025


================
@@ -5405,6 +5405,14 @@ SDValue DAGCombiner::visitREM(SDNode *N) {
   if (SDValue DivRem = useDivRem(N))
     return DivRem.getValue(1);
 
+  // fold urem(urem(A, BCst), Op1Cst) -> urem(A, Op1Cst)
+  SDValue A;
+  APInt Op1Cst, BCst;
+  if (sd_match(N0, m_URem(m_Value(A), m_ConstInt(BCst))) &&
+      sd_match(N1, m_ConstInt(Op1Cst)) && BCst.urem(Op1Cst).isZero()) {
+    return DAG.getNode(ISD::UREM, DL, VT, A, DAG.getConstant(Op1Cst, DL, VT));
+  }
----------------
jayfoad wrote:

Or you could apply the optimisation for srem too? It seems to be correct so long as Op1Cst != -1 (because then you could run into the minint/-1 undefined case).

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


More information about the llvm-commits mailing list