Skip to content

Commit 84804b6

Browse files
committed
Simplify (T1)(T2)x over bv types where T1 and x have the same type
When T2 has the same width (and also is a bitvector type) then the typecasts do not change the result -- we can use `x` directly.
1 parent 6df204e commit 84804b6

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/util/simplify_expr.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,20 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
13661366
// might enable further simplification
13671367
return changed(simplify_typecast(new_expr)); // recursive call
13681368
}
1369+
// (T1)(T2)x over bv types where T1 and T2 have the same width and the type
1370+
// of x is T1 --> x
1371+
const exprt &inner_op = to_typecast_expr(operand).op();
1372+
if(
1373+
inner_op.type() == expr_type &&
1374+
(expr_type_id == ID_unsignedbv || expr_type_id == ID_signedbv ||
1375+
expr_type_id == ID_bv) &&
1376+
(op_type_id == ID_unsignedbv || op_type_id == ID_signedbv ||
1377+
op_type_id == ID_bv) &&
1378+
to_bitvector_type(expr_type).get_width() ==
1379+
to_bitvector_type(operand.type()).get_width())
1380+
{
1381+
return inner_op;
1382+
}
13691383
}
13701384
else if(operand.id()==ID_address_of)
13711385
{

unit/util/simplify_expr.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,3 +652,19 @@ TEST_CASE("Simplify quantifier", "[core][util]")
652652
REQUIRE(simplify_expr(forall_exprt{a, true_exprt{}}, ns) == true_exprt{});
653653
}
654654
}
655+
656+
657+
TEST_CASE("Simplify double typecast over bv types", "[core][util]")
658+
{
659+
const symbol_tablet symbol_table;
660+
const namespacet ns{symbol_table};
661+
662+
// (unsigned)(signed)x where x is unsigned and same width -> x
663+
const unsignedbv_typet u32{32};
664+
const signedbv_typet s32{32};
665+
const symbol_exprt x{"x", u32};
666+
const typecast_exprt inner{x, s32};
667+
const typecast_exprt outer{inner, u32};
668+
REQUIRE(simplify_expr(outer, ns) == x);
669+
}
670+

0 commit comments

Comments
 (0)