Skip to content

Commit aca30f7

Browse files
author
kroening
committed
Make convert_bv return a reference instead of writing to parameter
git-svn-id: svn+ssh://svn.cprover.org/srv/svn/cbmc/trunk@1054 6afb6bc1-c8e4-404c-8f48-9ae832c5b171
1 parent 4866c21 commit aca30f7

34 files changed

+104
-201
lines changed

src/solvers/flattening/boolbv.cpp

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -136,32 +136,28 @@ Function: boolbvt::convert_bv
136136
137137
\*******************************************************************/
138138

139-
void boolbvt::convert_bv(const exprt &expr, bvt &bv)
139+
const bvt& boolbvt::convert_bv(const exprt &expr)
140140
{
141141
// check cache first
142-
142+
std::pair<bv_cachet::iterator, bool> cache_result=
143+
bv_cache.insert(std::make_pair(expr, bvt()));
144+
if(!cache_result.second)
143145
{
144-
bv_cachet::const_iterator cache_result=bv_cache.find(expr);
145-
if(cache_result!=bv_cache.end())
146-
{
147-
//std::cerr << "Cache hit on " << expr << "\n";
148-
bv=cache_result->second;
149-
return;
150-
}
146+
//std::cerr << "Cache hit on " << expr << "\n";
147+
return cache_result.first->second;
151148
}
152149

153-
convert_bitvector(expr, bv);
150+
convert_bitvector(expr, cache_result.first->second);
154151

155152
// check
156-
forall_literals(it, bv)
153+
forall_literals(it, cache_result.first->second)
157154
if(it->var_no()==literalt::unused_var_no())
158155
{
159156
std::cout << "unused_var_no: " << expr.pretty() << std::endl;
160157
assert(false);
161158
}
162159

163-
// insert into cache
164-
bv_cache.insert(std::pair<const exprt, bvt>(expr, bv));
160+
return cache_result.first->second;
165161
}
166162

167163
/*******************************************************************\
@@ -370,26 +366,24 @@ void boolbvt::convert_array(const exprt &expr, bvt &bv)
370366
if(width==0)
371367
return conversion_failed(expr, bv);
372368

373-
bv.resize(width);
369+
bv.reserve(width);
374370

375371
if(expr.type().id()==ID_array)
376372
{
377-
unsigned op_width=width/expr.operands().size();
378-
unsigned offset=0;
373+
assert(expr.has_operands());
374+
const exprt::operandst &operands=expr.operands();
375+
assert(!operands.empty());
376+
unsigned op_width=width/operands.size();
379377

380-
forall_operands(it, expr)
378+
forall_expr(it, operands)
381379
{
382-
bvt tmp;
383-
384-
convert_bv(*it, tmp);
380+
const bvt &tmp=convert_bv(*it);
385381

386382
if(tmp.size()!=op_width)
387383
throw "convert_array: unexpected operand width";
388384

389-
for(unsigned j=0; j<op_width; j++)
390-
bv[offset+j]=tmp[j];
391-
392-
offset+=op_width;
385+
forall_literals(it2, tmp)
386+
bv.push_back(*it2);
393387
}
394388

395389
return;
@@ -440,8 +434,7 @@ void boolbvt::convert_lambda(const exprt &expr, bvt &bv)
440434
exprt expr(expr.op1());
441435
replace_expr(expr.op0(), counter, expr);
442436

443-
bvt tmp;
444-
convert_bv(expr, tmp);
437+
const bvt &tmp=convert_bv(expr);
445438

446439
unsigned offset=integer2long(i*tmp.size());
447440

@@ -614,8 +607,7 @@ literalt boolbvt::convert_rest(const exprt &expr)
614607
if(expr.operands().size()!=1)
615608
throw "sign expects one operand";
616609

617-
bvt bv;
618-
convert_bv(operands[0], bv);
610+
const bvt &bv=convert_bv(operands[0]);
619611

620612
if(bv.size()<1)
621613
throw "sign operator takes one non-empty operand";
@@ -640,8 +632,7 @@ literalt boolbvt::convert_rest(const exprt &expr)
640632
if(expr.operands().size()!=1)
641633
throw "isnan expects one operand";
642634

643-
bvt bv;
644-
convert_bv(operands[0], bv);
635+
const bvt &bv=convert_bv(operands[0]);
645636

646637
if(expr.op0().type().id()==ID_floatbv)
647638
{
@@ -659,8 +650,7 @@ literalt boolbvt::convert_rest(const exprt &expr)
659650
if(expr.operands().size()!=1)
660651
throw "isfinite expects one operand";
661652

662-
bvt bv;
663-
convert_bv(operands[0], bv);
653+
const bvt &bv=convert_bv(operands[0]);
664654

665655
if(expr.op0().type().id()==ID_floatbv)
666656
{
@@ -680,8 +670,7 @@ literalt boolbvt::convert_rest(const exprt &expr)
680670
if(expr.operands().size()!=1)
681671
throw "isinf expects one operand";
682672

683-
bvt bv;
684-
convert_bv(operands[0], bv);
673+
const bvt &bv=convert_bv(operands[0]);
685674

686675
if(expr.op0().type().id()==ID_floatbv)
687676
{
@@ -699,8 +688,7 @@ literalt boolbvt::convert_rest(const exprt &expr)
699688
if(expr.operands().size()!=1)
700689
throw "isnormal expects one operand";
701690

702-
bvt bv;
703-
convert_bv(operands[0], bv);
691+
const bvt &bv=convert_bv(operands[0]);
704692

705693
if(expr.op0().type().id()==ID_floatbv)
706694
{
@@ -747,11 +735,10 @@ bool boolbvt::boolbv_set_equality_to_true(const exprt &expr)
747735
if(is_unbounded_array(type))
748736
return true;
749737

750-
bvt bv1;
751-
convert_bv(operands[1], bv1);
738+
const bvt &bv1=convert_bv(operands[1]);
752739

753740
const irep_idt &identifier=
754-
operands[0].get(ID_identifier);
741+
to_symbol_expr(operands[0]).get_identifier();
755742

756743
for(unsigned i=0; i<bv1.size(); i++)
757744
map.set_literal(identifier, i, type, bv1[i]);

src/solvers/flattening/boolbv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class boolbvt:public arrayst
4242
{
4343
}
4444

45-
virtual void convert_bv(const exprt &expr, bvt &bv); // check cache
45+
virtual const bvt& convert_bv(const exprt &expr); // check cache
4646
virtual void convert_bitvector(const exprt &expr, bvt &bv); // no cache
4747

4848
// overloading

src/solvers/flattening/boolbv_abs.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ void boolbvt::convert_abs(const exprt &expr, bvt &bv)
4141

4242
const exprt &op0=expr.op0();
4343

44-
bvt op_bv;
45-
convert_bv(op0, op_bv);
44+
const bvt &op_bv=convert_bv(op0);
4645

4746
if(op0.type()!=expr.type())
4847
return conversion_failed(expr, bv);

src/solvers/flattening/boolbv_add_sub.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void boolbvt::convert_add_sub(const exprt &expr, bvt &bv)
5858
throw "add/sub with mixed types";
5959
}
6060

61-
convert_bv(op0, bv);
61+
bv=convert_bv(op0);
6262

6363
if(bv.size()!=width)
6464
throw "convert_add_sub: unexpected operand 0 width";
@@ -87,8 +87,7 @@ void boolbvt::convert_add_sub(const exprt &expr, bvt &bv)
8787
throw "add/sub with mixed types";
8888
}
8989

90-
bvt op;
91-
convert_bv(*it, op);
90+
const bvt &op=convert_bv(*it);
9291

9392
if(op.size()!=width)
9493
throw "convert_add_sub: unexpected operand width";

src/solvers/flattening/boolbv_array_of.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ void boolbvt::convert_array_of(const exprt &expr, bvt &bv)
4848
if(to_integer(array_size, size))
4949
return conversion_failed(expr, bv);
5050

51-
bvt tmp;
52-
convert_bv(expr.op0(), tmp);
51+
const bvt &tmp=convert_bv(expr.op0());
5352

5453
bv.resize(width);
5554

src/solvers/flattening/boolbv_bitwise.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ void boolbvt::convert_bitwise(const exprt &expr, bvt &bv)
3434

3535
const exprt &op0=expr.op0();
3636

37-
bvt op_bv;
38-
convert_bv(op0, op_bv);
37+
const bvt &op_bv=convert_bv(op0);
3938

4039
bv.resize(width);
4140

@@ -57,9 +56,7 @@ void boolbvt::convert_bitwise(const exprt &expr, bvt &bv)
5756

5857
forall_operands(it, expr)
5958
{
60-
bvt op;
61-
62-
convert_bv(*it, op);
59+
const bvt &op=convert_bv(*it);
6360

6461
if(op.size()!=width)
6562
throw "convert_bitwise: unexpected operand width";

src/solvers/flattening/boolbv_bv_rel.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ literalt boolbvt::convert_bv_rel(const exprt &expr)
3737
const exprt &op0=expr.op0();
3838
const exprt &op1=expr.op1();
3939

40-
bvt bv0, bv1;
41-
42-
convert_bv(op0, bv0);
43-
convert_bv(op1, bv1);
40+
const bvt &bv0=convert_bv(op0);
41+
const bvt &bv1=convert_bv(op1);
4442

4543
bvtypet bvtype0=get_bvtype(op0.type());
4644
bvtypet bvtype1=get_bvtype(op1.type());

src/solvers/flattening/boolbv_byte_extract.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void boolbvt::convert_byte_extract(const exprt &expr, bvt &bv)
3636
if(is_unbounded_array(expr.op0().type()))
3737
{
3838
exprt tmp=flatten_byte_extract(expr, ns);
39-
convert_bv(tmp, bv);
39+
bv=convert_bv(tmp);
4040
return;
4141
}
4242

@@ -59,8 +59,7 @@ void boolbvt::convert_byte_extract(const exprt &expr, bvt &bv)
5959

6060
// first do op0
6161

62-
bvt op0_bv;
63-
convert_bv(op0, op0_bv);
62+
const bvt &op0_bv=convert_bv(op0);
6463

6564
// see if the byte number is constant
6665

src/solvers/flattening/boolbv_byte_update.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ void boolbvt::convert_byte_update(const exprt &expr, bvt &bv)
4545
else
4646
assert(false);
4747

48-
convert_bv(op0, bv);
48+
bv=convert_bv(op0);
4949

50-
bvt op2_bv;
51-
convert_bv(op2, op2_bv);
50+
const bvt &op2_bv=convert_bv(op2);
5251
unsigned update_width=op2_bv.size();
5352
unsigned byte_width=8;
5453

src/solvers/flattening/boolbv_case.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ void boolbvt::convert_case(const exprt &expr, bvt &bv)
5050

5151
forall_operands(it, expr)
5252
{
53-
bvt op;
54-
55-
convert_bv(*it, op);
53+
bvt op=convert_bv(*it);
5654

5755
switch(what)
5856
{

src/solvers/flattening/boolbv_concatenation.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ void boolbvt::convert_concatenation(const exprt &expr, bvt &bv)
3737

3838
forall_expr(it, operands)
3939
{
40-
bvt op;
41-
42-
convert_bv(*it, op);
40+
const bvt &op=convert_bv(*it);
4341

4442
if(op.size()>offset)
4543
throw "concatenation operand width too big";

src/solvers/flattening/boolbv_cond.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ void boolbvt::convert_cond(const exprt &expr, bvt &bv)
6060
}
6161
else
6262
{
63-
bvt op;
64-
65-
convert_bv(*it, op);
63+
const bvt &op=convert_bv(*it);
6664

6765
if(bv.size()!=op.size())
6866
{
@@ -94,9 +92,7 @@ void boolbvt::convert_cond(const exprt &expr, bvt &bv)
9492

9593
literalt cond_literal=convert(cond);
9694

97-
bvt op;
98-
99-
convert_bv(value, op);
95+
const bvt &op=convert_bv(value);
10096

10197
if(bv.size()!=op.size())
10298
throw "unexpected operand size in convert_cond";

src/solvers/flattening/boolbv_constant.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ void boolbvt::convert_constant(const exprt &expr, bvt &bv)
3636

3737
forall_operands(it, expr)
3838
{
39-
bvt tmp;
40-
41-
convert_bv(*it, tmp);
39+
const bvt &tmp=convert_bv(*it);
4240

4341
if(tmp.size()!=op_width)
4442
throw "convert_constant: unexpected operand width";

src/solvers/flattening/boolbv_constraint_select_one.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ void boolbvt::convert_constraint_select_one(const exprt &expr, bvt &bv)
3636
if(prop.has_set_to())
3737
{
3838
std::vector<bvt> op_bv;
39-
op_bv.resize(expr.operands().size());
39+
op_bv.reserve(expr.operands().size());
4040

41-
unsigned i=0;
4241
forall_operands(it, expr)
43-
convert_bv(*it, op_bv[i++]);
42+
op_bv.push_back(convert_bv(*it));
4443

4544
bv=op_bv[0];
4645

@@ -70,8 +69,7 @@ void boolbvt::convert_constraint_select_one(const exprt &expr, bvt &bv)
7069
unsigned op_nr=0;
7170
forall_operands(it, expr)
7271
{
73-
bvt op_bv;
74-
convert_bv(*it, op_bv);
72+
const bvt &op_bv=convert_bv(*it);
7573

7674
if(op_nr==0)
7775
bv=op_bv;

src/solvers/flattening/boolbv_div.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ void boolbvt::convert_div(const exprt &expr, bvt &bv)
4646
expr.op1().type().id()!=expr.type().id())
4747
return conversion_failed(expr, bv);
4848

49-
bvt op0, op1;
50-
51-
convert_bv(expr.op0(), op0);
52-
convert_bv(expr.op1(), op1);
49+
bvt op0=convert_bv(expr.op0());
50+
bvt op1=convert_bv(expr.op1());
5351

5452
if(op0.size()!=width ||
5553
op1.size()!=width)

src/solvers/flattening/boolbv_equality.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,8 @@ literalt boolbvt::convert_equality(const equal_exprt &expr)
5454
return record_array_equality(expr);
5555
}
5656

57-
bvt bv0, bv1;
58-
59-
convert_bv(expr.lhs(), bv0);
60-
convert_bv(expr.rhs(), bv1);
57+
const bvt &bv0=convert_bv(expr.lhs());
58+
const bvt &bv1=convert_bv(expr.rhs());
6159

6260
if(bv0.size()!=bv1.size())
6361
{

src/solvers/flattening/boolbv_extractbit.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ literalt boolbvt::convert_extractbit(const extractbit_exprt &expr)
3333
if(operands.size()!=2)
3434
throw "extractbit takes two operands";
3535

36-
bvt bv0;
37-
convert_bv(operands[0], bv0);
36+
const bvt &bv0=convert_bv(operands[0]);
3837

3938
// constant?
4039
if(operands[1].is_constant())

src/solvers/flattening/boolbv_extractbits.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ void boolbvt::convert_extractbits(const extractbits_exprt &expr, bvt &bv)
3939
throw "extractbits takes three operands";
4040

4141
mp_integer o1, o2;
42-
bvt bv0;
43-
44-
convert_bv(expr.op0(), bv0);
42+
const bvt &bv0=convert_bv(expr.op0());
4543

4644
// We only do constants for now.
4745
// Should implement a shift here.

0 commit comments

Comments
 (0)