Skip to content

Commit 86bf547

Browse files
committed
replace_expr: avoid breaking irep sharing
Previously this broke all sharing; now it only breaks those sub-ireps that actually match an expression to replace.
1 parent a15b75f commit 86bf547

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/util/replace_expr.cpp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,46 @@ Author: Daniel Kroening, [email protected]
66
77
\*******************************************************************/
88

9-
109
#include "replace_expr.h"
10+
#include "expr_iterator.h"
1111

1212
bool replace_expr(const exprt &what, const exprt &by, exprt &dest)
1313
{
14-
if(dest==what)
14+
bool no_change = true;
15+
16+
for(auto it = dest.depth_begin(), itend = dest.depth_end();
17+
it != itend;) // no ++it
1518
{
16-
dest=by;
17-
return false;
19+
if(*it == what)
20+
{
21+
it.mutate() = by;
22+
no_change = false;
23+
it.next_sibling_or_parent();
24+
}
25+
else
26+
++it;
1827
}
1928

20-
bool result=true;
21-
22-
Forall_operands(it, dest)
23-
result=replace_expr(what, by, *it) && result;
24-
25-
return result;
29+
return no_change;
2630
}
2731

2832
bool replace_expr(const replace_mapt &what, exprt &dest)
2933
{
30-
{
31-
replace_mapt::const_iterator it=what.find(dest);
34+
bool no_change = true;
3235

33-
if(it!=what.end())
36+
for(auto it = dest.depth_begin(), itend = dest.depth_end();
37+
it != itend;) // No ++it
38+
{
39+
replace_mapt::const_iterator findit = what.find(*it);
40+
if(findit != what.end())
3441
{
35-
dest=it->second;
36-
return false;
42+
it.mutate() = findit->second;
43+
no_change = false;
44+
it.next_sibling_or_parent();
3745
}
46+
else
47+
++it;
3848
}
3949

40-
bool result=true;
41-
42-
Forall_operands(it, dest)
43-
result=replace_expr(what, *it) && result;
44-
45-
return result;
50+
return no_change;
4651
}

0 commit comments

Comments
 (0)