Skip to content

Commit 63ca05e

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 e7787ad commit 63ca05e

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
@@ -816,9 +816,8 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
816816
// rewrite (T)(bool) to bool?1:0
817817
auto one = from_integer(1, expr_type);
818818
auto zero = from_integer(0, expr_type);
819-
exprt new_expr = if_exprt(expr.op(), std::move(one), std::move(zero));
820-
simplify_if_preorder(to_if_expr(new_expr));
821-
return new_expr;
819+
return changed(simplify_if_preorder(
820+
if_exprt{expr.op(), std::move(one), std::move(zero)}));
822821
}
823822

824823
// circular casts through types shorter than `int`
@@ -1336,33 +1335,33 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
13361335
return unchanged(expr);
13371336
}
13381337

1339-
bool simplify_exprt::simplify_typecast_preorder(typecast_exprt &expr)
1338+
simplify_exprt::resultt<>
1339+
simplify_exprt::simplify_typecast_preorder(const typecast_exprt &expr)
13401340
{
1341-
const typet &expr_type = as_const(expr).type();
1342-
const typet &op_type = as_const(expr).op().type();
1341+
const typet &expr_type = expr.type();
1342+
const typet &op_type = expr.op().type();
13431343

13441344
// (T)(a?b:c) --> a?(T)b:(T)c; don't do this for floating-point type casts as
13451345
// the type cast itself may be costly
13461346
if(
1347-
as_const(expr).op().id() == ID_if && expr_type.id() != ID_floatbv &&
1347+
expr.op().id() == ID_if && expr_type.id() != ID_floatbv &&
13481348
op_type.id() != ID_floatbv)
13491349
{
13501350
if_exprt if_expr = lift_if(expr, 0);
1351-
simplify_if_preorder(if_expr);
1352-
expr.swap(if_expr);
1353-
return false;
1351+
return changed(simplify_if_preorder(if_expr));
13541352
}
13551353
else
13561354
{
13571355
auto r_it = simplify_rec(expr.op()); // recursive call
13581356
if(r_it.has_changed())
13591357
{
1360-
expr.op() = r_it.expr;
1361-
return false;
1358+
auto tmp = expr;
1359+
tmp.op() = r_it.expr;
1360+
return tmp;
13621361
}
1363-
else
1364-
return true;
13651362
}
1363+
1364+
return unchanged(expr);
13661365
}
13671366

13681367
simplify_exprt::resultt<>
@@ -2697,9 +2696,10 @@ simplify_exprt::simplify_overflow_result(const overflow_result_exprt &expr)
26972696
}
26982697
}
26992698

2700-
bool simplify_exprt::simplify_node_preorder(exprt &expr)
2699+
simplify_exprt::resultt<>
2700+
simplify_exprt::simplify_node_preorder(const exprt &expr)
27012701
{
2702-
bool result=true;
2702+
auto result = unchanged(expr);
27032703

27042704
// The ifs below could one day be replaced by a switch()
27052705

@@ -2708,40 +2708,50 @@ bool simplify_exprt::simplify_node_preorder(exprt &expr)
27082708
// the argument of this expression needs special treatment
27092709
}
27102710
else if(expr.id()==ID_if)
2711-
result=simplify_if_preorder(to_if_expr(expr));
2711+
{
2712+
result = simplify_if_preorder(to_if_expr(expr));
2713+
}
27122714
else if(expr.id() == ID_typecast)
2715+
{
27132716
result = simplify_typecast_preorder(to_typecast_expr(expr));
2714-
else
2717+
}
2718+
else if(expr.has_operands())
27152719
{
2716-
if(expr.has_operands())
2720+
optionalt<exprt::operandst> new_operands;
2721+
2722+
for(std::size_t i = 0; i < expr.operands().size(); ++i)
27172723
{
2718-
Forall_operands(it, expr)
2724+
auto r_it = simplify_rec(expr.operands()[i]); // recursive call
2725+
if(r_it.has_changed())
27192726
{
2720-
auto r_it = simplify_rec(*it); // recursive call
2721-
if(r_it.has_changed())
2722-
{
2723-
*it = r_it.expr;
2724-
result=false;
2725-
}
2727+
if(!new_operands.has_value())
2728+
new_operands = expr.operands();
2729+
(*new_operands)[i] = std::move(r_it.expr);
27262730
}
27272731
}
2732+
2733+
if(new_operands.has_value())
2734+
{
2735+
std::swap(result.expr.operands(), *new_operands);
2736+
result.expr_changed = resultt<>::CHANGED;
2737+
}
27282738
}
27292739

2730-
if(as_const(expr).type().id() == ID_array)
2740+
if(as_const(result.expr).type().id() == ID_array)
27312741
{
2732-
const array_typet &array_type = to_array_type(as_const(expr).type());
2742+
const array_typet &array_type = to_array_type(as_const(result.expr).type());
27332743
resultt<> simp_size = simplify_rec(array_type.size());
27342744
if(simp_size.has_changed())
27352745
{
2736-
to_array_type(expr.type()).size() = simp_size.expr;
2737-
result = false;
2746+
to_array_type(result.expr.type()).size() = simp_size.expr;
2747+
result.expr_changed = resultt<>::CHANGED;
27382748
}
27392749
}
27402750

27412751
return result;
27422752
}
27432753

2744-
simplify_exprt::resultt<> simplify_exprt::simplify_node(exprt node)
2754+
simplify_exprt::resultt<> simplify_exprt::simplify_node(const exprt &node)
27452755
{
27462756
if(!node.has_operands())
27472757
return unchanged(node); // no change
@@ -3026,53 +3036,54 @@ simplify_exprt::resultt<> simplify_exprt::simplify_rec(const exprt &expr)
30263036
#endif
30273037

30283038
// We work on a copy to prevent unnecessary destruction of sharing.
3029-
exprt tmp=expr;
3030-
bool no_change = simplify_node_preorder(tmp);
3039+
auto simplify_node_preorder_result = simplify_node_preorder(expr);
30313040

3032-
auto simplify_node_result = simplify_node(tmp);
3041+
auto simplify_node_result = simplify_node(simplify_node_preorder_result.expr);
30333042

3034-
if(simplify_node_result.has_changed())
3043+
if(
3044+
!simplify_node_result.has_changed() &&
3045+
simplify_node_preorder_result.has_changed())
30353046
{
3036-
no_change = false;
3037-
tmp = simplify_node_result.expr;
3047+
simplify_node_result.expr_changed =
3048+
simplify_node_preorder_result.expr_changed;
30383049
}
30393050

30403051
#ifdef USE_LOCAL_REPLACE_MAP
3041-
#if 1
3042-
replace_mapt::const_iterator it=local_replace_map.find(tmp);
3052+
exprt tmp = simplify_node_result.expr;
3053+
# if 1
3054+
replace_mapt::const_iterator it =
3055+
local_replace_map.find(simplify_node_result.expr);
30433056
if(it!=local_replace_map.end())
3057+
simplify_node_result = changed(it->second);
3058+
# else
3059+
if(
3060+
!local_replace_map.empty() &&
3061+
!replace_expr(local_replace_map, simplify_node_result.expr))
30443062
{
3045-
tmp=it->second;
3046-
no_change = false;
3047-
}
3048-
#else
3049-
if(!local_replace_map.empty() &&
3050-
!replace_expr(local_replace_map, tmp))
3051-
{
3052-
simplify_rec(tmp);
3053-
no_change = false;
3063+
simplify_node_result = changed(simplify_rec(simplify_node_result.expr));
30543064
}
3055-
#endif
3065+
# endif
30563066
#endif
30573067

3058-
if(no_change) // no change
3068+
if(!simplify_node_result.has_changed())
30593069
{
30603070
return unchanged(expr);
30613071
}
3062-
else // change, new expression is 'tmp'
3072+
else
30633073
{
30643074
POSTCONDITION_WITH_DIAGNOSTICS(
3065-
(as_const(tmp).type().id() == ID_array && expr.type().id() == ID_array) ||
3066-
as_const(tmp).type() == expr.type(),
3067-
tmp.pretty(),
3075+
(as_const(simplify_node_result.expr).type().id() == ID_array &&
3076+
expr.type().id() == ID_array) ||
3077+
as_const(simplify_node_result.expr).type() == expr.type(),
3078+
simplify_node_result.expr.pretty(),
30683079
expr.pretty());
30693080

30703081
#ifdef USE_CACHE
30713082
// save in cache
3072-
cache_result.first->second = tmp;
3083+
cache_result.first->second = simplify_node_result.expr;
30733084
#endif
30743085

3075-
return std::move(tmp);
3086+
return simplify_node_result;
30763087
}
30773088
}
30783089

src/util/simplify_expr_class.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class simplify_exprt
147147
// These below all return 'true' if the simplification wasn't applicable.
148148
// If false is returned, the expression has changed.
149149
NODISCARD resultt<> simplify_typecast(const typecast_exprt &);
150-
bool simplify_typecast_preorder(typecast_exprt &);
150+
NODISCARD resultt<> simplify_typecast_preorder(const typecast_exprt &);
151151
NODISCARD resultt<> simplify_extractbit(const extractbit_exprt &);
152152
NODISCARD resultt<> simplify_extractbits(const extractbits_exprt &);
153153
NODISCARD resultt<> simplify_concatenation(const concatenation_exprt &);
@@ -161,7 +161,7 @@ class simplify_exprt
161161
NODISCARD resultt<> simplify_shifts(const shift_exprt &);
162162
NODISCARD resultt<> simplify_power(const binary_exprt &);
163163
NODISCARD resultt<> simplify_bitwise(const multi_ary_exprt &);
164-
bool simplify_if_preorder(if_exprt &expr);
164+
NODISCARD resultt<> simplify_if_preorder(const if_exprt &expr);
165165
NODISCARD resultt<> simplify_if(const if_exprt &);
166166
NODISCARD resultt<> simplify_bitnot(const bitnot_exprt &);
167167
NODISCARD resultt<> simplify_not(const not_exprt &);
@@ -249,8 +249,8 @@ class simplify_exprt
249249
simplify_inequality_pointer_object(const binary_relation_exprt &);
250250

251251
// main recursion
252-
NODISCARD resultt<> simplify_node(exprt);
253-
bool simplify_node_preorder(exprt &expr);
252+
NODISCARD resultt<> simplify_node(const exprt &);
253+
NODISCARD resultt<> simplify_node_preorder(const exprt &);
254254
NODISCARD resultt<> simplify_rec(const exprt &);
255255

256256
virtual bool simplify(exprt &expr);

0 commit comments

Comments
 (0)