|
15 | 15 | #include <util/byte_operators.h> |
16 | 16 | #include <util/c_types.h> |
17 | 17 | #include <util/exception_utils.h> |
| 18 | +#include <util/expr_iterator.h> |
18 | 19 | #include <util/expr_util.h> |
19 | 20 | #include <util/invariant.h> |
20 | 21 | #include <util/pointer_offset_size.h> |
@@ -223,6 +224,58 @@ void goto_symext::dereference_rec(exprt &expr, statet &state, bool write) |
223 | 224 | // first make sure there are no dereferences in there |
224 | 225 | dereference_rec(tmp1, state, false); |
225 | 226 |
|
| 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 | + |
226 | 279 | // we need to set up some elaborate call-backs |
227 | 280 | symex_dereference_statet symex_dereference_state(state, ns); |
228 | 281 |
|
|
0 commit comments