Skip to content

Commit 56dc239

Browse files
committed
byte_extract lowering of arrays: optimise for constant index
No need to construct index_exprt when we can just pick the operand out of a vector.
1 parent 06cd197 commit 56dc239

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/solvers/lowering/byte_operators.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,22 @@ exprt lower_byte_extract(const byte_extract_exprt &src, const namespacet &ns)
517517
little_endian?(width_bytes-i-1):i;
518518

519519
plus_exprt offset_i(from_integer(offset_int, offset.type()), offset);
520-
index_exprt index_expr(root, offset_i);
521-
op.push_back(index_expr);
520+
simplify(offset_i, ns);
521+
522+
mp_integer index = 0;
523+
if(
524+
offset_i.is_constant() &&
525+
(root.id() == ID_array || root.id() == ID_vector) &&
526+
!to_integer(to_constant_expr(offset_i), index) &&
527+
index < root.operands().size() && index >= 0)
528+
{
529+
// offset is constant and in range
530+
op.push_back(root.operands()[numeric_cast_v<std::size_t>(index)]);
531+
}
532+
else
533+
{
534+
op.push_back(index_exprt(root, offset_i));
535+
}
522536
}
523537

524538
if(width_bytes==1)

0 commit comments

Comments
 (0)