Skip to content

Commit 1c38550

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 d7099ae commit 1c38550

File tree

3 files changed

+133
-116
lines changed

3 files changed

+133
-116
lines changed

src/util/simplify_expr.cpp

+64-53
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<>
@@ -2629,40 +2628,50 @@ simplify_exprt::simplify_overflow_result(const overflow_result_exprt &expr)
26292628
}
26302629
}
26312630

2632-
bool simplify_exprt::simplify_node_preorder(exprt &expr)
2631+
simplify_exprt::resultt<>
2632+
simplify_exprt::simplify_node_preorder(const exprt &expr)
26332633
{
2634-
bool result=true;
2635-
26362634
// The ifs below could one day be replaced by a switch()
26372635

26382636
if(expr.id()==ID_address_of)
26392637
{
26402638
// the argument of this expression needs special treatment
26412639
}
26422640
else if(expr.id()==ID_if)
2643-
result=simplify_if_preorder(to_if_expr(expr));
2641+
{
2642+
return simplify_if_preorder(to_if_expr(expr));
2643+
}
26442644
else if(expr.id() == ID_typecast)
2645-
result = simplify_typecast_preorder(to_typecast_expr(expr));
2646-
else
26472645
{
2648-
if(expr.has_operands())
2646+
return simplify_typecast_preorder(to_typecast_expr(expr));
2647+
}
2648+
else if(expr.has_operands())
2649+
{
2650+
optionalt<exprt::operandst> new_operands;
2651+
2652+
for(std::size_t i = 0; i < expr.operands().size(); ++i)
26492653
{
2650-
Forall_operands(it, expr)
2654+
auto r_it = simplify_rec(expr.operands()[i]); // recursive call
2655+
if(r_it.has_changed())
26512656
{
2652-
auto r_it = simplify_rec(*it); // recursive call
2653-
if(r_it.has_changed())
2654-
{
2655-
*it = r_it.expr;
2656-
result=false;
2657-
}
2657+
if(!new_operands.has_value())
2658+
new_operands = expr.operands();
2659+
(*new_operands)[i] = std::move(r_it.expr);
26582660
}
26592661
}
2662+
2663+
if(new_operands.has_value())
2664+
{
2665+
exprt result = expr;
2666+
std::swap(result.operands(), *new_operands);
2667+
return result;
2668+
}
26602669
}
26612670

2662-
return result;
2671+
return unchanged(expr);
26632672
}
26642673

2665-
simplify_exprt::resultt<> simplify_exprt::simplify_node(exprt node)
2674+
simplify_exprt::resultt<> simplify_exprt::simplify_node(const exprt &node)
26662675
{
26672676
if(!node.has_operands())
26682677
return unchanged(node); // no change
@@ -2947,50 +2956,52 @@ simplify_exprt::resultt<> simplify_exprt::simplify_rec(const exprt &expr)
29472956
#endif
29482957

29492958
// We work on a copy to prevent unnecessary destruction of sharing.
2950-
exprt tmp=expr;
2951-
bool no_change = simplify_node_preorder(tmp);
2959+
auto simplify_node_preorder_result = simplify_node_preorder(expr);
29522960

2953-
auto simplify_node_result = simplify_node(tmp);
2961+
auto simplify_node_result = simplify_node(simplify_node_preorder_result.expr);
29542962

2955-
if(simplify_node_result.has_changed())
2963+
if(
2964+
!simplify_node_result.has_changed() &&
2965+
simplify_node_preorder_result.has_changed())
29562966
{
2957-
no_change = false;
2958-
tmp = simplify_node_result.expr;
2967+
simplify_node_result.expr_changed =
2968+
simplify_node_preorder_result.expr_changed;
29592969
}
29602970

29612971
#ifdef USE_LOCAL_REPLACE_MAP
2962-
#if 1
2963-
replace_mapt::const_iterator it=local_replace_map.find(tmp);
2972+
exprt tmp = simplify_node_result.expr;
2973+
# if 1
2974+
replace_mapt::const_iterator it =
2975+
local_replace_map.find(simplify_node_result.expr);
29642976
if(it!=local_replace_map.end())
2977+
simplify_node_result = changed(it->second);
2978+
# else
2979+
if(
2980+
!local_replace_map.empty() &&
2981+
!replace_expr(local_replace_map, simplify_node_result.expr))
29652982
{
2966-
tmp=it->second;
2967-
no_change = false;
2968-
}
2969-
#else
2970-
if(!local_replace_map.empty() &&
2971-
!replace_expr(local_replace_map, tmp))
2972-
{
2973-
simplify_rec(tmp);
2974-
no_change = false;
2983+
simplify_node_result = changed(simplify_rec(simplify_node_result.expr));
29752984
}
2976-
#endif
2985+
# endif
29772986
#endif
29782987

2979-
if(no_change) // no change
2988+
if(!simplify_node_result.has_changed())
29802989
{
29812990
return unchanged(expr);
29822991
}
2983-
else // change, new expression is 'tmp'
2992+
else
29842993
{
29852994
POSTCONDITION_WITH_DIAGNOSTICS(
2986-
as_const(tmp).type() == expr.type(), tmp.pretty(), expr.pretty());
2995+
as_const(simplify_node_result.expr).type() == expr.type(),
2996+
simplify_node_result.expr.pretty(),
2997+
expr.pretty());
29872998

29882999
#ifdef USE_CACHE
29893000
// save in cache
2990-
cache_result.first->second = tmp;
3001+
cache_result.first->second = simplify_node_result.expr;
29913002
#endif
29923003

2993-
return std::move(tmp);
3004+
return simplify_node_result;
29943005
}
29953006
}
29963007

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)