Skip to content

Commit c68ee9f

Browse files
committed
Simplify byte extract of empty union or struct
Byte-extracting an empty compound object trivially yields a constant expression.
1 parent 279cb5f commit c68ee9f

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/util/simplify_expr.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,19 @@ simplify_exprt::simplify_byte_extract(const byte_extract_exprt &expr)
17051705
}
17061706
}
17071707

1708+
if(
1709+
(expr.type().id() == ID_union || expr.type().id() == ID_union_tag) &&
1710+
to_union_type(ns.follow(expr.type())).components().empty())
1711+
{
1712+
return empty_union_exprt{expr.type()};
1713+
}
1714+
else if(
1715+
(expr.type().id() == ID_struct || expr.type().id() == ID_struct_tag) &&
1716+
to_struct_type(ns.follow(expr.type())).components().empty())
1717+
{
1718+
return struct_exprt{{}, expr.type()};
1719+
}
1720+
17081721
// no proper simplification for expr.type()==void
17091722
// or types of unknown size
17101723
if(!el_size.has_value() || *el_size == 0)

src/util/simplify_utils.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ optionalt<exprt> bits2expr(
264264
const union_typet &union_type = to_union_type(type);
265265
const union_typet::componentst &components = union_type.components();
266266

267+
if(components.empty() && bits.empty())
268+
return empty_union_exprt{type};
269+
267270
for(const auto &component : components)
268271
{
269272
auto val = bits2expr(bits, component.type(), little_endian, ns);

0 commit comments

Comments
 (0)