Skip to content

Commit f496571

Browse files
committed
Cleaned up body in primitive CEGIS processor.
1 parent 38250f6 commit f496571

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

src/cegis/refactor/instructionset/cegis_processor_body_factory.cpp

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <algorithm>
2+
13
#include <ansi-c/expr2c.h>
24
#include <util/arith_tools.h>
35
#include <util/expr_util.h>
@@ -13,6 +15,10 @@
1315
#define OPERAND_TMP_RESULT_PREFIX "result_op_"
1416
#define INSTR_INDEX "i"
1517

18+
// XXX: Debug
19+
#include <iostream>
20+
// XXX: Debug
21+
1622
std::string cegis_operand_base_name(const size_t op)
1723
{
1824
std::string name(OPERAND_ID_MEMBER_NAME_PREFIX);
@@ -370,6 +376,49 @@ class body_factoryt
370376
add_goto(guard, loop_head);
371377
}
372378
};
379+
380+
bool is_forward_goto(const goto_programt::instructiont &instr)
381+
{
382+
return instr.is_goto() && !instr.is_backwards_goto();
383+
}
384+
385+
void remove_singleton_switch_cases(goto_programt &body)
386+
{
387+
body.compute_location_numbers();
388+
goto_programt::instructionst &instrs=body.instructions;
389+
const goto_programt::targett end(instrs.end());
390+
for (goto_programt::targett pos=instrs.begin(); pos != end; ++pos)
391+
{
392+
if (!is_forward_goto(*pos)) continue;
393+
const auto pred(std::mem_fun_ref(goto_programt::instructiont::is_skip));
394+
const goto_programt::targett tail=std::find_if(pos, end, pred);
395+
assert(end != tail);
396+
if (pos->get_target() == tail) instrs.erase(pos);
397+
pos=tail;
398+
}
399+
}
400+
401+
void remove_goto_next(goto_programt::instructionst &instrs)
402+
{
403+
for (goto_programt::targett pos=instrs.begin(); pos != instrs.end(); ++pos)
404+
if (is_forward_goto(*pos) && pos->get_target() == std::next(pos))
405+
pos=instrs.erase(pos);
406+
}
407+
408+
void remove_skips(goto_programt::instructionst &instrs)
409+
{
410+
const goto_programt::targett first(instrs.begin());
411+
const goto_programt::targett last(instrs.end());
412+
for (goto_programt::targett pos=first; pos != last; ++pos)
413+
{
414+
if (!pos->is_skip()) continue;
415+
const goto_programt::targett successor(std::next(pos));
416+
for (goto_programt::instructiont &instr : instrs)
417+
for (goto_programt::targett &target : instr.targets)
418+
if (target == pos) target=successor;
419+
pos=instrs.erase(pos);
420+
}
421+
}
373422
}
374423

375424
void generate_processor_body(symbol_tablet &st, goto_programt &body,
@@ -387,9 +436,9 @@ void generate_processor_body(symbol_tablet &st, goto_programt &body,
387436
factory.add_index_goto();
388437
}
389438
body.add_instruction(goto_program_instruction_typet::END_FUNCTION);
390-
// TODO: Remove single-switch-cases
391-
// TODO: Remove goto-next
392-
// TODO: Remove skips
439+
remove_singleton_switch_cases(body);
440+
remove_goto_next(body.instructions);
441+
remove_skips(body.instructions);
393442
body.compute_loop_numbers();
394443
body.update();
395444
}

0 commit comments

Comments
 (0)