@@ -1007,21 +1007,6 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
1007
1007
}
1008
1008
}
1009
1009
1010
- #if 0
1011
- // (T)(a?b:c) --> a?(T)b:(T)c
1012
- if(expr.op().id()==ID_if &&
1013
- expr.op().operands().size()==3)
1014
- {
1015
- typecast_exprt tmp_op1(expr.op().op1(), expr_type);
1016
- typecast_exprt tmp_op2(expr.op().op2(), expr_type);
1017
- simplify_typecast(tmp_op1);
1018
- simplify_typecast(tmp_op2);
1019
- auto new_expr=if_exprt(expr.op().op0(), tmp_op1, tmp_op2, expr_type);
1020
- simplify_if(new_expr);
1021
- return std::move(new_expr);
1022
- }
1023
- #endif
1024
-
1025
1010
const irep_idt &expr_type_id=expr_type.id ();
1026
1011
const exprt &operand = expr.op ();
1027
1012
const irep_idt &op_type_id=op_type.id ();
@@ -1349,6 +1334,35 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
1349
1334
return unchanged (expr);
1350
1335
}
1351
1336
1337
+ bool simplify_exprt::simplify_typecast_preorder (typecast_exprt &expr)
1338
+ {
1339
+ const typet &expr_type = as_const (expr).type ();
1340
+ const typet &op_type = as_const (expr).op ().type ();
1341
+
1342
+ // (T)(a?b:c) --> a?(T)b:(T)c; don't do this for floating-point type casts as
1343
+ // the type cast itself may be costly
1344
+ if (
1345
+ as_const (expr).op ().id () == ID_if && expr_type.id () != ID_floatbv &&
1346
+ op_type.id () != ID_floatbv)
1347
+ {
1348
+ if_exprt if_expr = lift_if (expr, 0 );
1349
+ simplify_if_preorder (if_expr);
1350
+ expr.swap (if_expr);
1351
+ return false ;
1352
+ }
1353
+ else
1354
+ {
1355
+ auto r_it = simplify_rec (expr.op ()); // recursive call
1356
+ if (r_it.has_changed ())
1357
+ {
1358
+ expr.op () = r_it.expr ;
1359
+ return false ;
1360
+ }
1361
+ else
1362
+ return true ;
1363
+ }
1364
+ }
1365
+
1352
1366
simplify_exprt::resultt<>
1353
1367
simplify_exprt::simplify_dereference (const dereference_exprt &expr)
1354
1368
{
@@ -2616,6 +2630,8 @@ bool simplify_exprt::simplify_node_preorder(exprt &expr)
2616
2630
}
2617
2631
else if (expr.id ()==ID_if)
2618
2632
result=simplify_if_preorder (to_if_expr (expr));
2633
+ else if (expr.id () == ID_typecast)
2634
+ result = simplify_typecast_preorder (to_typecast_expr (expr));
2619
2635
else
2620
2636
{
2621
2637
if (expr.has_operands ())
0 commit comments