Split off local-optimization combinator into compiler.cfg.local, factor out CFG -> MR into compiler.cfg.mr, split off GC check insertion into a new compiler.cfg.gc-checks pass
							parent
							
								
									3e00dc8c8d
								
							
						
					
					
						commit
						692b479302
					
				| 
						 | 
				
			
			@ -4,7 +4,7 @@ USING: kernel math namespaces assocs hashtables sequences arrays
 | 
			
		|||
accessors vectors combinators sets classes compiler.cfg
 | 
			
		||||
compiler.cfg.registers compiler.cfg.instructions
 | 
			
		||||
compiler.cfg.copy-prop compiler.cfg.rpo
 | 
			
		||||
compiler.cfg.liveness ;
 | 
			
		||||
compiler.cfg.liveness compiler.cfg.local ;
 | 
			
		||||
IN: compiler.cfg.alias-analysis
 | 
			
		||||
 | 
			
		||||
! We try to eliminate redundant slot operations using some simple heuristics.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -54,5 +54,5 @@ ERROR: undefined-values uses defs ;
 | 
			
		|||
    compute-liveness
 | 
			
		||||
    [ entry>> live-in assoc-empty? [ bad-live-in ] unless ]
 | 
			
		||||
    [ [ check-basic-block ] each-basic-block ]
 | 
			
		||||
    [ build-mr check-mr ]
 | 
			
		||||
    [ flatten-cfg check-mr ]
 | 
			
		||||
    tri ;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,7 +7,8 @@ parser compiler.tree.builder compiler.tree.optimizer
 | 
			
		|||
compiler.cfg.builder compiler.cfg.linearization
 | 
			
		||||
compiler.cfg.registers compiler.cfg.stack-frame
 | 
			
		||||
compiler.cfg.linear-scan compiler.cfg.two-operand
 | 
			
		||||
compiler.cfg.optimizer ;
 | 
			
		||||
compiler.cfg.liveness compiler.cfg.optimizer
 | 
			
		||||
compiler.cfg.mr ;
 | 
			
		||||
IN: compiler.cfg.debugger
 | 
			
		||||
 | 
			
		||||
GENERIC: test-cfg ( quot -- cfgs )
 | 
			
		||||
| 
						 | 
				
			
			@ -18,20 +19,14 @@ M: callable test-cfg
 | 
			
		|||
M: word test-cfg
 | 
			
		||||
    [ build-tree optimize-tree ] keep build-cfg ;
 | 
			
		||||
 | 
			
		||||
SYMBOL: allocate-registers?
 | 
			
		||||
 | 
			
		||||
: test-mr ( quot -- mrs )
 | 
			
		||||
    test-cfg [
 | 
			
		||||
        optimize-cfg
 | 
			
		||||
        convert-two-operand
 | 
			
		||||
        allocate-registers? get [ linear-scan ] when
 | 
			
		||||
        build-mr
 | 
			
		||||
        allocate-registers? get [ build-stack-frame ] when
 | 
			
		||||
    ] map ;
 | 
			
		||||
 | 
			
		||||
: insn. ( insn -- )
 | 
			
		||||
    tuple>array allocate-registers? get [ but-last ] unless
 | 
			
		||||
    [ pprint bl ] each nl ;
 | 
			
		||||
    tuple>array [ pprint bl ] each nl ;
 | 
			
		||||
 | 
			
		||||
: mr. ( mrs -- )
 | 
			
		||||
    [
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -54,6 +54,7 @@ M: ##phi uses-vregs inputs>> ;
 | 
			
		|||
M: _conditional-branch uses-vregs [ src1>> ] [ src2>> ] bi 2array ;
 | 
			
		||||
M: _compare-imm-branch uses-vregs src1>> 1array ;
 | 
			
		||||
M: _dispatch uses-vregs src>> 1array ;
 | 
			
		||||
M: _gc uses-vregs live-in>> ;
 | 
			
		||||
M: insn uses-vregs drop f ;
 | 
			
		||||
 | 
			
		||||
! Instructions that use vregs
 | 
			
		||||
| 
						 | 
				
			
			@ -67,4 +68,5 @@ UNION: vreg-insn
 | 
			
		|||
##compare-imm-branch
 | 
			
		||||
_conditional-branch
 | 
			
		||||
_compare-imm-branch
 | 
			
		||||
_dispatch ;
 | 
			
		||||
_dispatch
 | 
			
		||||
_gc ;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
Slava Pestov
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,22 @@
 | 
			
		|||
! Copyright (C) 2009 Slava Pestov.
 | 
			
		||||
! See http://factorcode.org/license.txt for BSD license.
 | 
			
		||||
USING: accessors kernel sequences assocs
 | 
			
		||||
cpu.architecture compiler.cfg.rpo
 | 
			
		||||
compiler.cfg.liveness compiler.cfg.instructions ;
 | 
			
		||||
IN: compiler.cfg.gc-checks
 | 
			
		||||
 | 
			
		||||
: gc? ( bb -- ? )
 | 
			
		||||
    instructions>> [ ##allocation? ] any? ;
 | 
			
		||||
 | 
			
		||||
: object-pointer-regs ( basic-block -- vregs )
 | 
			
		||||
    live-in keys [ reg-class>> int-regs eq? ] filter ;
 | 
			
		||||
 | 
			
		||||
: insert-gc-check ( basic-block -- )
 | 
			
		||||
    dup gc? [
 | 
			
		||||
        dup
 | 
			
		||||
        [ swap object-pointer-regs \ _gc new-insn suffix ]
 | 
			
		||||
        change-instructions drop
 | 
			
		||||
    ] [ drop ] if ;
 | 
			
		||||
 | 
			
		||||
: insert-gc-checks ( cfg -- cfg' )
 | 
			
		||||
    dup [ insert-gc-check ] each-basic-block ;
 | 
			
		||||
| 
						 | 
				
			
			@ -2,7 +2,7 @@
 | 
			
		|||
! See http://factorcode.org/license.txt for BSD license.
 | 
			
		||||
USING: accessors math namespaces sequences kernel fry
 | 
			
		||||
compiler.cfg compiler.cfg.registers compiler.cfg.instructions
 | 
			
		||||
compiler.cfg.liveness ;
 | 
			
		||||
compiler.cfg.liveness compiler.cfg.local ;
 | 
			
		||||
IN: compiler.cfg.height
 | 
			
		||||
 | 
			
		||||
! Combine multiple stack height changes into one at the
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -12,20 +12,10 @@ IN: compiler.cfg.linearization
 | 
			
		|||
! Convert CFG IR to machine IR.
 | 
			
		||||
GENERIC: linearize-insn ( basic-block insn -- )
 | 
			
		||||
 | 
			
		||||
: linearize-insns ( bb insns -- )
 | 
			
		||||
    dup instructions>> [ linearize-insn ] with each ;
 | 
			
		||||
 | 
			
		||||
: gc? ( bb -- ? )
 | 
			
		||||
    instructions>> [ ##allocation? ] any? ;
 | 
			
		||||
 | 
			
		||||
: object-pointer-regs ( basic-block -- vregs )
 | 
			
		||||
    live-in keys [ reg-class>> int-regs eq? ] filter ;
 | 
			
		||||
 | 
			
		||||
: linearize-basic-block ( bb -- )
 | 
			
		||||
    [ number>> _label ]
 | 
			
		||||
    [ dup gc? [ object-pointer-regs _gc ] [ drop ] if ]
 | 
			
		||||
    [ linearize-insns ]
 | 
			
		||||
    tri ;
 | 
			
		||||
    [ dup instructions>> [ linearize-insn ] with each ]
 | 
			
		||||
    bi ;
 | 
			
		||||
 | 
			
		||||
M: insn linearize-insn , drop ;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -85,6 +75,6 @@ M: ##dispatch linearize-insn
 | 
			
		|||
        bi
 | 
			
		||||
    ] { } make ;
 | 
			
		||||
 | 
			
		||||
: build-mr ( cfg -- mr )
 | 
			
		||||
: flatten-cfg ( cfg -- mr )
 | 
			
		||||
    [ linearize-basic-blocks ] [ word>> ] [ label>> ] tri
 | 
			
		||||
    <mr> ;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -76,6 +76,3 @@ SYMBOL: work-list
 | 
			
		|||
    H{ } clone live-outs set
 | 
			
		||||
    dup post-order add-to-work-list
 | 
			
		||||
    work-list get [ liveness-step ] slurp-deque ;
 | 
			
		||||
 | 
			
		||||
: local-optimization ( cfg init-quot: ( live-in -- ) insn-quot: ( insns -- insns' ) -- cfg' )
 | 
			
		||||
    [ dup ] 2dip '[ _ _ optimize-basic-block ] each-basic-block ;
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
Slava Pestov
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,10 @@
 | 
			
		|||
! Copyright (C) 2009 Slava Pestov.
 | 
			
		||||
! See http://factorcode.org/license.txt for BSD license.
 | 
			
		||||
USING: fry accessors kernel assocs compiler.cfg.liveness compiler.cfg.rpo ;
 | 
			
		||||
IN: compiler.cfg.local
 | 
			
		||||
 | 
			
		||||
: optimize-basic-block ( bb init-quot insn-quot -- )
 | 
			
		||||
    [ '[ live-in keys @ ] ] [ '[ _ change-instructions drop ] ] bi* bi ; inline
 | 
			
		||||
 | 
			
		||||
: local-optimization ( cfg init-quot: ( live-in -- ) insn-quot: ( insns -- insns' ) -- cfg' )
 | 
			
		||||
    [ dup ] 2dip '[ _ _ optimize-basic-block ] each-basic-block ;
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
Slava Pestov
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,14 @@
 | 
			
		|||
! Copyright (C) 2009 Slava Pestov.
 | 
			
		||||
! See http://factorcode.org/license.txt for BSD license.
 | 
			
		||||
USING: compiler.cfg.linearization compiler.cfg.two-operand
 | 
			
		||||
compiler.cfg.liveness compiler.cfg.gc-checks compiler.cfg.linear-scan
 | 
			
		||||
compiler.cfg.stack-frame compiler.cfg.rpo ;
 | 
			
		||||
IN: compiler.cfg.mr
 | 
			
		||||
 | 
			
		||||
: build-mr ( cfg -- mr )
 | 
			
		||||
    convert-two-operand
 | 
			
		||||
    compute-liveness
 | 
			
		||||
    insert-gc-checks
 | 
			
		||||
    linear-scan
 | 
			
		||||
    flatten-cfg
 | 
			
		||||
    build-stack-frame ;
 | 
			
		||||
| 
						 | 
				
			
			@ -34,6 +34,3 @@ SYMBOL: visited
 | 
			
		|||
 | 
			
		||||
: each-basic-block ( cfg quot -- )
 | 
			
		||||
    [ reverse-post-order ] dip each ; inline
 | 
			
		||||
 | 
			
		||||
: optimize-basic-block ( bb init-quot insn-quot -- )
 | 
			
		||||
    [ '[ live-in keys @ ] ] [ '[ _ change-instructions drop ] ] bi* bi ; inline
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -31,7 +31,7 @@ IN: compiler.cfg.stack-analysis.tests
 | 
			
		|||
    dup check-for-redundant-ops ;
 | 
			
		||||
 | 
			
		||||
: linearize ( cfg -- mr )
 | 
			
		||||
    build-mr instructions>> ;
 | 
			
		||||
    flatten-cfg instructions>> ;
 | 
			
		||||
 | 
			
		||||
[ ] [ [ ] test-stack-analysis drop ] unit-test
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -98,9 +98,13 @@ UNION: sync-if-back-edge
 | 
			
		|||
    ##compare-imm-branch
 | 
			
		||||
    ##dispatch ;
 | 
			
		||||
 | 
			
		||||
SYMBOL: local-only?
 | 
			
		||||
 | 
			
		||||
t local-only? set-global
 | 
			
		||||
 | 
			
		||||
M: sync-if-back-edge visit
 | 
			
		||||
    basic-block get [ successors>> ] [ number>> ] bi
 | 
			
		||||
    '[ number>> _ < ] any?
 | 
			
		||||
    '[ number>> _ < local-only? get or ] any?
 | 
			
		||||
    [ sync-state ] when
 | 
			
		||||
    , ;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,6 +2,7 @@
 | 
			
		|||
! See http://factorcode.org/license.txt for BSD license.
 | 
			
		||||
USING: namespaces assocs biassocs classes kernel math accessors
 | 
			
		||||
sorting sets sequences
 | 
			
		||||
compiler.cfg.local
 | 
			
		||||
compiler.cfg.liveness
 | 
			
		||||
compiler.cfg.value-numbering.graph
 | 
			
		||||
compiler.cfg.value-numbering.expressions
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,7 +2,7 @@
 | 
			
		|||
! See http://factorcode.org/license.txt for BSD license.
 | 
			
		||||
USING: kernel accessors namespaces assocs sets sequences locals
 | 
			
		||||
compiler.cfg compiler.cfg.instructions compiler.cfg.copy-prop
 | 
			
		||||
compiler.cfg.liveness ;
 | 
			
		||||
compiler.cfg.liveness compiler.cfg.local ;
 | 
			
		||||
IN: compiler.cfg.write-barrier
 | 
			
		||||
 | 
			
		||||
! Eliminate redundant write barrier hits.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,13 +3,20 @@
 | 
			
		|||
USING: accessors kernel namespaces arrays sequences io words fry
 | 
			
		||||
continuations vocabs assocs dlists definitions math graphs generic
 | 
			
		||||
generic.single combinators deques search-deques macros
 | 
			
		||||
source-files.errors stack-checker stack-checker.state
 | 
			
		||||
stack-checker.inlining stack-checker.errors combinators.short-circuit
 | 
			
		||||
compiler.errors compiler.units compiler.tree.builder
 | 
			
		||||
compiler.tree.optimizer compiler.cfg.builder compiler.cfg.optimizer
 | 
			
		||||
compiler.cfg.linearization compiler.cfg.two-operand
 | 
			
		||||
compiler.cfg.linear-scan compiler.cfg.stack-frame compiler.cfg.rpo
 | 
			
		||||
compiler.codegen compiler.utilities ;
 | 
			
		||||
source-files.errors combinators.short-circuit
 | 
			
		||||
 | 
			
		||||
stack-checker stack-checker.state stack-checker.inlining stack-checker.errors
 | 
			
		||||
 | 
			
		||||
compiler.errors compiler.units compiler.utilities
 | 
			
		||||
 | 
			
		||||
compiler.tree.builder
 | 
			
		||||
compiler.tree.optimizer
 | 
			
		||||
 | 
			
		||||
compiler.cfg.builder
 | 
			
		||||
compiler.cfg.optimizer
 | 
			
		||||
compiler.cfg.mr
 | 
			
		||||
 | 
			
		||||
compiler.codegen ;
 | 
			
		||||
IN: compiler
 | 
			
		||||
 | 
			
		||||
SYMBOL: compile-queue
 | 
			
		||||
| 
						 | 
				
			
			@ -146,10 +153,7 @@ t compile-dependencies? set-global
 | 
			
		|||
: backend ( nodes word -- )
 | 
			
		||||
    build-cfg [
 | 
			
		||||
        optimize-cfg
 | 
			
		||||
        convert-two-operand
 | 
			
		||||
        linear-scan
 | 
			
		||||
        build-mr
 | 
			
		||||
        build-stack-frame
 | 
			
		||||
        generate
 | 
			
		||||
        save-asm
 | 
			
		||||
    ] each ;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue