graph-dominators

Find immediate dominators in a directed graph.

  1. graph-dominators
  2. Usage
  3. Documentation
    1. Procedures
  4. Examples
  5. About this egg
    1. Author
    2. Version history
    3. License

Usage

(require-extension graph-dominators)

Documentation

In a control-flow graph (CFG), block M dominates block N if every path from the entry that reaches block N has to pass through block M. The entry block dominates all blocks.

Block M immediately dominates block N if M dominates N, and there is no intervening block P such that M dominates P and P dominates N. In other words, M is the last dominator on any path from entry to N. Each block has a unique immediate dominator, if it has any at all.

This fast dominator code is based upon Lengauer and Tarjan, A Fast Algorithm for Finding Dominators in a Flowgraph, ACM TOPLAS 1:1, pp. 121--141, July 1979. It runs in time O(|E|\log|V|), where |E|is the number of edges and |V| is the number of vertices. A smaller time bound of O(|E|\alpha(|E|,|V|)), where alpha is the inverse of Ackerman's function, can be achieved with more complex versions of the internal link! and eval! procedures.

Procedures

[procedure] graph-find-dominators-quickly!:: G -> UNDEFINED

Computes the dominator tree of the given rooted, directed graph. When done, the meta-data field of each node will contain its immediate dominator. It assumes that the meta-data slot for each node contains a list, and modifies the car of that list. Requires that the list initially contain #f as its car value.

[procedure] graph-find-dominators-slowly!:: G -> UNDEFINED

The fast dominator algorithm is difficult to prove correct, so this procedure is provided in order to check its results. The slow algorithm, which runs in time O(|E||V|), is adapted from Aho and Ullman, The Theory of Parsing, Translation, and Compiling, Prentice-Hall, 1973, p. 916.

Examples

;; example adapted from graph example in the Boost library documentation
(require-extension srfi-1)
(require-extension digraph)
(require-extension graph-dominators)

(define g (make-digraph 'depgraph "dependency graph"))

(define used-by
   (list 
     (cons 'dax_h 'foo_cpp) (cons 'dax_h 'bar_cpp) (cons 'dax_h 'yow_h)
     (cons 'yow_h 'bar_cpp) (cons 'yow_h 'zag_cpp) (cons 'boz_h 'bar_cpp)
     (cons 'boz_h 'zig_cpp) (cons 'boz_h 'zag_cpp) 
     (cons 'foo_cpp 'foo_o) (cons 'foo_o 'libfoobar_a) 
     (cons 'bar_cpp 'bar_o) (cons 'bar_o 'libfoobar_a) 
     (cons 'libfoobar_a 'libzigzag_a)  (cons 'zig_cpp 'zig_o) 
     (cons 'zig_o 'libzigzag_a) (cons 'libfoobar_a 'dax_h) (cons 'zag_cpp 'zag_o) 
     (cons 'zag_o 'libzigzag_a) (cons 'libzigzag_a 'killerapp)))


(define node-list (delete-duplicates 
		   (concatenate (list (map car used-by) (map cdr used-by)))))

(define node-ids (list-tabulate (length node-list) values))
 
(for-each (lambda (i n) ((g 'add-node!) i (list #f n))) node-ids node-list)
(define node-map (zip node-list node-ids))

(for-each (lambda (e) 
	    (match e ((ni . nj) (let ((i (car (alist-ref ni node-map)))
				      (j (car (alist-ref nj node-map))))
				  ((g 'add-edge!) (list i j (format "~A->~A" ni nj)))))
		   (else (error "invalid edge " e))))
	  used-by)

(graph-find-dominators-quickly! g)

About this egg

Author

Richard Kelsey; ported to Chicken by Ivan Raikov

Version history

1.5
Documentation converted to wiki format
1.4
Added matchable to list of required eggs
1.3
Ported to Chicken 4
1.0
Initial release

License

Copyright by Richard Kelsey.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

- Neither name of the copyright holders nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND THE
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR THE
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.