Skip to content

Commit 6db234c

Browse files
smowtonromainbrenguier
authored andcommitted
Resolve array indices after dereferencing
1 parent b38ce5d commit 6db234c

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/goto-symex/symex_dereference.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515
#include <util/byte_operators.h>
1616
#include <util/c_types.h>
1717
#include <util/exception_utils.h>
18+
#include <util/expr_iterator.h>
1819
#include <util/expr_util.h>
1920
#include <util/invariant.h>
2021
#include <util/pointer_offset_size.h>
@@ -223,6 +224,58 @@ void goto_symext::dereference_rec(exprt &expr, statet &state, bool write)
223224
// first make sure there are no dereferences in there
224225
dereference_rec(tmp1, state, false);
225226

227+
bool resolved_any_indices = false;
228+
229+
// Before we try to deref it, see if we can constant-prop away any
230+
// non-constant index references:
231+
for(auto expr_it = tmp1.depth_begin(), expr_end = tmp1.depth_end();
232+
expr_it != expr_end;
233+
++expr_it)
234+
{
235+
if(expr_it->id() == ID_index)
236+
{
237+
const exprt &array_cell = to_index_expr(*expr_it).index();
238+
if(!array_cell.is_constant())
239+
{
240+
exprt constant_array_cell = array_cell;
241+
state.rename(constant_array_cell, ns);
242+
do_simplify(constant_array_cell);
243+
if(constant_array_cell.is_constant())
244+
{
245+
// Insert the newly constant array index, and try to promote to a
246+
// SSA identifier:
247+
index_exprt &index_expr = to_index_expr(expr_it.mutate());
248+
index_expr.index() = constant_array_cell;
249+
resolved_any_indices = true;
250+
}
251+
}
252+
}
253+
else if(expr_it->id() == ID_plus && expr_it->type().id() == ID_pointer)
254+
{
255+
const exprt &rhs = expr_it->op1();
256+
if(!rhs.is_constant())
257+
{
258+
exprt constant_rhs = rhs;
259+
state.rename(constant_rhs, ns);
260+
do_simplify(constant_rhs);
261+
if(constant_rhs.is_constant())
262+
{
263+
// Insert the newly constant offset, and try to promote to a
264+
// SSA identifier:
265+
exprt &improved_expr = expr_it.mutate();
266+
improved_expr.op1() = constant_rhs;
267+
resolved_any_indices = true;
268+
}
269+
}
270+
}
271+
}
272+
273+
if(resolved_any_indices)
274+
{
275+
do_simplify(tmp1);
276+
tmp1 = state.field_sensitivity.apply(ns, state, std::move(tmp1), write);
277+
}
278+
226279
// we need to set up some elaborate call-backs
227280
symex_dereference_statet symex_dereference_state(state, ns);
228281

0 commit comments

Comments
 (0)