1
+ #include < algorithm>
2
+
1
3
#include < ansi-c/expr2c.h>
2
4
#include < util/arith_tools.h>
3
5
#include < util/expr_util.h>
13
15
#define OPERAND_TMP_RESULT_PREFIX " result_op_"
14
16
#define INSTR_INDEX " i"
15
17
18
+ // XXX: Debug
19
+ #include < iostream>
20
+ // XXX: Debug
21
+
16
22
std::string cegis_operand_base_name (const size_t op)
17
23
{
18
24
std::string name (OPERAND_ID_MEMBER_NAME_PREFIX);
@@ -370,6 +376,49 @@ class body_factoryt
370
376
add_goto (guard, loop_head);
371
377
}
372
378
};
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
+ }
373
422
}
374
423
375
424
void generate_processor_body (symbol_tablet &st, goto_programt &body,
@@ -387,9 +436,9 @@ void generate_processor_body(symbol_tablet &st, goto_programt &body,
387
436
factory.add_index_goto ();
388
437
}
389
438
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 );
393
442
body.compute_loop_numbers ();
394
443
body.update ();
395
444
}
0 commit comments