Skip to content

GOTO-symex: propagate notequal conditions for boolean types [TG-9390] #5089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/goto-symex/goto_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,72 @@ void goto_statet::apply_condition(
{
if(condition.id() == ID_and)
{
// A == B && C == D && E == F ...
// -->
// Apply each condition individually
for(const auto &op : condition.operands())
apply_condition(op, previous_state, ns);
}
else if(condition.id() == ID_notequal &&
to_notequal_expr(condition).lhs().type() == bool_typet())
{
// A != (true|false)
// -->
// A == (false|true)
exprt lhs = to_notequal_expr(condition).lhs();
exprt rhs = to_notequal_expr(condition).rhs();
if(is_ssa_expr(rhs))
std::swap(lhs, rhs);

if(rhs.is_true())
apply_condition(equal_exprt(lhs, false_exprt()), previous_state, ns);
else if(rhs.is_false())
apply_condition(equal_exprt(lhs, true_exprt()), previous_state, ns);
}
else if(condition.id() == ID_not &&
to_not_expr(condition).op().id() == ID_notequal)
{
// !(A != B)
// -->
// A == B
const auto &notequal_expr = to_notequal_expr(to_not_expr(condition).op());
apply_condition(
equal_exprt(notequal_expr.lhs(), notequal_expr.rhs()),
previous_state,
ns);
}
else if(condition.id() == ID_not &&
to_not_expr(condition).op().id() == ID_equal)
{
// !(A == B)
// -->
// A != B
const auto &equal_expr = to_equal_expr(to_not_expr(condition).op());
apply_condition(
notequal_exprt(equal_expr.lhs(), equal_expr.rhs()),
previous_state,
ns);
}
else if(condition.id() == ID_not)
{
// !A
// -->
// A == false
apply_condition(
equal_exprt(to_not_expr(condition).op(), false_exprt()),
previous_state,
ns);
}
else if(condition.id() == ID_symbol && condition.type() == bool_typet())
{
// A
// -->
// A == true
apply_condition(equal_exprt(condition, true_exprt()), previous_state, ns);
}
else if(condition.id() == ID_equal)
{
// Base case: try to apply a single equality constraint
exprt lhs = to_equal_expr(condition).lhs();
exprt rhs = to_equal_expr(condition).rhs();
if(is_ssa_expr(rhs))
Expand Down
17 changes: 4 additions & 13 deletions src/goto-symex/symex_goto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,12 @@ void goto_symext::apply_goto_condition(

jump_taken_state.apply_condition(new_guard, current_state, ns);

// Try to find a negative condition that implies an equality constraint on
// the branch-not-taken path.
// Could use not_exprt + simplify, but let's avoid paying that cost on quite
// a hot path:
if(new_guard.id() == ID_not)
jump_not_taken_state.apply_condition(
to_not_expr(new_guard).op(), current_state, ns);
else if(new_guard.id() == ID_notequal)
{
auto &not_equal_expr = to_notequal_expr(new_guard);
jump_not_taken_state.apply_condition(
equal_exprt(not_equal_expr.lhs(), not_equal_expr.rhs()),
current_state,
ns);
}
exprt negated_new_guard = new_guard.id() ==
ID_not ? to_not_expr(new_guard).op() : not_exprt(new_guard);
jump_not_taken_state.apply_condition(
negated_new_guard, current_state, ns);
}

/// Try to evaluate a simple pointer comparison.
Expand Down