Skip to content

Commit c2143d0

Browse files
committed
Clean up replace_symbolt interface
Add set(), with meaning similar to insert() but overwriting any existing value, and an overload of erase() that takes an iterator. These allow the constant propagator to use it more cleanly (though not entirely; it still needs get_expr_map() for some tasks such as merging two replace_symbolt objects). No behavioural change is intended here.
1 parent b9b40ed commit c2143d0

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed

src/analyses/constant_propagator.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ void constant_propagator_domaint::transform(
214214
const code_typet &type=to_code_type(symbol.type);
215215

216216
for(const auto &param : type.parameters())
217-
values.set_to_top(param.get_identifier());
217+
values.set_to_top(symbol_exprt(param.get_identifier(), param.type()));
218218
}
219219

220220
INVARIANT(from->is_goto() || !values.is_bottom,
@@ -334,9 +334,10 @@ bool constant_propagator_domaint::valuest::is_constant_address_of(
334334
}
335335

336336
/// Do not call this when iterating over replace_const.expr_map!
337-
bool constant_propagator_domaint::valuest::set_to_top(const irep_idt &id)
337+
bool constant_propagator_domaint::valuest::set_to_top(
338+
const symbol_exprt &symbol_expr)
338339
{
339-
const auto n_erased = replace_const.erase(id);
340+
const auto n_erased = replace_const.erase(symbol_expr.get_identifier());
340341

341342
INVARIANT(n_erased==0 || !is_bottom, "bottom should have no elements at all");
342343

@@ -360,7 +361,9 @@ void constant_propagator_domaint::valuest::set_dirty_to_top(
360361

361362
if((!symbol.is_procedure_local() || dirty(id)) &&
362363
!symbol.type.get_bool(ID_C_constant))
363-
it=expr_map.erase(it);
364+
{
365+
it = replace_const.erase(it);
366+
}
364367
else
365368
it++;
366369
}
@@ -456,15 +459,15 @@ bool constant_propagator_domaint::valuest::merge(const valuest &src)
456459

457460
if(expr!=src_expr)
458461
{
459-
it=expr_map.erase(it);
462+
it = replace_const.erase(it);
460463
changed=true;
461464
}
462465
else
463466
it++;
464467
}
465468
else
466469
{
467-
it=expr_map.erase(it);
470+
it = replace_const.erase(it);
468471
changed=true;
469472
}
470473
}
@@ -499,10 +502,11 @@ bool constant_propagator_domaint::valuest::meet(
499502
}
500503
else
501504
{
505+
const typet &m_id_type = ns.lookup(m.first).type;
502506
DATA_INVARIANT(
503-
base_type_eq(ns.lookup(m.first).type, m.second.type(), ns),
507+
base_type_eq(m_id_type, m.second.type(), ns),
504508
"type of constant to be stored should match");
505-
set_to(m.first, m.second);
509+
set_to(symbol_exprt(m.first, m_id_type), m.second);
506510
changed=true;
507511
}
508512
}
@@ -558,7 +562,7 @@ bool constant_propagator_domaint::partial_evaluate_with_all_rounding_modes(
558562
{
559563
constant_propagator_domaint child(*this);
560564
child.values.set_to(
561-
ID_cprover_rounding_mode_str,
565+
symbol_exprt(ID_cprover_rounding_mode_str, integer_typet()),
562566
from_integer(rounding_modes[i], integer_typet()));
563567
exprt result = expr;
564568
if(child.replace_constants_and_simplify(result, ns))

src/analyses/constant_propagator.h

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,13 @@ class constant_propagator_domaint:public ai_domain_baset
111111
return !is_bottom && replace_const.empty();
112112
}
113113

114-
// set single identifier
115-
116-
void set_to(const irep_idt &lhs, const exprt &rhs)
117-
{
118-
replace_const.get_expr_map()[lhs] = rhs;
119-
is_bottom=false;
120-
}
121-
122114
void set_to(const symbol_exprt &lhs, const exprt &rhs)
123115
{
124-
set_to(lhs.get_identifier(), rhs);
125-
}
126-
127-
bool set_to_top(const symbol_exprt &expr)
128-
{
129-
return set_to_top(expr.get_identifier());
116+
replace_const.set(lhs, rhs);
117+
is_bottom=false;
130118
}
131119

132-
bool set_to_top(const irep_idt &id);
120+
bool set_to_top(const symbol_exprt &expr);
133121

134122
void set_dirty_to_top(const dirtyt &dirty, const namespacet &ns);
135123

src/util/replace_symbol.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ void replace_symbolt::insert(
3030
old_expr.get_identifier(), new_expr));
3131
}
3232

33+
void replace_symbolt::set(const symbol_exprt &old_expr, const exprt &new_expr)
34+
{
35+
PRECONDITION(old_expr.type() == new_expr.type());
36+
expr_map[old_expr.get_identifier()] = new_expr;
37+
}
38+
39+
3340
bool replace_symbolt::replace_symbol_expr(symbol_exprt &s) const
3441
{
3542
expr_mapt::const_iterator it = expr_map.find(s.get_identifier());

src/util/replace_symbol.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ class replace_symbolt
2626
public:
2727
typedef std::unordered_map<irep_idt, exprt> expr_mapt;
2828

29+
/// Sets old_expr to be replaced by new_expr if we don't already have a
30+
/// replacement; otherwise does nothing (i.e. std::map::insert-like
31+
/// behaviour).
2932
void insert(const class symbol_exprt &old_expr,
3033
const exprt &new_expr);
3134

35+
/// Sets old_expr to be replaced by new_expr
36+
void set(const class symbol_exprt &old_expr, const exprt &new_expr);
37+
3238
virtual bool replace(exprt &dest) const;
3339
virtual bool replace(typet &dest) const;
3440

@@ -57,6 +63,11 @@ class replace_symbolt
5763
return expr_map.erase(id);
5864
}
5965

66+
expr_mapt::iterator erase(expr_mapt::iterator it)
67+
{
68+
return expr_map.erase(it);
69+
}
70+
6071
bool replaces_symbol(const irep_idt &id) const
6172
{
6273
return expr_map.find(id) != expr_map.end();

0 commit comments

Comments
 (0)