Skip to content

Commit 69f6e7b

Browse files
authored
Merge pull request diffblue#2032 from tautschnig/replace-rename-performance
Improve performance replace_symbolt/rename_symbolt
2 parents ec43b00 + 20d2445 commit 69f6e7b

File tree

4 files changed

+56
-88
lines changed

4 files changed

+56
-88
lines changed

src/util/rename.cpp

+1-33
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ Author: Daniel Kroening, [email protected]
88

99
#include "rename.h"
1010

11-
#include <algorithm>
11+
#include <string>
1212

1313
#include "symbol.h"
14-
#include "expr.h"
1514
#include "namespace.h"
1615

1716
/// automated variable renaming
@@ -35,34 +34,3 @@ void get_new_name(irep_idt &new_name, const namespacet &ns)
3534

3635
new_name=prefix+std::to_string(ns.get_max(prefix)+1);
3736
}
38-
39-
/// automated variable renaming
40-
/// \par parameters: expression, old name, new name
41-
/// \return modifies the expression returns false iff something was renamed
42-
bool rename(exprt &expr, const irep_idt &old_name,
43-
const irep_idt &new_name)
44-
{
45-
bool result=true;
46-
47-
if(expr.id()==ID_symbol)
48-
{
49-
if(expr.get(ID_identifier)==old_name)
50-
{
51-
expr.set(ID_identifier, new_name);
52-
result=false;
53-
}
54-
}
55-
else
56-
{
57-
if(expr.id()==ID_address_of)
58-
{
59-
// TODO
60-
}
61-
else
62-
Forall_operands(it, expr)
63-
if(!rename(*it, old_name, new_name))
64-
result=false;
65-
}
66-
67-
return result;
68-
}

src/util/rename.h

-6
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,4 @@ void get_new_name(symbolt &symbol,
2626
void get_new_name(irep_idt &new_name,
2727
const namespacet &ns);
2828

29-
// true: did nothing
30-
// false: renamed something in the expression
31-
32-
bool rename(exprt &expr, const irep_idt &old_name,
33-
const irep_idt &new_name);
34-
3529
#endif // CPROVER_UTIL_RENAME_H

src/util/rename_symbol.cpp

+39-30
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Author: Daniel Kroening, [email protected]
88

99
#include "rename_symbol.h"
1010

11+
#include "expr_iterator.h"
1112
#include "std_types.h"
1213
#include "std_expr.h"
1314

@@ -30,44 +31,52 @@ bool rename_symbolt::rename(exprt &dest) const
3031
{
3132
bool result=true;
3233

33-
// first look at type
34-
35-
if(have_to_rename(dest.type()))
36-
if(!rename(dest.type()))
37-
result=false;
38-
39-
// now do expression itself
40-
41-
if(!have_to_rename(dest))
42-
return result;
43-
44-
if(dest.id()==ID_symbol)
34+
for(auto it = dest.depth_begin(), end = dest.depth_end(); it != end; ++it)
4535
{
46-
expr_mapt::const_iterator it=
47-
expr_map.find(to_symbol_expr(dest).get_identifier());
36+
exprt * modifiable_expr = nullptr;
4837

49-
if(it!=expr_map.end())
38+
// first look at type
39+
if(have_to_rename(it->type()))
5040
{
51-
to_symbol_expr(dest).set_identifier(it->second);
52-
return false;
41+
modifiable_expr = &it.mutate();
42+
result &= rename(modifiable_expr->type());
5343
}
54-
}
5544

56-
Forall_operands(it, dest)
57-
if(!rename(*it))
58-
result=false;
59-
60-
const irept &c_sizeof_type=dest.find(ID_C_c_sizeof_type);
45+
// now do expression itself
46+
if(it->id()==ID_symbol)
47+
{
48+
expr_mapt::const_iterator entry =
49+
expr_map.find(to_symbol_expr(*it).get_identifier());
6150

62-
if(c_sizeof_type.is_not_nil() &&
63-
!rename(static_cast<typet&>(dest.add(ID_C_c_sizeof_type))))
64-
result=false;
51+
if(entry != expr_map.end())
52+
{
53+
if(!modifiable_expr)
54+
modifiable_expr = &it.mutate();
55+
to_symbol_expr(*modifiable_expr).set_identifier(entry->second);
56+
result = false;
57+
}
58+
}
6559

66-
const irept &va_arg_type=dest.find(ID_C_va_arg_type);
60+
const typet &c_sizeof_type =
61+
static_cast<const typet&>(it->find(ID_C_c_sizeof_type));
62+
if(c_sizeof_type.is_not_nil() && have_to_rename(c_sizeof_type))
63+
{
64+
if(!modifiable_expr)
65+
modifiable_expr = &it.mutate();
66+
result &=
67+
rename(static_cast<typet&>(modifiable_expr->add(ID_C_c_sizeof_type)));
68+
}
6769

68-
if(va_arg_type.is_not_nil() &&
69-
!rename(static_cast<typet&>(dest.add(ID_C_va_arg_type))))
70-
result=false;
70+
const typet &va_arg_type =
71+
static_cast<const typet&>(it->find(ID_C_va_arg_type));
72+
if(va_arg_type.is_not_nil() && have_to_rename(va_arg_type))
73+
{
74+
if(!modifiable_expr)
75+
modifiable_expr = &it.mutate();
76+
result &=
77+
rename(static_cast<typet&>(modifiable_expr->add(ID_C_va_arg_type)));
78+
}
79+
}
7180

7281
return result;
7382
}

src/util/replace_symbol.cpp

+16-19
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ bool replace_symbolt::replace(
3535

3636
// first look at type
3737

38-
if(have_to_replace(dest.type()))
38+
const exprt &const_dest(dest);
39+
if(have_to_replace(const_dest.type()))
3940
if(!replace(dest.type()))
4041
result=false;
4142

@@ -94,31 +95,24 @@ bool replace_symbolt::replace(
9495
result=false;
9596
}
9697

97-
const irept &c_sizeof_type=dest.find(ID_C_c_sizeof_type);
98-
99-
if(c_sizeof_type.is_not_nil())
100-
{
101-
typet &type=static_cast<typet &>(dest.add(ID_C_c_sizeof_type));
98+
const typet &c_sizeof_type =
99+
static_cast<const typet&>(dest.find(ID_C_c_sizeof_type));
100+
if(c_sizeof_type.is_not_nil() && have_to_replace(c_sizeof_type))
101+
result &= replace(static_cast<typet&>(dest.add(ID_C_c_sizeof_type)));
102102

103-
if(!replace(type))
104-
result=false;
105-
}
106-
107-
const irept &va_arg_type=dest.find(ID_C_va_arg_type);
108-
109-
if(va_arg_type.is_not_nil())
110-
{
111-
typet &type=static_cast<typet &>(dest.add(ID_C_va_arg_type));
112-
113-
if(!replace(type))
114-
result=false;
115-
}
103+
const typet &va_arg_type =
104+
static_cast<const typet&>(dest.find(ID_C_va_arg_type));
105+
if(va_arg_type.is_not_nil() && have_to_replace(va_arg_type))
106+
result &= replace(static_cast<typet&>(dest.add(ID_C_va_arg_type)));
116107

117108
return result;
118109
}
119110

120111
bool replace_symbolt::have_to_replace(const exprt &dest) const
121112
{
113+
if(expr_map.empty() && type_map.empty())
114+
return false;
115+
122116
// first look at type
123117

124118
if(have_to_replace(dest.type()))
@@ -211,6 +205,9 @@ bool replace_symbolt::replace(typet &dest) const
211205

212206
bool replace_symbolt::have_to_replace(const typet &dest) const
213207
{
208+
if(expr_map.empty() && type_map.empty())
209+
return false;
210+
214211
if(dest.has_subtype())
215212
if(have_to_replace(dest.subtype()))
216213
return true;

0 commit comments

Comments
 (0)