cprover
Toggle main menu visibility
Loading...
Searching...
No Matches
natural_loops.h
Go to the documentation of this file.
1
/*******************************************************************\
2
3
Module: Compute natural loops in a goto_function
4
5
Author: Georg Weissenbacher, georg@weissenbacher.name
6
7
\*******************************************************************/
8
19
20
#ifndef CPROVER_ANALYSES_NATURAL_LOOPS_H
21
#define CPROVER_ANALYSES_NATURAL_LOOPS_H
22
23
#include <stack>
24
#include <iosfwd>
25
#include <set>
26
27
#include <
goto-programs/goto_model.h
>
28
29
#include "
cfg_dominators.h
"
30
#include "
loop_analysis.h
"
31
47
template
<
class
P,
class
T,
typename
C>
48
class
natural_loops_templatet
:
public
loop_analysist
<T, C>
49
{
50
typedef
loop_analysist<T, C>
parentt
;
51
52
public
:
53
typedef
typename
parentt::loopt
natural_loopt
;
54
55
void
operator()
(P &program)
56
{
57
compute
(program);
58
}
59
60
const
cfg_dominators_templatet<P, T, false>
&
get_dominator_info
()
const
61
{
62
return
cfg_dominators
;
63
}
64
65
natural_loops_templatet
()
66
{
67
}
68
69
explicit
natural_loops_templatet
(P &program)
70
{
71
compute
(program);
72
}
73
74
protected
:
75
cfg_dominators_templatet<P, T, false>
cfg_dominators
;
76
typedef
typename
cfg_dominators_templatet<P, T, false>::cfgt::nodet
nodet
;
77
78
void
compute
(P &program);
79
void
compute_natural_loop
(T, T);
80
};
81
84
class
natural_loopst
:
public
natural_loops_templatet
<
85
const goto_programt,
86
goto_programt::const_targett,
87
goto_programt::target_less_than>
88
{
89
};
90
91
typedef
natural_loops_templatet
<
92
goto_programt
,
93
goto_programt::targett
,
94
goto_programt::target_less_than
>
95
natural_loops_mutablet
;
96
97
inline
void
show_natural_loops
(
const
goto_modelt
&goto_model, std::ostream &out)
98
{
99
show_loops<natural_loopst>
(goto_model, out);
100
}
101
102
#ifdef DEBUG
103
#include <iostream>
104
#endif
105
107
template
<
class
P,
class
T,
typename
C>
108
void
natural_loops_templatet<P, T, C>::compute
(P &program)
109
{
110
cfg_dominators
(program);
111
112
#ifdef DEBUG
113
cfg_dominators
.output(std::cout);
114
#endif
115
116
// find back-edges m->n
117
for
(T m_it=program.instructions.begin();
118
m_it!=program.instructions.end();
119
++m_it)
120
{
121
if
(m_it->is_backwards_goto())
122
{
123
const
auto
&target=m_it->get_target();
124
125
if
(target->location_number<=m_it->location_number)
126
{
127
#ifdef DEBUG
128
std::cout <<
"Computing loop for "
129
<< m_it->location_number <<
" -> "
130
<< target->location_number <<
"\n"
;
131
#endif
132
133
if
(
cfg_dominators
.dominates(target, m_it))
134
compute_natural_loop
(m_it, target);
135
}
136
}
137
}
138
}
139
141
template
<
class
P,
class
T,
typename
C>
142
void
natural_loops_templatet<P, T, C>::compute_natural_loop
(T m, T n)
143
{
144
PRECONDITION
(n->location_number <= m->location_number);
145
146
std::stack<T> stack;
147
148
auto
insert_result =
parentt::loop_map
.emplace(n,
natural_loopt
{});
149
// Note the emplace *may* access a loop that already exists: this happens when
150
// a given header has more than one incoming edge, such as
151
// head: if(x) goto head; else goto head;
152
// In this case this compute routine is run twice, one for each backedge, with
153
// each adding whatever instructions can reach this 'm' (the program point
154
// that branches to the loop header, 'n').
155
natural_loopt
&loop = insert_result.first->second;
156
157
loop.
insert_instruction
(n);
158
loop.
insert_instruction
(m);
159
160
if
(n!=m)
161
stack.push(m);
162
163
while
(!stack.empty())
164
{
165
T p=stack.top();
166
stack.pop();
167
168
const
nodet
&node =
cfg_dominators
.get_node(p);
169
170
for
(
const
auto
&edge : node.
in
)
171
{
172
T q=
cfg_dominators
.cfg[edge.first].PC;
173
if
(loop.
insert_instruction
(q))
174
stack.push(q);
175
}
176
}
177
}
178
179
#endif
// CPROVER_ANALYSES_NATURAL_LOOPS_H
cfg_dominators.h
Compute dominators for CFG of goto_function.
cfg_baset::nodet
base_grapht::nodet nodet
Definition
cfg.h:92
cfg_dominators_templatet
Dominator graph.
Definition
cfg_dominators.h:37
goto_modelt
Definition
goto_model.h:27
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition
goto_program.h:72
goto_programt::targett
instructionst::iterator targett
Definition
goto_program.h:613
goto_programt::target_less_than
instructiont::target_less_than target_less_than
Definition
goto_program.h:618
graph_nodet::in
edgest in
Definition
graph.h:42
loop_analysist::loop_map
loop_mapt loop_map
Definition
loop_analysis.h:88
loop_analysist::loopt
loop_templatet< T, C > loopt
Definition
loop_analysis.h:84
loop_analysist::loop_analysist
loop_analysist()=default
loop_templatet::insert_instruction
bool insert_instruction(const T instruction)
Adds instruction to this loop.
Definition
loop_analysis.h:74
natural_loops_templatet
Main driver for working out if a class (normally goto_programt) has any natural loops.
Definition
natural_loops.h:49
natural_loops_templatet::natural_loops_templatet
natural_loops_templatet()
Definition
natural_loops.h:65
natural_loops_templatet< goto_programt, goto_programt::targett, goto_programt::target_less_than >::cfg_dominators
cfg_dominators_templatet< goto_programt, goto_programt::targett, false > cfg_dominators
Definition
natural_loops.h:75
natural_loops_templatet::operator()
void operator()(P &program)
Definition
natural_loops.h:55
natural_loops_templatet::natural_loopt
parentt::loopt natural_loopt
Definition
natural_loops.h:53
natural_loops_templatet::compute_natural_loop
void compute_natural_loop(T, T)
Computes the natural loop for a given back-edge (see Muchnick section 7.4).
Definition
natural_loops.h:142
natural_loops_templatet::nodet
cfg_dominators_templatet< P, T, false >::cfgt::nodet nodet
Definition
natural_loops.h:76
natural_loops_templatet::parentt
loop_analysist< T, C > parentt
Definition
natural_loops.h:50
natural_loops_templatet::compute
void compute(P &program)
Finds all back-edges and computes the natural loops.
Definition
natural_loops.h:108
natural_loops_templatet::get_dominator_info
const cfg_dominators_templatet< P, T, false > & get_dominator_info() const
Definition
natural_loops.h:60
natural_loops_templatet::natural_loops_templatet
natural_loops_templatet(P &program)
Definition
natural_loops.h:69
natural_loopst
A concretized version of natural_loops_templatet<const goto_programt, goto_programt::const_targett>.
Definition
natural_loops.h:88
goto_model.h
Symbol Table + CFG.
loop_analysis.h
Data structure representing a loop in a GOTO program and an interface shared by all analyses that fin...
show_loops
void show_loops(const goto_modelt &goto_model, std::ostream &out)
Definition
loop_analysis.h:200
natural_loops_mutablet
natural_loops_templatet< goto_programt, goto_programt::targett, goto_programt::target_less_than > natural_loops_mutablet
Definition
natural_loops.h:95
show_natural_loops
void show_natural_loops(const goto_modelt &goto_model, std::ostream &out)
Definition
natural_loops.h:97
PRECONDITION
#define PRECONDITION(CONDITION)
Definition
invariant.h:463
analyses
natural_loops.h
Generated by
1.17.0