Skip to content

Commit e7039b0

Browse files
committed
goto-convert: introduce assignment_lhs_needs_temporary
This introduces goto_convertt::assignment_lhs_needs_temporary, to be used instead of needs_cleaning when determining whether to introduce a temporary for the value of an assignment expression, compound assignment expression, or pre- or post-increment or decrement operator. This allows weakening the condition computed by needs_cleaning.
1 parent 72303a7 commit e7039b0

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/goto-programs/goto_clean_expr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ void goto_convertt::clean_expr(
384384
clean_expr(side_effect_assign.lhs(), dest, mode);
385385
exprt lhs = side_effect_assign.lhs();
386386

387-
const bool must_use_rhs = needs_cleaning(lhs);
387+
const bool must_use_rhs = assignment_lhs_needs_temporary(lhs);
388388
if(must_use_rhs)
389389
{
390390
remove_function_call(

src/goto-programs/goto_convert_class.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ class goto_convertt:public messaget
8989

9090
static bool needs_cleaning(const exprt &expr);
9191

92+
// Do we need to introduce a temporary for the value of an assignment
93+
// to the given lhs? E.g., a[i] needs a temporary as its value may change
94+
// when i is changed; likewise, *p needs a temporary as its value may change
95+
// when p is changed.
96+
static bool assignment_lhs_needs_temporary(const exprt &lhs)
97+
{
98+
return lhs.id() != ID_symbol;
99+
}
100+
92101
void make_temp_symbol(
93102
exprt &expr,
94103
const std::string &suffix,

src/goto-programs/goto_convert_side_effect.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ void goto_convertt::remove_assignment(
5050
{
5151
auto &old_assignment = to_side_effect_expr_assign(expr);
5252

53-
if(result_is_used && !address_taken && needs_cleaning(old_assignment.lhs()))
53+
if(
54+
result_is_used && !address_taken &&
55+
assignment_lhs_needs_temporary(old_assignment.lhs()))
5456
{
5557
if(!old_assignment.rhs().is_constant())
5658
make_temp_symbol(old_assignment.rhs(), "assign", dest, mode);
@@ -122,7 +124,9 @@ void goto_convertt::remove_assignment(
122124
exprt rhs = binary_exprt{binary_expr.op0(), new_id, binary_expr.op1()};
123125
rhs.add_source_location() = expr.source_location();
124126

125-
if(result_is_used && !address_taken && needs_cleaning(binary_expr.op0()))
127+
if(
128+
result_is_used && !address_taken &&
129+
assignment_lhs_needs_temporary(binary_expr.op0()))
126130
{
127131
make_temp_symbol(rhs, "assign", dest, mode);
128132
replacement_expr_opt = rhs;
@@ -237,7 +241,7 @@ void goto_convertt::remove_pre(
237241
}
238242

239243
const bool cannot_use_lhs =
240-
result_is_used && !address_taken && needs_cleaning(lhs);
244+
result_is_used && !address_taken && assignment_lhs_needs_temporary(lhs);
241245
if(cannot_use_lhs)
242246
make_temp_symbol(rhs, "pre", dest, mode);
243247

0 commit comments

Comments
 (0)