Skip to content

Commit 659d220

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 2ea01c2 commit 659d220

11 files changed

+65
-73
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>(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>
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: 9 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<goto_symex_statet::L2>(ssa_rhs, ns);
216-
do_simplify(ssa_rhs);
215+
exprt l2_rhs = state.rename<goto_symex_statet::L2>(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,8 @@ 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<goto_symex_statet::L2>(ssa_full_lhs, ns);
231+
exprt l2_full_lhs =
232+
state.rename<goto_symex_statet::L2>(std::move(ssa_full_lhs), ns);
232233
state.record_events=record_events;
233234

234235
guardt tmp_guard(state.guard);
@@ -254,8 +255,9 @@ void goto_symext::symex_assign_symbol(
254255
target.assignment(
255256
tmp_guard.as_expr(),
256257
ssa_lhs,
257-
ssa_full_lhs, add_to_lhs(full_lhs, ssa_lhs.get_original_expr()),
258-
ssa_rhs,
258+
l2_full_lhs,
259+
add_to_lhs(full_lhs, ssa_lhs.get_original_expr()),
260+
l2_rhs,
259261
state.source,
260262
assignment_type);
261263
}
@@ -406,8 +408,7 @@ void goto_symext::symex_assign_if(
406408

407409
guardt old_guard=guard;
408410

409-
exprt renamed_guard=lhs.cond();
410-
state.rename<goto_symex_statet::L2>(renamed_guard, ns);
411+
exprt renamed_guard = state.rename<goto_symex_statet::L2>(lhs.cond(), ns);
411412
do_simplify(renamed_guard);
412413

413414
if(!renamed_guard.is_false())

src/goto-symex/symex_builtin_functions.cpp

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ void goto_symext::symex_allocate(
7070
}
7171
else
7272
{
73-
exprt tmp_size=size;
74-
state.rename<goto_symex_statet::L2>(
75-
tmp_size, ns); // to allow constant propagation
73+
// to allow constant propagation
74+
exprt tmp_size = state.rename<goto_symex_statet::L2>(size, ns);
7675
simplify(tmp_size, ns);
7776

7877
// special treatment for sizeof(T)*x
@@ -163,9 +162,8 @@ void goto_symext::symex_allocate(
163162

164163
state.symbol_table.add(value_symbol);
165164

166-
exprt zero_init=code.op1();
167-
state.rename<goto_symex_statet::L2>(
168-
zero_init, ns); // to allow constant propagation
165+
// to allow constant propagation
166+
exprt zero_init = state.rename<goto_symex_statet::L2>(code.op1(), ns);
169167
simplify(zero_init, ns);
170168

171169
INVARIANT(
@@ -233,8 +231,8 @@ void goto_symext::symex_gcc_builtin_va_arg_next(
233231
if(lhs.is_nil())
234232
return; // ignore
235233

236-
exprt tmp=code.op0();
237-
state.rename<goto_symex_statet::L2>(tmp, ns); // to allow constant propagation
234+
// to allow constant propagation
235+
exprt tmp = state.rename<goto_symex_statet::L2>(code.op0(), ns);
238236
do_simplify(tmp);
239237
irep_idt id=get_symbol(tmp);
240238

@@ -311,11 +309,10 @@ void goto_symext::symex_printf(
311309
{
312310
PRECONDITION(!rhs.operands().empty());
313311

314-
exprt tmp_rhs=rhs;
315-
state.rename<goto_symex_statet::L2>(tmp_rhs, ns);
312+
exprt tmp_rhs = state.rename<goto_symex_statet::L2>(rhs, ns);
316313
do_simplify(tmp_rhs);
317314

318-
const exprt::operandst &operands=tmp_rhs.operands();
315+
const exprt::operandst &operands = tmp_rhs.operands();
319316
std::list<exprt> args;
320317

321318
for(std::size_t i=1; i<operands.size(); i++)
@@ -336,17 +333,16 @@ void goto_symext::symex_input(
336333
{
337334
PRECONDITION(code.operands().size() >= 2);
338335

339-
exprt id_arg=code.op0();
340-
341-
state.rename<goto_symex_statet::L2>(id_arg, ns);
336+
exprt id_arg = state.rename<goto_symex_statet::L2>(code.op0(), ns);
342337

343338
std::list<exprt> args;
344339

345340
for(std::size_t i=1; i<code.operands().size(); i++)
346341
{
347342
args.push_back(code.operands()[i]);
348-
state.rename<goto_symex_statet::L2>(args.back(), ns);
349-
do_simplify(args.back());
343+
exprt l2_arg = state.rename<goto_symex_statet::L2>(args.back(), ns);
344+
do_simplify(l2_arg);
345+
args.back() = std::move(l2_arg);
350346
}
351347

352348
const irep_idt input_id=get_string_argument(id_arg, ns);
@@ -359,18 +355,16 @@ void goto_symext::symex_output(
359355
const codet &code)
360356
{
361357
PRECONDITION(code.operands().size() >= 2);
362-
363-
exprt id_arg=code.op0();
364-
365-
state.rename<goto_symex_statet::L2>(id_arg, ns);
358+
exprt id_arg = state.rename<goto_symex_statet::L2>(code.op0(), ns);
366359

367360
std::list<exprt> args;
368361

369362
for(std::size_t i=1; i<code.operands().size(); i++)
370363
{
371364
args.push_back(code.operands()[i]);
372-
state.rename<goto_symex_statet::L2>(args.back(), ns);
373-
do_simplify(args.back());
365+
exprt l2_arg = state.rename<goto_symex_statet::L2>(args.back(), ns);
366+
do_simplify(l2_arg);
367+
args.back() = std::move(l2_arg);
374368
}
375369

376370
const irep_idt output_id=get_string_argument(id_arg, ns);
@@ -481,11 +475,8 @@ 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<goto_symex_statet::L2>(var, ns);
487-
vars.push_back(var);
488-
}
478+
vars.push_back(
479+
state.rename<goto_symex_statet::L2>(code.arguments()[j], ns));
489480

490481
target.output(state.guard.as_expr(), state.source, event, vars);
491482
}

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<goto_symex_statet::L2>(ssa, ns);
72+
exprt expr_l2 = state.rename<statet::L2>(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<goto_symex_statet::L2>(a, ns);
264+
a = state.rename<statet::L2>(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: 3 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<goto_symex_statet::L2>(new_guard, ns);
40+
exprt new_guard = state.rename<goto_symex_statet::L2>(old_guard, ns);
4241
do_simplify(new_guard);
4342

4443
if(new_guard.is_false())
@@ -269,8 +268,8 @@ 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<goto_symex_statet::L2>(guard_expr, ns);
271+
guard_expr = state.rename<goto_symex_statet::L2>(
272+
boolean_negate(guard_symbol_expr), ns);
274273
}
275274

276275
if(state.has_saved_jump_target)

src/goto-symex/symex_main.cpp

Lines changed: 7 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<goto_symex_statet::L2>(expr, ns);
98+
exprt l2_expr = state.rename<goto_symex_statet::L2>(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,8 @@ void goto_symext::symex_step(
410410
{
411411
exprt tmp = instruction.get_condition();
412412
clean_expr(tmp, state, false);
413-
state.rename<goto_symex_statet::L2>(tmp, ns);
414-
symex_assume(state, tmp);
413+
symex_assume(
414+
state, state.rename<goto_symex_statet::L2>(std::move(tmp), ns));
415415
}
416416

417417
symex_transition(state);

0 commit comments

Comments
 (0)