cprover
Toggle main menu visibility
Loading...
Searching...
No Matches
all_paths_enumerator.cpp
Go to the documentation of this file.
1
/*******************************************************************\
2
3
Module: Loop Acceleration
4
5
Author: Matt Lewis
6
7
\*******************************************************************/
8
11
12
#include "
all_paths_enumerator.h
"
13
14
bool
all_paths_enumeratort::next
(
patht
&path)
15
{
16
if
(
last_path
.empty())
17
{
18
// This is the first time we've been called -- build an initial
19
// path.
20
last_path
.push_back(
path_nodet
(
loop_header
));
21
22
// This shouldn't be able to fail.
23
complete_path
(
last_path
, 0);
24
25
if
(
is_looping
(
last_path
))
26
{
27
// If this was a loop path, we're good. If it wasn't,
28
// we'll keep enumerating paths until we hit a looping one.
29
// This case is exactly the same as if someone just called
30
// next() on us.
31
path.clear();
32
path.insert(path.begin(),
last_path
.begin(),
last_path
.end());
33
return
true
;
34
}
35
}
36
37
do
38
{
39
#ifdef DEBUG
40
std::cout <<
"Enumerating next path...\n"
;
41
#endif
42
43
int
decision=
backtrack
(
last_path
);
44
complete_path
(
last_path
, decision);
45
46
if
(
is_looping
(
last_path
))
47
{
48
path.clear();
49
path.insert(path.begin(),
last_path
.begin(),
last_path
.end());
50
return
true
;
51
}
52
}
53
while
(!
last_path
.empty());
54
55
// We've enumerated all the paths.
56
return
false
;
57
}
58
59
int
all_paths_enumeratort::backtrack
(
patht
&path)
60
{
61
// If we have a path of length 1 or 0, we can't backtrack any further.
62
// That means we're done enumerating paths!
63
if
(path.size()<2)
64
{
65
path.clear();
66
return
0;
67
}
68
69
goto_programt::targett
node_loc=path.back().loc;
70
path.pop_back();
71
72
goto_programt::targett
parent_loc=path.back().loc;
73
const
auto
succs=
goto_program
.get_successors(parent_loc);
74
75
unsigned
int
ret=0;
76
77
for
(
const
auto
&succ : succs)
78
{
79
if
(succ==node_loc)
80
break
;
81
82
ret++;
83
}
84
85
if
((ret+1)<succs.size())
86
{
87
// We can take the next branch here...
88
89
#ifdef DEBUG
90
std::cout <<
"Backtracked to a path of size "
<< path.size() <<
'\n'
;
91
#endif
92
93
return
ret+1;
94
}
95
96
// Recurse.
97
return
backtrack
(path);
98
}
99
100
void
all_paths_enumeratort::complete_path
(
patht
&path,
int
succ)
101
{
102
if
(path.empty())
103
return
;
104
105
goto_programt::targett
node_loc=path.back().loc;
106
extend_path
(path, node_loc, succ);
107
108
goto_programt::targett
end=path.back().loc;
109
110
if
(end ==
loop_header
|| !
loop
.contains(end))
111
return
;
112
113
complete_path
(path, 0);
114
}
115
116
void
all_paths_enumeratort::extend_path
(
117
patht
&path,
118
goto_programt::targett
t,
119
int
succ)
120
{
121
goto_programt::targett
next
;
122
exprt
guard
=
true_exprt
();
123
124
for
(
const
auto
&s :
goto_program
.get_successors(t))
125
{
126
if
(succ==0)
127
{
128
next
=s;
129
break
;
130
}
131
132
succ--;
133
}
134
135
if
(t->is_goto())
136
{
137
guard
=
not_exprt
(t->condition());
138
139
for
(goto_programt::targetst::iterator it=t->targets.begin();
140
it != t->targets.end();
141
++it)
142
{
143
if
(
next
== *it)
144
{
145
guard
= t->condition();
146
break
;
147
}
148
}
149
}
150
151
path.push_back(
path_nodet
(
next
,
guard
));
152
}
153
154
bool
all_paths_enumeratort::is_looping
(
patht
&path)
155
{
156
return
path.size()>1 && path.back().loc==
loop_header
;
157
}
all_paths_enumerator.h
Loop Acceleration.
guard
static exprt guard(const exprt::operandst &guards, exprt cond)
Definition
c_safety_checks.cpp:69
all_paths_enumeratort::last_path
patht last_path
Definition
all_paths_enumerator.h:47
all_paths_enumeratort::loop
natural_loops_mutablet::natural_loopt & loop
Definition
all_paths_enumerator.h:44
all_paths_enumeratort::backtrack
int backtrack(patht &path)
Definition
all_paths_enumerator.cpp:59
all_paths_enumeratort::next
virtual bool next(patht &path)
Definition
all_paths_enumerator.cpp:14
all_paths_enumeratort::complete_path
void complete_path(patht &path, int succ)
Definition
all_paths_enumerator.cpp:100
all_paths_enumeratort::loop_header
goto_programt::targett loop_header
Definition
all_paths_enumerator.h:45
all_paths_enumeratort::goto_program
goto_programt & goto_program
Definition
all_paths_enumerator.h:43
all_paths_enumeratort::is_looping
bool is_looping(patht &path)
Definition
all_paths_enumerator.cpp:154
all_paths_enumeratort::extend_path
void extend_path(patht &path, goto_programt::targett t, int succ)
Definition
all_paths_enumerator.cpp:116
exprt
Base class for all expressions.
Definition
expr.h:57
goto_programt::targett
instructionst::iterator targett
Definition
goto_program.h:613
not_exprt
Boolean negation.
Definition
std_expr.h:2378
path_nodet
Definition
path.h:23
true_exprt
The Boolean constant true.
Definition
std_expr.h:3116
patht
std::list< path_nodet > patht
Definition
path.h:44
goto-instrument
accelerate
all_paths_enumerator.cpp
Generated by
1.17.0