@@ -273,6 +273,24 @@ simplify_exprt::resultt<> simplify_exprt::simplify_mult(const mult_exprt &expr)
273273 }
274274}
275275
276+ static std::optional<std::pair<exprt, mp_integer>>
277+ is_multiplication_by_constant (const exprt &expr)
278+ {
279+ if (expr.id () != ID_mult)
280+ return {};
281+ if (expr.operands ().size () != 2 )
282+ return {};
283+ const multi_ary_exprt &mul = to_multi_ary_expr (expr);
284+ if (mul.op0 ().is_constant ())
285+ return {
286+ {mul.op1 (), numeric_cast_v<mp_integer>(to_constant_expr (mul.op0 ()))}};
287+ else if (mul.op1 ().is_constant ())
288+ return {
289+ {mul.op0 (), numeric_cast_v<mp_integer>(to_constant_expr (mul.op1 ()))}};
290+ else
291+ return {};
292+ }
293+
276294simplify_exprt::resultt<> simplify_exprt::simplify_div (const div_exprt &expr)
277295{
278296 if (!is_number (expr.type ()))
@@ -315,6 +333,16 @@ simplify_exprt::resultt<> simplify_exprt::simplify_div(const div_exprt &expr)
315333 mp_integer result = *int_value0 / *int_value1;
316334 return from_integer (result, expr_type);
317335 }
336+
337+ #if 0
338+ // (x * n) / n
339+ if(int_value1.has_value())
340+ {
341+ auto mul_by_int_opt = is_multiplication_by_constant(expr.op0());
342+ if(mul_by_int_opt.has_value() && mul_by_int_opt->second == *int_value1)
343+ return mul_by_int_opt->first;
344+ }
345+ #endif
318346 }
319347 else if (expr_type.id ()==ID_rational)
320348 {
@@ -397,6 +425,14 @@ simplify_exprt::resultt<> simplify_exprt::simplify_mod(const mod_exprt &expr)
397425 mp_integer result = *int_value0 % *int_value1;
398426 return from_integer (result, expr.type ());
399427 }
428+
429+ // (x * n) % n is zero
430+ if (int_value1.has_value ())
431+ {
432+ auto mul_by_int_opt = is_multiplication_by_constant (expr.op0 ());
433+ if (mul_by_int_opt.has_value () && mul_by_int_opt->second == *int_value1)
434+ return from_integer (0 , expr.type ());
435+ }
400436 }
401437 }
402438
0 commit comments