2009-05-29 14:11:34 -04:00
|
|
|
! Copyright (C) 2008, 2009 Slava Pestov.
|
2008-10-20 02:56:28 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-07-26 22:10:14 -04:00
|
|
|
USING: accessors arrays kernel assocs sequences namespaces fry
|
|
|
|
sets compiler.cfg.rpo compiler.cfg.instructions ;
|
2008-10-20 02:56:28 -04:00
|
|
|
IN: compiler.cfg.def-use
|
|
|
|
|
|
|
|
GENERIC: defs-vregs ( insn -- seq )
|
2009-05-29 14:11:34 -04:00
|
|
|
GENERIC: temp-vregs ( insn -- seq )
|
2008-10-20 02:56:28 -04:00
|
|
|
GENERIC: uses-vregs ( insn -- seq )
|
|
|
|
|
|
|
|
M: ##flushable defs-vregs dst>> 1array ;
|
2009-07-16 19:29:40 -04:00
|
|
|
M: ##fixnum-overflow defs-vregs dst>> 1array ;
|
2009-07-19 21:12:04 -04:00
|
|
|
M: _fixnum-overflow defs-vregs dst>> 1array ;
|
2008-10-20 02:56:28 -04:00
|
|
|
M: insn defs-vregs drop f ;
|
|
|
|
|
2009-05-29 14:11:34 -04:00
|
|
|
M: ##write-barrier temp-vregs [ card#>> ] [ table>> ] bi 2array ;
|
|
|
|
M: ##unary/temp temp-vregs temp>> 1array ;
|
|
|
|
M: ##allot temp-vregs temp>> 1array ;
|
|
|
|
M: ##dispatch temp-vregs temp>> 1array ;
|
|
|
|
M: ##slot temp-vregs temp>> 1array ;
|
|
|
|
M: ##set-slot temp-vregs temp>> 1array ;
|
|
|
|
M: ##string-nth temp-vregs temp>> 1array ;
|
|
|
|
M: ##set-string-nth-fast temp-vregs temp>> 1array ;
|
|
|
|
M: ##compare temp-vregs temp>> 1array ;
|
|
|
|
M: ##compare-imm temp-vregs temp>> 1array ;
|
|
|
|
M: ##compare-float temp-vregs temp>> 1array ;
|
2009-06-02 19:23:47 -04:00
|
|
|
M: ##gc temp-vregs [ temp1>> ] [ temp2>> ] bi 2array ;
|
2009-05-29 14:11:34 -04:00
|
|
|
M: _dispatch temp-vregs temp>> 1array ;
|
|
|
|
M: insn temp-vregs drop f ;
|
|
|
|
|
2008-10-20 02:56:28 -04:00
|
|
|
M: ##unary uses-vregs src>> 1array ;
|
|
|
|
M: ##binary uses-vregs [ src1>> ] [ src2>> ] bi 2array ;
|
|
|
|
M: ##binary-imm uses-vregs src1>> 1array ;
|
|
|
|
M: ##effect uses-vregs src>> 1array ;
|
|
|
|
M: ##slot uses-vregs [ obj>> ] [ slot>> ] bi 2array ;
|
|
|
|
M: ##slot-imm uses-vregs obj>> 1array ;
|
|
|
|
M: ##set-slot uses-vregs [ src>> ] [ obj>> ] [ slot>> ] tri 3array ;
|
|
|
|
M: ##set-slot-imm uses-vregs [ src>> ] [ obj>> ] bi 2array ;
|
2008-11-06 02:11:28 -05:00
|
|
|
M: ##string-nth uses-vregs [ obj>> ] [ index>> ] bi 2array ;
|
2008-12-05 07:38:51 -05:00
|
|
|
M: ##set-string-nth-fast uses-vregs [ src>> ] [ obj>> ] [ index>> ] tri 3array ;
|
2008-10-20 06:55:20 -04:00
|
|
|
M: ##conditional-branch uses-vregs [ src1>> ] [ src2>> ] bi 2array ;
|
|
|
|
M: ##compare-imm-branch uses-vregs src1>> 1array ;
|
2008-10-20 02:56:28 -04:00
|
|
|
M: ##dispatch uses-vregs src>> 1array ;
|
2008-10-22 00:17:32 -04:00
|
|
|
M: ##alien-getter uses-vregs src>> 1array ;
|
|
|
|
M: ##alien-setter uses-vregs [ src>> ] [ value>> ] bi 2array ;
|
2008-11-28 06:33:58 -05:00
|
|
|
M: ##fixnum-overflow uses-vregs [ src1>> ] [ src2>> ] bi 2array ;
|
2009-07-02 18:10:50 -04:00
|
|
|
M: ##phi uses-vregs inputs>> values ;
|
2008-10-20 06:55:20 -04:00
|
|
|
M: _conditional-branch uses-vregs [ src1>> ] [ src2>> ] bi 2array ;
|
|
|
|
M: _compare-imm-branch uses-vregs src1>> 1array ;
|
2009-05-29 06:36:04 -04:00
|
|
|
M: _dispatch uses-vregs src>> 1array ;
|
2008-10-20 02:56:28 -04:00
|
|
|
M: insn uses-vregs drop f ;
|
2009-07-26 22:10:14 -04:00
|
|
|
|
|
|
|
! Computing def-use chains. We don't assume a program is in SSA form,
|
|
|
|
! since SSA construction itself needs def-use information. defs-1
|
|
|
|
! is only useful if the program is SSA.
|
|
|
|
SYMBOLS: defs defs-1 insns uses ;
|
|
|
|
|
|
|
|
: def-of ( vreg -- node ) defs-1 get at ;
|
|
|
|
: defs-of ( vreg -- nodes ) defs get at ;
|
|
|
|
: uses-of ( vreg -- nodes ) uses get at ;
|
|
|
|
: insn-of ( vreg -- insn ) insns get at ;
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: finish-defs ( -- )
|
|
|
|
defs [ [ keys ] assoc-map ] change ;
|
|
|
|
|
|
|
|
: finish-uses ( -- )
|
|
|
|
uses [ [ keys ] assoc-map ] change ;
|
|
|
|
|
|
|
|
: (compute-def-use) ( cfg quot -- assoc )
|
|
|
|
H{ } clone [
|
|
|
|
'[
|
|
|
|
dup instructions>> [
|
|
|
|
@ [
|
|
|
|
_ conjoin-at
|
|
|
|
] with each
|
|
|
|
] with each
|
|
|
|
] each-basic-block
|
|
|
|
] keep
|
|
|
|
[ keys ] assoc-map ; inline
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: compute-defs ( cfg -- )
|
|
|
|
[ defs-vregs ] (compute-def-use)
|
|
|
|
[ defs set ] [ [ first ] assoc-map defs-1 set ] bi ;
|
|
|
|
|
|
|
|
: compute-uses ( cfg -- )
|
|
|
|
[ uses-vregs ] (compute-def-use) uses set ;
|
|
|
|
|
|
|
|
: compute-insns ( cfg -- )
|
|
|
|
H{ } clone [
|
|
|
|
'[
|
|
|
|
instructions>> [
|
|
|
|
dup defs-vregs [
|
|
|
|
_ set-at
|
|
|
|
] with each
|
|
|
|
] each
|
|
|
|
] each-basic-block
|
|
|
|
] keep insns set ;
|
|
|
|
|
|
|
|
: compute-def-use ( cfg -- )
|
|
|
|
[ compute-defs ] [ compute-uses ] [ compute-insns ] tri ;
|