2008-07-27 21:25:42 -04:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-07-30 04:38:10 -04:00
|
|
|
USING: accessors kernel arrays sequences math math.order
|
2008-08-31 20:17:04 -04:00
|
|
|
math.partial-dispatch generic generic.standard generic.math
|
|
|
|
classes.algebra classes.union sets quotations assocs combinators
|
2008-09-12 19:08:38 -04:00
|
|
|
words namespaces continuations
|
2008-07-30 04:38:10 -04:00
|
|
|
compiler.tree
|
|
|
|
compiler.tree.builder
|
2008-09-12 06:17:27 -04:00
|
|
|
compiler.tree.recursive
|
2008-09-12 09:18:44 -04:00
|
|
|
compiler.tree.combinators
|
2008-07-30 04:38:10 -04:00
|
|
|
compiler.tree.normalization
|
|
|
|
compiler.tree.propagation.info
|
|
|
|
compiler.tree.propagation.nodes ;
|
2008-07-27 21:25:42 -04:00
|
|
|
IN: compiler.tree.propagation.inlining
|
2008-07-30 04:38:10 -04:00
|
|
|
|
2008-09-12 09:18:44 -04:00
|
|
|
! We count nodes up-front; if there are relatively few nodes,
|
|
|
|
! we are more eager to inline
|
|
|
|
SYMBOL: node-count
|
|
|
|
|
|
|
|
: count-nodes ( nodes -- )
|
|
|
|
0 swap [ drop 1+ ] each-node node-count set ;
|
|
|
|
|
2008-07-30 04:38:10 -04:00
|
|
|
! Splicing nodes
|
|
|
|
GENERIC: splicing-nodes ( #call word/quot/f -- nodes )
|
|
|
|
|
|
|
|
M: word splicing-nodes
|
|
|
|
[ [ in-d>> ] [ out-d>> ] bi ] dip #call 1array ;
|
|
|
|
|
|
|
|
M: quotation splicing-nodes
|
2008-09-12 06:36:06 -04:00
|
|
|
build-sub-tree analyze-recursive normalize ;
|
2008-07-30 04:38:10 -04:00
|
|
|
|
|
|
|
: propagate-body ( #call -- )
|
2008-08-07 02:08:11 -04:00
|
|
|
body>> (propagate) ;
|
2008-07-30 04:38:10 -04:00
|
|
|
|
|
|
|
! Dispatch elimination
|
2008-09-12 19:08:38 -04:00
|
|
|
: eliminate-dispatch ( #call class/f word/quot/f -- ? )
|
2008-08-30 03:31:27 -04:00
|
|
|
dup [
|
|
|
|
[ >>class ] dip
|
2008-07-30 04:38:10 -04:00
|
|
|
over method>> over = [ drop ] [
|
|
|
|
2dup splicing-nodes
|
|
|
|
[ >>method ] [ >>body ] bi*
|
|
|
|
] if
|
|
|
|
propagate-body t
|
2008-08-30 03:31:27 -04:00
|
|
|
] [ 2drop f >>method f >>body f >>class drop f ] if ;
|
2008-07-30 04:38:10 -04:00
|
|
|
|
2008-08-30 03:31:27 -04:00
|
|
|
: inlining-standard-method ( #call word -- class/f method/f )
|
2008-07-30 04:38:10 -04:00
|
|
|
[ in-d>> <reversed> ] [ [ dispatch# ] keep ] bi*
|
2008-08-30 03:31:27 -04:00
|
|
|
[ swap nth value-info class>> dup ] dip
|
2008-07-30 04:38:10 -04:00
|
|
|
specific-method ;
|
|
|
|
|
|
|
|
: inline-standard-method ( #call word -- ? )
|
|
|
|
dupd inlining-standard-method eliminate-dispatch ;
|
|
|
|
|
|
|
|
: normalize-math-class ( class -- class' )
|
|
|
|
{
|
|
|
|
null
|
|
|
|
fixnum bignum integer
|
|
|
|
ratio rational
|
|
|
|
float real
|
|
|
|
complex number
|
|
|
|
object
|
|
|
|
} [ class<= ] with find nip ;
|
|
|
|
|
2008-08-30 03:31:27 -04:00
|
|
|
: inlining-math-method ( #call word -- class/f quot/f )
|
2008-07-30 04:38:10 -04:00
|
|
|
swap in-d>>
|
|
|
|
first2 [ value-info class>> normalize-math-class ] bi@
|
2008-08-30 03:31:27 -04:00
|
|
|
3dup math-both-known?
|
|
|
|
[ math-method* ] [ 3drop f ] if
|
|
|
|
number swap ;
|
2008-07-30 04:38:10 -04:00
|
|
|
|
|
|
|
: inline-math-method ( #call word -- ? )
|
|
|
|
dupd inlining-math-method eliminate-dispatch ;
|
|
|
|
|
2008-08-30 03:31:27 -04:00
|
|
|
: inlining-math-partial ( #call word -- class/f quot/f )
|
2008-07-30 04:38:10 -04:00
|
|
|
[ "derived-from" word-prop first inlining-math-method ]
|
|
|
|
[ nip 1quotation ] 2bi
|
|
|
|
[ = not ] [ drop ] 2bi and ;
|
|
|
|
|
|
|
|
: inline-math-partial ( #call word -- ? )
|
|
|
|
dupd inlining-math-partial eliminate-dispatch ;
|
|
|
|
|
|
|
|
! Method body inlining
|
|
|
|
SYMBOL: recursive-calls
|
|
|
|
DEFER: (flat-length)
|
|
|
|
|
|
|
|
: word-flat-length ( word -- n )
|
|
|
|
{
|
|
|
|
! not inline
|
|
|
|
{ [ dup inline? not ] [ drop 1 ] }
|
|
|
|
! recursive and inline
|
|
|
|
{ [ dup recursive-calls get key? ] [ drop 10 ] }
|
|
|
|
! inline
|
|
|
|
[ [ recursive-calls get conjoin ] [ def>> (flat-length) ] bi ]
|
|
|
|
} cond ;
|
|
|
|
|
|
|
|
: (flat-length) ( seq -- n )
|
|
|
|
[
|
|
|
|
{
|
|
|
|
{ [ dup quotation? ] [ (flat-length) 2 + ] }
|
|
|
|
{ [ dup array? ] [ (flat-length) ] }
|
|
|
|
{ [ dup word? ] [ word-flat-length ] }
|
|
|
|
[ drop 0 ]
|
|
|
|
} cond
|
|
|
|
] sigma ;
|
|
|
|
|
|
|
|
: flat-length ( word -- n )
|
|
|
|
H{ } clone recursive-calls [
|
|
|
|
[ recursive-calls get conjoin ]
|
|
|
|
[ def>> (flat-length) 5 /i ]
|
|
|
|
bi
|
|
|
|
] with-variable ;
|
|
|
|
|
|
|
|
: classes-known? ( #call -- ? )
|
|
|
|
in-d>> [
|
|
|
|
value-info class>>
|
|
|
|
[ class-types length 1 = ]
|
|
|
|
[ union-class? not ]
|
|
|
|
bi and
|
|
|
|
] contains? ;
|
|
|
|
|
|
|
|
: inlining-rank ( #call word -- n )
|
|
|
|
[ classes-known? 2 0 ? ]
|
|
|
|
[
|
|
|
|
{
|
2008-09-12 09:18:44 -04:00
|
|
|
[ drop node-count get 45 swap [-] 8 /i ]
|
2008-07-30 04:38:10 -04:00
|
|
|
[ flat-length 24 swap [-] 4 /i ]
|
|
|
|
[ "default" word-prop -4 0 ? ]
|
|
|
|
[ "specializer" word-prop 1 0 ? ]
|
|
|
|
[ method-body? 1 0 ? ]
|
|
|
|
} cleave
|
2008-09-12 09:18:44 -04:00
|
|
|
] bi* + + + + + ;
|
2008-07-30 04:38:10 -04:00
|
|
|
|
|
|
|
: should-inline? ( #call word -- ? )
|
|
|
|
inlining-rank 5 >= ;
|
|
|
|
|
|
|
|
SYMBOL: history
|
|
|
|
|
|
|
|
: remember-inlining ( word -- )
|
2008-08-15 00:35:19 -04:00
|
|
|
history [ swap suffix ] change ;
|
2008-07-30 04:38:10 -04:00
|
|
|
|
2008-08-28 23:28:34 -04:00
|
|
|
: inline-word ( #call word -- ? )
|
2008-07-30 04:38:10 -04:00
|
|
|
dup history get memq? [
|
2008-08-28 23:28:34 -04:00
|
|
|
2drop f
|
2008-07-30 04:38:10 -04:00
|
|
|
] [
|
|
|
|
[
|
|
|
|
dup remember-inlining
|
|
|
|
dupd def>> splicing-nodes >>body
|
|
|
|
propagate-body
|
|
|
|
] with-scope
|
2008-08-28 23:28:34 -04:00
|
|
|
t
|
2008-07-30 04:38:10 -04:00
|
|
|
] if ;
|
|
|
|
|
|
|
|
: inline-method-body ( #call word -- ? )
|
2008-08-28 23:28:34 -04:00
|
|
|
2dup should-inline? [ inline-word ] [ 2drop f ] if ;
|
2008-08-02 00:31:43 -04:00
|
|
|
|
|
|
|
: always-inline-word? ( word -- ? )
|
|
|
|
{ curry compose } memq? ;
|
2008-08-31 20:17:04 -04:00
|
|
|
|
2008-09-12 19:08:38 -04:00
|
|
|
: custom-inlining? ( word -- ? )
|
|
|
|
"custom-inlining" word-prop ;
|
|
|
|
|
|
|
|
: inline-custom ( #call word -- ? )
|
|
|
|
[ dup 1array ] [ "custom-inlining" word-prop ] bi* with-datastack
|
|
|
|
first object swap eliminate-dispatch ;
|
|
|
|
|
2008-08-31 20:17:04 -04:00
|
|
|
: do-inlining ( #call word -- ? )
|
|
|
|
{
|
2008-09-12 19:08:38 -04:00
|
|
|
{ [ dup custom-inlining? ] [ inline-custom ] }
|
2008-08-31 20:17:04 -04:00
|
|
|
{ [ dup always-inline-word? ] [ inline-word ] }
|
|
|
|
{ [ dup standard-generic? ] [ inline-standard-method ] }
|
|
|
|
{ [ dup math-generic? ] [ inline-math-method ] }
|
|
|
|
{ [ dup method-body? ] [ inline-method-body ] }
|
|
|
|
[ 2drop f ]
|
|
|
|
} cond ;
|