factor/library/inference/recursive-values.factor

48 lines
1.5 KiB
Factor
Raw Normal View History

! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2005-09-09 00:17:19 -04:00
IN: optimizer
USING: inference kernel namespaces prettyprint sequences vectors ;
GENERIC: collect-recursion* ( label node -- )
M: node collect-recursion* ( label node -- ) 2drop ;
M: #call-label collect-recursion* ( label node -- )
2005-09-24 15:21:17 -04:00
tuck node-param = [ node-in-d , ] [ drop ] if ;
2005-09-17 04:15:05 -04:00
: collect-recursion ( #label -- seq )
#! Collect the input stacks of all #call-label nodes that
#! call given label.
2005-09-17 04:15:05 -04:00
dup node-param swap
[ [ collect-recursion* ] each-node-with ] { } make ;
GENERIC: solve-recursion*
M: node solve-recursion* ( node -- ) drop ;
2005-09-17 04:15:05 -04:00
: purge-invariants ( stacks -- seq )
#! Output a sequence of values which are not present in the
#! same position in each sequence of the stacks sequence.
2005-09-18 01:37:28 -04:00
flip [ all-eq? not ] subset concat ;
2005-09-17 04:15:05 -04:00
: join-values ( calls entry -- new old live )
add unify-lengths
[ flip [ unify-values ] map ] keep
[ peek ] keep
purge-invariants ;
: entry-values ( node -- new old live )
dup collect-recursion swap node-child node-in-d join-values ;
M: #label solve-recursion* ( node -- )
2005-09-17 04:15:05 -04:00
#! #entry node-out-d is abused; its not a stack slice, but
#! a set of values.
[ entry-values ] keep node-child
[ set-node-out-d ] keep
node-successor subst-values ;
: solve-recursion ( node -- )
#! Figure out which values survive inner recursions in
#! #labels, and those that don't should be fudged.
2005-08-06 01:59:49 -04:00
[ solve-recursion* ] each-node ;