Skip to content

Commit e565999

Browse files
authored
Merge pull request #7250 from tautschnig/feature/simp-empty
Simplify byte extract of empty union or struct
2 parents a1ac0f6 + b5b1584 commit e565999

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/solvers/smt2/smt2_conv.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -965,12 +965,17 @@ static bool is_zero_width(const typet &type, const namespacet &ns)
965965
return true;
966966
else if(type.id() == ID_struct_tag)
967967
return is_zero_width(ns.follow_tag(to_struct_tag_type(type)), ns);
968-
else if(type.id() == ID_struct)
969-
return to_struct_type(type).components().empty();
970968
else if(type.id() == ID_union_tag)
971969
return is_zero_width(ns.follow_tag(to_union_tag_type(type)), ns);
972-
else if(type.id() == ID_union)
973-
return to_union_type(type).components().empty();
970+
else if(type.id() == ID_struct || type.id() == ID_union)
971+
{
972+
for(const auto &comp : to_struct_union_type(type).components())
973+
{
974+
if(!is_zero_width(comp.type(), ns))
975+
return false;
976+
}
977+
return true;
978+
}
974979
else
975980
return false;
976981
}

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)