Skip to content

Commit 1b899b1

Browse files
author
Remi Delmas
committed
CONTRACTS: loop assigns clause inference function
1 parent 6b828d5 commit 1b899b1

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

src/goto-instrument/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ SRC = accelerate/accelerate.cpp \
2424
contracts/dynamic-frames/dfcc_loop_tags.cpp \
2525
contracts/dynamic-frames/dfcc_check_loop_normal_form.cpp \
2626
contracts/dynamic-frames/dfcc_root_object.cpp \
27+
contracts/dynamic-frames/dfcc_infer_loop_assigns.cpp \
2728
contracts/dynamic-frames/dfcc_library.cpp \
2829
contracts/dynamic-frames/dfcc_is_cprover_symbol.cpp \
2930
contracts/dynamic-frames/dfcc_is_fresh.cpp \
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*******************************************************************\
2+
3+
Module: Dynamic frame condition checking
4+
5+
Author: Remi Delmas, [email protected]
6+
7+
\*******************************************************************/
8+
#include "dfcc_infer_loop_assigns.h"
9+
10+
#include <util/expr.h>
11+
#include <util/find_symbols.h>
12+
#include <util/message.h>
13+
#include <util/pointer_expr.h>
14+
#include <util/std_code.h>
15+
16+
#include <goto-instrument/havoc_utils.h>
17+
18+
#include "dfcc_root_object.h"
19+
20+
/// Builds a call expression `object_whole(expr)`
21+
static exprt
22+
make_object_whole_call_expr(const exprt &expr, const namespacet &ns)
23+
{
24+
const symbolt &object_whole_sym = ns.lookup(CPROVER_PREFIX "object_whole");
25+
const code_typet &object_whole_code_type =
26+
to_code_type(object_whole_sym.type);
27+
return side_effect_expr_function_callt(
28+
object_whole_sym.symbol_expr(),
29+
{{expr}},
30+
object_whole_code_type.return_type(),
31+
expr.source_location());
32+
}
33+
34+
/// Returns true iff \p expr contains at least one identifier found in
35+
/// \p identifiers.
36+
static bool
37+
depends_on(const exprt &expr, std::unordered_set<irep_idt> identifiers)
38+
{
39+
const std::unordered_set<irep_idt> ids = find_symbol_identifiers(expr);
40+
for(const auto &id : ids)
41+
{
42+
if(identifiers.find(id) != identifiers.end())
43+
return true;
44+
}
45+
return false;
46+
}
47+
48+
assignst dfcc_infer_loop_assigns(
49+
const local_may_aliast &local_may_alias,
50+
const loopt &loop_instructions,
51+
const source_locationt &loop_head_location,
52+
const namespacet &ns)
53+
{
54+
// infer
55+
assignst assigns;
56+
get_assigns(local_may_alias, loop_instructions, assigns);
57+
58+
// compute locals
59+
std::unordered_set<irep_idt> loop_locals;
60+
for(const auto &target : loop_instructions)
61+
{
62+
if(target->is_decl())
63+
loop_locals.insert(target->decl_symbol().get_identifier());
64+
}
65+
66+
// widen or drop targets that depend on loop-locals or are non-constant,
67+
// ie. depend on other locations assigned by the loop.
68+
// e.g: if the loop assigns {i, a[i]}, then a[i] is non-constant.
69+
havoc_utils_is_constantt is_constant(assigns, ns);
70+
assignst result;
71+
for(const auto &expr : assigns)
72+
{
73+
if(depends_on(expr, loop_locals))
74+
{
75+
// Target depends on loop locals, attempt widening to the root object
76+
auto root_objects = dfcc_root_objects(expr);
77+
for(const auto &root_object : root_objects)
78+
{
79+
if(!depends_on(root_object, loop_locals))
80+
{
81+
address_of_exprt address_of_root_object(root_object);
82+
address_of_root_object.add_source_location() =
83+
root_object.source_location();
84+
result.emplace(
85+
make_object_whole_call_expr(address_of_root_object, ns));
86+
}
87+
}
88+
}
89+
else
90+
{
91+
address_of_exprt address_of_expr(expr);
92+
address_of_expr.add_source_location() = expr.source_location();
93+
if(!is_constant(address_of_expr))
94+
{
95+
// Target address is not constant, widening to the whole object
96+
result.emplace(make_object_whole_call_expr(address_of_expr, ns));
97+
}
98+
else
99+
{
100+
result.emplace(expr);
101+
}
102+
}
103+
}
104+
105+
return result;
106+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*******************************************************************\
2+
3+
Module: Dynamic frame condition checking
4+
5+
Author: Remi Delmas, [email protected]
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Infer a set of assigns clause targets for a natural loop.
11+
12+
#ifndef CPROVER_GOTO_INSTRUMENT_CONTRACTS_DYNAMIC_FRAMES_DFCC_INFER_LOOP_ASSIGNS_H
13+
#define CPROVER_GOTO_INSTRUMENT_CONTRACTS_DYNAMIC_FRAMES_DFCC_INFER_LOOP_ASSIGNS_H
14+
15+
#include <analyses/local_may_alias.h>
16+
#include <goto-instrument/loop_utils.h>
17+
class source_locationt;
18+
class messaget;
19+
class namespacet;
20+
21+
// \brief Infer assigns clause targets for a loop from its instructions and a
22+
// may alias analysis at the function level.
23+
assignst dfcc_infer_loop_assigns(
24+
const local_may_aliast &local_may_alias,
25+
const loopt &loop_instructions,
26+
const source_locationt &loop_head_location,
27+
const namespacet &ns);
28+
29+
#endif

0 commit comments

Comments
 (0)