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-05-09 02:34:15 -04:00
|
|
|
IN: compiler-frontend
|
2005-04-02 02:39:33 -05:00
|
|
|
USING: inference kernel kernel-internals lists namespaces
|
|
|
|
sequences vectors words words ;
|
2004-12-02 22:44:36 -05:00
|
|
|
|
|
|
|
! The optimizer transforms dataflow IR to dataflow IR. Currently
|
2004-12-10 18:23:50 -05:00
|
|
|
! it removes literals that are eventually dropped, and never
|
|
|
|
! arise as inputs to any other type of function. Such 'dead'
|
|
|
|
! literals arise when combinators are inlined and quotations are
|
|
|
|
! lifted to their call sites. Also, #label nodes are inlined if
|
|
|
|
! their children do not make a recursive call to the label.
|
2004-12-02 22:44:36 -05:00
|
|
|
|
|
|
|
: scan-literal ( node -- )
|
2004-12-03 22:12:58 -05:00
|
|
|
#! If the node represents a literal push, add the literal to
|
|
|
|
#! the list being constructed.
|
|
|
|
"scan-literal" [ drop ] apply-dataflow ;
|
|
|
|
|
|
|
|
: (scan-literals) ( dataflow -- )
|
|
|
|
[ scan-literal ] each ;
|
2004-12-02 22:44:36 -05:00
|
|
|
|
|
|
|
: scan-literals ( dataflow -- list )
|
2004-12-03 22:12:58 -05:00
|
|
|
[ (scan-literals) ] make-list ;
|
2004-12-02 22:44:36 -05:00
|
|
|
|
|
|
|
: scan-branches ( branches -- )
|
2004-12-03 22:12:58 -05:00
|
|
|
#! Collect all literals from all branches.
|
|
|
|
[ node-param get ] bind [ [ scan-literal ] each ] each ;
|
2004-12-02 22:44:36 -05:00
|
|
|
|
2004-12-19 22:53:41 -05:00
|
|
|
: mentions-literal? ( literal list -- ? )
|
2004-12-02 22:44:36 -05:00
|
|
|
#! Does the given list of result objects refer to this
|
|
|
|
#! literal?
|
2005-01-19 21:01:47 -05:00
|
|
|
[ value= ] some-with? ;
|
2004-12-02 22:44:36 -05:00
|
|
|
|
2004-12-03 22:12:58 -05:00
|
|
|
: consumes-literal? ( literal node -- ? )
|
|
|
|
#! Does the dataflow node consume the literal?
|
2004-12-02 22:44:36 -05:00
|
|
|
[
|
2004-12-19 22:53:41 -05:00
|
|
|
dup node-consume-d get mentions-literal? swap
|
|
|
|
dup node-consume-r get mentions-literal? nip or
|
2004-12-03 22:12:58 -05:00
|
|
|
] bind ;
|
|
|
|
|
|
|
|
: produces-literal? ( literal node -- ? )
|
|
|
|
#! Does the dataflow node produce the literal?
|
|
|
|
[
|
2004-12-19 22:53:41 -05:00
|
|
|
dup node-produce-d get mentions-literal? swap
|
|
|
|
dup node-produce-r get mentions-literal? nip or
|
2004-12-02 22:44:36 -05:00
|
|
|
] bind ;
|
|
|
|
|
2004-12-03 17:11:49 -05:00
|
|
|
: (can-kill?) ( literal node -- ? )
|
|
|
|
#! Return false if the literal appears as input to this
|
|
|
|
#! node, and this node is not a stack operation.
|
2004-12-10 02:41:52 -05:00
|
|
|
2dup consumes-literal? >r produces-literal? r> or not ;
|
2004-12-03 17:11:49 -05:00
|
|
|
|
2004-12-02 22:44:36 -05:00
|
|
|
: can-kill? ( literal dataflow -- ? )
|
2004-12-03 22:12:58 -05:00
|
|
|
#! Return false if the literal appears in any node in the
|
|
|
|
#! list.
|
2004-12-10 02:41:52 -05:00
|
|
|
[ dupd "can-kill" [ (can-kill?) ] apply-dataflow ] all? nip ;
|
2004-12-02 22:44:36 -05:00
|
|
|
|
|
|
|
: kill-set ( dataflow -- list )
|
|
|
|
#! Push a list of literals that may be killed in the IR.
|
|
|
|
dup scan-literals [ over can-kill? ] subset nip ;
|
|
|
|
|
2005-01-17 15:33:12 -05:00
|
|
|
SYMBOL: branch-returns
|
|
|
|
|
2004-12-03 17:11:49 -05:00
|
|
|
: can-kill-branches? ( literal node -- ? )
|
2005-01-17 15:33:12 -05:00
|
|
|
#! Check if the literal appears in either branch. This
|
|
|
|
#! assumes that the last element of each branch is a #values
|
|
|
|
#! node.
|
2004-12-10 02:41:52 -05:00
|
|
|
2dup consumes-literal? [
|
|
|
|
2drop f
|
|
|
|
] [
|
2005-01-17 15:33:12 -05:00
|
|
|
[ node-param get ] bind
|
|
|
|
[
|
|
|
|
dup [
|
2005-04-26 00:35:55 -04:00
|
|
|
peek [ node-consume-d get >vector ] bind
|
2005-01-17 15:33:12 -05:00
|
|
|
] map
|
2005-04-02 02:39:33 -05:00
|
|
|
unify-stacks >list
|
2005-01-17 15:33:12 -05:00
|
|
|
branch-returns set
|
|
|
|
[ dupd can-kill? ] all? nip
|
|
|
|
] with-scope
|
2004-12-10 02:41:52 -05:00
|
|
|
] ifte ;
|
2004-12-03 17:11:49 -05:00
|
|
|
|
2004-12-10 18:23:50 -05:00
|
|
|
: kill-node ( literals node -- )
|
2004-12-10 02:41:52 -05:00
|
|
|
swap [ over (can-kill?) ] all? [ , ] [ drop ] ifte ;
|
2004-12-03 22:12:58 -05:00
|
|
|
|
2004-12-10 18:23:50 -05:00
|
|
|
: (kill-nodes) ( literals dataflow -- )
|
|
|
|
#! Append live nodes to currently constructing list.
|
2005-01-01 17:20:48 -05:00
|
|
|
[ "kill-node" [ nip , ] apply-dataflow ] each-with ;
|
2004-12-04 15:10:46 -05:00
|
|
|
|
2004-12-04 15:48:44 -05:00
|
|
|
: kill-nodes ( literals dataflow -- dataflow )
|
2004-12-03 22:12:58 -05:00
|
|
|
#! Remove literals and construct a list.
|
2004-12-10 18:23:50 -05:00
|
|
|
[ (kill-nodes) ] make-list ;
|
2004-12-03 22:12:58 -05:00
|
|
|
|
2004-12-04 15:10:46 -05:00
|
|
|
: optimize ( dataflow -- dataflow )
|
2004-12-04 15:48:44 -05:00
|
|
|
#! Remove redundant literals from the IR. The original IR
|
|
|
|
#! is destructively modified.
|
|
|
|
dup kill-set swap kill-nodes ;
|
|
|
|
|
2004-12-04 23:45:41 -05:00
|
|
|
: kill-branches ( literals node -- )
|
|
|
|
[
|
|
|
|
node-param [ [ dupd kill-nodes ] map nip ] change
|
|
|
|
] extend , ;
|
2004-12-03 22:12:58 -05:00
|
|
|
|
2005-03-05 14:45:23 -05:00
|
|
|
#push [ [ node-param get ] bind , ] "scan-literal" set-word-prop
|
|
|
|
#push [ consumes-literal? not ] "can-kill" set-word-prop
|
|
|
|
#push [ kill-node ] "kill-node" set-word-prop
|
2004-12-03 22:12:58 -05:00
|
|
|
|
|
|
|
#label [
|
|
|
|
[ node-param get ] bind (scan-literals)
|
2005-03-05 14:45:23 -05:00
|
|
|
] "scan-literal" set-word-prop
|
2004-12-03 22:12:58 -05:00
|
|
|
|
|
|
|
#label [
|
|
|
|
[ node-param get ] bind can-kill?
|
2005-03-05 14:45:23 -05:00
|
|
|
] "can-kill" set-word-prop
|
2004-12-03 22:12:58 -05:00
|
|
|
|
2004-12-10 18:23:50 -05:00
|
|
|
#call-label [
|
|
|
|
[ node-param get ] bind =
|
2005-03-05 14:45:23 -05:00
|
|
|
] "calls-label" set-word-prop
|
2004-12-10 18:23:50 -05:00
|
|
|
|
|
|
|
: calls-label? ( label list -- ? )
|
2005-01-19 21:01:47 -05:00
|
|
|
[ "calls-label" [ 2drop f ] apply-dataflow ] some-with? ;
|
2004-12-10 18:23:50 -05:00
|
|
|
|
|
|
|
#label [
|
|
|
|
[ node-param get ] bind calls-label?
|
2005-03-05 14:45:23 -05:00
|
|
|
] "calls-label" set-word-prop
|
2004-12-10 18:23:50 -05:00
|
|
|
|
2004-12-11 18:18:43 -05:00
|
|
|
#simple-label [
|
|
|
|
[ node-param get ] bind calls-label?
|
2005-03-05 14:45:23 -05:00
|
|
|
] "calls-label" set-word-prop
|
2004-12-11 18:18:43 -05:00
|
|
|
|
2004-12-10 18:23:50 -05:00
|
|
|
: branches-call-label? ( label list -- ? )
|
2005-01-19 21:01:47 -05:00
|
|
|
[ calls-label? ] some-with? ;
|
2004-12-10 18:23:50 -05:00
|
|
|
|
2005-01-14 14:56:19 -05:00
|
|
|
\ ifte [
|
2004-12-10 18:23:50 -05:00
|
|
|
[ node-param get ] bind branches-call-label?
|
2005-03-05 14:45:23 -05:00
|
|
|
] "calls-label" set-word-prop
|
2004-12-10 18:23:50 -05:00
|
|
|
|
2005-01-14 14:56:19 -05:00
|
|
|
\ dispatch [
|
2004-12-10 18:23:50 -05:00
|
|
|
[ node-param get ] bind branches-call-label?
|
2005-03-05 14:45:23 -05:00
|
|
|
] "calls-label" set-word-prop
|
2004-12-10 18:23:50 -05:00
|
|
|
|
2004-12-11 18:18:43 -05:00
|
|
|
: optimize-label ( -- op )
|
2004-12-10 18:23:50 -05:00
|
|
|
#! Does the label node contain calls to itself?
|
2004-12-11 18:18:43 -05:00
|
|
|
node-label get node-param get calls-label?
|
|
|
|
#label #simple-label ? ;
|
2004-12-10 18:23:50 -05:00
|
|
|
|
2004-12-04 15:10:46 -05:00
|
|
|
#label [ ( literals node -- )
|
2004-12-11 18:18:43 -05:00
|
|
|
[
|
|
|
|
optimize-label node-op set
|
|
|
|
node-param [ kill-nodes ] change
|
|
|
|
] extend ,
|
2005-03-05 14:45:23 -05:00
|
|
|
] "kill-node" set-word-prop
|
2004-12-04 15:10:46 -05:00
|
|
|
|
2005-01-17 15:33:12 -05:00
|
|
|
#values [
|
|
|
|
dupd consumes-literal? [
|
|
|
|
branch-returns get mentions-literal?
|
|
|
|
] [
|
|
|
|
drop t
|
|
|
|
] ifte
|
2005-03-05 14:45:23 -05:00
|
|
|
] "can-kill" set-word-prop
|
2005-01-17 15:33:12 -05:00
|
|
|
|
2005-03-05 14:45:23 -05:00
|
|
|
\ ifte [ scan-branches ] "scan-literal" set-word-prop
|
|
|
|
\ ifte [ can-kill-branches? ] "can-kill" set-word-prop
|
|
|
|
\ ifte [ kill-branches ] "kill-node" set-word-prop
|
2004-12-04 15:48:44 -05:00
|
|
|
|
2005-03-05 14:45:23 -05:00
|
|
|
\ dispatch [ scan-branches ] "scan-literal" set-word-prop
|
|
|
|
\ dispatch [ can-kill-branches? ] "can-kill" set-word-prop
|
|
|
|
\ dispatch [ kill-branches ] "kill-node" set-word-prop
|
2004-12-04 15:48:44 -05:00
|
|
|
|
2004-12-03 22:12:58 -05:00
|
|
|
! Don't care about inputs to recursive combinator calls
|
2005-03-05 14:45:23 -05:00
|
|
|
#call-label [ 2drop t ] "can-kill" set-word-prop
|
2004-12-03 22:12:58 -05:00
|
|
|
|
2005-03-05 14:45:23 -05:00
|
|
|
\ drop [ 2drop t ] "can-kill" set-word-prop
|
|
|
|
\ drop [ kill-node ] "kill-node" set-word-prop
|
|
|
|
\ dup [ 2drop t ] "can-kill" set-word-prop
|
|
|
|
\ dup [ kill-node ] "kill-node" set-word-prop
|
|
|
|
\ swap [ 2drop t ] "can-kill" set-word-prop
|
|
|
|
\ swap [ kill-node ] "kill-node" set-word-prop
|
2004-12-10 17:27:07 -05:00
|
|
|
|
2004-12-19 22:53:41 -05:00
|
|
|
: kill-mask ( killing inputs -- mask )
|
|
|
|
[ over [ over value= ] some? >boolean nip ] map nip ;
|
2004-12-10 02:41:52 -05:00
|
|
|
|
2004-12-10 17:27:07 -05:00
|
|
|
: reduce-stack-op ( literals node map -- )
|
2004-12-10 02:41:52 -05:00
|
|
|
#! If certain values passing through a stack op are being
|
|
|
|
#! killed, the stack op can be reduced, in extreme cases
|
|
|
|
#! to a no-op.
|
2004-12-19 22:53:41 -05:00
|
|
|
-rot [
|
|
|
|
[ node-consume-d get ] bind kill-mask swap assoc
|
|
|
|
] keep
|
2004-12-10 17:27:07 -05:00
|
|
|
over [ [ node-op set ] extend , ] [ 2drop ] ifte ;
|
2004-12-10 02:41:52 -05:00
|
|
|
|
2005-03-05 14:45:23 -05:00
|
|
|
\ over [ 2drop t ] "can-kill" set-word-prop
|
2005-01-06 21:42:07 -05:00
|
|
|
\ over [
|
2004-12-10 02:41:52 -05:00
|
|
|
[
|
2005-01-13 19:49:47 -05:00
|
|
|
[[ [ f f ] over ]]
|
|
|
|
[[ [ f t ] dup ]]
|
2004-12-10 17:27:07 -05:00
|
|
|
] reduce-stack-op
|
2005-03-05 14:45:23 -05:00
|
|
|
] "kill-node" set-word-prop
|
2004-12-10 02:41:52 -05:00
|
|
|
|
2005-03-05 14:45:23 -05:00
|
|
|
\ pick [ 2drop t ] "can-kill" set-word-prop
|
2005-01-06 21:42:07 -05:00
|
|
|
\ pick [
|
2004-12-10 02:41:52 -05:00
|
|
|
[
|
2005-01-13 19:49:47 -05:00
|
|
|
[[ [ f f f ] pick ]]
|
|
|
|
[[ [ f f t ] over ]]
|
|
|
|
[[ [ f t f ] over ]]
|
|
|
|
[[ [ f t t ] dup ]]
|
2004-12-10 17:27:07 -05:00
|
|
|
] reduce-stack-op
|
2005-03-05 14:45:23 -05:00
|
|
|
] "kill-node" set-word-prop
|
2004-12-03 22:12:58 -05:00
|
|
|
|
2005-03-05 14:45:23 -05:00
|
|
|
\ >r [ 2drop t ] "can-kill" set-word-prop
|
|
|
|
\ >r [ kill-node ] "kill-node" set-word-prop
|
|
|
|
\ r> [ 2drop t ] "can-kill" set-word-prop
|
|
|
|
\ r> [ kill-node ] "kill-node" set-word-prop
|