|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Dynamic frame condition checking |
| 4 | +
|
| 5 | +Author: Qinheping Hu, [email protected] |
| 6 | +
|
| 7 | +Author: Remi Delmas, [email protected] |
| 8 | +
|
| 9 | +Date: March 2023 |
| 10 | +
|
| 11 | +\*******************************************************************/ |
| 12 | + |
| 13 | +#include "dfcc_loop_nesting_graph.h" |
| 14 | + |
| 15 | +#include <util/invariant.h> |
| 16 | +#include <util/message.h> |
| 17 | + |
| 18 | +dfcc_loop_nesting_graph_nodet::dfcc_loop_nesting_graph_nodet( |
| 19 | + const goto_programt::targett &head, |
| 20 | + const goto_programt::targett &latch, |
| 21 | + const loopt &instructions) |
| 22 | + : head(head), latch(latch), instructions(instructions) |
| 23 | +{ |
| 24 | +} |
| 25 | + |
| 26 | +/// \pre Loop normal form properties must hold. |
| 27 | +dfcc_loop_nesting_grapht |
| 28 | +build_loop_nesting_graph(goto_programt &goto_program, messaget &log) |
| 29 | +{ |
| 30 | + natural_loops_mutablet natural_loops(goto_program); |
| 31 | + dfcc_loop_nesting_grapht loop_nesting_graph; |
| 32 | + // Add a node per natural loop |
| 33 | + for(auto &loop : natural_loops.loop_map) |
| 34 | + { |
| 35 | + auto loop_head = loop.first; |
| 36 | + auto &loop_instructions = loop.second; |
| 37 | + |
| 38 | + if(loop_instructions.size() <= 1) |
| 39 | + { |
| 40 | + // ignore single instruction loops of the form |
| 41 | + // LOOP: goto LOOP; |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + // Find the instruction that jumps back to `loop_head` and has the |
| 46 | + // highest GOTO location number, define it as the latch. |
| 47 | + auto loop_latch = loop_head; |
| 48 | + for(const auto &t : loop_instructions) |
| 49 | + { |
| 50 | + if( |
| 51 | + t->is_goto() && t->get_target() == loop_head && |
| 52 | + t->location_number > loop_latch->location_number) |
| 53 | + loop_latch = t; |
| 54 | + } |
| 55 | + |
| 56 | + DATA_INVARIANT(loop_latch != loop_head, "Natural loop latch is found"); |
| 57 | + |
| 58 | + loop_nesting_graph.add_node(loop_head, loop_latch, loop_instructions); |
| 59 | + } |
| 60 | + |
| 61 | + // Add edges to the graph, pointing from inner loop to outer loops. |
| 62 | + // An `inner` will be considered nested in `outer` iff both its head |
| 63 | + // and latch instructions are instructions of `outer`. |
| 64 | + for(size_t outer = 0; outer < loop_nesting_graph.size(); ++outer) |
| 65 | + { |
| 66 | + const auto &outer_instructions = loop_nesting_graph[outer].instructions; |
| 67 | + |
| 68 | + for(size_t inner = 0; inner < loop_nesting_graph.size(); ++inner) |
| 69 | + { |
| 70 | + if(inner == outer) |
| 71 | + continue; |
| 72 | + |
| 73 | + if(outer_instructions.contains(loop_nesting_graph[inner].head)) |
| 74 | + { |
| 75 | + loop_nesting_graph.add_edge(inner, outer); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + auto topsorted_size = loop_nesting_graph.topsort().size(); |
| 81 | + INVARIANT( |
| 82 | + topsorted_size == loop_nesting_graph.size(), |
| 83 | + "loop_nesting_graph must be acyclic"); |
| 84 | + |
| 85 | + return loop_nesting_graph; |
| 86 | +} |
0 commit comments