Skip to content

Commit 32ad1ea

Browse files
author
Remi Delmas
committed
CONTRACTS: loop assigns clause inference function
1 parent d11f7a2 commit 32ad1ea

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

src/goto-instrument/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ SRC = accelerate/accelerate.cpp \
2222
contracts/dynamic-frames/dfcc_loop_nesting_graph.cpp \
2323
contracts/dynamic-frames/dfcc_contract_mode.cpp \
2424
contracts/dynamic-frames/dfcc_loop_contract_mode.cpp \
25+
contracts/dynamic-frames/dfcc_infer_loop_assigns.cpp \
2526
contracts/dynamic-frames/dfcc_library.cpp \
2627
contracts/dynamic-frames/dfcc_is_cprover_symbol.cpp \
2728
contracts/dynamic-frames/dfcc_is_fresh.cpp \
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
#include <langapi/language_util.h>
18+
19+
#include "dfcc_root_object.h"
20+
21+
/// Builds a call expression `object_whole(expr)`
22+
static exprt
23+
make_object_whole_call_expr(const exprt &expr, const namespacet &ns)
24+
{
25+
const symbolt &object_whole_sym = ns.lookup(CPROVER_PREFIX "object_whole");
26+
code_typet object_whole_code_type = to_code_type(object_whole_sym.type);
27+
exprt::operandst args{{expr}};
28+
return side_effect_expr_function_callt(
29+
object_whole_sym.symbol_expr(),
30+
args,
31+
object_whole_code_type.return_type(),
32+
expr.source_location());
33+
}
34+
35+
static bool
36+
depends_on(const exprt &expr, std::unordered_set<irep_idt> identifiers)
37+
{
38+
const std::unordered_set<irep_idt> ids = find_symbol_identifiers(expr);
39+
for(const auto &id : ids)
40+
{
41+
if(identifiers.find(id) != identifiers.end())
42+
return true;
43+
}
44+
return false;
45+
}
46+
47+
std::set<exprt> dfcc_infer_loop_assigns(
48+
const local_may_aliast &local_may_alias,
49+
const loopt &loop_instructions,
50+
const source_locationt &loop_head_location,
51+
messaget &log,
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+
havoc_utils_is_constantt is_constant(assigns, ns);
68+
assignst result;
69+
for(const auto &expr : assigns)
70+
{
71+
if(depends_on(expr, loop_locals))
72+
{
73+
// Target depends on loop locals, attempt widening to the root object
74+
auto root_object = dfcc_assigns_target_single_root_object(expr, ns, log);
75+
if(!depends_on(root_object, loop_locals))
76+
{
77+
address_of_exprt address_of_root_object(root_object);
78+
address_of_root_object.add_source_location() =
79+
root_object.source_location();
80+
result.emplace(make_object_whole_call_expr(address_of_root_object, ns));
81+
}
82+
}
83+
else
84+
{
85+
address_of_exprt address_of_expr(expr);
86+
address_of_expr.add_source_location() = expr.source_location();
87+
// Target address is not constant, widening to the whole object
88+
if(!is_constant(address_of_expr))
89+
{
90+
result.emplace(make_object_whole_call_expr(address_of_expr, ns));
91+
}
92+
else
93+
{
94+
result.emplace(expr);
95+
}
96+
}
97+
}
98+
99+
return result;
100+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
std::set<exprt> 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+
messaget &log,
28+
const namespacet &ns);
29+
30+
#endif

0 commit comments

Comments
 (0)