@@ -816,9 +816,8 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
816
816
// rewrite (T)(bool) to bool?1:0
817
817
auto one = from_integer (1 , expr_type);
818
818
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)}));
822
821
}
823
822
824
823
// circular casts through types shorter than `int`
@@ -1336,33 +1335,33 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
1336
1335
return unchanged (expr);
1337
1336
}
1338
1337
1339
- bool simplify_exprt::simplify_typecast_preorder (typecast_exprt &expr)
1338
+ simplify_exprt::resultt<>
1339
+ simplify_exprt::simplify_typecast_preorder (const typecast_exprt &expr)
1340
1340
{
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 ();
1343
1343
1344
1344
// (T)(a?b:c) --> a?(T)b:(T)c; don't do this for floating-point type casts as
1345
1345
// the type cast itself may be costly
1346
1346
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 &&
1348
1348
op_type.id () != ID_floatbv)
1349
1349
{
1350
1350
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));
1354
1352
}
1355
1353
else
1356
1354
{
1357
1355
auto r_it = simplify_rec (expr.op ()); // recursive call
1358
1356
if (r_it.has_changed ())
1359
1357
{
1360
- expr.op () = r_it.expr ;
1361
- return false ;
1358
+ auto tmp = expr;
1359
+ tmp.op () = r_it.expr ;
1360
+ return tmp;
1362
1361
}
1363
- else
1364
- return true ;
1365
1362
}
1363
+
1364
+ return unchanged (expr);
1366
1365
}
1367
1366
1368
1367
simplify_exprt::resultt<>
@@ -2629,40 +2628,50 @@ simplify_exprt::simplify_overflow_result(const overflow_result_exprt &expr)
2629
2628
}
2630
2629
}
2631
2630
2632
- bool simplify_exprt::simplify_node_preorder (exprt &expr)
2631
+ simplify_exprt::resultt<>
2632
+ simplify_exprt::simplify_node_preorder (const exprt &expr)
2633
2633
{
2634
- bool result=true ;
2635
-
2636
2634
// The ifs below could one day be replaced by a switch()
2637
2635
2638
2636
if (expr.id ()==ID_address_of)
2639
2637
{
2640
2638
// the argument of this expression needs special treatment
2641
2639
}
2642
2640
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
+ }
2644
2644
else if (expr.id () == ID_typecast)
2645
- result = simplify_typecast_preorder (to_typecast_expr (expr));
2646
- else
2647
2645
{
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)
2649
2653
{
2650
- Forall_operands (it, expr)
2654
+ auto r_it = simplify_rec (expr.operands ()[i]); // recursive call
2655
+ if (r_it.has_changed ())
2651
2656
{
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 );
2658
2660
}
2659
2661
}
2662
+
2663
+ if (new_operands.has_value ())
2664
+ {
2665
+ exprt result = expr;
2666
+ std::swap (result.operands (), *new_operands);
2667
+ return result;
2668
+ }
2660
2669
}
2661
2670
2662
- return result ;
2671
+ return unchanged (expr) ;
2663
2672
}
2664
2673
2665
- simplify_exprt::resultt<> simplify_exprt::simplify_node (exprt node)
2674
+ simplify_exprt::resultt<> simplify_exprt::simplify_node (const exprt & node)
2666
2675
{
2667
2676
if (!node.has_operands ())
2668
2677
return unchanged (node); // no change
@@ -2947,50 +2956,52 @@ simplify_exprt::resultt<> simplify_exprt::simplify_rec(const exprt &expr)
2947
2956
#endif
2948
2957
2949
2958
// 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);
2952
2960
2953
- auto simplify_node_result = simplify_node (tmp );
2961
+ auto simplify_node_result = simplify_node (simplify_node_preorder_result. expr );
2954
2962
2955
- if (simplify_node_result.has_changed ())
2963
+ if (
2964
+ !simplify_node_result.has_changed () &&
2965
+ simplify_node_preorder_result.has_changed ())
2956
2966
{
2957
- no_change = false ;
2958
- tmp = simplify_node_result. expr ;
2967
+ simplify_node_result. expr_changed =
2968
+ simplify_node_preorder_result. expr_changed ;
2959
2969
}
2960
2970
2961
2971
#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 );
2964
2976
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 ))
2965
2982
{
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 ));
2975
2984
}
2976
- # endif
2985
+ # endif
2977
2986
#endif
2978
2987
2979
- if (no_change) // no change
2988
+ if (!simplify_node_result. has_changed ())
2980
2989
{
2981
2990
return unchanged (expr);
2982
2991
}
2983
- else // change, new expression is 'tmp'
2992
+ else
2984
2993
{
2985
2994
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 ());
2987
2998
2988
2999
#ifdef USE_CACHE
2989
3000
// save in cache
2990
- cache_result.first ->second = tmp ;
3001
+ cache_result.first ->second = simplify_node_result. expr ;
2991
3002
#endif
2992
3003
2993
- return std::move (tmp) ;
3004
+ return simplify_node_result ;
2994
3005
}
2995
3006
}
2996
3007
0 commit comments