2010-04-27 10:51:00 -04:00
|
|
|
! Copyright (C) 2008, 2010 Slava Pestov.
|
2008-09-11 03:05:22 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: kernel math accessors sequences namespaces make
|
2009-08-08 21:02:56 -04:00
|
|
|
combinators assocs arrays locals layouts hashtables
|
2009-10-01 20:53:30 -04:00
|
|
|
cpu.architecture generalizations
|
2008-09-15 02:54:48 -04:00
|
|
|
compiler.cfg
|
2009-07-13 15:42:52 -04:00
|
|
|
compiler.cfg.comparisons
|
2009-06-02 19:23:47 -04:00
|
|
|
compiler.cfg.stack-frame
|
2009-07-22 07:05:17 -04:00
|
|
|
compiler.cfg.instructions
|
2009-07-28 22:31:08 -04:00
|
|
|
compiler.cfg.utilities
|
|
|
|
compiler.cfg.linearization.order ;
|
2008-09-11 03:05:22 -04:00
|
|
|
IN: compiler.cfg.linearization
|
|
|
|
|
2009-08-08 21:02:56 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
SYMBOL: numbers
|
|
|
|
|
|
|
|
: block-number ( bb -- n ) numbers get at ;
|
|
|
|
|
|
|
|
: number-blocks ( bbs -- ) [ 2array ] map-index >hashtable numbers set ;
|
|
|
|
|
2008-09-11 03:05:22 -04:00
|
|
|
GENERIC: linearize-insn ( basic-block insn -- )
|
|
|
|
|
|
|
|
M: insn linearize-insn , drop ;
|
|
|
|
|
|
|
|
: useless-branch? ( basic-block successor -- ? )
|
2009-07-28 22:31:08 -04:00
|
|
|
! If our successor immediately follows us in linearization
|
|
|
|
! order then we don't need to branch.
|
|
|
|
[ block-number ] bi@ 1 - = ; inline
|
2009-07-22 07:05:17 -04:00
|
|
|
|
|
|
|
: emit-branch ( bb successor -- )
|
2009-07-28 22:31:08 -04:00
|
|
|
2dup useless-branch? [ 2drop ] [ nip block-number _branch ] if ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
2008-09-17 01:46:38 -04:00
|
|
|
M: ##branch linearize-insn
|
2008-09-11 03:05:22 -04:00
|
|
|
drop dup successors>> first emit-branch ;
|
|
|
|
|
2010-04-27 10:51:00 -04:00
|
|
|
GENERIC: negate-insn-cc ( insn -- )
|
2008-10-20 06:55:20 -04:00
|
|
|
|
2010-04-27 10:51:00 -04:00
|
|
|
M: conditional-branch-insn negate-insn-cc
|
|
|
|
[ negate-cc ] change-cc drop ;
|
2010-04-22 04:21:23 -04:00
|
|
|
|
2010-04-27 10:51:00 -04:00
|
|
|
M: ##test-vector-branch negate-insn-cc
|
|
|
|
[ negate-vcc ] change-vcc drop ;
|
2010-04-22 04:21:23 -04:00
|
|
|
|
2010-04-27 10:51:00 -04:00
|
|
|
M:: conditional-branch-insn linearize-insn ( bb insn -- )
|
|
|
|
bb successors>> first2 :> ( first second )
|
|
|
|
bb second useless-branch?
|
|
|
|
[ bb second first ]
|
|
|
|
[ bb first second insn negate-insn-cc ] if
|
|
|
|
block-number insn _conditional-branch
|
|
|
|
emit-branch ;
|
2009-07-16 19:29:40 -04:00
|
|
|
|
2009-05-29 06:36:04 -04:00
|
|
|
M: ##dispatch linearize-insn
|
2010-04-27 10:51:00 -04:00
|
|
|
, successors>> [ block-number _dispatch-label ] each ;
|
|
|
|
|
|
|
|
: linearize-basic-block ( bb -- )
|
|
|
|
[ block-number _label ]
|
|
|
|
[ dup instructions>> [ linearize-insn ] with each ]
|
|
|
|
bi ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
2009-05-29 14:11:34 -04:00
|
|
|
: linearize-basic-blocks ( cfg -- insns )
|
|
|
|
[
|
2009-08-08 21:02:56 -04:00
|
|
|
[
|
|
|
|
linearization-order
|
|
|
|
[ number-blocks ]
|
|
|
|
[ [ linearize-basic-block ] each ] bi
|
|
|
|
] [ spill-area-size>> _spill-area-size ] bi
|
2009-05-29 14:11:34 -04:00
|
|
|
] { } make ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
2009-08-08 21:02:56 -04:00
|
|
|
PRIVATE>
|
2010-04-27 10:51:00 -04:00
|
|
|
|
2009-05-31 13:20:46 -04:00
|
|
|
: flatten-cfg ( cfg -- mr )
|
2009-05-29 14:11:34 -04:00
|
|
|
[ linearize-basic-blocks ] [ word>> ] [ label>> ] tri
|
|
|
|
<mr> ;
|