cprover
Toggle main menu visibility
Loading...
Searching...
No Matches
slice.cpp
Go to the documentation of this file.
1
/*******************************************************************\
2
3
Module: Slicer for symex traces
4
5
Author: Daniel Kroening, kroening@kroening.com
6
7
\*******************************************************************/
8
11
12
#include "
slice.h
"
13
#include "
symex_slice_class.h
"
14
15
#include <
util/find_symbols.h
>
16
#include <
util/std_expr.h
>
17
18
void
symex_slicet::get_symbols
(
const
exprt
&expr)
19
{
20
find_symbols
(expr,
depends
);
21
}
22
23
void
symex_slicet::slice
(
24
symex_target_equationt
&equation,
25
const
std::list<exprt> &exprs)
26
{
27
// collect dependencies
28
for
(
const
auto
&expr : exprs)
29
get_symbols
(expr);
30
31
slice
(equation);
32
}
33
34
void
symex_slicet::slice
(
symex_target_equationt
&equation)
35
{
36
simple_slice
(equation);
37
38
for
(symex_target_equationt::SSA_stepst::reverse_iterator
39
it=equation.
SSA_steps
.rbegin();
40
it!=equation.
SSA_steps
.rend();
41
it++)
42
{
43
if
(!it->ignore)
44
slice
(*it);
45
}
46
}
47
48
void
symex_slicet::slice
(
SSA_stept
&SSA_step)
49
{
50
switch
(SSA_step.
type
)
51
{
52
case
goto_trace_stept::typet::ASSERT
:
53
get_symbols
(SSA_step.
cond_expr
);
54
break
;
55
56
case
goto_trace_stept::typet::ASSUME
:
57
get_symbols
(SSA_step.
cond_expr
);
58
break
;
59
60
case
goto_trace_stept::typet::GOTO
:
61
// ignore
62
break
;
63
64
case
goto_trace_stept::typet::LOCATION
:
65
// ignore
66
break
;
67
68
case
goto_trace_stept::typet::ASSIGNMENT
:
69
slice_assignment
(SSA_step);
70
break
;
71
72
case
goto_trace_stept::typet::DECL
:
73
slice_decl
(SSA_step);
74
break
;
75
76
case
goto_trace_stept::typet::OUTPUT
:
77
case
goto_trace_stept::typet::INPUT
:
78
break
;
79
80
case
goto_trace_stept::typet::DEAD
:
81
// ignore for now
82
break
;
83
84
case
goto_trace_stept::typet::CONSTRAINT
:
85
case
goto_trace_stept::typet::SHARED_READ
:
86
case
goto_trace_stept::typet::SHARED_WRITE
:
87
case
goto_trace_stept::typet::ATOMIC_BEGIN
:
88
case
goto_trace_stept::typet::ATOMIC_END
:
89
case
goto_trace_stept::typet::SPAWN
:
90
case
goto_trace_stept::typet::MEMORY_BARRIER
:
91
// ignore for now
92
break
;
93
94
case
goto_trace_stept::typet::FUNCTION_CALL
:
95
case
goto_trace_stept::typet::FUNCTION_RETURN
:
96
// ignore for now
97
break
;
98
99
case
goto_trace_stept::typet::NONE
:
100
UNREACHABLE
;
101
}
102
}
103
104
void
symex_slicet::slice_assignment
(
SSA_stept
&SSA_step)
105
{
106
PRECONDITION
(SSA_step.
ssa_lhs
.
id
() == ID_symbol);
107
const
irep_idt
&
id
=SSA_step.
ssa_lhs
.
get_identifier
();
108
109
auto
entry =
depends
.find(
id
);
110
if
(entry ==
depends
.end())
111
{
112
// we don't really need it
113
SSA_step.
ignore
=
true
;
114
}
115
else
116
{
117
// we have resolved this dependency
118
depends
.erase(entry);
119
get_symbols
(SSA_step.
ssa_rhs
);
120
}
121
}
122
123
void
symex_slicet::slice_decl
(
SSA_stept
&SSA_step)
124
{
125
const
irep_idt
&
id
=
to_symbol_expr
(SSA_step.
ssa_lhs
).
get_identifier
();
126
127
if
(
depends
.find(
id
)==
depends
.end())
128
{
129
// we don't really need it
130
SSA_step.
ignore
=
true
;
131
}
132
}
133
138
void
symex_slicet::collect_open_variables
(
139
const
symex_target_equationt
&equation,
140
symbol_sett
&open_variables)
141
{
142
symbol_sett
lhs;
143
144
for
(symex_target_equationt::SSA_stepst::const_iterator
145
it=equation.
SSA_steps
.begin();
146
it!=equation.
SSA_steps
.end();
147
it++)
148
{
149
const
SSA_stept
&SSA_step = *it;
150
151
get_symbols
(SSA_step.
guard
);
152
153
switch
(SSA_step.
type
)
154
{
155
case
goto_trace_stept::typet::ASSERT
:
156
get_symbols
(SSA_step.
cond_expr
);
157
break
;
158
159
case
goto_trace_stept::typet::ASSUME
:
160
get_symbols
(SSA_step.
cond_expr
);
161
break
;
162
163
case
goto_trace_stept::typet::GOTO
:
164
// ignore
165
break
;
166
167
case
goto_trace_stept::typet::LOCATION
:
168
// ignore
169
break
;
170
171
case
goto_trace_stept::typet::ASSIGNMENT
:
172
get_symbols
(SSA_step.
ssa_rhs
);
173
lhs.insert(SSA_step.
ssa_lhs
.
get_identifier
());
174
break
;
175
176
case
goto_trace_stept::typet::OUTPUT
:
177
case
goto_trace_stept::typet::INPUT
:
178
case
goto_trace_stept::typet::DEAD
:
179
break
;
180
181
case
goto_trace_stept::typet::DECL
:
182
case
goto_trace_stept::typet::FUNCTION_CALL
:
183
case
goto_trace_stept::typet::FUNCTION_RETURN
:
184
case
goto_trace_stept::typet::CONSTRAINT
:
185
case
goto_trace_stept::typet::SHARED_READ
:
186
case
goto_trace_stept::typet::SHARED_WRITE
:
187
case
goto_trace_stept::typet::ATOMIC_BEGIN
:
188
case
goto_trace_stept::typet::ATOMIC_END
:
189
case
goto_trace_stept::typet::SPAWN
:
190
case
goto_trace_stept::typet::MEMORY_BARRIER
:
191
// ignore for now
192
break
;
193
194
case
goto_trace_stept::typet::NONE
:
195
UNREACHABLE
;
196
}
197
}
198
199
open_variables=
depends
;
200
201
// remove the ones that are defined
202
open_variables.erase(lhs.begin(), lhs.end());
203
}
204
205
void
slice
(
symex_target_equationt
&equation)
206
{
207
symex_slicet
symex_slice;
208
symex_slice.
slice
(equation);
209
}
210
215
void
collect_open_variables
(
216
const
symex_target_equationt
&equation,
217
symbol_sett
&open_variables)
218
{
219
symex_slicet
symex_slice;
220
symex_slice.
collect_open_variables
(equation, open_variables);
221
}
222
226
void
slice
(
227
symex_target_equationt
&equation,
228
const
std::list<exprt> &expressions)
229
{
230
symex_slicet
symex_slice;
231
symex_slice.
slice
(equation, expressions);
232
}
233
234
void
simple_slice
(
symex_target_equationt
&equation)
235
{
236
// just find the last assertion
237
symex_target_equationt::SSA_stepst::iterator
238
last_assertion=equation.
SSA_steps
.end();
239
240
for
(symex_target_equationt::SSA_stepst::iterator
241
it=equation.
SSA_steps
.begin();
242
it!=equation.
SSA_steps
.end();
243
it++)
244
if
(it->is_assert())
245
last_assertion=it;
246
247
// slice away anything after it
248
249
symex_target_equationt::SSA_stepst::iterator s_it=
250
last_assertion;
251
252
if
(s_it!=equation.
SSA_steps
.end())
253
{
254
for
(s_it++;
255
s_it!=equation.
SSA_steps
.end();
256
s_it++)
257
s_it->ignore=
true
;
258
}
259
}
260
261
void
revert_slice
(
symex_target_equationt
&equation)
262
{
263
// set ignore to false
264
for
(
auto
&step : equation.
SSA_steps
)
265
{
266
step.ignore =
false
;
267
}
268
}
SSA_stept
Single SSA step in the equation.
Definition
ssa_step.h:47
SSA_stept::ignore
bool ignore
Definition
ssa_step.h:172
SSA_stept::cond_expr
exprt cond_expr
Definition
ssa_step.h:145
SSA_stept::type
goto_trace_stept::typet type
Definition
ssa_step.h:50
SSA_stept::guard
exprt guard
Definition
ssa_step.h:135
SSA_stept::ssa_rhs
exprt ssa_rhs
Definition
ssa_step.h:141
SSA_stept::ssa_lhs
ssa_exprt ssa_lhs
Definition
ssa_step.h:139
exprt
Base class for all expressions.
Definition
expr.h:57
goto_trace_stept::typet::SPAWN
@ SPAWN
Definition
goto_trace.h:91
goto_trace_stept::typet::ASSERT
@ ASSERT
Definition
goto_trace.h:79
goto_trace_stept::typet::ATOMIC_END
@ ATOMIC_END
Definition
goto_trace.h:94
goto_trace_stept::typet::GOTO
@ GOTO
Definition
goto_trace.h:80
goto_trace_stept::typet::OUTPUT
@ OUTPUT
Definition
goto_trace.h:83
goto_trace_stept::typet::MEMORY_BARRIER
@ MEMORY_BARRIER
Definition
goto_trace.h:92
goto_trace_stept::typet::SHARED_WRITE
@ SHARED_WRITE
Definition
goto_trace.h:90
goto_trace_stept::typet::SHARED_READ
@ SHARED_READ
Definition
goto_trace.h:89
goto_trace_stept::typet::LOCATION
@ LOCATION
Definition
goto_trace.h:81
goto_trace_stept::typet::DECL
@ DECL
Definition
goto_trace.h:84
goto_trace_stept::typet::ASSIGNMENT
@ ASSIGNMENT
Definition
goto_trace.h:77
goto_trace_stept::typet::INPUT
@ INPUT
Definition
goto_trace.h:82
goto_trace_stept::typet::NONE
@ NONE
Definition
goto_trace.h:76
goto_trace_stept::typet::DEAD
@ DEAD
Definition
goto_trace.h:85
goto_trace_stept::typet::FUNCTION_RETURN
@ FUNCTION_RETURN
Definition
goto_trace.h:87
goto_trace_stept::typet::ASSUME
@ ASSUME
Definition
goto_trace.h:78
goto_trace_stept::typet::CONSTRAINT
@ CONSTRAINT
Definition
goto_trace.h:88
goto_trace_stept::typet::ATOMIC_BEGIN
@ ATOMIC_BEGIN
Definition
goto_trace.h:93
goto_trace_stept::typet::FUNCTION_CALL
@ FUNCTION_CALL
Definition
goto_trace.h:86
irept::id
const irep_idt & id() const
Definition
irep.h:388
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition
std_expr.h:161
symex_slicet
Definition
symex_slice_class.h:19
symex_slicet::get_symbols
void get_symbols(const exprt &expr)
Definition
slice.cpp:18
symex_slicet::collect_open_variables
void collect_open_variables(const symex_target_equationt &equation, symbol_sett &open_variables)
Collect the open variables, i.e., variables that are used in RHS but never written in LHS.
Definition
slice.cpp:138
symex_slicet::depends
symbol_sett depends
Definition
symex_slice_class.h:30
symex_slicet::slice
void slice(symex_target_equationt &equation)
Definition
slice.cpp:34
symex_slicet::slice_decl
void slice_decl(SSA_stept &SSA_step)
Definition
slice.cpp:123
symex_slicet::slice_assignment
void slice_assignment(SSA_stept &SSA_step)
Definition
slice.cpp:104
symex_target_equationt
Inheriting the interface of symex_targett this class represents the SSA form of the input program as ...
Definition
symex_target_equation.h:34
symex_target_equationt::SSA_steps
SSA_stepst SSA_steps
Definition
symex_target_equation.h:251
find_symbols
static bool find_symbols(symbol_kindt, const typet &, std::function< bool(const symbol_exprt &)>, std::unordered_set< irep_idt > &bindings, const std::vector< irep_idt > &subs_to_find)
Find identifiers with id ID_symbol of the sub expressions and the subs with ID in subs_to_find consid...
Definition
find_symbols.cpp:152
find_symbols.h
revert_slice
void revert_slice(symex_target_equationt &equation)
Undo whatever has been done by slice.
Definition
slice.cpp:261
slice
void slice(symex_target_equationt &equation)
Definition
slice.cpp:205
simple_slice
void simple_slice(symex_target_equationt &equation)
Definition
slice.cpp:234
collect_open_variables
void collect_open_variables(const symex_target_equationt &equation, symbol_sett &open_variables)
Collect the open variables, i.e.
Definition
slice.cpp:215
slice.h
Slicer for symex traces.
symbol_sett
std::unordered_set< irep_idt > symbol_sett
Definition
slice.h:39
UNREACHABLE
#define UNREACHABLE
This should be used to mark dead code.
Definition
invariant.h:525
PRECONDITION
#define PRECONDITION(CONDITION)
Definition
invariant.h:463
std_expr.h
API to expression classes.
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition
std_expr.h:211
symex_slice_class.h
Slicer for symex traces.
irep_idt
dstringt irep_idt
Definition
verification_result.h:16
goto-symex
slice.cpp
Generated by
1.17.0