| 1 |
\section{The MLRISC IR} |
| 2 |
\subsection{Introduction} |
| 3 |
|
| 4 |
In this section we will describe the MLRISC intermediate representation. |
| 5 |
|
| 6 |
\subsubsection{Control Flow Graph} |
| 7 |
The control flow graph is the main view of the IR. |
| 8 |
A control flow graph satisfies the following signature: |
| 9 |
\begin{SML} |
| 10 |
signature \mlrischref{IR/mlrisc-cfg.sig}{CONTROL_FLOW_GRAPH} = sig |
| 11 |
structure I : INSTRUCTIONS |
| 12 |
structure P : PSEUDO_OPS |
| 13 |
structure C : CELLS |
| 14 |
structure W : FIXED_POINT |
| 15 |
sharing I.C = C |
| 16 |
|
| 17 |
\italics{definitions} |
| 18 |
end |
| 19 |
\end{SML} |
| 20 |
|
| 21 |
The following structures nested within a CFG: |
| 22 |
\begin{itemize} |
| 23 |
\item \sml{I : INSTRUCTIONS} is the instruction structure. |
| 24 |
\item \sml{P : PSEUDO_OPS} is the structure with the definition |
| 25 |
of pseudo ops. |
| 26 |
\item \sml{C : CELLS} is the cells structure describing the |
| 27 |
register conventions of the architecture. |
| 28 |
\item \sml{W : FIXED_POINT} is a structure that contains |
| 29 |
a fixed point type used in execution frequency annotations. |
| 30 |
\end{itemize} |
| 31 |
|
| 32 |
The type \sml{weight} below is used in execution frequency annotations: |
| 33 |
\begin{SML} |
| 34 |
type weight = W.fixed_point |
| 35 |
\end{SML} |
| 36 |
|
| 37 |
There are a few different kinds of basic blocks, described |
| 38 |
by the type \sml{block_kind} below: |
| 39 |
\begin{SML} |
| 40 |
datatype block_kind = |
| 41 |
START |
| 42 |
| STOP |
| 43 |
| FUNCTION_ENTRY |
| 44 |
| NORMAL |
| 45 |
| HYPERBLOCK |
| 46 |
\end{SML} |
| 47 |
|
| 48 |
A basic block is defined as the datatype \sml{block}, defined below: |
| 49 |
\begin{SML} |
| 50 |
and data = LABEL of Label.label |
| 51 |
| PSEUDO of P.pseudo_op |
| 52 |
|
| 53 |
and block = |
| 54 |
BLOCK of |
| 55 |
\{ id : int, |
| 56 |
kind : block_kind, |
| 57 |
name : B.name, |
| 58 |
freq : weight ref, |
| 59 |
data : data list ref, |
| 60 |
labels : Label.label list ref, |
| 61 |
insns : I.instruction list ref, |
| 62 |
annotations : Annotations.annotations ref |
| 63 |
\} |
| 64 |
\end{SML} |
| 65 |
|
| 66 |
Edges in a CFG are annotated with the type \sml{edge_info}, |
| 67 |
defined below: |
| 68 |
\begin{SML} |
| 69 |
and edge_kind = ENTRY |
| 70 |
| EXIT |
| 71 |
| JUMP |
| 72 |
| FALLSTHRU |
| 73 |
| BRANCH of bool |
| 74 |
| SWITCH of int |
| 75 |
| SIDEEXIT of int |
| 76 |
|
| 77 |
and edge_info = |
| 78 |
EDGE of \{ k : edge_kind, |
| 79 |
w : weight ref, |
| 80 |
a : Annotations.annotations ref |
| 81 |
\} |
| 82 |
\end{SML} |
| 83 |
|
| 84 |
Type \sml{cfg} below defines a control flow graph: |
| 85 |
\begin{SML} |
| 86 |
type edge = edge_info edge |
| 87 |
type node = block node |
| 88 |
|
| 89 |
datatype info = |
| 90 |
INFO of \{ regmap : C.regmap, |
| 91 |
annotations : Annotations.annotations ref, |
| 92 |
firstBlock : int ref, |
| 93 |
reorder : bool ref |
| 94 |
\} |
| 95 |
type cfg = (block,edge_info,info) graph |
| 96 |
\end{SML} |
| 97 |
|
| 98 |
\subsubsection{Low-level Interface} |
| 99 |
The following subsection describes the low-level interface to a CFG. |
| 100 |
These functions should be used with care since they do not |
| 101 |
always maintain high-level structural invariants imposed on |
| 102 |
the representation. In general, higher level interfaces exist |
| 103 |
so knowledge of this interface is usually not necessary for customizing |
| 104 |
MLRISC. |
| 105 |
|
| 106 |
Various kinds of annotations on basic blocks are defined below: |
| 107 |
\begin{SML} |
| 108 |
exception LIVEOUT of C.cellset |
| 109 |
exception CHANGED of unit -> unit |
| 110 |
exception CHANGEDONCE of unit -> unit |
| 111 |
\end{SML} |
| 112 |
The annotation \sml{LIVEOUT} is used record live-out information |
| 113 |
on an escaping block. |
| 114 |
The annotations \sml{CHANGED} and \sml{CHANGEDONCE} are used |
| 115 |
internally for maintaining views on a CFG. These should not be used |
| 116 |
directly. |
| 117 |
|
| 118 |
The following are low-level functions for building new basic blocks. |
| 119 |
The functions \sml{new}\emph{XXX} build empty basic blocks of a specific |
| 120 |
type. The function \sml{defineLabel} returns a label to a basic block; |
| 121 |
and if one does not exist then a new label will be generated automatically. |
| 122 |
The functions \sml{emit} and \sml{show_block} are low-level |
| 123 |
routines for displaying a basic block. |
| 124 |
\begin{SML} |
| 125 |
val newBlock : int * B.name -> block |
| 126 |
val newStart : int -> block |
| 127 |
val newStop : int -> block |
| 128 |
val newFunctionEntry : int -> block |
| 129 |
val copyBlock : int * block -> block |
| 130 |
val defineLabel : block -> Label.label |
| 131 |
val emit : C.regmap -> block -> unit |
| 132 |
val show_block : C.regmap -> block -> string |
| 133 |
\end{SML} |
| 134 |
|
| 135 |
Methods for building a CFG are listed as follows: |
| 136 |
\begin{SML} |
| 137 |
val cfg : info -> cfg |
| 138 |
val new : C.regmap -> cfg |
| 139 |
val subgraph : cfg -> cfg |
| 140 |
val init : cfg -> unit |
| 141 |
val changed : cfg -> unit |
| 142 |
val removeEdge : cfg -> edge -> unit |
| 143 |
\end{SML} |
| 144 |
Again, these methods should be used only with care. |
| 145 |
|
| 146 |
The following functions allow the user to extract low-level information |
| 147 |
from a flowgraph. Function \sml{regmap} returns the current register map. |
| 148 |
Function \sml{regmap} returns a function that lookups the current register |
| 149 |
map. Function \sml{liveOut} returns liveOut information from a block; |
| 150 |
it returns the empty cellset if the block is not an escaping block. |
| 151 |
Function \sml{fallsThruFrom} takes a node id $v$ and locates the |
| 152 |
block $u$ (if any) that flows into $v$ without going through a branch |
| 153 |
instruction. Similarly, the function \sml{fallsThruTo} takes |
| 154 |
a node id $u$ and locates the block (if any) that $u$ flows into |
| 155 |
with going through a branch instruction. If $u$ falls through to |
| 156 |
$v$ in any feasible code layout $u$ must preceed $v$. |
| 157 |
\begin{SML} |
| 158 |
val regmap : cfg -> C.regmap |
| 159 |
val reglookup : cfg -> C.register -> C.register |
| 160 |
val liveOut : block -> C.cellset |
| 161 |
val fallsThruFrom : cfg * node_id -> node_id option |
| 162 |
val fallsThruTo : cfg * node_id -> node_id option |
| 163 |
\end{SML} |
| 164 |
|
| 165 |
To support graph viewing of a CFG, the following low-level |
| 166 |
primitives are provided: |
| 167 |
\begin{SML} |
| 168 |
val viewStyle : cfg -> (block,edge_info,info) GraphLayout.style |
| 169 |
val viewLayout : cfg -> GraphLayout.layout |
| 170 |
val headerText : block -> string |
| 171 |
val footerText : block -> string |
| 172 |
val subgraphLayout : { cfg : cfg, subgraph : cfg } -> GraphLayout.layout |
| 173 |
\end{SML} |
| 174 |
|
| 175 |
Finally, a miscellany function for control dependence graph building. |
| 176 |
\begin{SML} |
| 177 |
val cdgEdge : edge_info -> bool |
| 178 |
\end{SML} |
| 179 |
|
| 180 |
\subsubsection{IR} |
| 181 |
The MLRISC intermediate representation is a composite |
| 182 |
view of various compiler data structures, including the control |
| 183 |
flow graph, (post-)dominator trees, control dependence graph, and |
| 184 |
loop nesting tree. Basic compiler optimizations in MLRISC |
| 185 |
operate on this data structure; advance optimizations |
| 186 |
operate on more complex representations which use this |
| 187 |
representation as the base layer. |
| 188 |
\begin{wrapfigure}{r}{4.5in} |
| 189 |
\begin{Boxit} |
| 190 |
% \psfig{figure=../pictures/eps/mlrisc-IR.eps,width=4.5in} |
| 191 |
\includegraphics[width=4.5in]{../pictures/pdf/mlrisc-IR} |
| 192 |
\end{Boxit} |
| 193 |
\caption{The MLRISC IR} |
| 194 |
\end{wrapfigure} |
| 195 |
|
| 196 |
This IR provides a few additional functionalities: |
| 197 |
\begin{itemize} |
| 198 |
\item Edge frequencies -- execution frequencies |
| 199 |
are maintained on all control flow edges. |
| 200 |
\item Extensible annotations -- semantics information can be |
| 201 |
represented as annotations on the graph. |
| 202 |
\item Multiple facets -- |
| 203 |
Facets are high-level views that automatically keep themselves |
| 204 |
up-to-date. Computed facets are cached and out-of-date facets |
| 205 |
are recomputed by demand. |
| 206 |
The IR defines a mechanism to attach multiple facets to the IR. |
| 207 |
\end{itemize} |
| 208 |
|
| 209 |
The signature of the IR is listed below |
| 210 |
\begin{SML} |
| 211 |
signature \mlrischref{IR/mlrisc-ir.sig}{MLRISC_IR} = sig |
| 212 |
structure I : INSTRUCTIONS |
| 213 |
structure CFG : CONTROL_FLOW_GRAPH |
| 214 |
structure Dom : DOMINATOR_TREE |
| 215 |
structure CDG : CONTROL_DEPENDENCE_GRAPH |
| 216 |
structure Loop : LOOP_STRUCTURE |
| 217 |
structure Util : CFG_UTIL |
| 218 |
sharing Util.CFG = CFG |
| 219 |
sharing CFG.I = I |
| 220 |
sharing Loop.Dom = CDG.Dom = Dom |
| 221 |
|
| 222 |
type cfg = CFG.cfg |
| 223 |
type IR = CFG.cfg |
| 224 |
type dom = (CFG.block,CFG.edge_info,CFG.info) Dom.dominator_tree |
| 225 |
type pdom = (CFG.block,CFG.edge_info,CFG.info) Dom.postdominator_tree |
| 226 |
type cdg = (CFG.block,CFG.edge_info,CFG.info) CDG.cdg |
| 227 |
type loop = (CFG.block,CFG.edge_info,CFG.info) Loop.loop_structure |
| 228 |
|
| 229 |
val dom : IR -> dom |
| 230 |
val pdom : IR -> pdom |
| 231 |
val cdg : IR -> cdg |
| 232 |
val loop : IR -> loop |
| 233 |
|
| 234 |
val changed : IR -> unit |
| 235 |
val memo : (IR -> 'facet) -> IR -> 'facet |
| 236 |
val addLayout : string -> (IR -> GraphLayout.layout) -> unit |
| 237 |
val view : string -> IR -> unit |
| 238 |
val views : string list -> IR -> unit |
| 239 |
val viewSubgraph : IR -> cfg -> unit |
| 240 |
end |
| 241 |
\end{SML} |
| 242 |
|
| 243 |
The following facets are predefined: dominator, post-dominator tree, |
| 244 |
control dependence graph and loop nesting structure. |
| 245 |
The functions \sml{dom}, \sml{pdom}, \sml{cdg}, \sml{loop} |
| 246 |
are \newdef{facet extraction} methods that |
| 247 |
compute up-to-date views of these facets. |
| 248 |
|
| 249 |
The following protocol is used for facets: |
| 250 |
\begin{itemize} |
| 251 |
\item When the IR is changed, |
| 252 |
the function \sml{changed} should be called to |
| 253 |
signal that all facets attached to the IR should be updated. |
| 254 |
\item To add a new facet of type \sml{F} that is computed by demand, |
| 255 |
the programmer has to provide a facet construction |
| 256 |
function \sml{f : IR -> F}. Call the function \sml{mem} |
| 257 |
to register the new facet. For example, let \sml{val g = memo f}. |
| 258 |
Then the function \sml{g} can be used to as a new facet extraction |
| 259 |
function for facet \sml{F}. |
| 260 |
\item To register a graph viewing function, call |
| 261 |
the function \sml{addLayout} and provide an appropriate |
| 262 |
graph layout function. For example, we can say |
| 263 |
\sml{addLayout "F" layoutF} to register a graph layout function |
| 264 |
for a facet called ``F''. |
| 265 |
\end{itemize} |
| 266 |
|
| 267 |
To view an IR, the functions \sml{view}, \sml{views} or |
| 268 |
\sml{viewSubgraph} can be used. They have the following interpretation: |
| 269 |
\begin{itemize} |
| 270 |
\item \sml{view} computes a layout for one facet of the IR and displays |
| 271 |
it. The predefined facets are called |
| 272 |
``dom'', ``pdom'', ``cdg'', ``loop.'' The IR can be |
| 273 |
viewed as the facet ``cfg.'' In addition, there is a layout |
| 274 |
named ``doms'' which displays the dominator tree and the post-dominator |
| 275 |
tree together, with the post-dominator inverted. |
| 276 |
\item \sml{views} computes a set of facets and displays it together |
| 277 |
in one single picture. |
| 278 |
\item \sml{viewSubgraph} layouts a subgraph of the IR. |
| 279 |
This creates a picture with the subgraph highlighted and embedded |
| 280 |
in the whole IR. |
| 281 |
\end{itemize} |
| 282 |
|
| 283 |
\subsubsection{Building a CFG} |
| 284 |
|
| 285 |
There are two basic methods of building a CFG: |
| 286 |
\begin{itemize} |
| 287 |
\item convert a sequence of machine instructions |
| 288 |
into a CFG through the emitter interface, described below, and |
| 289 |
\item convert it from a \newdef{cluster}, which is |
| 290 |
the basic linearized representation used in the MLRISC system. |
| 291 |
\end{itemize} |
| 292 |
The first method requires you to perform instruction selection |
| 293 |
from a compiler front-end, but allows you to bypass all other |
| 294 |
MLRISC phases if desired. The second method allows you |
| 295 |
to take advantage of various MLRISC's instruction selection modules |
| 296 |
currently available. We describe these methods in this section. |
| 297 |
|
| 298 |
\paragraph{Directly from Instructions} |
| 299 |
Signature \sml{CODE_EMITTER} below describes an abstract emitter interface |
| 300 |
for accepting a linear stream of instructions from a source |
| 301 |
and perform a sequence of actions based on this |
| 302 |
stream\footnote{Unlike the signature {\tt EMITTER\_NEW} or |
| 303 |
{\tt FLOWGRAPH\_GEN}, it has the advantage that it is not |
| 304 |
tied into any form of specific flowgraph representation.}. |
| 305 |
|
| 306 |
\begin{SML} |
| 307 |
signature \mlrischref{extensions/code-emitter.sig}{CODE_EMITTER} = sig |
| 308 |
structure I : INSTRUCTIONS |
| 309 |
structure C : CELLS |
| 310 |
structure P : PSEUDO_OPS |
| 311 |
sharing I.C = C |
| 312 |
|
| 313 |
type emitter = |
| 314 |
\{ defineLabel : Label.label -> unit, |
| 315 |
entryLabel : Label.label -> unit, |
| 316 |
exitBlock : C.cellset -> unit, |
| 317 |
pseudoOp : P.pseudo_op -> unit, |
| 318 |
emitInstr : I.instruction -> unit, |
| 319 |
comment : string -> unit, |
| 320 |
init : int -> unit, |
| 321 |
finish : unit -> unit |
| 322 |
\} |
| 323 |
end |
| 324 |
\end{SML} |
| 325 |
|
| 326 |
The code emitter interface has the following informal protocol. |
| 327 |
\begin{methods} |
| 328 |
init($n$) & Initializes the emitter and signals that |
| 329 |
the back-end should |
| 330 |
allocate space for $n$ bytes of machine code. |
| 331 |
The number is ignored for non-machine code back-ends. \\ |
| 332 |
defineLabel($l$) & Defines a new label $l$ at the current position.\\ |
| 333 |
entryLabel($l$) & Defines a new entry label $l$ at the current position. |
| 334 |
An entry label defines an entry point into the current flow graph. |
| 335 |
Note that multiple entry points are allowed\\ |
| 336 |
exitBlock($c$) & Defines an exit at the current position. |
| 337 |
The cellset $c$ represents the live-out information \\ |
| 338 |
pseudOp($p$) & Emits an pseudo op $p$ at the current position \\ |
| 339 |
emitInstr($i$) & Emits an instruction $i$ at the current position \\ |
| 340 |
blockName($b$) & Changes the block name to $b$ \\ |
| 341 |
comment($msg$) & Emits a comment $msg$ at the current position \\ |
| 342 |
finish & Signals that the use of the emitter is finished. |
| 343 |
The emitter is free to perform its post-processing functions. |
| 344 |
When this is finished the CFG is built. |
| 345 |
\end{methods} |
| 346 |
|
| 347 |
The functor \sml{ControlFlowGraphGen} below can be |
| 348 |
used to create a CFG builder that uses the \sml{CODE_EMITTER} interface. |
| 349 |
\begin{SML} |
| 350 |
signature \mlrischref{IR/mlrisc-cfg-gen.sig}{CONTROL_FLOW_GRAPH_GEN} = sig |
| 351 |
structure CFG : CONTROL_FLOW_GRAPH |
| 352 |
structure Emitter : CODE_EMITTER |
| 353 |
sharing Emitter.I = CFG.I |
| 354 |
sharing Emitter.P = CFG.P |
| 355 |
val emitter : CFG.cfg -> Emitter.emitter |
| 356 |
end |
| 357 |
functor \mlrischref{IR/mlrisc-cfg-gen.sml}{ControlFlowGraphGen} |
| 358 |
(structure CFG : CONTROL_FLOW_GRAPH |
| 359 |
structure Emitter : CODE_EMITTER |
| 360 |
structure P : INSN_PROPERTIES |
| 361 |
sharing CFG.I = Emitter.I = P.I |
| 362 |
sharing CFG.P = Emitter.P |
| 363 |
sharing CFG.B = Emitter.B |
| 364 |
) : CONTROL_FLOW_GRAPH_GEN |
| 365 |
\end{SML} |
| 366 |
|
| 367 |
\paragraph{Cluster to CFG} |
| 368 |
|
| 369 |
The core \MLRISC{} system implements many instruction selection |
| 370 |
front-ends. The result of an instruction selection module is a linear |
| 371 |
code layout block called a cluster. The functor \sml{Cluster2CFG} below |
| 372 |
generates a translator that translates a cluster into a CFG: |
| 373 |
\begin{SML} |
| 374 |
signature \mlrischref{IR/mlrisc-cluster2cfg.sig}{CLUSTER2CFG} = sig |
| 375 |
structure CFG : CONTROL_FLOW_GRAPH |
| 376 |
structure F : FLOWGRAPH |
| 377 |
sharing CFG.I = F.I |
| 378 |
sharing CFG.P = F.P |
| 379 |
sharing CFG.B = F.B |
| 380 |
val cluster2cfg : F.cluster -> CFG.cfg |
| 381 |
end |
| 382 |
functor \mlrischref{IR/mlrisc-cluster2cfg.sml}{Cluster2CFG} |
| 383 |
(structure CFG : CONTROL_FLOW_GRAPH |
| 384 |
structure F : FLOWGRAPH |
| 385 |
structure P : INSN_PROPERTIES |
| 386 |
sharing CFG.I = F.I = P.I |
| 387 |
sharing CFG.P = F.P |
| 388 |
sharing CFG.B = F.B |
| 389 |
) : CLUSTER2CFG |
| 390 |
\end{SML} |
| 391 |
|
| 392 |
\paragraph{CFG to Cluster} |
| 393 |
|
| 394 |
The basic \MLRISC{} system also implements many back-end functions |
| 395 |
such as register allocation, assembly output and machine code output. |
| 396 |
These modules all utilize the cluster representation. The |
| 397 |
functor \mlrischref{IR/mlrisc-cfg2cluster.sml}{CFG2Cluster} |
| 398 |
below generates a translator |
| 399 |
that converts a CFG into a cluster. With the previous functor, |
| 400 |
the CFG and the cluster presentation can be freely inter-converted. |
| 401 |
\begin{SML} |
| 402 |
signature \mlrischref{IR/mlrisc-cfg2cluster.sig}{CFG2CLUSTER} = sig |
| 403 |
structure CFG : CONTROL_FLOW_GRAPH |
| 404 |
structure F : FLOWGRAPH |
| 405 |
sharing CFG.I = F.I |
| 406 |
sharing CFG.P = F.P |
| 407 |
sharing CFG.B = F.B |
| 408 |
val cfg2cluster : { cfg : CFG.cfg, relayout : bool } -> F.cluster |
| 409 |
end |
| 410 |
functor \mlrischref{IR/mlrisc-cfg2cluster.sml}{CFG2Cluster} |
| 411 |
(structure CFG : CONTROL_FLOW_GRAPH |
| 412 |
structure F : FLOWGRAPH |
| 413 |
sharing CFG.I = F.I |
| 414 |
sharing CFG.P = F.P |
| 415 |
sharing CFG.B = F.B |
| 416 |
val patchBranch : {instr:CFG.I.instruction, backwards:bool} -> |
| 417 |
CFG.I.instruction list |
| 418 |
) : CFG2CLUSTER |
| 419 |
\end{SML} |
| 420 |
|
| 421 |
When a CFG originates from a cluster, we try to preserve |
| 422 |
the same code layout through out all optimizations when possible. |
| 423 |
The function \sml{cfg2cluster} takes an optional flag |
| 424 |
that specifies we should force the recomputation of |
| 425 |
the code layout of a control flow graph when translating a CFG |
| 426 |
back into a cluster. |
| 427 |
|
| 428 |
\subsubsection{Basic CFG Transformations} |
| 429 |
|
| 430 |
Basic CFG transformations are implemented in the functor |
| 431 |
\sml{CFGUtil}. These transformations include splitting edges, merging |
| 432 |
edges, removing unreachable code and tail duplication. |
| 433 |
\begin{SML} |
| 434 |
functor \mlrischref{IR/mlrisc-cfg-util.sml}{CFGUtil} |
| 435 |
(structure CFG : CONTROL_FLOW_GRAPH |
| 436 |
structure P : INSN_PROPERTIES |
| 437 |
sharing P.I = CFG.I |
| 438 |
) : CFG_UTIL |
| 439 |
\end{SML} |
| 440 |
|
| 441 |
The signature of \sml{CFGUtil} is defined below: |
| 442 |
\begin{SML} |
| 443 |
signature \mlrischref{IR/mlrisc-cfg-util.sig}{CFG_UTIL} = sig |
| 444 |
structure CFG : CONTROL_FLOW_GRAPH |
| 445 |
val updateJumpLabel : CFG.cfg -> node_id -> unit |
| 446 |
val mergeEdge : CFG.cfg -> CFG.edge -> bool |
| 447 |
val eliminateJump : CFG.cfg -> node_id -> bool |
| 448 |
val insertJump : CFG.cfg -> node_id -> bool |
| 449 |
val splitEdge : CFG.cfg -> { edge : CFG.edge, jump : bool } |
| 450 |
-> { edge : CFG.edge, node : CFG.node } |
| 451 |
val isMerge : CFG.cfg -> node_id -> bool |
| 452 |
val isSplit : CFG.cfg -> node_id -> bool |
| 453 |
val hasSideExits : CFG.cfg -> node_id -> bool |
| 454 |
val isCriticalEdge : CFG.cfg -> CFG.edge -> bool |
| 455 |
val splitAllCriticalEdges : CFG.cfg -> unit |
| 456 |
val ceed : CFG.cfg -> node_id * node_id -> bool |
| 457 |
val tailDuplicate : CFG.cfg -> \{ subgraph : CFG.cfg, root : node_id \} |
| 458 |
-> \{ nodes : CFG.node list, |
| 459 |
edges : CFG.edge list \} |
| 460 |
val removeUnreachableCode : CFG.cfg -> unit |
| 461 |
val mergeAllEdges : CFG.cfg -> unit |
| 462 |
end |
| 463 |
\end{SML} |
| 464 |
|
| 465 |
These functions have the following meanings: |
| 466 |
\begin{itemize} |
| 467 |
\item \sml{updateJumpLabel} $G u$. This function |
| 468 |
updates the label of the branch instruction in a block $u$ |
| 469 |
to be consistent with the control flow edges with source $u$. |
| 470 |
This is an nop if the CFG is already consistent. |
| 471 |
\item \sml{mergeEdge} $G e$. This function merges edge |
| 472 |
$e \equiv u \edge{} v$ |
| 473 |
in the graph $G$ if possible. This is successful only if |
| 474 |
there are no other edges flowing into $v$ and no other edges |
| 475 |
flowing out from $u$. It returns true if the merge |
| 476 |
operation is successful. If successful, the nodes $u$ and $v$ |
| 477 |
will be coalesced into the block $u$. The jump instruction (if any) |
| 478 |
in the node $u$ will also be elided. |
| 479 |
\item \sml{eliminateJump} $G u$. This function eliminate the |
| 480 |
jump instruction at the end of block $u$ if it is feasible. |
| 481 |
\item \sml{insertJump} $G u$. This function inserts a jump |
| 482 |
instruction in block $u$ if it is feasible. |
| 483 |
\item \sml{splitEdge} $G e$. This function |
| 484 |
split the control flow edge $e$, and return a new edge $e'$ and the |
| 485 |
new block $u$ as return values. It addition, it takes as |
| 486 |
argument a flag \sml{jump}. If this flag is true, |
| 487 |
then a jump instruction is always placed in the |
| 488 |
split; otherwise, we try to eliminate the jump when feasible. |
| 489 |
\item \sml{isMerge} $G u$. This function tests whether block $u$ |
| 490 |
is a \newdef{merge} node. A merge node is a node that |
| 491 |
has two or more incoming flow edges. |
| 492 |
\item \sml{isSplit} $G u$. This function tests whether block $u$ |
| 493 |
is a \newdef{split} node. A split node is a node that |
| 494 |
has two or more outgoing flow edges. |
| 495 |
\item \sml{hasSideExits} $G u$. This function tests whether |
| 496 |
a block has side exits $G$. This assumes that $u$ |
| 497 |
is a hyperblock. |
| 498 |
\item \sml{isCriticalEdge} $G e$. This function tests whether |
| 499 |
the edge $e$ is a \newdef{critical} edge. The |
| 500 |
edge $e \equiv u \edge{} v$ is critical iff |
| 501 |
there are $u$ is merge node and $v$ is a split node. |
| 502 |
\item \sml{splitAllCriticalEdges} $G$. This function goes |
| 503 |
through the CFG $G$ and splits |
| 504 |
all critical edges in the CFG. |
| 505 |
This can introduce extra jumps and basic blocks in the program. |
| 506 |
\item \sml{mustPreceed} $G (u,v)$. This function |
| 507 |
checks whether two blocks $u$ and $v$ are necessarily adjacent. |
| 508 |
Blocks $u$ and $v$ must be adjacent iff $u$ must preceed $v$ |
| 509 |
in any feasible code layout. |
| 510 |
\item \sml{tailDuplicate}. |
| 511 |
\begin{SML} |
| 512 |
val tailDuplicate : CFG.cfg -> \{ subgraph : CFG.cfg, root : node_id \} |
| 513 |
-> \{ nodes : CFG.node list, |
| 514 |
edges : CFG.edge list \} |
| 515 |
\end{SML} |
| 516 |
\begin{Figure} |
| 517 |
\begin{boxit} |
| 518 |
%\cpsfig{figure=../pictures/eps/tail-duplication.eps,width=3in} |
| 519 |
\begin{center} |
| 520 |
\includegraphics[width=3in]{../pictures/pdf/tail-duplication} |
| 521 |
\end{center}% |
| 522 |
\end{boxit} |
| 523 |
\label{fig:tail-duplication} |
| 524 |
\caption{Tail-duplication} |
| 525 |
\end{Figure} |
| 526 |
|
| 527 |
This function tail-duplicates the region \sml{subgraph} |
| 528 |
until it only has a single entry \sml{root}. |
| 529 |
Return the set of new nodes and new edges. |
| 530 |
The region is represented as a subgraph view of the CFG. |
| 531 |
Figure~\ref{fig:tail-duplication} illustrates |
| 532 |
this transformation. |
| 533 |
|
| 534 |
\item \sml{removeUnreachableCode} $G$. This function |
| 535 |
removes all unreachable code from the graph. |
| 536 |
\item \sml{mergeAllEdges} $G$. This function tries to merge all |
| 537 |
the edges in the flowgraph $G$. Merging is performed in the |
| 538 |
non-increasing order of edge frequencies. |
| 539 |
\end{itemize} |
| 540 |
|
| 541 |
\subsubsection{Dataflow Analysis} |
| 542 |
MLRISC provides a simple customizable module for performing |
| 543 |
iterative dataflow analysis. A dataflow analyzer |
| 544 |
has the following signature: |
| 545 |
|
| 546 |
\begin{SML} |
| 547 |
signature \mlrischref{IR/dataflow.sig}{DATAFLOW_ANALYZER} = sig |
| 548 |
structure CFG : CONTROL_FLOW_GRAPH |
| 549 |
type dataflow_info |
| 550 |
val analyze : CFG.cfg * dataflow_info -> dataflow_info |
| 551 |
end |
| 552 |
\end{SML} |
| 553 |
|
| 554 |
A dataflow problem is described by the signature \sml{DATAFLOW_PROBLEM}, |
| 555 |
described below: |
| 556 |
\begin{SML} |
| 557 |
signature \mlrischref{IR/dataflow.sig}{DATAFLOW_PROBLEM} = sig |
| 558 |
structure CFG : CONTROL_FLOW_GRAPH |
| 559 |
type domain |
| 560 |
type dataflow_info |
| 561 |
val forward : bool |
| 562 |
val bot : domain |
| 563 |
val == : domain * domain -> bool |
| 564 |
val join : domain list -> domain |
| 565 |
val prologue : CFG.cfg * dataflow_info -> |
| 566 |
CFG.block node -> |
| 567 |
\{ input : domain, |
| 568 |
output : domain, |
| 569 |
transfer : domain -> domain |
| 570 |
\} |
| 571 |
val epilogue : CFG.cfg * dataflow_info -> |
| 572 |
\{ node : CFG.block node, |
| 573 |
input : domain, |
| 574 |
output : domain |
| 575 |
\} -> unit |
| 576 |
end |
| 577 |
\end{SML} |
| 578 |
This description contains the following items |
| 579 |
\begin{itemize} |
| 580 |
\item \sml{type domain} is the abstract lattice domain $D$. |
| 581 |
\item \sml{type dataflow_info} is where the dataflow information |
| 582 |
is stored. |
| 583 |
\item \sml{forward} is true iff the dataflow problem is in the |
| 584 |
forward direction |
| 585 |
\item \sml{bot} is the bottom element of $D$. |
| 586 |
\item \sml{==} is the equality function on $D$. |
| 587 |
\item \sml{join} is the least-upper-bound function on $D$. |
| 588 |
\item \sml{prologue} is a user-supplied function that performs |
| 589 |
pre-processing and setup. For each CFG node $X$, this function |
| 590 |
computes |
| 591 |
\begin{itemize} |
| 592 |
\item \sml{input} -- which is the initial input value of $X$ |
| 593 |
\item \sml{output} -- which is the initial output value of $X$ |
| 594 |
\item \sml{transfer} -- which is the transfer function on $X$. |
| 595 |
\end{itemize} |
| 596 |
\item \sml{epilogue} is a function that performs post-processing. |
| 597 |
It visits each node $X$ in the flowgraph and return the resulting |
| 598 |
\sml{input} and \sml{output} value for $X$. |
| 599 |
\end{itemize} |
| 600 |
|
| 601 |
To generate a new dataflow analyzer from a dataflow problem, |
| 602 |
the functor \sml{Dataflow} can be used: |
| 603 |
\begin{SML} |
| 604 |
functor \mlrischref{IR/dataflow.sml}{Dataflow}(P : DATAFLOW_PROBLEM) : DATAFLOW_ANALYZER = |
| 605 |
\end{SML} |
| 606 |
|
| 607 |
\subsubsection{Static Branch Prediction} |
| 608 |
|
| 609 |
\subsubsection{Branch Optimizations} |