Skip to content

Commit 3726631

Browse files
Make rename take a copy instead of reference
This makes the interface clearer as there is no argument that is both an input and an output. This also corresponds to how the function was used in a lot of occurences, in which case the code is now simpler. In the cases where we actually want the transformation to happen in-place, we can use std::move.
1 parent 9d18229 commit 3726631

11 files changed

+60
-72
lines changed

src/goto-instrument/accelerate/scratch_program.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ bool scratch_programt::check_sat(bool do_slice)
6969

7070
exprt scratch_programt::eval(const exprt &e)
7171
{
72-
exprt ssa=e;
73-
74-
symex_state->rename<goto_symex_statet::L2>(ssa, ns);
75-
76-
return checker->get(ssa);
72+
return checker->get(symex_state->rename<goto_symex_statet::L2>(e, ns));
7773
}
7874

7975
void scratch_programt::append(goto_programt::instructionst &new_instructions)

src/goto-symex/goto_symex_state.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ goto_symex_statet::rename_level1_ssa(ssa_exprt ssa, const namespacet &ns)
282282
}
283283

284284
template <goto_symex_statet::levelt level>
285-
void goto_symex_statet::rename(exprt &expr, const namespacet &ns)
285+
exprt goto_symex_statet::rename(exprt expr, const namespacet &ns)
286286
{
287287
// rename all the symbols with their last known value
288288

@@ -330,13 +330,9 @@ void goto_symex_statet::rename(exprt &expr, const namespacet &ns)
330330
{
331331
// we never rename function symbols
332332
if(as_const(expr).type().id() == ID_code)
333-
{
334333
rename<level>(expr.type(), to_symbol_expr(expr).get_identifier(), ns);
335-
return;
336-
}
337-
338-
expr=ssa_exprt(expr);
339-
rename<level>(expr, ns);
334+
else
335+
expr = rename<level>(ssa_exprt(expr), ns);
340336
}
341337
else if(expr.id()==ID_address_of)
342338
{
@@ -351,7 +347,7 @@ void goto_symex_statet::rename(exprt &expr, const namespacet &ns)
351347

352348
// do this recursively
353349
Forall_operands(it, expr)
354-
rename<level>(*it, ns);
350+
*it = rename<level>(std::move(*it), ns);
355351

356352
const exprt &c_expr = as_const(expr);
357353
INVARIANT(
@@ -363,6 +359,7 @@ void goto_symex_statet::rename(exprt &expr, const namespacet &ns)
363359
"Type of renamed expr should be the same as operands for with_exprt and "
364360
"if_exprt");
365361
}
362+
return expr;
366363
}
367364

368365
/// thread encoding
@@ -572,13 +569,13 @@ void goto_symex_statet::rename_address(exprt &expr, const namespacet &ns)
572569
expr.type() = to_array_type(index_expr.array().type()).subtype();
573570

574571
// the index is not an address
575-
rename<level>(index_expr.index(), ns);
572+
index_expr.index() = rename<level>(std::move(index_expr.index()), ns);
576573
}
577574
else if(expr.id()==ID_if)
578575
{
579576
// the condition is not an address
580577
if_exprt &if_expr=to_if_expr(expr);
581-
rename<level>(if_expr.cond(), ns);
578+
if_expr.cond() = rename<level>(std::move(if_expr.cond()), ns);
582579
rename_address<level>(if_expr.true_case(), ns);
583580
rename_address<level>(if_expr.false_case(), ns);
584581

@@ -717,7 +714,7 @@ void goto_symex_statet::rename(
717714
{
718715
auto &array_type = to_array_type(type);
719716
rename<level>(array_type.subtype(), irep_idt(), ns);
720-
rename<level>(array_type.size(), ns);
717+
array_type.size() = rename<level>(std::move(array_type.size()), ns);
721718
}
722719
else if(type.id() == ID_struct || type.id() == ID_union)
723720
{
@@ -728,7 +725,10 @@ void goto_symex_statet::rename(
728725
{
729726
// be careful, or it might get cyclic
730727
if(component.type().id() == ID_array)
731-
rename<level>(to_array_type(component.type()).size(), ns);
728+
{
729+
auto &array_type = to_array_type(component.type());
730+
array_type.size() = rename<level>(std::move(array_type.size()), ns);
731+
}
732732
else if(component.type().id() != ID_pointer)
733733
rename<level>(component.type(), irep_idt(), ns);
734734
}

src/goto-symex/goto_symex_state.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class goto_symex_statet final : public goto_statet
180180
/// A full explanation of SSA (which is why we do this renaming) is in
181181
/// the SSA section of background-concepts.md.
182182
template <levelt level = L2>
183-
void rename(exprt &expr, const namespacet &ns);
183+
exprt rename(exprt expr, const namespacet &ns);
184184

185185
ssa_exprt rename_level0_ssa(ssa_exprt ssa, const namespacet &ns);
186186

src/goto-symex/symex_assign.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,13 @@ void goto_symext::symex_assign_symbol(
212212
tmp_ssa_rhs.swap(ssa_rhs);
213213
}
214214

215-
state.rename(ssa_rhs, ns);
216-
do_simplify(ssa_rhs);
215+
exprt l2_rhs = state.rename(std::move(ssa_rhs), ns);
216+
do_simplify(l2_rhs);
217217

218218
ssa_exprt ssa_lhs=lhs;
219219
state.assignment(
220220
ssa_lhs,
221-
ssa_rhs,
221+
l2_rhs,
222222
ns,
223223
symex_config.simplify_opt,
224224
symex_config.constant_propagation,
@@ -228,7 +228,7 @@ void goto_symext::symex_assign_symbol(
228228
ssa_full_lhs=add_to_lhs(ssa_full_lhs, ssa_lhs);
229229
const bool record_events=state.record_events;
230230
state.record_events=false;
231-
state.rename(ssa_full_lhs, ns);
231+
exprt l2_full_lhs = state.rename(std::move(ssa_full_lhs), ns);
232232
state.record_events=record_events;
233233

234234
guardt tmp_guard(state.guard);
@@ -254,8 +254,9 @@ void goto_symext::symex_assign_symbol(
254254
target.assignment(
255255
tmp_guard.as_expr(),
256256
ssa_lhs,
257-
ssa_full_lhs, add_to_lhs(full_lhs, ssa_lhs.get_original_expr()),
258-
ssa_rhs,
257+
l2_full_lhs,
258+
add_to_lhs(full_lhs, ssa_lhs.get_original_expr()),
259+
l2_rhs,
259260
state.source,
260261
assignment_type);
261262
}
@@ -406,8 +407,7 @@ void goto_symext::symex_assign_if(
406407

407408
guardt old_guard=guard;
408409

409-
exprt renamed_guard=lhs.cond();
410-
state.rename(renamed_guard, ns);
410+
exprt renamed_guard = state.rename(lhs.cond(), ns);
411411
do_simplify(renamed_guard);
412412

413413
if(!renamed_guard.is_false())

src/goto-symex/symex_builtin_functions.cpp

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ void goto_symext::symex_allocate(
7171
}
7272
else
7373
{
74-
exprt tmp_size=size;
75-
state.rename(tmp_size, ns); // to allow constant propagation
74+
// to allow constant propagation
75+
exprt tmp_size = state.rename(size, ns);
7676
simplify(tmp_size, ns);
7777

7878
// special treatment for sizeof(T)*x
@@ -164,8 +164,8 @@ void goto_symext::symex_allocate(
164164

165165
state.symbol_table.add(value_symbol);
166166

167-
exprt zero_init=code.op1();
168-
state.rename(zero_init, ns); // to allow constant propagation
167+
// to allow constant propagation
168+
exprt zero_init = state.rename(code.op1(), ns);
169169
simplify(zero_init, ns);
170170

171171
INVARIANT(
@@ -233,8 +233,8 @@ void goto_symext::symex_gcc_builtin_va_arg_next(
233233
if(lhs.is_nil())
234234
return; // ignore
235235

236-
exprt tmp=code.op0();
237-
state.rename(tmp, ns); // to allow constant propagation
236+
// to allow constant propagation
237+
exprt tmp = state.rename(code.op0(), ns);
238238
do_simplify(tmp);
239239
irep_idt id=get_symbol(tmp);
240240

@@ -311,8 +311,7 @@ void goto_symext::symex_printf(
311311
{
312312
PRECONDITION(!rhs.operands().empty());
313313

314-
exprt tmp_rhs=rhs;
315-
state.rename(tmp_rhs, ns);
314+
exprt tmp_rhs = state.rename(rhs, ns);
316315
do_simplify(tmp_rhs);
317316

318317
const exprt::operandst &operands=tmp_rhs.operands();
@@ -336,17 +335,15 @@ void goto_symext::symex_input(
336335
{
337336
PRECONDITION(code.operands().size() >= 2);
338337

339-
exprt id_arg=code.op0();
340-
341-
state.rename(id_arg, ns);
338+
exprt id_arg = state.rename(code.op0(), ns);
342339

343340
std::list<exprt> args;
344341

345342
for(std::size_t i=1; i<code.operands().size(); i++)
346343
{
347-
args.push_back(code.operands()[i]);
348-
state.rename(args.back(), ns);
349-
do_simplify(args.back());
344+
exprt l2_arg = state.rename(code.operands()[i], ns);
345+
do_simplify(l2_arg);
346+
args.emplace_back(std::move(l2_arg));
350347
}
351348

352349
const irep_idt input_id=get_string_argument(id_arg, ns);
@@ -359,18 +356,15 @@ void goto_symext::symex_output(
359356
const codet &code)
360357
{
361358
PRECONDITION(code.operands().size() >= 2);
362-
363-
exprt id_arg=code.op0();
364-
365-
state.rename(id_arg, ns);
359+
exprt id_arg = state.rename(code.op0(), ns);
366360

367361
std::list<exprt> args;
368362

369363
for(std::size_t i=1; i<code.operands().size(); i++)
370364
{
371-
args.push_back(code.operands()[i]);
372-
state.rename(args.back(), ns);
373-
do_simplify(args.back());
365+
exprt l2_arg = state.rename(code.operands()[i], ns);
366+
do_simplify(l2_arg);
367+
args.emplace_back(std::move(l2_arg));
374368
}
375369

376370
const irep_idt output_id=get_string_argument(id_arg, ns);
@@ -481,11 +475,7 @@ void goto_symext::symex_trace(
481475
irep_idt event = to_string_constant(code.arguments()[1].op0()).get_value();
482476

483477
for(std::size_t j=2; j<code.arguments().size(); j++)
484-
{
485-
exprt var(code.arguments()[j]);
486-
state.rename(var, ns);
487-
vars.push_back(var);
488-
}
478+
vars.push_back(state.rename(code.arguments()[j], ns));
489479

490480
target.output(state.guard.as_expr(), state.source, event, vars);
491481
}

src/goto-symex/symex_dead.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ void goto_symext::symex_dead(statet &state)
3737
else
3838
rhs=exprt(ID_invalid);
3939

40-
state.rename<goto_symex_statet::L1>(rhs, ns);
41-
state.value_set.assign(ssa, rhs, ns, true, false);
40+
const exprt l1_rhs =
41+
state.rename<goto_symex_statet::L1>(std::move(rhs), ns);
42+
state.value_set.assign(ssa, l1_rhs, ns, true, false);
4243
}
4344

4445
const irep_idt &l1_identifier = ssa.get_identifier();

src/goto-symex/symex_decl.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ void goto_symext::symex_decl(statet &state, const symbol_exprt &expr)
5353
else
5454
rhs=exprt(ID_invalid);
5555

56-
state.rename<goto_symex_statet::L1>(rhs, ns);
57-
state.value_set.assign(ssa, rhs, ns, true, false);
56+
exprt l1_rhs = state.rename<goto_symex_statet::L1>(std::move(rhs), ns);
57+
state.value_set.assign(ssa, l1_rhs, ns, true, false);
5858
}
5959

6060
// prevent propagation
@@ -69,7 +69,11 @@ void goto_symext::symex_decl(statet &state, const symbol_exprt &expr)
6969
symex_renaming_levelt::increase_counter(level2_it);
7070
const bool record_events=state.record_events;
7171
state.record_events=false;
72-
state.rename(ssa, ns);
72+
exprt expr_l2 = state.rename(std::move(ssa), ns);
73+
INVARIANT(
74+
expr_l2.id() == ID_symbol && expr_l2.get_bool(ID_C_SSA_symbol),
75+
"symbol to declare should not be replaced by constant propagation");
76+
ssa = to_ssa_expr(expr_l2);
7377
state.record_events=record_events;
7478

7579
// we hide the declaration of auxiliary variables

src/goto-symex/symex_dereference.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,11 @@ void goto_symext::dereference(exprt &expr, statet &state)
357357
// from different frames. Would be enough to rename
358358
// symbols whose address is taken.
359359
PRECONDITION(!state.call_stack().empty());
360-
state.rename<goto_symex_statet::L1>(expr, ns);
360+
exprt l1_expr = state.rename<goto_symex_statet::L1>(expr, ns);
361361

362362
// start the recursion!
363-
dereference_rec(expr, state);
363+
dereference_rec(l1_expr, state);
364364
// dereferencing may introduce new symbol_exprt
365365
// (like __CPROVER_memory)
366-
state.rename<goto_symex_statet::L1>(expr, ns);
366+
expr = state.rename<goto_symex_statet::L1>(std::move(l1_expr), ns);
367367
}

src/goto-symex/symex_function_call.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ void goto_symext::symex_function_call_code(
261261
// read the arguments -- before the locality renaming
262262
exprt::operandst arguments = call.arguments();
263263
for(auto &a : arguments)
264-
state.rename(a, ns);
264+
a = state.rename(std::move(a), ns);
265265

266266
// we hide the call if the caller and callee are both hidden
267267
const bool hidden = state.top().hidden_function && goto_function.is_hidden();

src/goto-symex/symex_goto.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ void goto_symext::symex_goto(statet &state)
3737
exprt old_guard = instruction.get_condition();
3838
clean_expr(old_guard, state, false);
3939

40-
exprt new_guard=old_guard;
41-
state.rename(new_guard, ns);
40+
exprt new_guard = state.rename(old_guard, ns);
4241
do_simplify(new_guard);
4342

4443
if(new_guard.is_false())
@@ -269,8 +268,7 @@ void goto_symext::symex_goto(statet &state)
269268
original_source,
270269
symex_targett::assignment_typet::GUARD);
271270

272-
guard_expr = boolean_negate(guard_symbol_expr);
273-
state.rename(guard_expr, ns);
271+
guard_expr = state.rename(boolean_negate(guard_symbol_expr), ns);
274272
}
275273

276274
if(state.has_saved_jump_target)

src/goto-symex/symex_main.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,18 @@ void goto_symext::vcc(
9595
}
9696

9797
// now rename, enables propagation
98-
state.rename(expr, ns);
98+
exprt l2_expr = state.rename(std::move(expr), ns);
9999

100100
// now try simplifier on it
101-
do_simplify(expr);
101+
do_simplify(l2_expr);
102102

103-
if(expr.is_true())
103+
if(l2_expr.is_true())
104104
return;
105105

106-
state.guard.guard_expr(expr);
106+
state.guard.guard_expr(l2_expr);
107107

108108
state.remaining_vccs++;
109-
target.assertion(state.guard.as_expr(), expr, msg, state.source);
109+
target.assertion(state.guard.as_expr(), l2_expr, msg, state.source);
110110
}
111111

112112
void goto_symext::symex_assume(statet &state, const exprt &cond)
@@ -410,8 +410,7 @@ void goto_symext::symex_step(
410410
{
411411
exprt tmp = instruction.get_condition();
412412
clean_expr(tmp, state, false);
413-
state.rename(tmp, ns);
414-
symex_assume(state, tmp);
413+
symex_assume(state, state.rename(std::move(tmp), ns));
415414
}
416415

417416
symex_transition(state);

0 commit comments

Comments
 (0)