From: Florian Krohm Date: Wed, 10 Sep 2025 21:30:08 +0000 (+0000) Subject: ir_opt.c: Fix algebraic simplification for division. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e22b4d14a959238b71aa7b9af2e8f4fa43d46fc8;p=thirdparty%2Fvalgrind.git ir_opt.c: Fix algebraic simplification for division. x / x ==> 1 IFF x is not 0. But that cannot be decided at JIT time. Remove mkOneOfPrimopResultType as it is no longer needed. --- diff --git a/VEX/priv/ir_opt.c b/VEX/priv/ir_opt.c index ff3bf10c8..f3954fc60 100644 --- a/VEX/priv/ir_opt.c +++ b/VEX/priv/ir_opt.c @@ -1288,23 +1288,6 @@ static IRExpr* mkZeroOfPrimopResultType ( IROp op ) } } -/* Make an integer value of 1, which has the same type as the - result of the given primop. */ -static IRExpr* mkOneOfPrimopResultType ( IROp op ) -{ - switch (op) { - case Iop_DivU32: - case Iop_DivS32: - return IRExpr_Const(IRConst_U32(1)); - case Iop_DivU64: - case Iop_DivS64: - return IRExpr_Const(IRConst_U64(1)); - default: - ppIROp(op); - vpanic("mkOneOfPrimopResultType: bad primop"); - } -} - /* Make a Boolean False value */ static inline IRExpr* mkFalse(void) { @@ -2774,13 +2757,13 @@ static IRExpr* fold_Expr_WRK ( IRExpr** env, IRExpr* e ) e2 = e->Iex.Binop.arg1; break; } - /* Dividing x by x ==> 1 */ - if (! isZeroU(e->Iex.Binop.arg2)) { - if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { - e2 = mkOneOfPrimopResultType(e->Iex.Binop.op); - break; - } - } + + /* Dividing x by x ==> 1 + DON'T. The reason is that we cannot decide at JIT time whether + e->Iex.Binop.arg2 might evaluate to zero. Suppose it does. + Then we would be rewriting 0 / 0 ==> 1 and that is clearly + wrong. */ + /* Dividing 0 by x ==> 0 */ if (isZeroU(e->Iex.Binop.arg1)) { e2 = mkZeroOfPrimopResultType(e->Iex.Binop.op);