Skip to content

Simplify byte extract: use lowering when expression is a constant #7340

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 2 commits into from
Nov 29, 2022
Merged
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
4 changes: 2 additions & 2 deletions regression/cbmc-library/memcpy-01/constant-propagation.desc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CORE
main.c
--show-vcc
main::1::m!0@1#2 = .*byte_extract_(big|little)_endian\(cast\(44, signedbv\[\d+\]\*\), 0, signedbv\[\d+\]\).*

^Generated 1\d* VCC\(s\), 0 remaining after simplification$
^EXIT=0$
^SIGNAL=0$
--
Expand Down
9 changes: 6 additions & 3 deletions src/util/simplify_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1976,10 +1976,13 @@ simplify_exprt::simplify_byte_extract(const byte_extract_exprt &expr)
// try to refine it down to extracting from a member or an index in an array
auto subexpr =
get_subexpression_at_offset(expr.op(), *offset, expr.type(), ns);
if(!subexpr.has_value() || subexpr.value() == expr)
return unchanged(expr);
if(subexpr.has_value() && subexpr.value() != expr)
return changed(simplify_rec(subexpr.value())); // recursive call

if(is_constantt(ns)(expr))
return changed(simplify_rec(lower_byte_extract(expr, ns)));

return changed(simplify_rec(subexpr.value())); // recursive call
return unchanged(expr);
}

simplify_exprt::resultt<>
Expand Down
9 changes: 8 additions & 1 deletion src/util/simplify_expr_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,14 @@ simplify_exprt::simplify_concatenation(const concatenation_exprt &expr)
.set_width(
to_bitvector_type(eb_i.type()).get_width() +
to_bitvector_type(eb_n.type()).get_width());
opi = eb_merged;
if(expr.type().id() != eb_merged.type().id())
{
bitvector_typet bt = to_bitvector_type(expr.type());
bt.set_width(to_bitvector_type(eb_merged.type()).get_width());
opi = simplify_typecast(typecast_exprt{eb_merged, bt});
}
else
opi = eb_merged;
// erase opn
new_expr.operands().erase(new_expr.operands().begin() + i + 1);
no_change = false;
Expand Down