diff --git a/gcc/optabs.c b/gcc/optabs.c
index bdc692bbc735..0427063e2776 100644
--- a/gcc/optabs.c
+++ b/gcc/optabs.c
@@ -1081,7 +1081,8 @@ expand_doubleword_mod (machine_mode mode, rtx op0, rtx op1, bool unsignedp)
 		return NULL_RTX;
 	    }
 	}
-      rtx remainder = expand_divmod (1, TRUNC_MOD_EXPR, word_mode, sum, op1,
+      rtx remainder = expand_divmod (1, TRUNC_MOD_EXPR, word_mode, sum,
+				     gen_int_mode (INTVAL (op1), word_mode),
 				     NULL_RTX, 1, OPTAB_DIRECT);
       if (remainder == NULL_RTX)
 	return NULL_RTX;
@@ -1099,7 +1100,8 @@ expand_doubleword_mod (machine_mode mode, rtx op0, rtx op1, bool unsignedp)
 		return NULL_RTX;
 	    }
 	  mask = expand_simple_binop (word_mode, AND, mask,
-				      GEN_INT (1 - INTVAL (op1)),
+				      gen_int_mode (1 - INTVAL (op1),
+						    word_mode),
 				      NULL_RTX, 1, OPTAB_DIRECT);
 	  if (mask == NULL_RTX)
 	    return NULL_RTX;
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr98229.c b/gcc/testsuite/gcc.c-torture/compile/pr98229.c
new file mode 100644
index 000000000000..509719d0d34c
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/compile/pr98229.c
@@ -0,0 +1,7 @@
+/* PR rtl-optimization/98229 */
+
+unsigned long long
+foo (unsigned long long x)
+{
+  return x % ~0U;
+}