2009-05-19 18:28:13 -04:00
|
|
|
! Copyright (C) 2009 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-07-21 18:49:30 -04:00
|
|
|
USING: accessors assocs combinators sets math fry kernel math.order
|
2009-07-28 12:16:10 -04:00
|
|
|
dlists deques vectors namespaces sequences sorting locals
|
2009-08-08 21:02:56 -04:00
|
|
|
compiler.cfg.rpo compiler.cfg.predecessors ;
|
2010-02-26 16:01:01 -05:00
|
|
|
FROM: namespaces => set ;
|
2009-05-19 18:28:13 -04:00
|
|
|
IN: compiler.cfg.dominance
|
|
|
|
|
|
|
|
! Reference:
|
|
|
|
|
|
|
|
! A Simple, Fast Dominance Algorithm
|
|
|
|
! Keith D. Cooper, Timothy J. Harvey, and Ken Kennedy
|
|
|
|
! http://www.cs.rice.edu/~keith/EMBED/dom.pdf
|
|
|
|
|
2009-07-21 04:02:45 -04:00
|
|
|
! Also, a nice overview is given in these lecture notes:
|
|
|
|
! http://llvm.cs.uiuc.edu/~vadve/CS526/public_html/Notes/4ssa.4up.pdf
|
2009-05-19 18:28:13 -04:00
|
|
|
|
2009-07-21 04:02:45 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
! Maps bb -> idom(bb)
|
|
|
|
SYMBOL: dom-parents
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: dom-parent ( bb -- bb' ) dom-parents get at ;
|
2009-05-19 18:28:13 -04:00
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
2009-07-21 04:02:45 -04:00
|
|
|
: set-idom ( idom bb -- changed? )
|
|
|
|
dom-parents get maybe-set-at ;
|
2009-05-19 18:28:13 -04:00
|
|
|
|
|
|
|
: intersect ( finger1 finger2 -- bb )
|
|
|
|
2dup [ number>> ] compare {
|
2009-07-21 04:02:45 -04:00
|
|
|
{ +gt+ [ [ dom-parent ] dip intersect ] }
|
|
|
|
{ +lt+ [ dom-parent intersect ] }
|
2009-05-19 18:28:13 -04:00
|
|
|
[ 2drop ]
|
|
|
|
} case ;
|
|
|
|
|
|
|
|
: compute-idom ( bb -- idom )
|
2009-07-21 04:02:45 -04:00
|
|
|
predecessors>> [ dom-parent ] filter
|
2009-05-19 18:28:13 -04:00
|
|
|
[ ] [ intersect ] map-reduce ;
|
|
|
|
|
|
|
|
: iterate ( rpo -- changed? )
|
|
|
|
[ [ compute-idom ] keep set-idom ] map [ ] any? ;
|
|
|
|
|
2009-07-21 04:02:45 -04:00
|
|
|
: compute-dom-parents ( cfg -- )
|
|
|
|
H{ } clone dom-parents set
|
|
|
|
reverse-post-order
|
|
|
|
unclip dup set-idom drop '[ _ iterate ] loop ;
|
|
|
|
|
|
|
|
! Maps bb -> {bb' | idom(bb') = bb}
|
|
|
|
SYMBOL: dom-childrens
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: dom-children ( bb -- seq ) dom-childrens get at ;
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: compute-dom-children ( -- )
|
|
|
|
dom-parents get H{ } clone
|
|
|
|
[ '[ 2dup eq? [ 2drop ] [ _ push-at ] if ] assoc-each ] keep
|
|
|
|
dom-childrens set ;
|
|
|
|
|
2009-07-26 22:10:05 -04:00
|
|
|
SYMBOLS: preorder maxpreorder ;
|
|
|
|
|
2009-07-27 01:31:21 -04:00
|
|
|
PRIVATE>
|
|
|
|
|
2009-07-26 22:10:05 -04:00
|
|
|
: pre-of ( bb -- n ) [ preorder get at ] [ -1/0. ] if* ;
|
|
|
|
|
|
|
|
: maxpre-of ( bb -- n ) [ maxpreorder get at ] [ 1/0. ] if* ;
|
|
|
|
|
2009-07-27 01:31:21 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2009-07-26 22:10:05 -04:00
|
|
|
: (compute-dfs) ( n bb -- n )
|
|
|
|
[ 1 + ] dip
|
|
|
|
[ dupd preorder get set-at ]
|
|
|
|
[ dom-children [ (compute-dfs) ] each ]
|
|
|
|
[ dupd maxpreorder get set-at ]
|
|
|
|
tri ;
|
|
|
|
|
|
|
|
: compute-dfs ( cfg -- )
|
|
|
|
H{ } clone preorder set
|
|
|
|
H{ } clone maxpreorder set
|
|
|
|
[ 0 ] dip entry>> (compute-dfs) drop ;
|
|
|
|
|
2009-08-08 21:02:56 -04:00
|
|
|
: compute-dominance ( cfg -- cfg' )
|
|
|
|
[ compute-dom-parents compute-dom-children ] [ compute-dfs ] [ ] tri ;
|
|
|
|
|
2009-07-28 12:16:10 -04:00
|
|
|
PRIVATE>
|
|
|
|
|
2009-08-08 21:02:56 -04:00
|
|
|
: needs-dominance ( cfg -- cfg' )
|
|
|
|
needs-predecessors
|
|
|
|
dup dominance-valid?>> [ compute-dominance t >>dominance-valid? ] unless ;
|
2009-07-28 12:16:10 -04:00
|
|
|
|
2009-07-26 22:10:05 -04:00
|
|
|
: dominates? ( bb1 bb2 -- ? )
|
2009-07-28 12:16:10 -04:00
|
|
|
swap [ pre-of ] [ [ pre-of ] [ maxpre-of ] bi ] bi* between? ;
|
|
|
|
|
|
|
|
:: breadth-first-order ( cfg -- bfo )
|
|
|
|
<dlist> :> work-list
|
|
|
|
cfg post-order length <vector> :> accum
|
|
|
|
cfg entry>> work-list push-front
|
|
|
|
work-list [
|
|
|
|
[ accum push ]
|
|
|
|
[ dom-children work-list push-all-front ] bi
|
|
|
|
] slurp-deque
|
2010-02-26 16:01:01 -05:00
|
|
|
accum ;
|