2010-02-10 22:43:30 -05:00
|
|
|
! Copyright (C) 2009, 2010 Daniel Ehrenberg.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2014-11-07 18:49:59 -05:00
|
|
|
USING: accessors arrays assocs compiler.cfg.def-use compiler.cfg.dependence
|
|
|
|
compiler.cfg.instructions compiler.cfg.linear-scan.numbering compiler.cfg.rpo
|
|
|
|
cpu.architecture fry kernel make math namespaces sequences sets splitting ;
|
2014-11-09 01:34:31 -05:00
|
|
|
FROM: namespaces => set ;
|
2010-02-10 22:43:30 -05:00
|
|
|
IN: compiler.cfg.scheduling
|
|
|
|
|
|
|
|
! Instruction scheduling to reduce register pressure, from:
|
|
|
|
! "Register-sensitive selection, duplication, and
|
|
|
|
! sequencing of instructions"
|
|
|
|
! by Vivek Sarkar, et al.
|
|
|
|
! http://portal.acm.org/citation.cfm?id=377849
|
|
|
|
|
|
|
|
ERROR: bad-delete-at key assoc ;
|
|
|
|
|
|
|
|
: check-delete-at ( key assoc -- )
|
|
|
|
2dup key? [ delete-at ] [ bad-delete-at ] if ;
|
|
|
|
|
|
|
|
: set-parent-indices ( node -- )
|
|
|
|
children>> building get length
|
|
|
|
'[ _ >>parent-index drop ] each ;
|
|
|
|
|
2014-11-09 01:34:31 -05:00
|
|
|
: ready? ( node -- ? ) precedes>> assoc-empty? ;
|
|
|
|
|
2014-11-10 19:34:29 -05:00
|
|
|
: remove-node ( roots node -- )
|
|
|
|
dup follows>> [ [ precedes>> check-delete-at ] with each ] keep
|
|
|
|
[ ready? ] filter swap push-all ;
|
2010-02-10 22:43:30 -05:00
|
|
|
|
2014-11-08 13:01:00 -05:00
|
|
|
: score ( node -- n )
|
2010-02-12 00:12:17 -05:00
|
|
|
[ parent-index>> ] [ registers>> neg ] [ insn>> insn#>> ] tri 3array ;
|
2010-02-10 22:43:30 -05:00
|
|
|
|
2014-11-08 22:34:10 -05:00
|
|
|
: select ( vector quot: ( elt -- score ) -- elt )
|
|
|
|
dupd supremum-by swap dupd remove-eq! drop ; inline
|
2010-02-10 22:43:30 -05:00
|
|
|
|
2014-11-10 19:34:29 -05:00
|
|
|
: select-instruction ( roots -- insn/f )
|
|
|
|
[ f ] [
|
|
|
|
dup [ score ] select
|
|
|
|
[ remove-node ] keep
|
|
|
|
[ insn>> ] [ set-parent-indices ] bi
|
2010-02-10 22:43:30 -05:00
|
|
|
] if-empty ;
|
|
|
|
|
2014-11-10 19:34:29 -05:00
|
|
|
: (reorder) ( roots -- )
|
|
|
|
dup select-instruction [ , (reorder) ] [ drop ] if* ;
|
2010-02-10 22:43:30 -05:00
|
|
|
|
2010-07-15 17:38:34 -04:00
|
|
|
UNION: initial-insn ##phi ##inc-d ##inc-r ##callback-inputs ;
|
2010-02-10 22:43:30 -05:00
|
|
|
|
2010-07-15 17:38:34 -04:00
|
|
|
UNION: final-insn
|
|
|
|
##branch
|
2010-07-15 20:55:31 -04:00
|
|
|
##dispatch
|
2010-07-15 17:38:34 -04:00
|
|
|
conditional-branch-insn
|
2011-10-18 01:43:19 -04:00
|
|
|
##safepoint
|
2010-07-15 17:38:34 -04:00
|
|
|
##epilogue ##return
|
|
|
|
##callback-outputs ;
|
2010-02-24 15:20:43 -05:00
|
|
|
|
2010-07-15 17:38:34 -04:00
|
|
|
: initial-insn-end ( insns -- n )
|
|
|
|
[ initial-insn? not ] find drop 0 or ;
|
|
|
|
|
|
|
|
: final-insn-start ( insns -- n )
|
|
|
|
[ final-insn? not ] find-last drop [ 1 + ] [ 0 ] if* ;
|
|
|
|
|
2014-11-08 13:01:00 -05:00
|
|
|
: split-insns ( insns -- pre/body/post )
|
2014-11-08 19:46:04 -05:00
|
|
|
dup [ initial-insn-end ] [ final-insn-start ] bi 2array split-indices ;
|
2014-11-08 16:18:02 -05:00
|
|
|
|
2014-11-09 01:34:31 -05:00
|
|
|
: setup-root-nodes ( insns -- roots )
|
2014-11-08 16:18:02 -05:00
|
|
|
[ <node> ] map
|
2014-11-09 01:34:31 -05:00
|
|
|
[ build-dependence-graph ]
|
|
|
|
[ build-fan-in-trees ]
|
|
|
|
[ [ ready? ] V{ } filter-as ] tri ;
|
|
|
|
|
|
|
|
: reorder-body ( body -- body' )
|
2014-11-10 19:34:29 -05:00
|
|
|
setup-root-nodes [ (reorder) ] V{ } make reverse ;
|
2010-02-10 22:43:30 -05:00
|
|
|
|
|
|
|
: reorder ( insns -- insns' )
|
2014-11-08 16:18:02 -05:00
|
|
|
split-insns first3 [ reorder-body ] dip 3append ;
|
2010-02-10 22:43:30 -05:00
|
|
|
|
|
|
|
: schedule-block ( bb -- )
|
2014-11-07 18:49:59 -05:00
|
|
|
[ reorder ] change-instructions drop ;
|
|
|
|
|
|
|
|
! TODO: stack effect should be ( cfg -- )
|
|
|
|
: schedule-instructions ( cfg -- cfg' )
|
|
|
|
dup number-instructions
|
2014-11-08 22:34:10 -05:00
|
|
|
dup reverse-post-order [ kill-block?>> not ] filter
|
|
|
|
[ schedule-block ] each ;
|