Skip to content

Commit 872b51a

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 16f63e8 commit 872b51a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/util/simplify_expr.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,17 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
557557
return std::move(inequality);
558558
}
559559

560+
// eliminate casts from proper bool
561+
if(op_type.id() == ID_bool)
562+
{
563+
// rewrite (T)(bool) to bool?1:0
564+
auto one = from_integer(1, expr_type);
565+
auto zero = from_integer(0, expr_type);
566+
exprt new_expr = if_exprt(expr.op(), std::move(one), std::move(zero));
567+
simplify_node(new_expr);
568+
return std::move(new_expr);
569+
}
570+
560571
// circular casts through types shorter than `int`
561572
if(op_type == signedbv_typet(32) && expr.op().id() == ID_typecast)
562573
{

0 commit comments

Comments
 (0)