Skip to content

Commit 3333db0

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 8418997 commit 3333db0

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
@@ -281,7 +281,7 @@ goto_symex_statet::rename_level1_ssa(ssa_exprt ssa, const namespacet &ns)
281281
}
282282

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

@@ -329,13 +329,9 @@ void goto_symex_statet::rename(exprt &expr, const namespacet &ns)
329329
{
330330
// we never rename function symbols
331331
if(as_const(expr).type().id() == ID_code)
332-
{
333332
rename<level>(expr.type(), to_symbol_expr(expr).get_identifier(), ns);
334-
return;
335-
}
336-
337-
expr=ssa_exprt(expr);
338-
rename<level>(expr, ns);
333+
else
334+
expr = rename<level>(ssa_exprt(expr), ns);
339335
}
340336
else if(expr.id()==ID_address_of)
341337
{
@@ -350,7 +346,7 @@ void goto_symex_statet::rename(exprt &expr, const namespacet &ns)
350346

351347
// do this recursively
352348
Forall_operands(it, expr)
353-
rename<level>(*it, ns);
349+
*it = rename<level>(std::move(*it), ns);
354350

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

367364
/// thread encoding
@@ -571,13 +568,13 @@ void goto_symex_statet::rename_address(exprt &expr, const namespacet &ns)
571568
expr.type() = to_array_type(index_expr.array().type()).subtype();
572569

573570
// the index is not an address
574-
rename<level>(index_expr.index(), ns);
571+
index_expr.index() = rename<level>(std::move(index_expr.index()), ns);
575572
}
576573
else if(expr.id()==ID_if)
577574
{
578575
// the condition is not an address
579576
if_exprt &if_expr=to_if_expr(expr);
580-
rename<level>(if_expr.cond(), ns);
577+
if_expr.cond() = rename<level>(std::move(if_expr.cond()), ns);
581578
rename_address<level>(if_expr.true_case(), ns);
582579
rename_address<level>(if_expr.false_case(), ns);
583580

@@ -716,7 +713,7 @@ void goto_symex_statet::rename(
716713
{
717714
auto &array_type = to_array_type(type);
718715
rename<level>(array_type.subtype(), irep_idt(), ns);
719-
rename<level>(array_type.size(), ns);
716+
array_type.size() = rename<level>(std::move(array_type.size()), ns);
720717
}
721718
else if(type.id() == ID_struct || type.id() == ID_union)
722719
{
@@ -727,7 +724,10 @@ void goto_symex_statet::rename(
727724
{
728725
// be careful, or it might get cyclic
729726
if(component.type().id() == ID_array)
730-
rename<level>(to_array_type(component.type()).size(), ns);
727+
{
728+
auto &array_type = to_array_type(component.type());
729+
array_type.size() = rename<level>(std::move(array_type.size()), ns);
730+
}
731731
else if(component.type().id() != ID_pointer)
732732
rename<level>(component.type(), irep_idt(), ns);
733733
}

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
@@ -70,8 +70,8 @@ void goto_symext::symex_allocate(
7070
}
7171
else
7272
{
73-
exprt tmp_size=size;
74-
state.rename(tmp_size, ns); // to allow constant propagation
73+
// to allow constant propagation
74+
exprt tmp_size = state.rename(size, ns);
7575
simplify(tmp_size, ns);
7676

7777
// special treatment for sizeof(T)*x
@@ -162,8 +162,8 @@ void goto_symext::symex_allocate(
162162

163163
state.symbol_table.add(value_symbol);
164164

165-
exprt zero_init=code.op1();
166-
state.rename(zero_init, ns); // to allow constant propagation
165+
// to allow constant propagation
166+
exprt zero_init = state.rename(code.op1(), ns);
167167
simplify(zero_init, ns);
168168

169169
INVARIANT(
@@ -231,8 +231,8 @@ void goto_symext::symex_gcc_builtin_va_arg_next(
231231
if(lhs.is_nil())
232232
return; // ignore
233233

234-
exprt tmp=code.op0();
235-
state.rename(tmp, ns); // to allow constant propagation
234+
// to allow constant propagation
235+
exprt tmp = state.rename(code.op0(), ns);
236236
do_simplify(tmp);
237237
irep_idt id=get_symbol(tmp);
238238

@@ -309,8 +309,7 @@ void goto_symext::symex_printf(
309309
{
310310
PRECONDITION(!rhs.operands().empty());
311311

312-
exprt tmp_rhs=rhs;
313-
state.rename(tmp_rhs, ns);
312+
exprt tmp_rhs = state.rename(rhs, ns);
314313
do_simplify(tmp_rhs);
315314

316315
const exprt::operandst &operands=tmp_rhs.operands();
@@ -334,17 +333,15 @@ void goto_symext::symex_input(
334333
{
335334
PRECONDITION(code.operands().size() >= 2);
336335

337-
exprt id_arg=code.op0();
338-
339-
state.rename(id_arg, ns);
336+
exprt id_arg = state.rename(code.op0(), ns);
340337

341338
std::list<exprt> args;
342339

343340
for(std::size_t i=1; i<code.operands().size(); i++)
344341
{
345-
args.push_back(code.operands()[i]);
346-
state.rename(args.back(), ns);
347-
do_simplify(args.back());
342+
exprt l2_arg = state.rename(code.operands()[i], ns);
343+
do_simplify(l2_arg);
344+
args.emplace_back(std::move(l2_arg));
348345
}
349346

350347
const irep_idt input_id=get_string_argument(id_arg, ns);
@@ -357,18 +354,15 @@ void goto_symext::symex_output(
357354
const codet &code)
358355
{
359356
PRECONDITION(code.operands().size() >= 2);
360-
361-
exprt id_arg=code.op0();
362-
363-
state.rename(id_arg, ns);
357+
exprt id_arg = state.rename(code.op0(), ns);
364358

365359
std::list<exprt> args;
366360

367361
for(std::size_t i=1; i<code.operands().size(); i++)
368362
{
369-
args.push_back(code.operands()[i]);
370-
state.rename(args.back(), ns);
371-
do_simplify(args.back());
363+
exprt l2_arg = state.rename(code.operands()[i], ns);
364+
do_simplify(l2_arg);
365+
args.emplace_back(std::move(l2_arg));
372366
}
373367

374368
const irep_idt output_id=get_string_argument(id_arg, ns);
@@ -479,11 +473,7 @@ void goto_symext::symex_trace(
479473
irep_idt event = to_string_constant(code.arguments()[1].op0()).get_value();
480474

481475
for(std::size_t j=2; j<code.arguments().size(); j++)
482-
{
483-
exprt var(code.arguments()[j]);
484-
state.rename(var, ns);
485-
vars.push_back(var);
486-
}
476+
vars.push_back(state.rename(code.arguments()[j], ns));
487477

488478
target.output(state.guard.as_expr(), state.source, event, vars);
489479
}

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)