Skip to content

Commit fa90683

Browse files
author
Daniel Kroening
committed
simplifier: eliminate casts from bool to number
This usually enables subsequent simplification, e.g., ((int)B) != 0 turns into (B?1:0) != 0 and then into B?1!=0:0!=0 and then into B?true:false which then turns into B
1 parent 1edf4d9 commit fa90683

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/util/simplify_expr.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,19 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
576576
return std::move(inequality);
577577
}
578578

579+
// eliminate casts from proper bool
580+
if(
581+
op_type.id() == ID_bool &&
582+
(expr_type.id() == ID_signedbv || expr_type.id() == ID_unsignedbv ||
583+
expr_type.id() == ID_c_bool || expr_type.id() == ID_c_bit_field))
584+
{
585+
// rewrite (T)(bool) to bool?1:0
586+
auto one = from_integer(1, expr_type);
587+
auto zero = from_integer(0, expr_type);
588+
exprt new_expr = if_exprt(expr.op(), std::move(one), std::move(zero));
589+
return changed(simplify_rec(new_expr)); // recursive call
590+
}
591+
579592
// circular casts through types shorter than `int`
580593
if(op_type == signedbv_typet(32) && expr.op().id() == ID_typecast)
581594
{

0 commit comments

Comments
 (0)