Skip to content

Commit 5283612

Browse files
committed
Cleanup and use propt::new_variables
Several places still locally had a for loop where each step performed a call to propt::new_variable, which is all taken care of by propt::new_variables(width). Also override this method in cnft to avoid repeated calls to set_no_variables.
1 parent 8a117bc commit 5283612

File tree

12 files changed

+38
-67
lines changed

12 files changed

+38
-67
lines changed

src/solvers/flattening/boolbv.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,7 @@ exprt boolbvt::make_free_bv_expr(const typet &type)
534534
{
535535
const std::size_t width = boolbv_width(type);
536536
PRECONDITION(width != 0);
537-
bvt bv(width);
538-
for(auto &lit : bv)
539-
lit = prop.new_variable();
537+
bvt bv = prop.new_variables(width);
540538
return make_bv_expr(type, bv);
541539
}
542540

src/solvers/flattening/boolbv_byte_extract.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ bvt boolbvt::convert_byte_extract(const byte_extract_exprt &expr)
128128
if(prop.has_set_to())
129129
{
130130
// free variables
131-
for(std::size_t i=0; i<width; i++)
132-
bv[i]=prop.new_variable();
131+
bv = prop.new_variables(width);
133132

134133
// add implications
135134

src/solvers/flattening/boolbv_case.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ bvt boolbvt::convert_case(const exprt &expr)
2121
if(width==0)
2222
return conversion_failed(expr);
2323

24-
bvt bv;
25-
bv.resize(width);
26-
2724
// make it free variables
28-
for(auto &literal : bv)
29-
literal = prop.new_variable();
25+
bvt bv = prop.new_variables(width);
3026

3127
DATA_INVARIANT(
3228
operands.size() >= 3, "case should have at least three operands");

src/solvers/flattening/boolbv_cond.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ bvt boolbvt::convert_cond(const cond_exprt &expr)
2020
return conversion_failed(expr);
2121

2222
bvt bv;
23-
bv.resize(width);
2423

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

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

3635
// make it free variables
37-
for(auto &literal : bv)
38-
literal = prop.new_variable();
36+
bv = prop.new_variables(width);
3937

4038
forall_operands(it, expr)
4139
{
@@ -60,6 +58,8 @@ bvt boolbvt::convert_cond(const cond_exprt &expr)
6058
}
6159
else
6260
{
61+
bv.resize(width);
62+
6363
// functional version -- go backwards
6464
for(std::size_t i=expr.operands().size(); i!=0; i-=2)
6565
{

src/solvers/flattening/boolbv_index.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ bvt boolbvt::convert_index(const index_exprt &expr)
6767
else
6868
{
6969
// free variables
70-
bv.reserve(width);
71-
for(std::size_t i = 0; i < width; i++)
72-
bv.push_back(prop.new_variable());
70+
bv = prop.new_variables(width);
7371

7472
record_array_index(expr);
7573

@@ -219,10 +217,7 @@ bvt boolbvt::convert_index(const index_exprt &expr)
219217
if(prop.has_set_to())
220218
{
221219
// free variables
222-
223-
bv.resize(width);
224-
for(std::size_t i=0; i<width; i++)
225-
bv[i]=prop.new_variable();
220+
bv = prop.new_variables(width);
226221

227222
// add implications
228223

@@ -297,9 +292,6 @@ bvt boolbvt::convert_index(
297292
if(width==0)
298293
return conversion_failed(array);
299294

300-
bvt bv;
301-
bv.resize(width);
302-
303295
// TODO: If the underlying array can use one of the
304296
// improvements given above then it may be better to use
305297
// the array theory for short chains of updates and then
@@ -325,8 +317,8 @@ bvt boolbvt::convert_index(
325317
// array.id()!=ID_array);
326318
// If not there are large improvements possible as above
327319

328-
for(std::size_t i=0; i<width; i++)
329-
bv[i] = tmp[numeric_cast_v<std::size_t>(offset + i)];
320+
std::size_t offset_int = numeric_cast_v<std::size_t>(offset);
321+
return bvt(tmp.begin() + offset_int, tmp.begin() + offset_int + width);
330322
}
331323
else if(
332324
array.id() == ID_member || array.id() == ID_index ||
@@ -354,9 +346,6 @@ bvt boolbvt::convert_index(
354346
else
355347
{
356348
// out of bounds
357-
for(std::size_t i=0; i<width; i++)
358-
bv[i]=prop.new_variable();
349+
return prop.new_variables(width);
359350
}
360-
361-
return bv;
362351
}

src/solvers/flattening/bv_pointers.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,7 @@ bvt bv_pointerst::convert_pointer_type(const exprt &expr)
341341
}
342342
else if(expr.id()==ID_nondet_symbol)
343343
{
344-
bvt bv;
345-
bv.resize(bits);
346-
347-
for(auto &literal : bv)
348-
literal = prop.new_variable();
349-
350-
return bv;
344+
return prop.new_variables(bits);
351345
}
352346
else if(expr.id()==ID_typecast)
353347
{
@@ -576,11 +570,7 @@ bvt bv_pointerst::convert_bitvector(const exprt &expr)
576570
const bvt object_size_bv =
577571
bv_utils.zero_extension(convert_bv(object_size), width);
578572

579-
bvt bv;
580-
bv.reserve(width);
581-
582-
for(std::size_t i = 0; i < width; ++i)
583-
bv.push_back(prop.new_variable());
573+
bvt bv = prop.new_variables(width);
584574

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

663-
bvt bv;
664-
bv.resize(width);
665-
666-
for(std::size_t i=0; i<width; i++)
667-
bv[i]=prop.new_variable();
653+
bvt bv = prop.new_variables(width);
668654

669655
postponed_list.push_back(postponedt());
670656

src/solvers/flattening/bv_utils.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -936,14 +936,8 @@ void bv_utilst::unsigned_divider(
936936
literalt is_not_zero=prop.lor(op1);
937937

938938
// free variables for result of division
939-
940-
res.resize(width);
941-
rem.resize(width);
942-
for(std::size_t i=0; i<width; i++)
943-
{
944-
res[i]=prop.new_variable();
945-
rem[i]=prop.new_variable();
946-
}
939+
res = prop.new_variables(width);
940+
rem = prop.new_variables(width);
947941

948942
// add implications
949943

@@ -1166,9 +1160,7 @@ literalt bv_utilst::lt_or_le(
11661160
size_t start;
11671161
size_t i;
11681162

1169-
compareBelow.resize(bv0.size());
1170-
for(auto &literal : compareBelow)
1171-
literal = prop.new_variable();
1163+
compareBelow = prop.new_variables(bv0.size());
11721164
result = prop.new_variable();
11731165

11741166
if(rep==SIGNED)

src/solvers/flattening/equality.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,7 @@ void equalityt::add_equality_constraints(const typestructt &typestruct)
110110
eq_bvs.resize(no_elements);
111111

112112
for(std::size_t i=0; i<no_elements; i++)
113-
{
114-
eq_bvs[i].resize(bits);
115-
for(std::size_t j=0; j<bits; j++)
116-
eq_bvs[i][j]=prop.new_variable();
117-
}
113+
eq_bvs[i] = prop.new_variables(bits);
118114

119115
// generate equality constraints
120116

src/solvers/prop/prop.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ void propt::set_equal(literalt a, literalt b)
2020
bvt propt::new_variables(std::size_t width)
2121
{
2222
bvt result;
23-
result.resize(width);
23+
result.reserve(width);
2424
for(std::size_t i=0; i<width; i++)
25-
result[i]=new_variable();
25+
result.push_back(new_variable());
2626
return result;
2727
}
2828

src/solvers/prop/prop.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class propt
9292
virtual literalt new_variable()=0;
9393
virtual void set_variable_name(literalt, const irep_idt &) { }
9494
virtual size_t no_variables() const=0;
95-
bvt new_variables(std::size_t width);
95+
virtual bvt new_variables(std::size_t width);
9696

9797
// solving
9898
virtual const std::string solver_text()=0;

0 commit comments

Comments
 (0)