Skip to content

Commit 2bfaafc

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 e2265db commit 2bfaafc

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
@@ -516,8 +516,22 @@ exprt lower_byte_extract(const byte_extract_exprt &src, const namespacet &ns)
516516
little_endian?(width_bytes-i-1):i;
517517

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

523537
if(width_bytes==1)

0 commit comments

Comments
 (0)