Skip to content

Commit f825da3

Browse files
author
Daniel Kroening
committed
simplifier: use new interface
1 parent 30c134f commit f825da3

6 files changed

+182
-196
lines changed

src/util/simplify_expr.cpp

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ simplify_exprt::resultt<> simplify_exprt::simplify_abs(const abs_exprt &expr)
103103
return unchanged(expr);
104104
}
105105

106-
bool simplify_exprt::simplify_sign(exprt &expr)
106+
simplify_exprt::resultt<> simplify_exprt::simplify_sign(const exprt &expr)
107107
{
108108
if(expr.operands().size()!=1)
109-
return true;
109+
return unchanged(expr);
110110

111111
if(expr.op0().is_constant())
112112
{
@@ -115,22 +115,20 @@ bool simplify_exprt::simplify_sign(exprt &expr)
115115
if(type.id()==ID_floatbv)
116116
{
117117
ieee_floatt value(to_constant_expr(expr.op0()));
118-
expr = make_boolean_expr(value.get_sign());
119-
return false;
118+
return make_boolean_expr(value.get_sign());
120119
}
121120
else if(type.id()==ID_signedbv ||
122121
type.id()==ID_unsignedbv)
123122
{
124123
const auto value = numeric_cast<mp_integer>(expr.op0());
125124
if(value.has_value())
126125
{
127-
expr = make_boolean_expr(*value >= 0);
128-
return false;
126+
return make_boolean_expr(*value >= 0);
129127
}
130128
}
131129
}
132130

133-
return true;
131+
return unchanged(expr);
134132
}
135133

136134
simplify_exprt::resultt<>
@@ -1433,14 +1431,15 @@ simplify_exprt::resultt<> simplify_exprt::simplify_lambda(const exprt &expr)
14331431
return unchanged(expr);
14341432
}
14351433

1436-
bool simplify_exprt::simplify_with(exprt &expr)
1434+
simplify_exprt::resultt<> simplify_exprt::simplify_with(const exprt &expr)
14371435
{
1438-
bool result=true;
1436+
bool no_change = true;
14391437

14401438
if((expr.operands().size()%2)!=1)
1441-
return true;
1439+
return unchanged(expr);
14421440

1443-
auto &with_expr = to_with_expr(expr);
1441+
// copy
1442+
auto with_expr = to_with_expr(expr);
14441443

14451444
const typet old_type_followed = ns.follow(with_expr.old().type());
14461445

@@ -1456,17 +1455,20 @@ bool simplify_exprt::simplify_with(exprt &expr)
14561455
with_expr.where().get(ID_component_name);
14571456

14581457
if(!to_struct_type(old_type_followed).has_component(component_name))
1459-
return result;
1458+
return unchanged(expr);
14601459

14611460
std::size_t number =
14621461
to_struct_type(old_type_followed).component_number(component_name);
14631462

1463+
if(number >= with_expr.old().operands().size())
1464+
return unchanged(expr);
1465+
14641466
with_expr.old().operands()[number].swap(with_expr.new_value());
14651467

14661468
with_expr.operands().erase(++with_expr.operands().begin());
14671469
with_expr.operands().erase(++with_expr.operands().begin());
14681470

1469-
result=false;
1471+
no_change = false;
14701472
}
14711473
}
14721474
}
@@ -1475,10 +1477,10 @@ bool simplify_exprt::simplify_with(exprt &expr)
14751477
with_expr.old().type().id() == ID_vector)
14761478
{
14771479
if(
1478-
expr.op0().id() == ID_array || expr.op0().id() == ID_constant ||
1479-
expr.op0().id() == ID_vector)
1480+
with_expr.old().id() == ID_array || with_expr.old().id() == ID_constant ||
1481+
with_expr.old().id() == ID_vector)
14801482
{
1481-
while(expr.operands().size()>1)
1483+
while(with_expr.operands().size() > 1)
14821484
{
14831485
const auto i = numeric_cast<mp_integer>(with_expr.where());
14841486

@@ -1494,26 +1496,24 @@ bool simplify_exprt::simplify_with(exprt &expr)
14941496
with_expr.operands().erase(++with_expr.operands().begin());
14951497
with_expr.operands().erase(++with_expr.operands().begin());
14961498

1497-
result=false;
1499+
no_change = false;
14981500
}
14991501
}
15001502
}
15011503

1502-
if(expr.operands().size()==1)
1503-
{
1504-
exprt tmp;
1505-
tmp.swap(expr.op0());
1506-
expr.swap(tmp);
1507-
result=false;
1508-
}
1504+
if(with_expr.operands().size() == 1)
1505+
return with_expr.old();
15091506

1510-
return result;
1507+
if(no_change)
1508+
return unchanged(expr);
1509+
else
1510+
return std::move(with_expr);
15111511
}
15121512

1513-
bool simplify_exprt::simplify_update(exprt &expr)
1513+
simplify_exprt::resultt<> simplify_exprt::simplify_update(const exprt &expr)
15141514
{
15151515
if(expr.operands().size()!=3)
1516-
return true;
1516+
return unchanged(expr);
15171517

15181518
// this is to push updates into (possibly nested) constants
15191519

@@ -1532,10 +1532,10 @@ bool simplify_exprt::simplify_update(exprt &expr)
15321532
const auto i = numeric_cast<mp_integer>(e.op0());
15331533

15341534
if(!i.has_value())
1535-
return true;
1535+
return unchanged(expr);
15361536

15371537
if(*i < 0 || *i >= value_ptr->operands().size())
1538-
return true;
1538+
return unchanged(expr);
15391539

15401540
value_ptr = &value_ptr->operands()[numeric_cast_v<std::size_t>(*i)];
15411541
}
@@ -1547,20 +1547,18 @@ bool simplify_exprt::simplify_update(exprt &expr)
15471547
const struct_typet &value_ptr_struct_type =
15481548
to_struct_type(value_ptr_type);
15491549
if(!value_ptr_struct_type.has_component(component_name))
1550-
return true;
1550+
return unchanged(expr);
15511551
auto &designator_as_struct_expr = to_struct_expr(*value_ptr);
15521552
value_ptr = &designator_as_struct_expr.component(component_name, ns);
15531553
CHECK_RETURN(value_ptr->is_not_nil());
15541554
}
15551555
else
1556-
return true; // give up, unknown designator
1556+
return unchanged(expr); // give up, unknown designator
15571557
}
15581558

15591559
// found, done
15601560
*value_ptr=to_update_expr(expr).new_value();
1561-
expr.swap(updated_value);
1562-
1563-
return false;
1561+
return updated_value;
15641562
}
15651563

15661564
simplify_exprt::resultt<> simplify_exprt::simplify_object(const exprt &expr)
@@ -2490,8 +2488,7 @@ bool simplify_exprt::simplify_node(exprt &expr)
24902488
expr.id()==ID_gt || expr.id()==ID_lt ||
24912489
expr.id()==ID_ge || expr.id()==ID_le)
24922490
{
2493-
if(!simplify_inequality(expr))
2494-
r = changed(expr);
2491+
r = simplify_inequality(expr);
24952492
}
24962493
else if(expr.id()==ID_if)
24972494
{
@@ -2503,23 +2500,19 @@ bool simplify_exprt::simplify_node(exprt &expr)
25032500
}
25042501
else if(expr.id()==ID_with)
25052502
{
2506-
if(!simplify_with(expr))
2507-
r = changed(expr);
2503+
r = simplify_with(expr);
25082504
}
25092505
else if(expr.id()==ID_update)
25102506
{
2511-
if(!simplify_update(expr))
2512-
r = changed(expr);
2507+
r = simplify_update(expr);
25132508
}
25142509
else if(expr.id()==ID_index)
25152510
{
2516-
if(!simplify_index(expr))
2517-
r = changed(expr);
2511+
r = simplify_index(expr);
25182512
}
25192513
else if(expr.id()==ID_member)
25202514
{
2521-
if(!simplify_member(expr))
2522-
r = changed(expr);
2515+
r = simplify_member(expr);
25232516
}
25242517
else if(expr.id()==ID_byte_update_little_endian ||
25252518
expr.id()==ID_byte_update_big_endian)
@@ -2669,8 +2662,7 @@ bool simplify_exprt::simplify_node(exprt &expr)
26692662
}
26702663
else if(expr.id()==ID_sign)
26712664
{
2672-
if(!simplify_sign(expr))
2673-
r = changed(expr);
2665+
r = simplify_sign(expr);
26742666
}
26752667
else if(expr.id() == ID_popcount)
26762668
{

0 commit comments

Comments
 (0)