Updating CFG builder

db4
Slava Pestov 2008-08-11 02:49:37 -05:00
parent 58a40025f4
commit 5e9a323ac1
4 changed files with 104 additions and 105 deletions

View File

@ -0,0 +1,4 @@
IN: compiler.cfg.builder.tests
USING: compiler.cfg.builder tools.test ;
\ build-cfg must-infer

View File

@ -1,29 +1,33 @@
! Copyright (C) 2008 Slava Pestov. ! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: arrays kernel assocs sequences sequences.lib fry accessors USING: arrays kernel assocs sequences sequences.lib fry accessors
compiler.cfg compiler.vops compiler.vops.builder namespaces math combinators math.order
namespaces math inference.dataflow optimizer.allot combinators compiler.tree
math.order ; compiler.tree.combinators
compiler.tree.propagation.info
compiler.cfg
compiler.vops
compiler.vops.builder ;
IN: compiler.cfg.builder IN: compiler.cfg.builder
! Convert dataflow IR to procedure CFG. ! Convert tree SSA IR to CFG SSA IR.
! We construct the graph and set successors first, then we ! We construct the graph and set successors first, then we
! set predecessors in a separate pass. This simplifies the ! set predecessors in a separate pass. This simplifies the
! logic. ! logic.
SYMBOL: procedures SYMBOL: procedures
SYMBOL: values>vregs
SYMBOL: loop-nesting SYMBOL: loop-nesting
GENERIC: convert* ( node -- ) SYMBOL: values>vregs
GENERIC: convert ( node -- ) GENERIC: convert ( node -- )
M: #introduce convert drop ;
: init-builder ( -- ) : init-builder ( -- )
H{ } clone values>vregs set H{ } clone values>vregs set ;
V{ } clone loop-nesting set ;
: end-basic-block ( -- ) : end-basic-block ( -- )
basic-block get [ %b emit ] when ; basic-block get [ %b emit ] when ;
@ -40,15 +44,12 @@ GENERIC: convert ( node -- )
set-basic-block ; set-basic-block ;
: convert-nodes ( node -- ) : convert-nodes ( node -- )
dup basic-block get and [ [ convert ] each ;
[ convert ] [ successor>> convert-nodes ] bi
] [ drop ] if ;
: (build-cfg) ( node word -- ) : (build-cfg) ( node word -- )
init-builder init-builder
begin-basic-block begin-basic-block
basic-block get swap procedures get set-at basic-block get swap procedures get set-at
%prolog emit
convert-nodes ; convert-nodes ;
: build-cfg ( node word -- procedures ) : build-cfg ( node word -- procedures )
@ -73,10 +74,9 @@ GENERIC: convert ( node -- )
2bi 2bi
] if ; ] if ;
: load-inputs ( node -- ) : load-in-d ( node -- ) in-d>> %data (load-inputs) ;
[ in-d>> %data (load-inputs) ]
[ in-r>> %retain (load-inputs) ] : load-in-r ( node -- ) in-r>> %retain (load-inputs) ;
bi ;
: (store-outputs) ( seq stack -- ) : (store-outputs) ( seq stack -- )
over empty? [ 2drop ] [ over empty? [ 2drop ] [
@ -86,40 +86,21 @@ GENERIC: convert ( node -- )
2bi 2bi
] if ; ] if ;
: store-outputs ( node -- ) : store-out-d ( node -- ) out-d>> %data (store-outputs) ;
[ out-d>> %data (store-outputs) ]
[ out-r>> %retain (store-outputs) ]
bi ;
M: #push convert* : store-out-r ( node -- ) out-r>> %retain (store-outputs) ;
out-d>> [
[ produce-vreg ] [ value-literal ] bi
emit-literal
] each ;
M: #shuffle convert* drop ;
M: #>r convert* drop ;
M: #r> convert* drop ;
M: node convert
[ load-inputs ]
[ convert* ]
[ store-outputs ]
tri ;
: (emit-call) ( word -- ) : (emit-call) ( word -- )
begin-basic-block %call emit begin-basic-block ; begin-basic-block %call emit begin-basic-block ;
: intrinsic-inputs ( node -- ) : intrinsic-inputs ( node -- )
[ load-inputs ] [ load-in-d ]
[ in-d>> { #1 #2 #3 #4 } [ [ value>vreg ] dip set ] 2each ] [ in-d>> { #1 #2 #3 #4 } [ [ value>vreg ] dip set ] 2each ]
bi ; bi ;
: intrinsic-outputs ( node -- ) : intrinsic-outputs ( node -- )
[ out-d>> { ^1 ^2 ^3 ^4 } [ get output-vreg ] 2each ] [ out-d>> { ^1 ^2 ^3 ^4 } [ get output-vreg ] 2each ]
[ store-outputs ] [ store-out-d ]
bi ; bi ;
: intrinsic ( node quot -- ) : intrinsic ( node quot -- )
@ -132,19 +113,17 @@ M: node convert
tri tri
] with-scope ; inline ] with-scope ; inline
USING: kernel.private math.private slots.private USING: kernel.private math.private slots.private ;
optimizer.allot ;
: maybe-emit-fixnum-shift-fast ( node -- node ) : maybe-emit-fixnum-shift-fast ( node -- node )
dup dup in-d>> second node-literal? [ dup dup in-d>> second node-value-info literal>> dup fixnum? [
dup dup in-d>> second node-literal
'[ , emit-fixnum-shift-fast ] intrinsic '[ , emit-fixnum-shift-fast ] intrinsic
] [ ] [
dup param>> (emit-call) drop dup word>> (emit-call)
] if ; ] if ;
: emit-call ( node -- ) : emit-call ( node -- )
dup param>> { dup word>> {
{ \ tag [ [ emit-tag ] intrinsic ] } { \ tag [ [ emit-tag ] intrinsic ] }
{ \ slot [ [ dup emit-slot ] intrinsic ] } { \ slot [ [ dup emit-slot ] intrinsic ] }
@ -175,24 +154,43 @@ optimizer.allot ;
{ \ float> [ [ emit-float> ] intrinsic ] } { \ float> [ [ emit-float> ] intrinsic ] }
{ \ float? [ [ emit-float= ] intrinsic ] } { \ float? [ [ emit-float= ] intrinsic ] }
{ \ (tuple) [ dup first-input '[ , emit-(tuple) ] intrinsic ] } ! { \ (tuple) [ dup first-input '[ , emit-(tuple) ] intrinsic ] }
{ \ (array) [ dup first-input '[ , emit-(array) ] intrinsic ] } ! { \ (array) [ dup first-input '[ , emit-(array) ] intrinsic ] }
{ \ (byte-array) [ dup first-input '[ , emit-(byte-array) ] intrinsic ] } ! { \ (byte-array) [ dup first-input '[ , emit-(byte-array) ] intrinsic ] }
[ (emit-call) ] [ (emit-call) ]
} case drop ; } case drop ;
M: #call convert emit-call ; M: #call convert emit-call ;
M: #call-label convert : emit-call-loop ( #recursive -- )
dup param>> loop-nesting get at [ dup label>> loop-nesting get at basic-block get successors>> push
basic-block get successors>> push end-basic-block
end-basic-block basic-block off
basic-block off drop ;
drop
] [ : emit-call-recursive ( #recursive -- )
(emit-call) label>> id>> (emit-call) ;
] if* ;
M: #call-recursive convert
dup label>> loop?>>
[ emit-call-loop ] [ emit-call-recursive ] if ;
M: #push convert
[
[ out-d>> first produce-vreg ]
[ node-output-infos first literal>> ]
bi emit-literal
]
[ store-out-d ] bi ;
M: #shuffle convert [ load-in-d ] [ store-out-d ] bi ;
M: #>r convert [ load-in-d ] [ store-out-r ] bi ;
M: #r> convert [ load-in-r ] [ store-out-d ] bi ;
M: #terminate convert drop ;
: integer-conditional ( in1 in2 cc -- ) : integer-conditional ( in1 in2 cc -- )
[ [ next-vreg dup ] 2dip %icmp emit ] dip %bi emit ; inline [ [ next-vreg dup ] 2dip %icmp emit ] dip %bi emit ; inline
@ -221,50 +219,38 @@ M: #call-label convert
[ set-basic-block ] [ set-basic-block ]
bi ; bi ;
: phi-inputs ( #if -- vregs-seq )
children>>
[ last-node ] map
[ #values? ] filter
[ in-d>> [ value>vreg ] map ] map ;
: phi-outputs ( #if -- vregs )
successor>> out-d>> [ produce-vreg ] map ;
: emit-phi ( #if -- )
[ phi-outputs ] [ phi-inputs ] bi %phi emit ;
M: #if convert M: #if convert
{ [ load-in-d ] [ emit-if ] [ convert-if-children ] tri ;
[ load-inputs ]
[ emit-if ]
[ convert-if-children ]
[ emit-phi ]
} cleave ;
M: #values convert drop ; M: #dispatch convert
"Unimplemented" throw ;
M: #merge convert drop ; M: #phi convert drop ;
M: #entry convert drop ;
M: #declare convert drop ; M: #declare convert drop ;
M: #terminate convert drop ; M: #return convert drop %return emit ;
M: #label convert : convert-recursive ( #recursive -- )
#! Labels create a new procedure. [ [ label>> id>> ] [ child>> ] bi (build-cfg) ]
[ [ param>> ] [ node-child ] bi (build-cfg) ] [ (emit-call) ] bi ; [ (emit-call) ]
bi ;
M: #loop convert : begin-loop ( #recursive -- )
#! Loops become part of the current CFG. label>> basic-block get 2array loop-nesting get push ;
begin-basic-block
[ param>> basic-block get 2array loop-nesting get push ] : end-loop ( -- )
[ node-child convert-nodes ]
bi
loop-nesting get pop* ; loop-nesting get pop* ;
M: #return convert : convert-loop ( #recursive -- )
param>> loop-nesting get key? [ begin-basic-block
%epilog emit [ begin-loop ]
%return emit [ child>> convert-nodes ]
] unless ; [ drop end-loop ]
tri ;
M: #recursive convert
dup label>> loop?>>
[ convert-loop ] [ convert-recursive ] if ;
M: #copy convert drop ;

View File

@ -1,12 +1,17 @@
! Copyright (C) 2008 Slava Pestov. ! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel namespaces sequences assocs io USING: accessors kernel namespaces sequences assocs io
prettyprint inference generator optimizer compiler.vops prettyprint inference generator optimizer
compiler.cfg.builder compiler.cfg.simplifier compiler.vops
compiler.machine.builder compiler.machine.simplifier ; compiler.tree.builder
IN: compiler.machine.debug compiler.tree.optimizer
compiler.cfg.builder
compiler.cfg.simplifier
compiler.machine.builder
compiler.machine.simplifier ;
IN: compiler.machine.debugger
: dataflow>linear ( dataflow word -- linear ) : tree>linear ( tree word -- linear )
[ [
init-counter init-counter
build-cfg build-cfg
@ -20,15 +25,16 @@ IN: compiler.machine.debug
] assoc-each ; ] assoc-each ;
: linearized-quot. ( quot -- ) : linearized-quot. ( quot -- )
dataflow optimize build-tree optimize-tree
"Anonymous quotation" dataflow>linear "Anonymous quotation" tree>linear
linear. ; linear. ;
: linearized-word. ( word -- ) : linearized-word. ( word -- )
dup word-dataflow nip optimize swap dataflow>linear linear. ; dup build-tree-from-word nip optimize-tree
dup word-dataflow nip optimize swap tree>linear linear. ;
: >basic-block ( quot -- basic-block ) : >basic-block ( quot -- basic-block )
dataflow optimize build-tree optimize-tree
[ [
init-counter init-counter
"Anonymous quotation" build-cfg "Anonymous quotation" build-cfg

View File

@ -23,6 +23,7 @@ MATCH-VARS: ?a ?b ?c ;
: pretty-shuffle ( in out -- word/f ) : pretty-shuffle ( in out -- word/f )
2array { 2array {
{ { { } { } } [ ] }
{ { { ?a } { ?a } } [ ] } { { { ?a } { ?a } } [ ] }
{ { { ?a ?b } { ?a ?b } } [ ] } { { { ?a ?b } { ?a ?b } } [ ] }
{ { { ?a ?b ?c } { ?a ?b ?c } } [ ] } { { { ?a ?b ?c } { ?a ?b ?c } } [ ] }
@ -34,6 +35,8 @@ MATCH-VARS: ?a ?b ?c ;
{ { { ?a ?b ?c } { ?a ?b ?c ?a ?b ?c } } [ 3dup ] } { { { ?a ?b ?c } { ?a ?b ?c ?a ?b ?c } } [ 3dup ] }
{ { { ?a ?b } { ?a ?b ?a } } [ over ] } { { { ?a ?b } { ?a ?b ?a } } [ over ] }
{ { { ?b ?a } { ?a ?b } } [ swap ] } { { { ?b ?a } { ?a ?b } } [ swap ] }
{ { { ?b ?a ?c } { ?a ?b ?c } } [ swapd ] }
{ { { ?a ?b } { ?a ?a ?b } } [ dupd ] }
{ { { ?a ?b } { ?b ?a ?b } } [ tuck ] } { { { ?a ?b } { ?b ?a ?b } } [ tuck ] }
{ { { ?a ?b ?c } { ?a ?b ?c ?a } } [ pick ] } { { { ?a ?b ?c } { ?a ?b ?c ?a } } [ pick ] }
{ { { ?a ?b ?c } { ?c ?a ?b } } [ -rot ] } { { { ?a ?b ?c } { ?c ?a ?b } } [ -rot ] }
@ -88,7 +91,7 @@ M: node node>quot drop ;
: nodes>quot ( node -- quot ) : nodes>quot ( node -- quot )
[ [ node>quot ] each ] [ ] make ; [ [ node>quot ] each ] [ ] make ;
: optimized-quot. ( quot -- ) : optimized. ( quot/word -- )
dup word? [ specialized-def ] when dup word? [ specialized-def ] when
build-tree optimize-tree nodes>quot . ; build-tree optimize-tree nodes>quot . ;