2009-05-26 20:31:19 -04:00
|
|
|
! Copyright (C) 2008, 2009 Slava Pestov.
|
2008-09-11 03:05:22 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-10-22 19:39:41 -04:00
|
|
|
USING: kernel accessors namespaces make math sequences sets
|
2009-05-26 20:56:56 -04:00
|
|
|
assocs fry compiler.cfg compiler.cfg.instructions
|
|
|
|
compiler.cfg.liveness ;
|
2008-09-11 03:05:22 -04:00
|
|
|
IN: compiler.cfg.rpo
|
|
|
|
|
2008-10-22 19:39:41 -04:00
|
|
|
SYMBOL: visited
|
|
|
|
|
|
|
|
: post-order-traversal ( bb -- )
|
2009-05-26 20:31:19 -04:00
|
|
|
dup visited get key? [ drop ] [
|
|
|
|
dup visited get conjoin
|
2008-11-13 04:52:01 -05:00
|
|
|
[
|
|
|
|
successors>> <reversed>
|
|
|
|
[ post-order-traversal ] each
|
|
|
|
] [ , ] bi
|
2008-09-11 03:05:22 -04:00
|
|
|
] if ;
|
|
|
|
|
2009-05-26 20:31:19 -04:00
|
|
|
: post-order ( cfg -- blocks )
|
|
|
|
[ entry>> post-order-traversal ] { } make ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
|
|
|
: number-blocks ( blocks -- )
|
|
|
|
[ >>number drop ] each-index ;
|
|
|
|
|
2009-05-26 20:31:19 -04:00
|
|
|
: reverse-post-order ( cfg -- blocks )
|
2008-10-22 19:39:41 -04:00
|
|
|
H{ } clone visited [
|
|
|
|
post-order <reversed> dup number-blocks
|
|
|
|
] with-variable ; inline
|
|
|
|
|
|
|
|
: each-basic-block ( cfg quot -- )
|
2009-05-26 20:31:19 -04:00
|
|
|
[ reverse-post-order ] dip each ; inline
|
2009-05-26 20:56:56 -04:00
|
|
|
|
|
|
|
: optimize-basic-block ( bb init-quot insn-quot -- )
|
2009-05-27 19:58:41 -04:00
|
|
|
[ '[ live-in keys _ call ] ] [ '[ _ change-instructions drop ] ] bi* bi ; inline
|
2009-05-26 20:56:56 -04:00
|
|
|
|
|
|
|
: local-optimization ( rpo init-quot: ( live-in -- ) insn-quot: ( insns -- insns' ) -- )
|
|
|
|
'[ _ _ optimize-basic-block ] each ;
|