Skip to content

Commit 3e07b01

Browse files
committed
simplifier: use resultt<> instead of bool
The consistent use of resultt<> makes the code easier to read.
1 parent c6f83a2 commit 3e07b01

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/util/simplify_expr.cpp

+27-13
Original file line numberDiff line numberDiff line change
@@ -2295,20 +2295,27 @@ simplify_exprt::simplify_overflow_unary(const unary_overflow_exprt &expr)
22952295
return false_exprt{};
22962296
}
22972297

2298-
bool simplify_exprt::simplify_node_preorder(exprt &expr)
2298+
simplify_exprt::resultt<> simplify_exprt::simplify_node_preorder(exprt expr)
22992299
{
2300-
bool result=true;
2301-
23022300
// The ifs below could one day be replaced by a switch()
23032301

23042302
if(expr.id()==ID_address_of)
23052303
{
23062304
// the argument of this expression needs special treatment
2305+
return unchanged(expr);
23072306
}
23082307
else if(expr.id()==ID_if)
2309-
result=simplify_if_preorder(to_if_expr(expr));
2308+
{
2309+
bool no_change = simplify_if_preorder(to_if_expr(expr));
2310+
if(no_change)
2311+
return unchanged(expr);
2312+
else
2313+
return expr;
2314+
}
23102315
else
23112316
{
2317+
bool has_changed = false;
2318+
23122319
if(expr.has_operands())
23132320
{
23142321
Forall_operands(it, expr)
@@ -2317,13 +2324,18 @@ bool simplify_exprt::simplify_node_preorder(exprt &expr)
23172324
if(r_it.has_changed())
23182325
{
23192326
*it = r_it.expr;
2320-
result=false;
2327+
has_changed = true;
23212328
}
23222329
}
23232330
}
2331+
2332+
if(has_changed)
2333+
return expr;
2334+
else
2335+
return unchanged(expr);
23242336
}
23252337

2326-
return result;
2338+
return unchanged(expr);
23272339
}
23282340

23292341
simplify_exprt::resultt<> simplify_exprt::simplify_node(exprt node)
@@ -2604,17 +2616,19 @@ simplify_exprt::resultt<> simplify_exprt::simplify_rec(const exprt &expr)
26042616
}
26052617
#endif
26062618

2607-
// We work on a copy to prevent unnecessary destruction of sharing.
2608-
exprt tmp=expr;
2609-
bool no_change = simplify_node_preorder(tmp);
2619+
bool no_change = true;
26102620

2611-
auto simplify_node_result = simplify_node(tmp);
2621+
auto preorder_result = simplify_node_preorder(expr);
2622+
2623+
if(preorder_result.has_changed())
2624+
no_change = false;
2625+
2626+
auto simplify_node_result = simplify_node(preorder_result.expr);
26122627

26132628
if(simplify_node_result.has_changed())
2614-
{
26152629
no_change = false;
2616-
tmp = simplify_node_result.expr;
2617-
}
2630+
2631+
exprt tmp = simplify_node_result.expr;
26182632

26192633
#ifdef USE_LOCAL_REPLACE_MAP
26202634
#if 1

src/util/simplify_expr_class.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ class simplify_exprt
239239

240240
// main recursion
241241
NODISCARD resultt<> simplify_node(exprt);
242-
bool simplify_node_preorder(exprt &expr);
242+
NODISCARD resultt<> simplify_node_preorder(exprt);
243243
NODISCARD resultt<> simplify_rec(const exprt &);
244244

245245
virtual bool simplify(exprt &expr);

0 commit comments

Comments
 (0)