Skip to content

Commit ad53d2f

Browse files
committed
Use simplify_exprtt::resultt in pre-order simplification steps
The use of resultt increases type safety as the expression to be simplified is no longer modified in place. All post-order simplification steps already use resultt, but pre-order steps had been left to be done.
1 parent d832f93 commit ad53d2f

File tree

3 files changed

+136
-119
lines changed

3 files changed

+136
-119
lines changed

src/util/simplify_expr.cpp

+67-56
Original file line numberDiff line numberDiff line change
@@ -820,9 +820,8 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
820820
// rewrite (T)(bool) to bool?1:0
821821
auto one = from_integer(1, expr_type);
822822
auto zero = from_integer(0, expr_type);
823-
exprt new_expr = if_exprt(expr.op(), std::move(one), std::move(zero));
824-
simplify_if_preorder(to_if_expr(new_expr));
825-
return new_expr;
823+
return changed(simplify_if_preorder(
824+
if_exprt{expr.op(), std::move(one), std::move(zero)}));
826825
}
827826

828827
// circular casts through types shorter than `int`
@@ -1340,33 +1339,33 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
13401339
return unchanged(expr);
13411340
}
13421341

1343-
bool simplify_exprt::simplify_typecast_preorder(typecast_exprt &expr)
1342+
simplify_exprt::resultt<>
1343+
simplify_exprt::simplify_typecast_preorder(const typecast_exprt &expr)
13441344
{
1345-
const typet &expr_type = as_const(expr).type();
1346-
const typet &op_type = as_const(expr).op().type();
1345+
const typet &expr_type = expr.type();
1346+
const typet &op_type = expr.op().type();
13471347

13481348
// (T)(a?b:c) --> a?(T)b:(T)c; don't do this for floating-point type casts as
13491349
// the type cast itself may be costly
13501350
if(
1351-
as_const(expr).op().id() == ID_if && expr_type.id() != ID_floatbv &&
1351+
expr.op().id() == ID_if && expr_type.id() != ID_floatbv &&
13521352
op_type.id() != ID_floatbv)
13531353
{
13541354
if_exprt if_expr = lift_if(expr, 0);
1355-
simplify_if_preorder(if_expr);
1356-
expr.swap(if_expr);
1357-
return false;
1355+
return changed(simplify_if_preorder(if_expr));
13581356
}
13591357
else
13601358
{
13611359
auto r_it = simplify_rec(expr.op()); // recursive call
13621360
if(r_it.has_changed())
13631361
{
1364-
expr.op() = r_it.expr;
1365-
return false;
1362+
auto tmp = expr;
1363+
tmp.op() = r_it.expr;
1364+
return tmp;
13661365
}
1367-
else
1368-
return true;
13691366
}
1367+
1368+
return unchanged(expr);
13701369
}
13711370

13721371
simplify_exprt::resultt<>
@@ -2706,9 +2705,10 @@ simplify_exprt::simplify_overflow_result(const overflow_result_exprt &expr)
27062705
}
27072706
}
27082707

2709-
bool simplify_exprt::simplify_node_preorder(exprt &expr)
2708+
simplify_exprt::resultt<>
2709+
simplify_exprt::simplify_node_preorder(const exprt &expr)
27102710
{
2711-
bool result=true;
2711+
auto result = unchanged(expr);
27122712

27132713
// The ifs below could one day be replaced by a switch()
27142714

@@ -2717,40 +2717,50 @@ bool simplify_exprt::simplify_node_preorder(exprt &expr)
27172717
// the argument of this expression needs special treatment
27182718
}
27192719
else if(expr.id()==ID_if)
2720-
result=simplify_if_preorder(to_if_expr(expr));
2720+
{
2721+
result = simplify_if_preorder(to_if_expr(expr));
2722+
}
27212723
else if(expr.id() == ID_typecast)
2724+
{
27222725
result = simplify_typecast_preorder(to_typecast_expr(expr));
2723-
else
2726+
}
2727+
else if(expr.has_operands())
27242728
{
2725-
if(expr.has_operands())
2729+
optionalt<exprt::operandst> new_operands;
2730+
2731+
for(std::size_t i = 0; i < expr.operands().size(); ++i)
27262732
{
2727-
Forall_operands(it, expr)
2733+
auto r_it = simplify_rec(expr.operands()[i]); // recursive call
2734+
if(r_it.has_changed())
27282735
{
2729-
auto r_it = simplify_rec(*it); // recursive call
2730-
if(r_it.has_changed())
2731-
{
2732-
*it = r_it.expr;
2733-
result=false;
2734-
}
2736+
if(!new_operands.has_value())
2737+
new_operands = expr.operands();
2738+
(*new_operands)[i] = std::move(r_it.expr);
27352739
}
27362740
}
2741+
2742+
if(new_operands.has_value())
2743+
{
2744+
std::swap(result.expr.operands(), *new_operands);
2745+
result.expr_changed = resultt<>::CHANGED;
2746+
}
27372747
}
27382748

2739-
if(as_const(expr).type().id() == ID_array)
2749+
if(as_const(result.expr).type().id() == ID_array)
27402750
{
2741-
const array_typet &array_type = to_array_type(as_const(expr).type());
2751+
const array_typet &array_type = to_array_type(as_const(result.expr).type());
27422752
resultt<> simp_size = simplify_rec(array_type.size());
27432753
if(simp_size.has_changed())
27442754
{
2745-
to_array_type(expr.type()).size() = simp_size.expr;
2746-
result = false;
2755+
to_array_type(result.expr.type()).size() = simp_size.expr;
2756+
result.expr_changed = resultt<>::CHANGED;
27472757
}
27482758
}
27492759

27502760
return result;
27512761
}
27522762

2753-
simplify_exprt::resultt<> simplify_exprt::simplify_node(exprt node)
2763+
simplify_exprt::resultt<> simplify_exprt::simplify_node(const exprt &node)
27542764
{
27552765
if(!node.has_operands())
27562766
return unchanged(node); // no change
@@ -3047,53 +3057,54 @@ simplify_exprt::resultt<> simplify_exprt::simplify_rec(const exprt &expr)
30473057
#endif
30483058

30493059
// We work on a copy to prevent unnecessary destruction of sharing.
3050-
exprt tmp=expr;
3051-
bool no_change = simplify_node_preorder(tmp);
3060+
auto simplify_node_preorder_result = simplify_node_preorder(expr);
30523061

3053-
auto simplify_node_result = simplify_node(tmp);
3062+
auto simplify_node_result = simplify_node(simplify_node_preorder_result.expr);
30543063

3055-
if(simplify_node_result.has_changed())
3064+
if(
3065+
!simplify_node_result.has_changed() &&
3066+
simplify_node_preorder_result.has_changed())
30563067
{
3057-
no_change = false;
3058-
tmp = simplify_node_result.expr;
3068+
simplify_node_result.expr_changed =
3069+
simplify_node_preorder_result.expr_changed;
30593070
}
30603071

30613072
#ifdef USE_LOCAL_REPLACE_MAP
3062-
#if 1
3063-
replace_mapt::const_iterator it=local_replace_map.find(tmp);
3073+
exprt tmp = simplify_node_result.expr;
3074+
# if 1
3075+
replace_mapt::const_iterator it =
3076+
local_replace_map.find(simplify_node_result.expr);
30643077
if(it!=local_replace_map.end())
3078+
simplify_node_result = changed(it->second);
3079+
# else
3080+
if(
3081+
!local_replace_map.empty() &&
3082+
!replace_expr(local_replace_map, simplify_node_result.expr))
30653083
{
3066-
tmp=it->second;
3067-
no_change = false;
3068-
}
3069-
#else
3070-
if(!local_replace_map.empty() &&
3071-
!replace_expr(local_replace_map, tmp))
3072-
{
3073-
simplify_rec(tmp);
3074-
no_change = false;
3084+
simplify_node_result = changed(simplify_rec(simplify_node_result.expr));
30753085
}
3076-
#endif
3086+
# endif
30773087
#endif
30783088

3079-
if(no_change) // no change
3089+
if(!simplify_node_result.has_changed())
30803090
{
30813091
return unchanged(expr);
30823092
}
3083-
else // change, new expression is 'tmp'
3093+
else
30843094
{
30853095
POSTCONDITION_WITH_DIAGNOSTICS(
3086-
(as_const(tmp).type().id() == ID_array && expr.type().id() == ID_array) ||
3087-
as_const(tmp).type() == expr.type(),
3088-
tmp.pretty(),
3096+
(as_const(simplify_node_result.expr).type().id() == ID_array &&
3097+
expr.type().id() == ID_array) ||
3098+
as_const(simplify_node_result.expr).type() == expr.type(),
3099+
simplify_node_result.expr.pretty(),
30893100
expr.pretty());
30903101

30913102
#ifdef USE_CACHE
30923103
// save in cache
3093-
cache_result.first->second = tmp;
3104+
cache_result.first->second = simplify_node_result.expr;
30943105
#endif
30953106

3096-
return std::move(tmp);
3107+
return simplify_node_result;
30973108
}
30983109
}
30993110

src/util/simplify_expr_class.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class simplify_exprt
149149
// These below all return 'true' if the simplification wasn't applicable.
150150
// If false is returned, the expression has changed.
151151
NODISCARD resultt<> simplify_typecast(const typecast_exprt &);
152-
bool simplify_typecast_preorder(typecast_exprt &);
152+
NODISCARD resultt<> simplify_typecast_preorder(const typecast_exprt &);
153153
NODISCARD resultt<> simplify_extractbit(const extractbit_exprt &);
154154
NODISCARD resultt<> simplify_extractbits(const extractbits_exprt &);
155155
NODISCARD resultt<> simplify_concatenation(const concatenation_exprt &);
@@ -163,7 +163,7 @@ class simplify_exprt
163163
NODISCARD resultt<> simplify_shifts(const shift_exprt &);
164164
NODISCARD resultt<> simplify_power(const binary_exprt &);
165165
NODISCARD resultt<> simplify_bitwise(const multi_ary_exprt &);
166-
bool simplify_if_preorder(if_exprt &expr);
166+
NODISCARD resultt<> simplify_if_preorder(const if_exprt &expr);
167167
NODISCARD resultt<> simplify_if(const if_exprt &);
168168
NODISCARD resultt<> simplify_bitnot(const bitnot_exprt &);
169169
NODISCARD resultt<> simplify_not(const not_exprt &);
@@ -259,8 +259,8 @@ class simplify_exprt
259259
simplify_inequality_pointer_object(const binary_relation_exprt &);
260260

261261
// main recursion
262-
NODISCARD resultt<> simplify_node(exprt);
263-
bool simplify_node_preorder(exprt &expr);
262+
NODISCARD resultt<> simplify_node(const exprt &);
263+
NODISCARD resultt<> simplify_node_preorder(const exprt &);
264264
NODISCARD resultt<> simplify_rec(const exprt &);
265265

266266
virtual bool simplify(exprt &expr);

0 commit comments

Comments
 (0)