Skip to content

Simplify byte_extract(byte_update(...)) without overlap #7251

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

Merged
merged 1 commit into from
Oct 24, 2022
Merged
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
34 changes: 34 additions & 0 deletions src/util/simplify_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,40 @@ simplify_exprt::simplify_byte_extract(const byte_extract_exprt &expr)
if(!offset.has_value() || *offset < 0)
return unchanged(expr);

// byte_extract(byte_update(root, offset_u, value), offset_e) so that the
// update does not affect what is being extracted simplifies to
// byte_extract(root, offset_e)
if(
expr.op().id() == ID_byte_update_big_endian ||
expr.op().id() == ID_byte_update_little_endian)
{
const byte_update_exprt &bu = to_byte_update_expr(expr.op());
const auto update_offset = numeric_cast<mp_integer>(bu.offset());
if(update_offset.has_value())
{
if(
*offset * expr.get_bits_per_byte() + *el_size <=
*update_offset * bu.get_bits_per_byte())
{
auto tmp = expr;
tmp.op() = bu.op();
return changed(simplify_byte_extract(tmp)); // recursive call
}
else
{
const auto update_size = pointer_offset_bits(bu.value().type(), ns);
if(
update_size.has_value() &&
*offset >= *update_offset * bu.get_bits_per_byte() + *update_size)
{
auto tmp = expr;
tmp.op() = bu.op();
return changed(simplify_byte_extract(tmp)); // recursive call
}
}
}
}

// don't do any of the following if endianness doesn't match, as
// bytes need to be swapped
if(
Expand Down