Skip to content

Commit 9a51a92

Browse files
committed
Simplify byte updates of constants
Mirror what we already did for byte extract operations.
1 parent a8592a7 commit 9a51a92

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/util/simplify_expr.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,40 @@ simplify_exprt::simplify_byte_update(const byte_update_exprt &expr)
17531753
return changed(simplify_byte_extract(be));
17541754
}
17551755

1756+
// update bits in a constant
1757+
const auto offset_int = numeric_cast<mp_integer>(offset);
1758+
if(
1759+
root_size.has_value() && *root_size >= 0 && val_size.has_value() &&
1760+
*val_size >= 0 && offset_int.has_value() && *offset_int >= 0 &&
1761+
*offset_int + *val_size <= *root_size)
1762+
{
1763+
auto root_bits =
1764+
expr2bits(root, expr.id() == ID_byte_update_little_endian, ns);
1765+
1766+
if(root_bits.has_value())
1767+
{
1768+
const auto val_bits =
1769+
expr2bits(value, expr.id() == ID_byte_update_little_endian, ns);
1770+
1771+
if(val_bits.has_value())
1772+
{
1773+
root_bits->replace(
1774+
numeric_cast_v<std::size_t>(*offset_int * 8),
1775+
numeric_cast_v<std::size_t>(*val_size),
1776+
*val_bits);
1777+
1778+
auto tmp = bits2expr(
1779+
*root_bits,
1780+
expr.type(),
1781+
expr.id() == ID_byte_update_little_endian,
1782+
ns);
1783+
1784+
if(tmp.has_value())
1785+
return std::move(*tmp);
1786+
}
1787+
}
1788+
}
1789+
17561790
/*
17571791
* byte_update(root, offset,
17581792
* extract(root, offset) WITH component:=value)
@@ -1836,7 +1870,6 @@ simplify_exprt::simplify_byte_update(const byte_update_exprt &expr)
18361870
}
18371871

18381872
// the following require a constant offset
1839-
const auto offset_int = numeric_cast<mp_integer>(offset);
18401873
if(!offset_int.has_value() || *offset_int < 0)
18411874
return unchanged(expr);
18421875

0 commit comments

Comments
 (0)