Skip to content

Commit

Permalink
Simplifier: c_bool (and others) are also bitvector types
Browse files Browse the repository at this point in the history
Remove simplifier's own is_bitvector_type in favour of using
can_cast_type<bitvector_typet>, which will make sure that expressions
like bitxor(false, false) over c_bool types gets simplified. Such
expressions were seen in Kani (the C front-end would promote bitxor
operands to int, and, therefore, not end up in this code path).
  • Loading branch information
tautschnig committed Mar 20, 2024
1 parent baaccb6 commit 1be9830
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
7 changes: 0 additions & 7 deletions src/util/simplify_expr_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,6 @@ class simplify_exprt

virtual bool simplify(exprt &expr);

static bool is_bitvector_type(const typet &type)
{
return type.id()==ID_unsignedbv ||
type.id()==ID_signedbv ||
type.id()==ID_bv;
}

protected:
const namespacet &ns;
#ifdef DEBUG_ON_DEMAND
Expand Down
31 changes: 15 additions & 16 deletions src/util/simplify_expr_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ simplify_exprt::simplify_minus(const minus_exprt &expr)
simplify_exprt::resultt<>
simplify_exprt::simplify_bitwise(const multi_ary_exprt &expr)
{
if(!is_bitvector_type(expr.type()))
if(!can_cast_type<bitvector_typet>(expr.type()))
return unchanged(expr);

// check if these are really boolean
Expand Down Expand Up @@ -838,7 +838,7 @@ simplify_exprt::simplify_extractbit(const extractbit_exprt &expr)
{
const typet &src_type = expr.src().type();

if(!is_bitvector_type(src_type))
if(!can_cast_type<bitvector_typet>(src_type))
return unchanged(expr);

const std::size_t src_bit_width = to_bitvector_type(src_type).get_width();
Expand Down Expand Up @@ -869,7 +869,7 @@ simplify_exprt::simplify_concatenation(const concatenation_exprt &expr)

concatenation_exprt new_expr = expr;

if(is_bitvector_type(new_expr.type()))
if(can_cast_type<bitvector_typet>(new_expr.type()))
{
// first, turn bool into bvec[1]
Forall_operands(it, new_expr)
Expand All @@ -891,10 +891,10 @@ simplify_exprt::simplify_concatenation(const concatenation_exprt &expr)
exprt &opi = new_expr.operands()[i];
exprt &opn = new_expr.operands()[i + 1];

if(opi.is_constant() &&
opn.is_constant() &&
is_bitvector_type(opi.type()) &&
is_bitvector_type(opn.type()))
if(
opi.is_constant() && opn.is_constant() &&
can_cast_type<bitvector_typet>(opi.type()) &&
can_cast_type<bitvector_typet>(opn.type()))
{
// merge!
const auto &value_i = to_constant_expr(opi).get_value();
Expand Down Expand Up @@ -963,12 +963,10 @@ simplify_exprt::simplify_concatenation(const concatenation_exprt &expr)
exprt &opi = new_expr.operands()[i];
exprt &opn = new_expr.operands()[i + 1];

if(opi.is_constant() &&
opn.is_constant() &&
(opi.type().id()==ID_verilog_unsignedbv ||
is_bitvector_type(opi.type())) &&
(opn.type().id()==ID_verilog_unsignedbv ||
is_bitvector_type(opn.type())))
if(
opi.is_constant() && opn.is_constant() &&
can_cast_type<bitvector_typet>(opi.type()) &&
can_cast_type<bitvector_typet>(opn.type()))

Check warning on line 969 in src/util/simplify_expr_int.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/simplify_expr_int.cpp#L966-L969

Added lines #L966 - L969 were not covered by tests
{
// merge!
const std::string new_value=
Expand Down Expand Up @@ -1001,7 +999,7 @@ simplify_exprt::simplify_concatenation(const concatenation_exprt &expr)
simplify_exprt::resultt<>
simplify_exprt::simplify_shifts(const shift_exprt &expr)
{
if(!is_bitvector_type(expr.type()))
if(!can_cast_type<bitvector_typet>(expr.type()))
return unchanged(expr);

const auto distance = numeric_cast<mp_integer>(expr.distance());
Expand Down Expand Up @@ -1132,8 +1130,9 @@ simplify_exprt::simplify_extractbits(const extractbits_exprt &expr)
{
const typet &op0_type = expr.src().type();

if(!is_bitvector_type(op0_type) &&
!is_bitvector_type(expr.type()))
if(
!can_cast_type<bitvector_typet>(op0_type) &&
!can_cast_type<bitvector_typet>(expr.type()))

Check warning on line 1135 in src/util/simplify_expr_int.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/simplify_expr_int.cpp#L1135

Added line #L1135 was not covered by tests
{
return unchanged(expr);
}
Expand Down

0 comments on commit 1be9830

Please sign in to comment.