Skip to content

Commit 91d47c2

Browse files
committed
remove_skip implementation restricted to a range of instructions
1 parent 41d38bc commit 91d47c2

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

src/goto-programs/remove_skip.cpp

+40-22
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ bool is_skip(const goto_programt &body, goto_programt::const_targett it)
6969
}
7070

7171
/// remove unnecessary skip statements
72-
void remove_skip(goto_programt &goto_program)
72+
/// \param goto_program goto program containing the instructions to be cleaned
73+
/// in the range [begin, end)
74+
/// \param begin iterator pointing to first instruction to be considered
75+
/// \param end iterator pointing beyond last instruction to be considered
76+
void remove_skip(
77+
goto_programt &goto_program,
78+
goto_programt::targett begin,
79+
goto_programt::targett end)
7380
{
7481
// This needs to be a fixed-point, as
7582
// removing a skip can turn a goto into a skip.
@@ -86,9 +93,7 @@ void remove_skip(goto_programt &goto_program)
8693

8794
// remove skip statements
8895

89-
for(goto_programt::instructionst::iterator
90-
it=goto_program.instructions.begin();
91-
it!=goto_program.instructions.end();)
96+
for(goto_programt::instructionst::iterator it = begin; it != end;)
9297
{
9398
goto_programt::targett old_target=it;
9499

@@ -99,7 +104,7 @@ void remove_skip(goto_programt &goto_program)
99104
{
100105
// don't remove the last skip statement,
101106
// it could be a target
102-
if(it==--goto_program.instructions.end())
107+
if(it == std::prev(end))
103108
break;
104109

105110
// save labels
@@ -121,41 +126,54 @@ void remove_skip(goto_programt &goto_program)
121126
it++;
122127
}
123128

124-
// adjust gotos
125-
126-
Forall_goto_program_instructions(i_it, goto_program)
127-
if(i_it->is_goto() || i_it->is_start_thread() || i_it->is_catch())
129+
// adjust gotos across the full goto program body
130+
for(auto &ins : goto_program.instructions)
131+
{
132+
if(ins.is_goto() || ins.is_start_thread() || ins.is_catch())
128133
{
129-
for(goto_programt::instructiont::targetst::iterator
130-
t_it=i_it->targets.begin();
131-
t_it!=i_it->targets.end();
132-
t_it++)
134+
for(auto &target : ins.targets)
133135
{
134-
new_targetst::const_iterator
135-
result=new_targets.find(*t_it);
136+
new_targetst::const_iterator result = new_targets.find(target);
136137

137138
if(result!=new_targets.end())
138-
*t_it=result->second;
139+
target = result->second;
139140
}
140141
}
142+
}
143+
144+
while(new_targets.find(begin) != new_targets.end())
145+
++begin;
141146

142147
// now delete the skips -- we do so after adjusting the
143148
// gotos to avoid dangling targets
144149
for(const auto &new_target : new_targets)
145150
goto_program.instructions.erase(new_target.first);
146151

147152
// remove the last skip statement unless it's a target
148-
goto_program.compute_incoming_edges();
153+
goto_program.compute_target_numbers();
154+
155+
if(begin != end)
156+
{
157+
goto_programt::targett last = std::prev(end);
158+
if(begin == last)
159+
++begin;
149160

150-
if(
151-
!goto_program.instructions.empty() &&
152-
is_skip(goto_program, --goto_program.instructions.end()) &&
153-
!goto_program.instructions.back().is_target())
154-
goto_program.instructions.pop_back();
161+
if(is_skip(goto_program, last) && !last->is_target())
162+
goto_program.instructions.erase(last);
163+
}
155164
}
156165
while(goto_program.instructions.size()<old_size);
157166
}
158167

168+
/// remove unnecessary skip statements
169+
void remove_skip(goto_programt &goto_program)
170+
{
171+
remove_skip(
172+
goto_program,
173+
goto_program.instructions.begin(),
174+
goto_program.instructions.end());
175+
}
176+
159177
/// remove unnecessary skip statements
160178
void remove_skip(goto_functionst &goto_functions)
161179
{

src/goto-programs/remove_skip.h

+5
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ void remove_skip(goto_programt &);
2222
void remove_skip(goto_functionst &);
2323
void remove_skip(goto_modelt &);
2424

25+
void remove_skip(
26+
goto_programt &goto_program,
27+
goto_programt::targett begin,
28+
const goto_programt::targett end);
29+
2530
#endif // CPROVER_GOTO_PROGRAMS_REMOVE_SKIP_H

0 commit comments

Comments
 (0)