| 1 |
(* cfg.sig |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2001 Bell Labs, Lucent Technologies |
| 4 |
* |
| 5 |
* Control flow graph data structure used by the MLRISC IR. |
| 6 |
* All basic optimizations are based on this representation. |
| 7 |
* |
| 8 |
* -- Allen |
| 9 |
*) |
| 10 |
|
| 11 |
signature CONTROL_FLOW_GRAPH = |
| 12 |
sig |
| 13 |
|
| 14 |
structure P : PSEUDO_OPS |
| 15 |
structure I : INSTRUCTIONS |
| 16 |
|
| 17 |
(* used to represent frequency of execution; we use reals because some |
| 18 |
* static prediction methods produce such. |
| 19 |
*) |
| 20 |
type weight = real |
| 21 |
|
| 22 |
datatype block_kind = |
| 23 |
START (* entry node *) |
| 24 |
| STOP (* exit node *) |
| 25 |
| NORMAL (* normal node *) |
| 26 |
|
| 27 |
(* |
| 28 |
* NOTE 1: the instructions are listed in reverse order. |
| 29 |
* This choice is for a few reasons: |
| 30 |
* |
| 31 |
* i) Clusters represent instructions in reverse order, so keeping this |
| 32 |
* the same avoid having to do conversions. |
| 33 |
* |
| 34 |
* ii) This makes it easier to add instructions at the end of the block, |
| 35 |
* which is more common than adding instructions to the front. |
| 36 |
* |
| 37 |
* iii) This also makes it easier to manipulate the branch/jump instruction |
| 38 |
* at the end of the block. |
| 39 |
* |
| 40 |
* NOTE 2: |
| 41 |
* Alignment always appear before labels in a block. |
| 42 |
*) |
| 43 |
|
| 44 |
and block = |
| 45 |
BLOCK of |
| 46 |
{ id : int, (* block id *) |
| 47 |
kind : block_kind, (* block kind *) |
| 48 |
freq : weight ref, (* execution frequency *) |
| 49 |
labels : Label.label list ref, (* labels on blocks *) |
| 50 |
insns : I.instruction list ref, (* in rev order *) |
| 51 |
align : P.pseudo_op option ref, (* alignment only *) |
| 52 |
annotations : Annotations.annotations ref (* annotations *) |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
(* We have the following invariant on blocks and out-edge kinds: |
| 57 |
* |
| 58 |
* If the last instruction of the block is an unconditional jump, then |
| 59 |
* there is one out edge labeled with JUMP. |
| 60 |
* |
| 61 |
* If the last instruction of the block is a conditional jump, then |
| 62 |
* there are two out edges. The one corresponding to the jump is |
| 63 |
* labeled BRANCH(true) and the other is labeled BRANCH(false). |
| 64 |
* |
| 65 |
* If the last instruction of the block is not a jump, then there is |
| 66 |
* one out edge labeled with FALLSTHRU. |
| 67 |
* |
| 68 |
* If the block ends with a switch, then the out edges are labeled with |
| 69 |
* SWITCH. |
| 70 |
* |
| 71 |
* If the block ends with a call that has been wrapped with a FLOW_TO, |
| 72 |
* then there will be one FALLSTHRU out edges and one or more FLOWSTO |
| 73 |
* out edges. |
| 74 |
* |
| 75 |
* Control-flow to outside the CFG is represented by edges to the unique |
| 76 |
* STOP node. When such edges are to labels that are defined outside |
| 77 |
* the CFG, then JUMP, BRANCH, or SWITCH edges are used (as appropriate). |
| 78 |
* When such edges are to unkonwn places (e.g., traps, returns, and |
| 79 |
* indirect jumps), then an EXIT edge is used. There should never be |
| 80 |
* a FALLSTHRU or ENTRY edge to the STOP node. |
| 81 |
*) |
| 82 |
and edge_kind (* edge kinds *) |
| 83 |
= ENTRY (* edge from START node *) |
| 84 |
| EXIT (* unlabeled edge to STOP node *) |
| 85 |
| JUMP (* unconditional jump *) |
| 86 |
| FALLSTHRU (* falls through to next block *) |
| 87 |
| BRANCH of bool (* branch *) |
| 88 |
| SWITCH of int (* computed goto *) |
| 89 |
| FLOWSTO (* FLOW_TO edge *) |
| 90 |
|
| 91 |
and edge_info = EDGE of { |
| 92 |
k : edge_kind, (* edge kind *) |
| 93 |
w : weight ref, (* edge freq *) |
| 94 |
a : Annotations.annotations ref (* annotations *) |
| 95 |
} |
| 96 |
|
| 97 |
type edge = edge_info Graph.edge |
| 98 |
type node = block Graph.node |
| 99 |
|
| 100 |
datatype info = |
| 101 |
INFO of |
| 102 |
{ annotations : Annotations.annotations ref, |
| 103 |
firstBlock : int ref, (* id of first block (UNUSED?) *) |
| 104 |
reorder : bool ref, (* has the CFG been reordered? *) |
| 105 |
data : P.pseudo_op list ref, (* reverse order of generation *) |
| 106 |
decls : P.pseudo_op list ref (* pseudo-ops before first section *) |
| 107 |
} |
| 108 |
|
| 109 |
type cfg = (block,edge_info,info) Graph.graph |
| 110 |
|
| 111 |
(*======================================================================== |
| 112 |
* |
| 113 |
* Various kinds of annotations on basic blocks |
| 114 |
* |
| 115 |
*========================================================================*) |
| 116 |
val LIVEOUT : I.C.cellset Annotations.property |
| 117 |
(* escaping live out information *) |
| 118 |
val CHANGED : (string * (unit -> unit)) Annotations.property |
| 119 |
|
| 120 |
(*======================================================================== |
| 121 |
* |
| 122 |
* Methods for manipulating basic blocks |
| 123 |
* |
| 124 |
*========================================================================*) |
| 125 |
val newBlock : int * weight ref -> block (* new empty block *) |
| 126 |
val newNode : cfg -> weight -> node (* new empty block hooked *) |
| 127 |
(* into the cfg *) |
| 128 |
val newStart : int * weight ref -> block (* start node *) |
| 129 |
val newStop : int * weight ref -> block (* stop node *) |
| 130 |
val copyBlock : int * block -> block (* copy a block *) |
| 131 |
val defineLabel : block -> Label.label (* define a label *) |
| 132 |
val insns : block -> I.instruction list ref |
| 133 |
val freq : block -> weight ref |
| 134 |
val edgeFreq : edge -> weight ref |
| 135 |
val sumEdgeFreqs : edge list -> weight |
| 136 |
val branchOf : edge_info -> bool option |
| 137 |
|
| 138 |
(* emit assembly *) |
| 139 |
val emit : Annotations.annotations -> block -> unit |
| 140 |
|
| 141 |
(*======================================================================== |
| 142 |
* |
| 143 |
* Methods for manipulating CFG |
| 144 |
* |
| 145 |
*========================================================================*) |
| 146 |
val cfg : info -> cfg (* create a new cfg *) |
| 147 |
val new : unit -> cfg (* create a new cfg *) |
| 148 |
val subgraph : cfg -> cfg (* mark as subgraph *) |
| 149 |
val init : cfg -> unit (* add start/stop nodes *) |
| 150 |
val changed : cfg -> unit (* mark cfg as changed *) |
| 151 |
(* IMPORTANT note: you MUST call this function after |
| 152 |
* changing the topology of the CFG. |
| 153 |
*) |
| 154 |
|
| 155 |
val annotations : cfg -> Annotations.annotations ref |
| 156 |
val liveOut : block -> I.C.cellset |
| 157 |
val fallsThruFrom : cfg * Graph.node_id -> Graph.node_id option |
| 158 |
val fallsThruTo : cfg * Graph.node_id -> Graph.node_id option |
| 159 |
val removeEdge : cfg -> edge -> unit |
| 160 |
val setBranch : cfg * Graph.node_id * bool -> I.instruction |
| 161 |
val edgeDir : edge_info Graph.edge -> bool option |
| 162 |
|
| 163 |
val entryId : cfg -> Graph.node_id (* the unique entry node ID *) |
| 164 |
val exitId : cfg -> Graph.node_id (* the unique exit node ID *) |
| 165 |
val entry : cfg -> node (* the unique entry node *) |
| 166 |
val exit : cfg -> node (* the unique exit node *) |
| 167 |
|
| 168 |
(*======================================================================= |
| 169 |
* |
| 170 |
* More complex methods for manipulating CFG. |
| 171 |
* These methods will guarantee all CFG invariants, like frequencies, |
| 172 |
* are preserved. |
| 173 |
* |
| 174 |
*=======================================================================*) |
| 175 |
|
| 176 |
(* get a label from block; generate one if necessary *) |
| 177 |
val labelOf : cfg -> Graph.node_id -> Label.label |
| 178 |
|
| 179 |
(* |
| 180 |
* Update the label of the branch instruction in a block |
| 181 |
* to be consistent with the control flow edges. |
| 182 |
* This is an NOP if the CFG is already consistent. |
| 183 |
* This is used internally after changing CFG edges, |
| 184 |
* but it could also be useful for others. |
| 185 |
*) |
| 186 |
val updateJumpLabel : cfg -> Graph.node_id -> unit |
| 187 |
|
| 188 |
(* |
| 189 |
* Deep copy an edge info |
| 190 |
*) |
| 191 |
val copyEdge : edge_info -> edge_info |
| 192 |
|
| 193 |
(* |
| 194 |
* Merge a control flow edge. |
| 195 |
* [See also the mustPreceed test below] |
| 196 |
* Return false if merging is unsuccessful. |
| 197 |
*) |
| 198 |
val mergeEdge : cfg -> edge -> bool |
| 199 |
|
| 200 |
(* |
| 201 |
* Eliminate the jump/insert a jump |
| 202 |
* at the end of the current block if it is feasible. |
| 203 |
* Return true iff it is successful. |
| 204 |
*) |
| 205 |
val eliminateJump : cfg -> Graph.node_id -> bool |
| 206 |
val insertJump : cfg -> Graph.node_id -> bool |
| 207 |
|
| 208 |
(* |
| 209 |
* Edge splitting: |
| 210 |
* |
| 211 |
* Split n groups of control flow edges, all initially entering block j, |
| 212 |
* |
| 213 |
* i_11 -> j, i_12 -> j, ... group 1 |
| 214 |
* i_21 -> j, i_22 -> j, ... group 2 |
| 215 |
* .... |
| 216 |
* i_n1 -> j, i_n2 -> j, ... group n |
| 217 |
* |
| 218 |
* into |
| 219 |
* |
| 220 |
* i_11 -> k_1 |
| 221 |
* i_12 -> k_1 |
| 222 |
* ... |
| 223 |
* i_21 -> k_2 |
| 224 |
* i_22 -> k_2 |
| 225 |
* ... |
| 226 |
* i_n1 -> k_n |
| 227 |
* i_n2 -> k_n |
| 228 |
* ... |
| 229 |
* |
| 230 |
* and the chain |
| 231 |
* k_1 -> k_2 |
| 232 |
* k_2 -> k_3 |
| 233 |
* ... |
| 234 |
* k_n -> j |
| 235 |
* |
| 236 |
* where k_1, ..., k_n are new basic blocks. |
| 237 |
* |
| 238 |
* Return the new edges |
| 239 |
* k_1-> k_2, ..., k_n -> j |
| 240 |
* |
| 241 |
* and the new blocks |
| 242 |
* k_1, ..., k_n. |
| 243 |
* |
| 244 |
* Each block k_1, ..., k_n can have instructions placed in them. |
| 245 |
* |
| 246 |
* If the jump flag is true, then a jump is always placed in the |
| 247 |
* new block k_n; otherwise, we try to eliminate the jump when feasible. |
| 248 |
*) |
| 249 |
val splitEdges : cfg -> |
| 250 |
{ groups : (edge list * |
| 251 |
I.instruction list (* reverse order *) |
| 252 |
) list, |
| 253 |
jump : bool |
| 254 |
} -> (node * edge) list (* k_i and k_i -> k_{i+1} *) |
| 255 |
|
| 256 |
(* |
| 257 |
* Test if an edge is critical. An edge i->j is critical iff |
| 258 |
* there are multiple entries into j and multiple exits out of i, |
| 259 |
* i.e. it is both a merge and a split node. |
| 260 |
* |
| 261 |
* Test if a node is a merge or split node. |
| 262 |
*) |
| 263 |
val isCriticalEdge : cfg -> edge -> bool |
| 264 |
val isMerge : cfg -> Graph.node_id -> bool |
| 265 |
val isSplit : cfg -> Graph.node_id -> bool |
| 266 |
|
| 267 |
|
| 268 |
(* |
| 269 |
* Split all critical edges in the CFG. |
| 270 |
* This may introduce extra jumps into the program. |
| 271 |
*) |
| 272 |
val splitAllCriticalEdges : cfg -> unit |
| 273 |
|
| 274 |
(* |
| 275 |
* Check whether two blocks are necessary connected. |
| 276 |
* Blocks i and j are connected iff i must be placed before j |
| 277 |
* in all feasible layouts.. |
| 278 |
*) |
| 279 |
val mustPreceed : cfg -> Graph.node_id * Graph.node_id -> bool |
| 280 |
|
| 281 |
(* |
| 282 |
* Try to merge all edges |
| 283 |
*) |
| 284 |
val mergeAllEdges : cfg -> unit |
| 285 |
|
| 286 |
(*======================================================================== |
| 287 |
* |
| 288 |
* For viewing |
| 289 |
* |
| 290 |
*========================================================================*) |
| 291 |
(***** |
| 292 |
val viewStyle : cfg -> (block,edge_info,info) GraphLayout.style |
| 293 |
val viewLayout : cfg -> GraphLayout.layout |
| 294 |
val headerText : block -> string |
| 295 |
val footerText : block -> string |
| 296 |
val subgraphLayout : { cfg : cfg, subgraph : cfg } -> GraphLayout.layout |
| 297 |
*****) |
| 298 |
|
| 299 |
(*======================================================================== |
| 300 |
* |
| 301 |
* Miscellaneous stuff |
| 302 |
* |
| 303 |
*========================================================================*) |
| 304 |
val cdgEdge : edge_info -> bool (* for building a CDG *) |
| 305 |
|
| 306 |
(*======================================================================== |
| 307 |
* |
| 308 |
* Methods for printing CFGs |
| 309 |
* |
| 310 |
*========================================================================*) |
| 311 |
val kindName : block_kind -> string |
| 312 |
val show_block : Annotations.annotations -> block -> string |
| 313 |
val show_edge : edge_info -> string |
| 314 |
val dumpBlock : (TextIO.outstream * cfg) -> node -> unit |
| 315 |
val dump : (TextIO.outstream * string * cfg) -> unit |
| 316 |
|
| 317 |
end |
| 318 |
|