@@ -815,9 +815,8 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
815
815
// rewrite (T)(bool) to bool?1:0
816
816
auto one = from_integer (1 , expr_type);
817
817
auto zero = from_integer (0 , expr_type);
818
- exprt new_expr = if_exprt (expr.op (), std::move (one), std::move (zero));
819
- simplify_if_preorder (to_if_expr (new_expr));
820
- return new_expr;
818
+ return changed (simplify_if_preorder (
819
+ if_exprt{expr.op (), std::move (one), std::move (zero)}));
821
820
}
822
821
823
822
// circular casts through types shorter than `int`
@@ -1334,33 +1333,33 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
1334
1333
return unchanged (expr);
1335
1334
}
1336
1335
1337
- bool simplify_exprt::simplify_typecast_preorder (typecast_exprt &expr)
1336
+ simplify_exprt::resultt<>
1337
+ simplify_exprt::simplify_typecast_preorder (const typecast_exprt &expr)
1338
1338
{
1339
- const typet &expr_type = as_const ( expr) .type ();
1340
- const typet &op_type = as_const ( expr) .op ().type ();
1339
+ const typet &expr_type = expr.type ();
1340
+ const typet &op_type = expr.op ().type ();
1341
1341
1342
1342
// (T)(a?b:c) --> a?(T)b:(T)c; don't do this for floating-point type casts as
1343
1343
// the type cast itself may be costly
1344
1344
if (
1345
- as_const ( expr) .op ().id () == ID_if && expr_type.id () != ID_floatbv &&
1345
+ expr.op ().id () == ID_if && expr_type.id () != ID_floatbv &&
1346
1346
op_type.id () != ID_floatbv)
1347
1347
{
1348
1348
if_exprt if_expr = lift_if (expr, 0 );
1349
- simplify_if_preorder (if_expr);
1350
- expr.swap (if_expr);
1351
- return false ;
1349
+ return changed (simplify_if_preorder (if_expr));
1352
1350
}
1353
1351
else
1354
1352
{
1355
1353
auto r_it = simplify_rec (expr.op ()); // recursive call
1356
1354
if (r_it.has_changed ())
1357
1355
{
1358
- expr.op () = r_it.expr ;
1359
- return false ;
1356
+ auto tmp = expr;
1357
+ tmp.op () = r_it.expr ;
1358
+ return tmp;
1360
1359
}
1361
- else
1362
- return true ;
1363
1360
}
1361
+
1362
+ return unchanged (expr);
1364
1363
}
1365
1364
1366
1365
simplify_exprt::resultt<>
@@ -2618,40 +2617,50 @@ simplify_exprt::simplify_overflow_result(const overflow_result_exprt &expr)
2618
2617
}
2619
2618
}
2620
2619
2621
- bool simplify_exprt::simplify_node_preorder (exprt &expr)
2620
+ simplify_exprt::resultt<>
2621
+ simplify_exprt::simplify_node_preorder (const exprt &expr)
2622
2622
{
2623
- bool result=true ;
2624
-
2625
2623
// The ifs below could one day be replaced by a switch()
2626
2624
2627
2625
if (expr.id ()==ID_address_of)
2628
2626
{
2629
2627
// the argument of this expression needs special treatment
2630
2628
}
2631
2629
else if (expr.id ()==ID_if)
2632
- result=simplify_if_preorder (to_if_expr (expr));
2630
+ {
2631
+ return simplify_if_preorder (to_if_expr (expr));
2632
+ }
2633
2633
else if (expr.id () == ID_typecast)
2634
- result = simplify_typecast_preorder (to_typecast_expr (expr));
2635
- else
2636
2634
{
2637
- if (expr.has_operands ())
2635
+ return simplify_typecast_preorder (to_typecast_expr (expr));
2636
+ }
2637
+ else if (expr.has_operands ())
2638
+ {
2639
+ optionalt<exprt::operandst> new_operands;
2640
+
2641
+ for (std::size_t i = 0 ; i < expr.operands ().size (); ++i)
2638
2642
{
2639
- Forall_operands (it, expr)
2643
+ auto r_it = simplify_rec (expr.operands ()[i]); // recursive call
2644
+ if (r_it.has_changed ())
2640
2645
{
2641
- auto r_it = simplify_rec (*it); // recursive call
2642
- if (r_it.has_changed ())
2643
- {
2644
- *it = r_it.expr ;
2645
- result=false ;
2646
- }
2646
+ if (!new_operands.has_value ())
2647
+ new_operands = expr.operands ();
2648
+ (*new_operands)[i] = std::move (r_it.expr );
2647
2649
}
2648
2650
}
2651
+
2652
+ if (new_operands.has_value ())
2653
+ {
2654
+ exprt result = expr;
2655
+ std::swap (result.operands (), *new_operands);
2656
+ return result;
2657
+ }
2649
2658
}
2650
2659
2651
- return result ;
2660
+ return unchanged (expr) ;
2652
2661
}
2653
2662
2654
- simplify_exprt::resultt<> simplify_exprt::simplify_node (exprt node)
2663
+ simplify_exprt::resultt<> simplify_exprt::simplify_node (const exprt & node)
2655
2664
{
2656
2665
if (!node.has_operands ())
2657
2666
return unchanged (node); // no change
@@ -2940,49 +2949,49 @@ simplify_exprt::resultt<> simplify_exprt::simplify_rec(const exprt &expr)
2940
2949
#endif
2941
2950
2942
2951
// We work on a copy to prevent unnecessary destruction of sharing.
2943
- exprt tmp=expr;
2944
- bool no_change = simplify_node_preorder (tmp);
2952
+ auto simplify_node_preorder_result = simplify_node_preorder (expr);
2945
2953
2946
- auto simplify_node_result = simplify_node (tmp );
2954
+ auto simplify_node_result = simplify_node (simplify_node_preorder_result. expr );
2947
2955
2948
- if (simplify_node_result.has_changed ())
2956
+ if (
2957
+ !simplify_node_result.has_changed () &&
2958
+ simplify_node_preorder_result.has_changed ())
2949
2959
{
2950
- no_change = false ;
2951
- tmp = simplify_node_result. expr ;
2960
+ simplify_node_result. expr_changed =
2961
+ simplify_node_preorder_result. expr_changed ;
2952
2962
}
2953
2963
2954
2964
#ifdef USE_LOCAL_REPLACE_MAP
2955
- #if 1
2956
- replace_mapt::const_iterator it=local_replace_map.find (tmp);
2965
+ exprt tmp = simplify_node_result.expr ;
2966
+ # if 1
2967
+ replace_mapt::const_iterator it =
2968
+ local_replace_map.find (simplify_node_result.expr );
2957
2969
if (it!=local_replace_map.end ())
2970
+ simplify_node_result = changed (it->second );
2971
+ # else
2972
+ if (
2973
+ !local_replace_map.empty () &&
2974
+ !replace_expr (local_replace_map, simplify_node_result.expr ))
2958
2975
{
2959
- tmp=it->second ;
2960
- no_change = false ;
2961
- }
2962
- #else
2963
- if(!local_replace_map.empty() &&
2964
- !replace_expr(local_replace_map, tmp))
2965
- {
2966
- simplify_rec(tmp);
2967
- no_change = false;
2976
+ simplify_node_result = changed (simplify_rec (simplify_node_result.expr ));
2968
2977
}
2969
- # endif
2978
+ # endif
2970
2979
#endif
2971
2980
2972
- if (no_change) // no change
2981
+ if (!simplify_node_result. has_changed ())
2973
2982
{
2974
2983
return unchanged (expr);
2975
2984
}
2976
- else // change, new expression is 'tmp'
2985
+ else
2977
2986
{
2978
- POSTCONDITION (as_const (tmp ).type () == expr.type ());
2987
+ POSTCONDITION (as_const (simplify_node_result. expr ).type () == expr.type ());
2979
2988
2980
2989
#ifdef USE_CACHE
2981
2990
// save in cache
2982
- cache_result.first ->second = tmp ;
2991
+ cache_result.first ->second = simplify_node_result. expr ;
2983
2992
#endif
2984
2993
2985
- return std::move (tmp) ;
2994
+ return simplify_node_result ;
2986
2995
}
2987
2996
}
2988
2997
0 commit comments