2005-04-02 02:39:33 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2005-07-27 01:46:06 -04:00
|
|
|
IN: inference
|
2005-08-04 17:39:39 -04:00
|
|
|
USING: generic hashtables inference kernel lists
|
2005-08-03 18:47:32 -04:00
|
|
|
matrices namespaces sequences vectors ;
|
|
|
|
|
|
|
|
! We use the recursive-state variable here, to track nested
|
|
|
|
! label scopes, to prevent infinite loops when inlining
|
|
|
|
! recursive methods.
|
2004-12-02 22:44:36 -05:00
|
|
|
|
2005-07-27 01:46:06 -04:00
|
|
|
GENERIC: optimize-node* ( node -- node )
|
2005-05-21 16:05:39 -04:00
|
|
|
|
2005-08-03 18:47:32 -04:00
|
|
|
GENERIC: optimize-children
|
|
|
|
|
2005-07-27 20:13:11 -04:00
|
|
|
: keep-optimizing ( node -- node ? )
|
2005-07-27 01:46:06 -04:00
|
|
|
dup optimize-node* dup t =
|
2005-07-27 20:13:11 -04:00
|
|
|
[ drop f ] [ nip keep-optimizing t or ] ifte ;
|
2005-05-22 02:35:38 -04:00
|
|
|
|
2005-07-27 20:13:11 -04:00
|
|
|
: optimize-node ( node -- node ? )
|
|
|
|
#! Outputs t if any changes were made.
|
|
|
|
keep-optimizing >r dup [
|
|
|
|
dup optimize-children >r
|
|
|
|
dup node-successor optimize-node >r
|
|
|
|
over set-node-successor r> r> r> or or
|
|
|
|
] [ r> ] ifte ;
|
2005-05-21 16:05:39 -04:00
|
|
|
|
2005-08-07 00:00:57 -04:00
|
|
|
M: node optimize-children ( node -- )
|
|
|
|
f swap [
|
|
|
|
node-children [ optimize-node swap >r or r> ] map
|
|
|
|
] keep set-node-children ;
|
|
|
|
|
|
|
|
: optimize-loop ( dataflow -- dataflow )
|
|
|
|
recursive-state off
|
|
|
|
dup kill-set over kill-node
|
|
|
|
dup infer-classes
|
|
|
|
optimize-node [ optimize-loop ] when ;
|
|
|
|
|
2005-05-21 16:05:39 -04:00
|
|
|
: optimize ( dataflow -- dataflow )
|
2005-08-03 18:47:32 -04:00
|
|
|
[
|
2005-08-04 23:59:45 -04:00
|
|
|
dup solve-recursion
|
2005-08-07 00:00:57 -04:00
|
|
|
optimize-loop
|
|
|
|
] with-scope ;
|
2005-07-27 01:46:06 -04:00
|
|
|
|
|
|
|
: prune-if ( node quot -- successor/t )
|
|
|
|
over >r call [ r> node-successor ] [ r> drop t ] ifte ;
|
2005-07-27 20:13:11 -04:00
|
|
|
inline
|
2005-05-21 16:05:39 -04:00
|
|
|
|
2005-05-22 02:35:38 -04:00
|
|
|
! Generic nodes
|
2005-07-27 01:46:06 -04:00
|
|
|
M: f optimize-node* drop t ;
|
2005-05-21 16:05:39 -04:00
|
|
|
|
2005-07-27 01:46:06 -04:00
|
|
|
M: node optimize-node* ( node -- t )
|
|
|
|
drop t ;
|
2005-05-21 16:05:39 -04:00
|
|
|
|
2005-05-22 02:35:38 -04:00
|
|
|
! #push
|
2005-07-27 01:46:06 -04:00
|
|
|
M: #push optimize-node* ( node -- node/t )
|
|
|
|
[ node-out-d empty? ] prune-if ;
|
2005-05-21 16:05:39 -04:00
|
|
|
|
2005-05-22 02:35:38 -04:00
|
|
|
! #drop
|
2005-07-27 01:46:06 -04:00
|
|
|
M: #drop optimize-node* ( node -- node/t )
|
|
|
|
[ node-in-d empty? ] prune-if ;
|
2005-05-21 16:05:39 -04:00
|
|
|
|
2005-05-22 02:35:38 -04:00
|
|
|
! #call
|
2005-07-27 01:46:06 -04:00
|
|
|
: flip-branches ( #ifte -- )
|
|
|
|
dup node-children 2unseq swap 2vector swap set-node-children ;
|
|
|
|
|
2005-05-22 02:35:38 -04:00
|
|
|
! #label
|
2005-08-04 17:39:39 -04:00
|
|
|
: optimize-label ( node -- node )
|
|
|
|
dup node-param recursive-state [ cons ] change
|
|
|
|
delegate optimize-children
|
|
|
|
recursive-state [ cdr ] change ;
|
|
|
|
|
2005-08-03 18:47:32 -04:00
|
|
|
M: #label optimize-children optimize-label ;
|
|
|
|
|
|
|
|
M: #simple-label optimize-children optimize-label ;
|
|
|
|
|
2005-07-27 20:13:11 -04:00
|
|
|
! #ifte
|
2005-07-27 01:46:06 -04:00
|
|
|
: static-branch? ( node -- lit ? )
|
2005-08-07 00:00:57 -04:00
|
|
|
node-in-d first dup literal? ;
|
2005-07-27 01:46:06 -04:00
|
|
|
|
|
|
|
: static-branch ( conditional n -- node )
|
2005-08-02 00:25:05 -04:00
|
|
|
>r [ drop-inputs ] keep r>
|
2005-07-27 01:46:06 -04:00
|
|
|
over node-children nth
|
|
|
|
over node-successor over last-node set-node-successor
|
|
|
|
pick set-node-successor drop ;
|
|
|
|
|
|
|
|
M: #ifte optimize-node* ( node -- node )
|
|
|
|
dup static-branch?
|
2005-08-04 23:59:45 -04:00
|
|
|
[ literal-value 0 1 ? static-branch ] [ 2drop t ] ifte ;
|
2005-07-27 01:46:06 -04:00
|
|
|
|
|
|
|
! #values
|
2005-07-27 20:13:11 -04:00
|
|
|
: values/merge ( #values #merge -- new old )
|
2005-08-07 00:00:57 -04:00
|
|
|
>r >r node-in-d r> node-in-d unify-length r> ;
|
2005-07-27 20:13:11 -04:00
|
|
|
|
2005-07-27 01:46:06 -04:00
|
|
|
: post-split ( #values -- node )
|
|
|
|
#! If a #values is followed by a #merge, we need to replace
|
|
|
|
#! meet values after the merge with their branch value in
|
|
|
|
#! #values.
|
|
|
|
dup node-successor dup node-successor
|
2005-07-27 20:13:11 -04:00
|
|
|
values/merge [ subst-values ] keep ;
|
2005-07-27 01:46:06 -04:00
|
|
|
|
2005-08-01 16:22:53 -04:00
|
|
|
M: #values optimize-node* ( node -- node ? )
|
2005-07-27 01:46:06 -04:00
|
|
|
dup node-successor #merge? [ post-split ] [ drop t ] ifte ;
|