| 1 |
(* |
| 2 |
* This module includes some very basic CFG transformations. |
| 3 |
* |
| 4 |
* -- Allen |
| 5 |
*) |
| 6 |
signature CFG_UTIL = |
| 7 |
sig |
| 8 |
|
| 9 |
structure CFG : CONTROL_FLOW_GRAPH |
| 10 |
|
| 11 |
(*======================================================================= |
| 12 |
* Get a label from a block |
| 13 |
*=======================================================================*) |
| 14 |
val labelOf : CFG.cfg -> Graph.node_id -> Label.label |
| 15 |
|
| 16 |
(*======================================================================= |
| 17 |
* Update the label of the branch instruction in a block |
| 18 |
* to be consistent with the control flow edges. |
| 19 |
* This is an NOP if the CFG is already consistent. |
| 20 |
*=======================================================================*) |
| 21 |
val updateJumpLabel : CFG.cfg -> Graph.node_id -> unit |
| 22 |
|
| 23 |
(*======================================================================= |
| 24 |
* Copy an edge |
| 25 |
*=======================================================================*) |
| 26 |
val copyEdge : CFG.edge_info -> CFG.edge_info |
| 27 |
|
| 28 |
(*======================================================================= |
| 29 |
* Merge a control flow edge. Return false if merging is unsuccessful. |
| 30 |
*=======================================================================*) |
| 31 |
val mergeEdge : CFG.cfg -> CFG.edge -> bool |
| 32 |
|
| 33 |
(*======================================================================= |
| 34 |
* Eliminate the jump (insert a jump) |
| 35 |
* at the end of the current block if it is feasible. |
| 36 |
* Return true iff it is successful. |
| 37 |
*=======================================================================*) |
| 38 |
val eliminateJump : CFG.cfg -> Graph.node_id -> bool |
| 39 |
val insertJump : CFG.cfg -> Graph.node_id -> bool |
| 40 |
|
| 41 |
(*======================================================================= |
| 42 |
* Split a control flow edge, return a new edge and the new block. |
| 43 |
* If the jump flag is true, then a jump is always placed in the |
| 44 |
* new block; otherwise, we try to eliminate the jump when feasible. |
| 45 |
*=======================================================================*) |
| 46 |
val splitEdge : CFG.cfg -> |
| 47 |
{ kind : CFG.block_kind, |
| 48 |
edge : CFG.edge, |
| 49 |
jump : bool |
| 50 |
} -> |
| 51 |
{ edge : CFG.edge, |
| 52 |
node : CFG.node |
| 53 |
} |
| 54 |
(*======================================================================= |
| 55 |
* Test if an edge is critical. An edge i->j is critical iff |
| 56 |
* there are multiple entries into j and multiple exits out of i, |
| 57 |
* i.e. it is both a merge and a split node. |
| 58 |
*=======================================================================*) |
| 59 |
val isMerge : CFG.cfg -> Graph.node_id -> bool |
| 60 |
val isSplit : CFG.cfg -> Graph.node_id -> bool |
| 61 |
val hasSideExits : CFG.cfg -> Graph.node_id -> bool |
| 62 |
val isCriticalEdge : CFG.cfg -> CFG.edge -> bool |
| 63 |
|
| 64 |
(*======================================================================= |
| 65 |
* Split all critical edges in the CFG. |
| 66 |
* This may introduce extra jumps into the program. |
| 67 |
*=======================================================================*) |
| 68 |
val splitAllCriticalEdges : CFG.cfg -> unit |
| 69 |
|
| 70 |
(*======================================================================= |
| 71 |
* Check whether two blocks are necessary connected. |
| 72 |
* Blocks i and j are connected iff i must be layout before j. |
| 73 |
*=======================================================================*) |
| 74 |
val mustPreceed : CFG.cfg -> Graph.node_id * Graph.node_id -> bool |
| 75 |
|
| 76 |
(*======================================================================= |
| 77 |
* Tail duplicate a region until it only has a single entry. |
| 78 |
* Return the set of new nodes and new edges. The region is represented |
| 79 |
* as a subgraph view. |
| 80 |
*=======================================================================*) |
| 81 |
val tailDuplicate : CFG.cfg -> |
| 82 |
{ subgraph : CFG.cfg, |
| 83 |
root : Graph.node_id |
| 84 |
} -> |
| 85 |
{ nodes : CFG.node list, |
| 86 |
edges : CFG.edge list |
| 87 |
} |
| 88 |
|
| 89 |
(*======================================================================= |
| 90 |
* Remove all unreachable code |
| 91 |
*=======================================================================*) |
| 92 |
val removeUnreachableCode : CFG.cfg -> unit |
| 93 |
|
| 94 |
(*======================================================================= |
| 95 |
* Try to merge all edges |
| 96 |
*=======================================================================*) |
| 97 |
val mergeAllEdges : CFG.cfg -> unit |
| 98 |
|
| 99 |
end |
| 100 |
|