Skip to content

Commit

Permalink
Merge pull request #8247 from tautschnig/features/simp-c_bool
Browse files Browse the repository at this point in the history
Simplifier: c_bool (and others) are also bitvector types
  • Loading branch information
kroening authored Jun 12, 2024
2 parents 8bab263 + a697de0 commit e1ff0e0
Show file tree
Hide file tree
Showing 3 changed files with 32 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 @@ -964,12 +964,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()))
{
// merge!
const std::string new_value=
Expand Down Expand Up @@ -1002,7 +1000,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 @@ -1133,8 +1131,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()))
{
return unchanged(expr);
}
Expand Down
17 changes: 17 additions & 0 deletions unit/util/simplify_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,3 +554,20 @@ TEST_CASE("Simplify inequality", "[core][util]")
REQUIRE(simp == true_exprt{});
}
}

TEST_CASE("Simplify bitxor", "[core][util]")
{
config.set_arch("none");

const symbol_tablet symbol_table;
const namespacet ns(symbol_table);

SECTION("Simplification for c_bool")
{
constant_exprt false_c_bool = from_integer(0, c_bool_type());

REQUIRE(
simplify_expr(bitxor_exprt{false_c_bool, false_c_bool}, ns) ==
false_c_bool);
}
}

0 comments on commit e1ff0e0

Please sign in to comment.