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

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 18 05:15:17 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));
+  }
----------------
kper wrote:

Thanks, I messed the condition up

I have adapted the code now by adding another check since we must not mix srem and urem

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


More information about the llvm-commits mailing list