Skip to content

Commit 490ca60

Browse files
Make field_sensitivity::apply handle non-constant index
When the index is not constant when accessing a constant size array, we should expand the expression into `{array[0]; array[1];...}[index]` so that the relation with field sensitivity expressions `array[[0]]`, `array[[1]]`, etc, is not lost.
1 parent 006d998 commit 490ca60

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/goto-symex/field_sensitivity.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ exprt field_sensitivityt::apply(
9696
// place the entire index expression, not just the array operand, in an
9797
// SSA expression
9898
ssa_exprt tmp = to_ssa_expr(index.array());
99+
auto l2_index = state.rename(index.index(), ns);
100+
l2_index.simplify(ns);
99101
bool was_l2 = !tmp.get_level_2().empty();
100102
exprt l2_size =
101103
state.rename(to_array_type(index.array().type()).size(), ns).get();
@@ -112,16 +114,29 @@ exprt field_sensitivityt::apply(
112114
if(
113115
l2_size.id() == ID_constant &&
114116
numeric_cast_v<mp_integer>(to_constant_expr(l2_size)) <=
115-
max_field_sensitive_array_size &&
116-
index.id() == ID_constant)
117+
max_field_sensitive_array_size)
117118
{
118-
tmp.remove_level_2();
119-
index.array() = tmp.get_original_expr();
120-
tmp.set_expression(index);
121-
if(was_l2)
122-
return state.rename(std::move(tmp), ns).get();
123-
else
124-
return std::move(tmp);
119+
if(l2_index.get().id() == ID_constant)
120+
{
121+
// place the entire index expression, not just the array operand,
122+
// in an SSA expression
123+
ssa_exprt ssa_array = to_ssa_expr(index.array());
124+
ssa_array.remove_level_2();
125+
index.array() = ssa_array.get_original_expr();
126+
index.index() = l2_index.get();
127+
tmp.set_expression(index);
128+
if(was_l2)
129+
return state.rename(std::move(tmp), ns).get();
130+
else
131+
return std::move(tmp);
132+
}
133+
else if(!write)
134+
{
135+
// Expand the array and return `{array[0]; array[1]; ...}[index]`
136+
exprt expanded_array =
137+
get_fields(ns, state, to_ssa_expr(index.array()));
138+
return index_exprt{std::move(expanded_array), index.index()};
139+
}
125140
}
126141
}
127142
}

0 commit comments

Comments
 (0)