Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] View of /sml/trunk/src/MLRISC/Doc/compiler-graphs.html
ViewVC logotype

View of /sml/trunk/src/MLRISC/Doc/compiler-graphs.html

Parent Directory Parent Directory | Revision Log Revision Log


Revision 409 - (download) (as text) (annotate)
Fri Sep 3 00:21:52 1999 UTC (19 years, 10 months ago) by monnier
File size: 20455 byte(s)
Initial revision
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="GENERATOR" CONTENT="Mozilla/4.07 [en] (X11; I; Linux 2.2.7 i686) [Netscape]">
</HEAD>
<BODY bgcolor="#FFFFFF">

<CENTER>
<H1>
<FONT COLOR="#aa0000">Basic Compiler Graphs</FONT></H1></CENTER>

<h2> Introduction </h2>
In this section we describe the set of basic compiler specific graphs and
algorithms implemented in MLRISC.
Mostly of these algorithms are parameterized with respect
to the actual intermediate representation, and as such they
do not provide many facilities that are provided by higher abstraction
layers.  


<h3> Dominator/Post-dominator Trees </h3>
<font color="#ff00000">Dominance</font> 
is a fundamental concept in compiler optimizations.
Node <em>A</em> <<em>dominates</em> <em>B</em> 
iff all paths from the start node
to <em>B</em> intersects A.  A dual notion is the concept of {\em post-dominance}:
<em>A</em> <font color="#ff0000">post-dominates</font> <em>B</em> iff all paths from <em>B</em> to the stop node
intersects <em>A</em>.  A (post-)dominator tree~\cite{dragon-book} can be used
to summarize the dominance/post-dominance relationship.

<pre>
 functor <a href="../ir/dominator.sml" target=code>DominatorTreeFn</a>
    (GraphImpl : GRAPH_IMPLEMENTATION) : DOMINATOR_TREE
</pre>
   The functor implements dominator analysis and 
creates a dominator/post-dominator tree from a graph <em>G</em>.  A dominator tree is implemented as a graph
with the following definition:
<pre>
 signature <a href="../ir/dominator.sig" target=code>DOMINATOR_TREE</a> = sig
    exception Dominator
    datatype 'n dom_node =
       DOM of { node : 'n, level : int, preorder : int, postorder : int }
    type ('n,'e,'g) dom_info
    type ('n,'e,'g) dominator_tree = ('n dom_node,unit,('n,'e,'g) dom_info) graph
    type ('n,'e,'g) postdominator_tree = ('n dom_node,unit,('n,'e,'g) dom_info) graph
</pre>

We annotated each node in
a dominator tree with three extra fields of information, which
is useful for other algorithms:
<ul>
  <li><tt>level</tt> is the nesting level of the tree.  The root
  node has level 0, children of the root has level 1 and so on.
  <li><tt>preorder</tt> is the preorder numbering of a node
  <li><tt>preorder</tt> is the postorder numbering of a node.
</ul>

To create a dominator tree and a postdominator tree
from a graph, the following function should be called.
<pre>
 val dominator_trees : ('n,'e,'g) graph ->
         ('n,'e,'g) dominator_tree * ('n,'e,'g) postdominator_tree
</pre>
We use the algorithm of Tarjan and Lengauer~\cite{tarjan-lengauer}, which
runs in time <em>O(|V+E|\alpha(|V+E|))</em> where <em>\alpha</em> is the functional
inverse of the Ackermann function.

To perform many common queries on a dominator tree, we first
call the function <tt>methods</tt> to obtain a method object.
<pre> 
  val methods : ('n,'e,'g) dominator_tree -> dominator_methods
</pre>

The methods are packed into the following type:
<pre>
   type dominator_methods =
         { dominates              : node_id * node_id -> bool,
           immediately_dominates  : node_id * node_id -> bool,
           strictly_dominates     : node_id * node_id -> bool,
           postdominates          : node_id * node_id -> bool,
           immediately_postdominates : node_id * node_id -> bool,
           strictly_postdominates : node_id * node_id -> bool,
           control_equivalent     : node_id * node_id -> bool,
           idom         : node_id -> node_id, <em>(* ~1 if none *)</em>
           idoms        : node_id -> node_id list,
           doms         : node_id -> node_id list,
           ipdom        : node_id -> node_id, <em>(* ~1 if none *)</em>
           ipdoms       : node_id -> node_id list,
           pdoms        : node_id -> node_id list,
           dom_lca      : node_id * node_id -> node_id,
           pdom_lca     : node_id * node_id -> node_id,
           dom_level    : node_id -> int,
           pdom_level   : node_id -> int,
           control_equivalent_partitions : unit -> node_id list list
         }
</pre>

The query methods are as follows:
\begin{methods}
  dominates(<em>a,b</em>)             & returns true iff <em>a</em> dominates <em>b</em> \\
  immediately\_dominates(<em>a,b</em>) & returns true iff <em>a</em> immediately dominates <em>b</em> \\
  strictly\_dominates(<em>a,b</em>)    & returns true iff <em>a</em> strictly dominates <em>b</em> \\
  postdominates(<em>a,b</em>)            & returns true iff <em>a</em> post-dominates <em>b</em> \\
  immediately\_postdominates(<em>a,b</em>) & returns true iff <em>a</em> immediately post-dominates <em>b</em> \\
  strictly\_postdominates(<em>a,b</em>) & returns true iff <em>a</em> strictly post-dominates <em>b</em> \\
  control\_equivalent(<em>a,b</em>) & 
  returns true iff <em>a</em> dominates <em>b</em> and vice versa \\ 
  idom(<em>a</em>) & returns the immediate dominator of <em>a</em>, or <em>-1</em> if none exists \\
  idoms(<em>a</em>) & returns all nodes that <em>a</em> immediately dominates \\
  doms(<em>a</em>) & returns all nodes that <em>a</em> dominates (including <em>a</em> itself) \\
  ipdom(<em>a</em>) & returns the immediate post-dominator of <em>a</em>, or <em>-1</em> if none exists \\
  ipdoms(<em>a</em>) & returns all nodes that <em>a</em> immediately post-dominates \\
  pdoms(<em>a</em>) & returns all nodes that <em>a</em> post-dominates (including <em>a</em> itself) \\
  dom\_lca(<em>a,b</em>) & returns the least common ancestor of <em>a</em> and <em>b</em> in
  the dominator tree \\
  pdom\_lca(<em>a,b</em>) & returns the least common ancestor of <em>a</em> and <em>b</em>
  in the post-dominator tree \\
  dom\_level(<em>a</em>) & returns the nesting level of <em>a</em> in the dominator tree \\
  pdom\_level(<em>b</em>) & returns the nesting level of <em>a</em> in the post-dominator 
  tree \\
  control\_equivalent\_partitions & partitions the graph into
  a set of control equivalent nodes.
\end{methods}

The methods \sml{dom\_lca}, <tt>pdom_lca</tt> and 
<tt>control_equivalent_partitions</tt> executes in <em>O(n)</em> time, where
<em>n</em> is the size of the dominator tree.  The other methods run in <em>O(1)</em> time.

<h3>Control Dependence Graph</h3>
Given two nodes <em>A</em> and <em>B</em> in a control flow graph <em>G</em>, 
we say that <em>B</em> is <font color="#ff0000">control dependent</font> on <em>A</em> iff
<ul>
  <li> <em>B</em> post-dominates a successor of <em>A</em>
  <li> <em>B</em> does not strictly post-dominates <em>A</em>
</ul>
Intuitively, <em>B</em> is control dependent on <em>A</em> means that
some path in the program that goes through <em>A</em> can by-passed <em>B</em>,
and furthermore, <em>A</em> is the point in which this divergence can occur.
Control dependence is used to various kinds of analysis and optimizations in
a compiler, such as code motion and global scheduling~\cite{bernstein-rodeh}.

To build a control dependence graph, the functor
<tt>ControlDependenceGraphFn</tt> can be used:
<pre>
 signature <a href="../ir/cdg.sig" target=code>CONTROL_DEPENDENCE_GRAPH</a> = sig
    type ('n,'e,'g) cdg = ('n,'e,'g) graph

    val control_dependence_graph :
          ('e -> bool) ->
          ('n,'e,'g) dominator_tree *
          ('n,'e,'g) postdominator_tree ->
          ('n,'e,'g) cdg
 end
 functor <a href="../ir/cdg.sml" target=code>ControlDependenceGraphFn</a>
    (structure Dom : DOMINATOR_TREE
     structure GraphImpl : GRAPH_IMPLEMENTATION
    ) : CONTROL_DEPENDENCE_GRAPH
</pre>
The control depedence graph is a subcomponent of the
program dependence graph~\cite{PDG} commonly used in
modern compiler optimizations.

<h3>Dominance Frontiers</h3>

Many algorithms involving the notion of control dependence or dominance
can be rephrased in terms of <font color="#ff0000">dominance frontiers</font>.
A node <em>A</em> is in the dominance frontiers of <em>B</em> iff
<em>B</em> dominates a predecessor of <em>A</em> but <em>B</em> does not strictly-dominate <em>A</em>.
We denote this as <em>A \in DF(B)</em>. 
The dual notion of <font color="#ff0000">post-dominance frontiers</font> can be defined
analogously using the post-dominator tree\footnote{Control dependence
can be defined in terms of post-dominance frontiers.}.  

<pre>
  functor <a href="../ir/dominance-frontier.sml" target=code>DominanceFrontiersFn</a>(Dom : DOMINATOR_TREE) : DOMINANCE_FRONTIERS
</pre>
The functor <tt>DominanceFrontiersFn</tt> can be used to
compute all the dominance frontiers of all the nodes in a graph.
It has the following signature. 

<pre>
 signature <a href="../ir/dominance-frontier.sig" target=code>DOMINANCE_FRONTIERS</a> = sig
   structure Dom : DOMINATOR_TREE
   type dominance_frontiers = node_id list array
   val DFs : ('n,'e,'g) Dom.dominator_tree -> dominance_frontiers
 end
</pre>

We use the algorithm described in~\cite{SSA}.
<h3>Iterated Dominance Frontiers</h3>

<font color="#ff0000">Iterated dominance frontiers</font> (denoted as <em>DF^+</em>) are defined
as the least fixed point of iterating the operation <em>DF</em>. Formally,
define the dominance frontiers on a set <em>S</em> as follows:
\[  DF(S) \defas \Union_{A \in S} DF(A) \]
Define iteration of <em>DF</em>, denoted as <em>DF^n</em>, as follows:
\begin{eqnarray*}
  DF^1(S)     & \defas & DF(S) \\
  DF^{n+1}(S) & \defas & DF(S \union DF^n(S)) \\
\end{eqnarray*}
The iterated dominance frontiers <em>DF^+(S)</em> on a set <em>S</em> are defined as
the limit:
\[  DF^+(S) \defas \lim_{n \to \infty} DF^n(S) \]

Iterated dominance frontiers of a set <em>S</em> can be computed in
time <em>O(|S|+|V|+|E|)</em> using the 
algorithm by Sreedhar and Gao~\cite{linear-time-IDF}\footnote{
In practice it is often sub-linear in <em>|V|+|E|</em>.}.

<pre>
  functor <a href="../ir/djgraph.sml" target=code>DJGraphFn</a>(Dom : DOMINATOR_TREE) : DJ_GRAPH
</pre>
The functor <tt>DJGraphFn</tt> implements this algorithm.
It satisfies the signature below:
<pre>
 signature <a href="../ir/djgraph.sig" target=code>DJ_GRAPH</a> = sig
    structure Dom : DOMINATOR_TREE
    type ('n,'e,'g) dj_graph = ('n,'e,'g) Dom.dominator_tree
    val dj_graph : ('n,'e,'g) dj_graph ->
        {  DF   : node_id -> node_id list,
           IDF  : node_id -> node_id list,
           IDFs : node_id list -> node_id list
        }
 end
</pre>
The function <tt>dj_graph</tt> takes a dominator tree and returns
three query methods for computing dominance and iterated dominance frontiers.
Method <tt>DF</tt> computes <em>DF(v)</em> for a single node <em>v</em>.
Method <tt>IDF</tt> computes the <em>DF^+({v})</em>, and method
<tt>IDFs</tt> computes <em>DF^+(S)</em> when given a set of node ids.
The dominator tree must not be updated while these operations
are being performed. 

Sreedhar's original algorithm is phrased in terms of the
DJ-graph, which is a fusion of the dominator tree
with its underlying flowgraph.  Our variant operates on the
dominator tree and the flowgraph at the same time, without
building an intermediate data structure.  

Iterated dominance frontiers are used
in many algorithms that deal with the notion of dominance.
For example, the SSA construction algorithm uses iterated
dominance frontiers to identify confluent points in the program
where <em>\phi</em>-functions are to be placed.

<h3>Loop Nesting Tree</h3>

A <font color="#ff0000">natural loop</font> <em>L</em> in a graph is a maximal 
strongly connected component 
such that all nodes in <em>L</em> are dominated by a single node <em>h</em>, called
the <font color="#ff0000">loop header</font>.  Loops tend to form good optimization candidates
and consequently <font color="#ff0000">loop detection</font> is an essential task in a compiler.
The functor 
<pre>
 functor <a href="../ir/loop-structure.sml" target=code>LoopStructureFn</a> 
  (structure GraphImpl : GRAPH_IMPLEMENTATION
   structure Dom       : DOMINATOR_TREE
  ) : LOOP_STRUCTURE 
</pre>
recognizes all natural loops in a graph and built a <font color="#ff0000">loop nesting tree</font>
that describes the loop nesting relationship between graphs.

<pre>
 signature <a href="../ir/loop-structure.sig" target=code>LOOP_STRUCTURE</a> = sig
   structure Dom : DOMINATOR_TREE
   datatype ('n,'e,'g) loop =
      LOOP of { nesting    : int,
                header     : node_id,
                loop_nodes : node_id list,
                backedges  : 'e edge list,
                exits      : 'e edge list
              }

   type ('n,'e,'g) loop_info
   type ('n,'e,'g) loop_structure = (('n,'e,'g) loop,unit, ('n,'e,'g) loop_info) graph

   val loop_structure : ('n,'e,'g) Dom.dominator_tree -> ('n,'e,'g) loop_structure
   val nesting_level : ('n,'e,'g) loop_structure -> node_id array
   val header        : ('n,'e,'g) loop_structure -> node_id array
 end
</pre>

Our algorithm computes the loop nesting tree in time 
<em>O((|V|+|E|)\alpha(|V|+|E|))</em>.
Each node in this tree represents a loop in the flowgraph, except the
root of the tree, which represents the entire graph.    
Given a flowgraph <em>G</em>, the root
of the loop nesting tree is defined to be the sole vertex in 
<tt>#entry</tt> <em>G</em>.  Other nodes in the tree
are indexed by the loop header node ids.

Loop detection classifies each loop and for 
each loop <em>L</em>, the following information is obtained:
<ul>
 <li> An integer <tt>nesting</tt>.   The root of the tree has nesting
 depth 0.  The top level loops have nesting depth 1, etc.
 <li> The node id of the loop <tt>header</tt> <em>h</em>.
 <li> A set of <tt>loop_nodes</tt>.  Loop nodes are
  nodes that are in the strongly connected
  component <em>L</em>, but excluding the header <em>h</em> 
  and all nodes that are part of any nested loops.
   Thus all nodes are uniquely partitioned in header nodes and
   loop nodes, and loop nodes are further partitioned into different
   sets according to which headers they are immediately nested under.
 <li> A set of <tt>backedges</tt>.  A back-edge is an
    edge that targets the header <em>h</em> and originates from a loop node
    in <em>L</em>.
 <li> A set of loop <tt>exits</tt>. An exit-edge is an edge
   that originates from a loop node within <em>L</em>
   targets a node outside of <em>L</em>.  Note that this set does not include
   any exit-edges contained in loops nested in <em>L</em> but 
   target a node out of <em>L</em>.
</ul>

<h3>Static Single Assignment</h3>

An SSA construction algorithm based on~\cite{SSA,Briggs-SSA,linear-time-IDF}
is implemented in the following functor:
<pre>
  functor <a href="../ir/ssa.sml" target=code>StaticSingleAssignmentFormFn</a>
     (Dom : DOMINATOR_TREE) : STATIC_SINGLE_ASSIGNMENT_FORM
</pre>

SSA-based optimizations in MLRISC
are actually implemented on top of a
high-level SSA layer described in Section~\ref{sec:ssa}. 
So it is not necessary to use this module directly.  Nevertheless,
there can be situations in which this module can be specialized in other
ways; for example, in the construction of sparse evaluation graphs.

<pre>
 signature <a href="../ir/ssa.sig" target=code>STATIC_SINGLE_ASSIGNMENT_FORM</a> = sig
   structure Dom : DOMINATOR_TREE
   type var     = int 
   type phi  = var * var * var list <em>(* orig def/def/uses *)</em>
   type renamer = {defs : var list, uses: var list} ->
                  {defs : var list, uses: var list}
   type copy    = {dst : var list, src: var list} -> unit

   val compute_ssa : 
       ('n,'e,'g) Dom.dominator_tree ->
       { max_var      : var,  
         defs         : 'n node -> var list,
         is_live      : var * int -> bool,
         rename_var   : var -> var,
         rename_stmt  : {rename:renamer,copy:copy} -> 'n node -> unit,
         insert_phi   : {block    : 'n node,
                         in_edges : 'e edge list,
                         phis     : phi list 
                        } -> unit
       } -> unit
 end
</pre>

This module defines the function <tt>compute_ssa</tt>, which
constructs an SSA graph.  It requires 
the following information from the client:
<ul>
<li> A dominator tree of the flowgraph.
<li> <tt>max_var</tt> -- the maximum variable id (integer) that exists
in the flowgraph.  All variables are assumed to be indexed by non-negative
 integers.
<li> <tt>defs</tt>(<em>X</em>) -- a function that returns <em><em>defs</em>(X)</em>, 
i.e.~the set of variable names defined in block <em>X</em>.
If a minimal SSA form is desired, this set should include all the definitions
in <em>X</em>.  If a pruned SSA form is required, this set should
include only the set of names that are live-out in <em>X</em>.
<li> <tt>is_live</tt>(<em>v,X</em>) -- a function that determines if
variable <em>v</em> is live-in into block <em>X</em>.  If not, a <em>\phi</em>-function will
not be placed in <em>X</em>.  For example, to compute
the minimal-SSA form, this function should always return true. 
<li> <tt>rename_var</tt>(<em>v</em>) -- a function that returns a new 
unique name for variable <em>v</em>.   
<li> <tt>rename_stmt</tt> -- a function of type
       <tt>{rename:renamer,copy:copy</tt> -> 'n node -> unit} where
<pre>
   type renamer = {defs : var list, uses: var list} ->
                  {defs : var list, uses: var list}
   type copy    = {dst : var list, src: var list} -> unit
</pre>
Function <tt>rename_stmt</tt> is called for each block
in the flowgraph in the order of the dominator tree, and
is responsible for renaming all the variables in <em>X</em> by
calling the functions <tt>renamer</tt> or <tt>copy</tt>.
Function <tt>renamer</tt> renames all definitions and uses of
a statement, while function <tt>copy</tt> renames
of a set of parallel assignments
<li> <tt>insert_phi</tt>(<em>X</em>,<em>es</em>,<em>phis</em>) --
   a function that inserts a set of 
   <em>\phi</em>-definitions <em>phis</em> in block <em>X</em>, where <em>es</em>
   is the list of control flow edges that merge into block <em>X</em>.
</ul>      

<h3>IDEFS/IUSE sets</h3>
Reif and Tarjan define the following useful notions for
computing approximate birth-points for expressions,  which in turn
can be used to drive other optimizations.
Given a node <em>X</em>, let <em>idom(X)</em> denote the immediate dominator of <em>X</em>.
Let <em><em>def</em>(X)</em> (<em>use(X)</em>) denote all the definitions (uses) in <em>X</em>. 
Given a path <em>p \equiv v_1\ldots v_n</em>, define <em><em>def</em>(p)</em> (<em>use(p)</em>) as
\begin{eqnarray*}
   <em>def</em>(v_1\ldots v_n) & \equiv &\union_{i \in 1 \ldots n} <em>def</em>(v_i) \\
   use(v_1\ldots v_n) & \equiv &\union_{i \in 1 \ldots n} use(v_i)
\end{eqnarray*}

Let <em>P(X)</em> denotes all the paths from <em>idom(X)</em> to <em>X</em>
that does not cross <em>idom(X)</em> internally.    Then define
<em><em>idef</em>(X)</em> (<em>iuse(X)</em>) as:
\begin{eqnarray*}
  <em>idef</em>(X) & \equiv & \Union_{idom(X) v_1 \ldots v_n X \in P(X)} 
     <em>def</em>(v_1\ldots v_n) \\
  iuse(X) & \equiv & \Union_{idom(X) v_1 \ldots v_n X \in P(X)} 
     use(v_1\ldots v_n) 
\end{eqnarray*}
The sets <em><em>ipostdef</em>(X)</em> and <em>ipostuse(X)</em> are defined analogously
using the postdominator tree.

<pre>
 signature <a href="../ir/idefs2.sig" target=code>IDEFS</a> = sig
   type var = int
   val compute_idefs : 
       {def_use : 'n Graph.node -> var list * var list,
        cfg     : ('n,'e,'g) Graph.graph
       } ->
       { idefuse      : unit -> (RegSet.regset * RegSet.regset) Array.array,
         ipostdefuse  : unit -> (RegSet.regset * RegSet.regset) Array.array
       }
 end
 structure \struct{IDefs}{../ir/idefs2.sml} : IDEFS
</pre>
Structure <tt>IDefs</tt> implements the function 
<tt>comput_idefs</tt> for computing
the <em><em>idef</em></em>, <em>iuse</em>, <em><em>ipostdef</em></em> and <em>ipostuse</em> sets of a control flow
graph.  It takes as arguments a flowgraph and a function <tt>def_use</tt>, which
takes a graph node and returns the def/use sets of the node.
It returns two functions <tt>idefuse</tt> and <tt>ipostdefuse</tt> which
compute the <em>idef/iuse</em> and <em>ipostdef/ipostuse</em> sets.  These sets
are returned as arrays indexed by node ids.

<h3>Tarjan's Fast Path Algorithm</h3>

{\em Describe Tarjan's fast path algorithm and it's relationship
with Gaussian elimination, interval analysis, etc.}



<HR>
    <FONT SIZE="-2">
<ADDRESS>
<A HREF="mailto:leunga@cs.nyu.edu">Allen Leung</A></ADDRESS>
<BR>

</BODY>
</HTML>

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0