Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aval/bval lowering for reduction operators #930

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions regression/verilog/expressions/reduction3.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE broken-smt-backend
reduction3.sv

^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
--
22 changes: 22 additions & 0 deletions regression/verilog/expressions/reduction3.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module main;

assert final ( & 4'b1001 === 'b0);
assert final ( & 4'bx111 === 'bx);
assert final ( & 4'bz111 === 'bx);
assert final (~& 4'b1001 === 'b1);
assert final (~& 4'bx001 === 'b1);
assert final (~& 4'bz001 === 'b1);
assert final ( | 4'b1001 === 'b1);
assert final ( | 4'bx000 === 'bx);
assert final ( | 4'bz000 === 'bx);
assert final (~| 4'b1001 === 'b0);
assert final (~| 4'bx001 === 'b0);
assert final (~| 4'bz001 === 'b0);
assert final ( ^ 4'b1001 === 'b0);
assert final ( ^ 4'bx001 === 'bx);
assert final ( ^ 4'bz001 === 'bx);
assert final (~^ 4'b1001 === 'b1);
assert final (~^ 4'bx001 === 'bx);
assert final (~^ 4'bz001 === 'bx);

endmodule
69 changes: 69 additions & 0 deletions src/verilog/aval_bval_encoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,75 @@ exprt aval_bval(const not_exprt &expr)
return if_exprt{has_xz, x, aval_bval_conversion(not_expr, x.type())};
}

exprt aval_bval_reduction(const unary_exprt &expr)
{
auto &type = expr.type();
auto type_aval_bval = lower_to_aval_bval(type);
PRECONDITION(is_four_valued(type));
PRECONDITION(is_aval_bval(expr.op()));

auto has_xz = ::has_xz(expr.op());
auto x = make_x(type);
auto op_aval = aval(expr.op());
auto op_bval = bval(expr.op());

if(expr.id() == ID_reduction_xor || expr.id() == ID_reduction_xnor)
{
auto reduction_expr = unary_exprt{expr.id(), op_aval, bool_typet{}};
return if_exprt{has_xz, x, aval_bval_conversion(reduction_expr, x.type())};
}
else if(expr.id() == ID_reduction_and || expr.id() == ID_reduction_nand)
{
auto has_zero = notequal_exprt{
bitor_exprt{op_aval, op_bval},
to_bv_type(op_aval.type()).all_ones_expr()};

auto one = combine_aval_bval(
bv_typet{1}.all_ones_expr(),
bv_typet{1}.all_zeros_expr(),
type_aval_bval);
auto zero = combine_aval_bval(
bv_typet{1}.all_zeros_expr(),
bv_typet{1}.all_zeros_expr(),
type_aval_bval);

if(expr.id() == ID_reduction_and)
{
return if_exprt{has_zero, zero, if_exprt{has_xz, x, one}};
}
else // nand
{
return if_exprt{has_zero, one, if_exprt{has_xz, x, zero}};
}
}
else if(expr.id() == ID_reduction_or || expr.id() == ID_reduction_nor)
{
auto has_one = notequal_exprt{
bitand_exprt{op_aval, bitnot_exprt{op_bval}},
to_bv_type(op_aval.type()).all_zeros_expr()};

auto one = combine_aval_bval(
bv_typet{1}.all_ones_expr(),
bv_typet{1}.all_zeros_expr(),
type_aval_bval);
auto zero = combine_aval_bval(
bv_typet{1}.all_zeros_expr(),
bv_typet{1}.all_zeros_expr(),
type_aval_bval);

if(expr.id() == ID_reduction_or)
{
return if_exprt{has_one, one, if_exprt{has_xz, x, zero}};
}
else // nor
{
return if_exprt{has_one, zero, if_exprt{has_xz, x, one}};
}
}
else
PRECONDITION(false);
}

exprt aval_bval(const bitnot_exprt &expr)
{
auto &type = expr.type();
Expand Down
2 changes: 2 additions & 0 deletions src/verilog/aval_bval_encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ exprt aval_bval(const not_exprt &);
exprt aval_bval(const bitnot_exprt &);
/// lowering for &, |, ^, ^~
exprt aval_bval_bitwise(const multi_ary_exprt &);
/// lowering for reduction operators
exprt aval_bval_reduction(const unary_exprt &);
/// lowering for ==?
exprt aval_bval(const verilog_wildcard_equality_exprt &);
/// lowering for !=?
Expand Down
11 changes: 11 additions & 0 deletions src/verilog/verilog_lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,17 @@ exprt verilog_lowering(exprt expr)
else
return expr; // leave as is
}
else if(
expr.id() == ID_reduction_or || expr.id() == ID_reduction_and ||
expr.id() == ID_reduction_nor || expr.id() == ID_reduction_nand ||
expr.id() == ID_reduction_xor || expr.id() == ID_reduction_xnor)
{
// encode into aval/bval
if(is_four_valued(expr.type()))
return aval_bval_reduction(to_unary_expr(expr));
else
return expr; // leave as is
}
else if(expr.id() == ID_verilog_iff)
{
auto &iff = to_verilog_iff_expr(expr);
Expand Down
Loading