Skip to content

Cleanup and use propt::new_variables #6053

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

Merged
merged 1 commit into from
Apr 23, 2021
Merged
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
4 changes: 1 addition & 3 deletions src/solvers/flattening/boolbv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,7 @@ exprt boolbvt::make_free_bv_expr(const typet &type)
{
const std::size_t width = boolbv_width(type);
PRECONDITION(width != 0);
bvt bv(width);
for(auto &lit : bv)
lit = prop.new_variable();
bvt bv = prop.new_variables(width);
return make_bv_expr(type, bv);
}

Expand Down
3 changes: 1 addition & 2 deletions src/solvers/flattening/boolbv_byte_extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ bvt boolbvt::convert_byte_extract(const byte_extract_exprt &expr)
if(prop.has_set_to())
{
// free variables
for(std::size_t i=0; i<width; i++)
bv[i]=prop.new_variable();
bv = prop.new_variables(width);

// add implications

Expand Down
6 changes: 1 addition & 5 deletions src/solvers/flattening/boolbv_case.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ bvt boolbvt::convert_case(const exprt &expr)
if(width==0)
return conversion_failed(expr);

bvt bv;
bv.resize(width);

// make it free variables
for(auto &literal : bv)
literal = prop.new_variable();
bvt bv = prop.new_variables(width);

DATA_INVARIANT(
operands.size() >= 3, "case should have at least three operands");
Expand Down
6 changes: 3 additions & 3 deletions src/solvers/flattening/boolbv_cond.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ bvt boolbvt::convert_cond(const cond_exprt &expr)
return conversion_failed(expr);

bvt bv;
bv.resize(width);

DATA_INVARIANT(operands.size() >= 2, "cond must have at least two operands");

Expand All @@ -34,8 +33,7 @@ bvt boolbvt::convert_cond(const cond_exprt &expr)
literalt cond_literal=const_literal(false);

// make it free variables
for(auto &literal : bv)
literal = prop.new_variable();
bv = prop.new_variables(width);

forall_operands(it, expr)
{
Expand All @@ -60,6 +58,8 @@ bvt boolbvt::convert_cond(const cond_exprt &expr)
}
else
{
bv.resize(width);

// functional version -- go backwards
for(std::size_t i=expr.operands().size(); i!=0; i-=2)
{
Expand Down
21 changes: 5 additions & 16 deletions src/solvers/flattening/boolbv_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ bvt boolbvt::convert_index(const index_exprt &expr)
else
{
// free variables
bv.reserve(width);
for(std::size_t i = 0; i < width; i++)
bv.push_back(prop.new_variable());
bv = prop.new_variables(width);

record_array_index(expr);

Expand Down Expand Up @@ -219,10 +217,7 @@ bvt boolbvt::convert_index(const index_exprt &expr)
if(prop.has_set_to())
{
// free variables

bv.resize(width);
for(std::size_t i=0; i<width; i++)
bv[i]=prop.new_variable();
bv = prop.new_variables(width);

// add implications

Expand Down Expand Up @@ -297,9 +292,6 @@ bvt boolbvt::convert_index(
if(width==0)
return conversion_failed(array);

bvt bv;
bv.resize(width);

// TODO: If the underlying array can use one of the
// improvements given above then it may be better to use
// the array theory for short chains of updates and then
Expand All @@ -325,8 +317,8 @@ bvt boolbvt::convert_index(
// array.id()!=ID_array);
// If not there are large improvements possible as above

for(std::size_t i=0; i<width; i++)
bv[i] = tmp[numeric_cast_v<std::size_t>(offset + i)];
std::size_t offset_int = numeric_cast_v<std::size_t>(offset);
return bvt(tmp.begin() + offset_int, tmp.begin() + offset_int + width);
}
else if(
array.id() == ID_member || array.id() == ID_index ||
Expand Down Expand Up @@ -354,9 +346,6 @@ bvt boolbvt::convert_index(
else
{
// out of bounds
for(std::size_t i=0; i<width; i++)
bv[i]=prop.new_variable();
return prop.new_variables(width);
}

return bv;
}
20 changes: 3 additions & 17 deletions src/solvers/flattening/bv_pointers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,7 @@ bvt bv_pointerst::convert_pointer_type(const exprt &expr)
}
else if(expr.id()==ID_nondet_symbol)
{
bvt bv;
bv.resize(bits);

for(auto &literal : bv)
literal = prop.new_variable();

return bv;
return prop.new_variables(bits);
}
else if(expr.id()==ID_typecast)
{
Expand Down Expand Up @@ -576,11 +570,7 @@ bvt bv_pointerst::convert_bitvector(const exprt &expr)
const bvt object_size_bv =
bv_utils.zero_extension(convert_bv(object_size), width);

bvt bv;
bv.reserve(width);

for(std::size_t i = 0; i < width; ++i)
bv.push_back(prop.new_variable());
bvt bv = prop.new_variables(width);

if(!same_object_bv[0].is_false())
{
Expand Down Expand Up @@ -660,11 +650,7 @@ bvt bv_pointerst::convert_bitvector(const exprt &expr)
// we postpone until we know the objects
std::size_t width=boolbv_width(expr.type());

bvt bv;
bv.resize(width);

for(std::size_t i=0; i<width; i++)
bv[i]=prop.new_variable();
bvt bv = prop.new_variables(width);

postponed_list.push_back(postponedt());

Expand Down
14 changes: 3 additions & 11 deletions src/solvers/flattening/bv_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -936,14 +936,8 @@ void bv_utilst::unsigned_divider(
literalt is_not_zero=prop.lor(op1);

// free variables for result of division

res.resize(width);
rem.resize(width);
for(std::size_t i=0; i<width; i++)
{
res[i]=prop.new_variable();
rem[i]=prop.new_variable();
}
res = prop.new_variables(width);
rem = prop.new_variables(width);

// add implications

Expand Down Expand Up @@ -1166,9 +1160,7 @@ literalt bv_utilst::lt_or_le(
size_t start;
size_t i;

compareBelow.resize(bv0.size());
for(auto &literal : compareBelow)
literal = prop.new_variable();
compareBelow = prop.new_variables(bv0.size());
result = prop.new_variable();

if(rep==SIGNED)
Expand Down
6 changes: 1 addition & 5 deletions src/solvers/flattening/equality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,7 @@ void equalityt::add_equality_constraints(const typestructt &typestruct)
eq_bvs.resize(no_elements);

for(std::size_t i=0; i<no_elements; i++)
{
eq_bvs[i].resize(bits);
for(std::size_t j=0; j<bits; j++)
eq_bvs[i][j]=prop.new_variable();
}
eq_bvs[i] = prop.new_variables(bits);

// generate equality constraints

Expand Down
4 changes: 2 additions & 2 deletions src/solvers/prop/prop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ void propt::set_equal(literalt a, literalt b)
bvt propt::new_variables(std::size_t width)
{
bvt result;
result.resize(width);
result.reserve(width);
for(std::size_t i=0; i<width; i++)
result[i]=new_variable();
result.push_back(new_variable());
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/solvers/prop/prop.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class propt
virtual literalt new_variable()=0;
virtual void set_variable_name(literalt, const irep_idt &) { }
virtual size_t no_variables() const=0;
bvt new_variables(std::size_t width);
virtual bvt new_variables(std::size_t width);

// solving
virtual const std::string solver_text()=0;
Expand Down
18 changes: 16 additions & 2 deletions src/solvers/sat/cnf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,28 @@ literalt cnft::lselect(literalt a, literalt b, literalt c)
/// \return New variable as literal
literalt cnft::new_variable()
{
literalt l;
l.set(_no_variables, false);
literalt l(_no_variables, false);

set_no_variables(_no_variables+1);

return l;
}

/// Generate a vector of new variables.
/// \return Vector of new variables.
bvt cnft::new_variables(std::size_t width)
{
bvt result;
result.reserve(width);

for(std::size_t i = _no_variables; i < _no_variables + width; ++i)
result.emplace_back(i, false);

set_no_variables(_no_variables + width);

return result;
}

/// eliminate duplicates from given vector of literals
/// \par parameters: set of literals given as vector
/// \return set of literals, duplicates removed
Expand Down
1 change: 1 addition & 0 deletions src/solvers/sat/cnf.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class cnft:public propt
// a?b:c
virtual literalt lselect(literalt a, literalt b, literalt c) override;
virtual literalt new_variable() override;
bvt new_variables(std::size_t width) override;
virtual size_t no_variables() const override { return _no_variables; }
virtual void set_no_variables(size_t no) { _no_variables=no; }
virtual size_t no_clauses() const=0;
Expand Down