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 ;
|
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 ;
|
|
|
|
|
|
|
|
: remove-node ( node -- )
|
2010-03-24 10:38:52 -04:00
|
|
|
[ follows>> members ] keep
|
2010-02-10 22:43:30 -05:00
|
|
|
'[ [ precedes>> _ swap check-delete-at ] each ]
|
|
|
|
[ [ ready? ] filter roots get push-all ] bi ;
|
|
|
|
|
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
|
|
|
|
|
|
|
: pull-out-nth ( n seq -- elt )
|
|
|
|
[ nth ] [ remove-nth! drop ] 2bi ;
|
|
|
|
|
|
|
|
: select ( vector quot -- elt )
|
|
|
|
! This could be sped up by a constant factor
|
|
|
|
[ dup <enum> ] dip '[ _ call( insn -- score ) ] assoc-map
|
|
|
|
dup values supremum '[ nip _ = ] assoc-find
|
|
|
|
2drop swap pull-out-nth ; inline
|
|
|
|
|
|
|
|
: select-instruction ( -- insn/f )
|
|
|
|
roots get [ f ] [
|
2014-11-07 18:49:59 -05:00
|
|
|
[ score ] select
|
2010-02-10 22:43:30 -05:00
|
|
|
[ insn>> ]
|
|
|
|
[ set-parent-indices ]
|
|
|
|
[ remove-node ] tri
|
|
|
|
] if-empty ;
|
|
|
|
|
|
|
|
: (reorder) ( -- )
|
|
|
|
select-instruction [
|
|
|
|
, (reorder)
|
|
|
|
] when* ;
|
|
|
|
|
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 16:18:02 -05:00
|
|
|
! hack to get bootstrapping working
|
|
|
|
: split-indices-int ( seq indices -- pieces )
|
|
|
|
over length suffix 0 swap [ dup swapd 2array ] map nip
|
|
|
|
[ first2 rot subseq ] with map ;
|
|
|
|
|
2014-11-08 13:01:00 -05:00
|
|
|
: split-insns ( insns -- pre/body/post )
|
2014-11-08 16:18:02 -05:00
|
|
|
dup [ initial-insn-end ] [ final-insn-start ] bi 2array split-indices-int ;
|
|
|
|
|
|
|
|
: reorder-body ( body -- body' )
|
|
|
|
[ <node> ] map
|
|
|
|
[ build-dependence-graph ] [ build-fan-in-trees ] bi
|
|
|
|
[ (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
|
|
|
|
dup reverse-post-order [ kill-block?>> not ] filter [ schedule-block ] each ;
|