@@ -103,10 +103,10 @@ simplify_exprt::resultt<> simplify_exprt::simplify_abs(const abs_exprt &expr)
103
103
return unchanged (expr);
104
104
}
105
105
106
- bool simplify_exprt::simplify_sign (exprt &expr)
106
+ simplify_exprt::resultt<> simplify_exprt::simplify_sign (const exprt &expr)
107
107
{
108
108
if (expr.operands ().size ()!=1 )
109
- return true ;
109
+ return unchanged (expr) ;
110
110
111
111
if (expr.op0 ().is_constant ())
112
112
{
@@ -115,22 +115,20 @@ bool simplify_exprt::simplify_sign(exprt &expr)
115
115
if (type.id ()==ID_floatbv)
116
116
{
117
117
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 ());
120
119
}
121
120
else if (type.id ()==ID_signedbv ||
122
121
type.id ()==ID_unsignedbv)
123
122
{
124
123
const auto value = numeric_cast<mp_integer>(expr.op0 ());
125
124
if (value.has_value ())
126
125
{
127
- expr = make_boolean_expr (*value >= 0 );
128
- return false ;
126
+ return make_boolean_expr (*value >= 0 );
129
127
}
130
128
}
131
129
}
132
130
133
- return true ;
131
+ return unchanged (expr) ;
134
132
}
135
133
136
134
simplify_exprt::resultt<>
@@ -1433,14 +1431,15 @@ simplify_exprt::resultt<> simplify_exprt::simplify_lambda(const exprt &expr)
1433
1431
return unchanged (expr);
1434
1432
}
1435
1433
1436
- bool simplify_exprt::simplify_with (exprt &expr)
1434
+ simplify_exprt::resultt<> simplify_exprt::simplify_with (const exprt &expr)
1437
1435
{
1438
- bool result= true ;
1436
+ bool no_change = true ;
1439
1437
1440
1438
if ((expr.operands ().size ()%2 )!=1 )
1441
- return true ;
1439
+ return unchanged (expr) ;
1442
1440
1443
- auto &with_expr = to_with_expr (expr);
1441
+ // copy
1442
+ auto with_expr = to_with_expr (expr);
1444
1443
1445
1444
const typet old_type_followed = ns.follow (with_expr.old ().type ());
1446
1445
@@ -1456,17 +1455,20 @@ bool simplify_exprt::simplify_with(exprt &expr)
1456
1455
with_expr.where ().get (ID_component_name);
1457
1456
1458
1457
if (!to_struct_type (old_type_followed).has_component (component_name))
1459
- return result ;
1458
+ return unchanged (expr) ;
1460
1459
1461
1460
std::size_t number =
1462
1461
to_struct_type (old_type_followed).component_number (component_name);
1463
1462
1463
+ if (number >= with_expr.old ().operands ().size ())
1464
+ return unchanged (expr);
1465
+
1464
1466
with_expr.old ().operands ()[number].swap (with_expr.new_value ());
1465
1467
1466
1468
with_expr.operands ().erase (++with_expr.operands ().begin ());
1467
1469
with_expr.operands ().erase (++with_expr.operands ().begin ());
1468
1470
1469
- result= false ;
1471
+ no_change = false ;
1470
1472
}
1471
1473
}
1472
1474
}
@@ -1475,10 +1477,10 @@ bool simplify_exprt::simplify_with(exprt &expr)
1475
1477
with_expr.old ().type ().id () == ID_vector)
1476
1478
{
1477
1479
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)
1480
1482
{
1481
- while (expr .operands ().size ()> 1 )
1483
+ while (with_expr .operands ().size () > 1 )
1482
1484
{
1483
1485
const auto i = numeric_cast<mp_integer>(with_expr.where ());
1484
1486
@@ -1494,26 +1496,24 @@ bool simplify_exprt::simplify_with(exprt &expr)
1494
1496
with_expr.operands ().erase (++with_expr.operands ().begin ());
1495
1497
with_expr.operands ().erase (++with_expr.operands ().begin ());
1496
1498
1497
- result= false ;
1499
+ no_change = false ;
1498
1500
}
1499
1501
}
1500
1502
}
1501
1503
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 ();
1509
1506
1510
- return result;
1507
+ if (no_change)
1508
+ return unchanged (expr);
1509
+ else
1510
+ return std::move (with_expr);
1511
1511
}
1512
1512
1513
- bool simplify_exprt::simplify_update (exprt &expr)
1513
+ simplify_exprt::resultt<> simplify_exprt::simplify_update (const exprt &expr)
1514
1514
{
1515
1515
if (expr.operands ().size ()!=3 )
1516
- return true ;
1516
+ return unchanged (expr) ;
1517
1517
1518
1518
// this is to push updates into (possibly nested) constants
1519
1519
@@ -1532,10 +1532,10 @@ bool simplify_exprt::simplify_update(exprt &expr)
1532
1532
const auto i = numeric_cast<mp_integer>(e.op0 ());
1533
1533
1534
1534
if (!i.has_value ())
1535
- return true ;
1535
+ return unchanged (expr) ;
1536
1536
1537
1537
if (*i < 0 || *i >= value_ptr->operands ().size ())
1538
- return true ;
1538
+ return unchanged (expr) ;
1539
1539
1540
1540
value_ptr = &value_ptr->operands ()[numeric_cast_v<std::size_t >(*i)];
1541
1541
}
@@ -1547,20 +1547,18 @@ bool simplify_exprt::simplify_update(exprt &expr)
1547
1547
const struct_typet &value_ptr_struct_type =
1548
1548
to_struct_type (value_ptr_type);
1549
1549
if (!value_ptr_struct_type.has_component (component_name))
1550
- return true ;
1550
+ return unchanged (expr) ;
1551
1551
auto &designator_as_struct_expr = to_struct_expr (*value_ptr);
1552
1552
value_ptr = &designator_as_struct_expr.component (component_name, ns);
1553
1553
CHECK_RETURN (value_ptr->is_not_nil ());
1554
1554
}
1555
1555
else
1556
- return true ; // give up, unknown designator
1556
+ return unchanged (expr) ; // give up, unknown designator
1557
1557
}
1558
1558
1559
1559
// found, done
1560
1560
*value_ptr=to_update_expr (expr).new_value ();
1561
- expr.swap (updated_value);
1562
-
1563
- return false ;
1561
+ return updated_value;
1564
1562
}
1565
1563
1566
1564
simplify_exprt::resultt<> simplify_exprt::simplify_object (const exprt &expr)
@@ -2490,8 +2488,7 @@ bool simplify_exprt::simplify_node(exprt &expr)
2490
2488
expr.id ()==ID_gt || expr.id ()==ID_lt ||
2491
2489
expr.id ()==ID_ge || expr.id ()==ID_le)
2492
2490
{
2493
- if (!simplify_inequality (expr))
2494
- r = changed (expr);
2491
+ r = simplify_inequality (expr);
2495
2492
}
2496
2493
else if (expr.id ()==ID_if)
2497
2494
{
@@ -2503,23 +2500,19 @@ bool simplify_exprt::simplify_node(exprt &expr)
2503
2500
}
2504
2501
else if (expr.id ()==ID_with)
2505
2502
{
2506
- if (!simplify_with (expr))
2507
- r = changed (expr);
2503
+ r = simplify_with (expr);
2508
2504
}
2509
2505
else if (expr.id ()==ID_update)
2510
2506
{
2511
- if (!simplify_update (expr))
2512
- r = changed (expr);
2507
+ r = simplify_update (expr);
2513
2508
}
2514
2509
else if (expr.id ()==ID_index)
2515
2510
{
2516
- if (!simplify_index (expr))
2517
- r = changed (expr);
2511
+ r = simplify_index (expr);
2518
2512
}
2519
2513
else if (expr.id ()==ID_member)
2520
2514
{
2521
- if (!simplify_member (expr))
2522
- r = changed (expr);
2515
+ r = simplify_member (expr);
2523
2516
}
2524
2517
else if (expr.id ()==ID_byte_update_little_endian ||
2525
2518
expr.id ()==ID_byte_update_big_endian)
@@ -2669,8 +2662,7 @@ bool simplify_exprt::simplify_node(exprt &expr)
2669
2662
}
2670
2663
else if (expr.id ()==ID_sign)
2671
2664
{
2672
- if (!simplify_sign (expr))
2673
- r = changed (expr);
2665
+ r = simplify_sign (expr);
2674
2666
}
2675
2667
else if (expr.id () == ID_popcount)
2676
2668
{
0 commit comments