Skip to content

Commit 0ae1c7e

Browse files
committed
Rewrite byte_extract from multi-dimensional array
As a follow-up to 78839a9: add support for rewriting multiple-of-element size access to arrays when working with multi-dimensional arrays.
1 parent 6b6c8d3 commit 0ae1c7e

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/util/pointer_offset_size.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,29 @@ std::optional<exprt> get_subexpression_at_offset(
793793
const exprt &other_factor =
794794
offset_mult.op0().is_constant() ? offset_mult.op1() : offset_mult.op0();
795795

796-
if(const_factor % (*elem_size_bits / config.ansi_c.char_width) != 0)
796+
const mp_integer elem_size_bytes =
797+
*elem_size_bits / config.ansi_c.char_width;
798+
if(
799+
array_type->element_type().id() == ID_array &&
800+
const_factor < elem_size_bytes && elem_size_bytes % const_factor == 0)
801+
{
802+
const mp_integer index_divider = elem_size_bytes / const_factor;
803+
exprt index = div_exprt{
804+
other_factor, from_integer(index_divider, other_factor.type())};
805+
exprt remaining_offset = mult_exprt{
806+
mod_exprt{
807+
other_factor, from_integer(index_divider, other_factor.type())},
808+
from_integer(const_factor, other_factor.type())};
809+
810+
return get_subexpression_at_offset(
811+
index_exprt{
812+
expr,
813+
typecast_exprt::conditional_cast(index, array_type->index_type())},
814+
remaining_offset,
815+
target_type,
816+
ns);
817+
}
818+
else if(const_factor % elem_size_bytes != 0)
797819
return {};
798820

799821
exprt index = mult_exprt{

0 commit comments

Comments
 (0)