2011-01-17 18:16:17 -05:00
|
|
|
! Copyright (C) 2009, 2011 Slava Pestov.
|
2009-07-26 22:11:26 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2011-01-17 18:16:17 -05:00
|
|
|
USING: accessors arrays assocs fry locals kernel make
|
|
|
|
namespaces sequences sequences.deep
|
2009-08-02 11:26:52 -04:00
|
|
|
sets vectors
|
2010-04-28 05:03:12 -04:00
|
|
|
cpu.architecture
|
2009-07-26 22:11:26 -04:00
|
|
|
compiler.cfg.rpo
|
2009-07-27 03:20:45 -04:00
|
|
|
compiler.cfg.def-use
|
2009-09-27 18:17:26 -04:00
|
|
|
compiler.cfg.registers
|
2009-07-26 22:11:26 -04:00
|
|
|
compiler.cfg.dominance
|
2009-07-27 01:31:21 -04:00
|
|
|
compiler.cfg.instructions
|
2010-09-25 17:36:58 -04:00
|
|
|
compiler.cfg.liveness
|
2009-08-02 11:26:52 -04:00
|
|
|
compiler.cfg.ssa.cssa
|
2012-12-31 13:30:17 -05:00
|
|
|
compiler.cfg.ssa.destruction.leaders
|
2009-08-02 11:26:52 -04:00
|
|
|
compiler.cfg.ssa.interference
|
2009-08-02 09:15:36 -04:00
|
|
|
compiler.cfg.ssa.interference.live-ranges
|
2011-01-17 18:16:17 -05:00
|
|
|
compiler.cfg.parallel-copy
|
2009-08-02 11:57:27 -04:00
|
|
|
compiler.cfg.utilities
|
2009-08-02 11:26:52 -04:00
|
|
|
compiler.utilities ;
|
2010-02-26 16:01:01 -05:00
|
|
|
FROM: namespaces => set ;
|
2009-07-28 10:34:08 -04:00
|
|
|
IN: compiler.cfg.ssa.destruction
|
2009-07-26 22:11:26 -04:00
|
|
|
|
2010-04-28 05:03:12 -04:00
|
|
|
! Because of the design of the register allocator, this pass
|
|
|
|
! has three peculiar properties.
|
|
|
|
!
|
|
|
|
! 1) Instead of renaming vreg usages in the CFG, a map from
|
|
|
|
! vregs to canonical representatives is computed. This allows
|
|
|
|
! the register allocator to use the original SSA names to get
|
|
|
|
! reaching definitions.
|
|
|
|
! 2) Useless ##copy instructions, and all ##phi instructions,
|
|
|
|
! are eliminated, so the register allocator does not have to
|
|
|
|
! remove any redundant operations.
|
2010-09-25 17:36:58 -04:00
|
|
|
! 3) This pass computes live sets and fills out GC maps with
|
|
|
|
! compiler.cfg.liveness, so the linear scan register allocator
|
|
|
|
! does not need to compute liveness again.
|
2010-04-28 05:03:12 -04:00
|
|
|
|
2009-08-02 11:26:52 -04:00
|
|
|
! Maps leaders to equivalence class elements.
|
|
|
|
SYMBOL: class-element-map
|
2009-07-26 22:11:26 -04:00
|
|
|
|
2009-08-02 11:26:52 -04:00
|
|
|
: class-elements ( vreg -- elts ) class-element-map get at ;
|
2009-07-27 03:20:45 -04:00
|
|
|
|
2010-04-28 05:03:12 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2009-08-02 11:26:52 -04:00
|
|
|
! Sequence of vreg pairs
|
|
|
|
SYMBOL: copies
|
2009-07-27 03:20:45 -04:00
|
|
|
|
2010-05-17 05:49:41 -04:00
|
|
|
: value-of ( vreg -- value )
|
2010-07-13 07:40:14 -04:00
|
|
|
dup insn-of dup ##tagged>integer? [ nip src>> ] [ drop ] if ;
|
2010-05-17 05:49:41 -04:00
|
|
|
|
2009-08-02 11:26:52 -04:00
|
|
|
: init-coalescing ( -- )
|
2010-05-17 05:49:41 -04:00
|
|
|
defs get
|
|
|
|
[ [ drop dup ] assoc-map leader-map set ]
|
|
|
|
[ [ [ dup dup value-of ] dip <vreg-info> 1array ] assoc-map class-element-map set ] bi
|
2009-08-02 11:26:52 -04:00
|
|
|
V{ } clone copies set ;
|
|
|
|
|
2010-05-17 05:49:41 -04:00
|
|
|
: coalesce-leaders ( vreg1 vreg2 -- )
|
|
|
|
! leader2 becomes the leader.
|
2009-08-02 11:26:52 -04:00
|
|
|
swap leader-map get set-at ;
|
|
|
|
|
2010-05-17 05:49:41 -04:00
|
|
|
: coalesce-elements ( merged vreg1 vreg2 -- )
|
|
|
|
! delete leader1's class, and set leader2's class to merged.
|
|
|
|
class-element-map get [ delete-at ] [ set-at ] bi-curry bi* ;
|
|
|
|
|
|
|
|
: coalesce-vregs ( merged leader1 leader2 -- )
|
|
|
|
[ coalesce-leaders ] [ coalesce-elements ] 2bi ;
|
2009-08-02 11:26:52 -04:00
|
|
|
|
|
|
|
GENERIC: prepare-insn ( insn -- )
|
|
|
|
|
2010-05-17 05:49:41 -04:00
|
|
|
: maybe-eliminate-copy-later ( dst src -- )
|
|
|
|
2array copies get push ;
|
2009-09-27 18:17:26 -04:00
|
|
|
|
2010-05-14 18:18:29 -04:00
|
|
|
M: insn prepare-insn drop ;
|
|
|
|
|
2012-12-29 14:15:12 -05:00
|
|
|
M: alien-call-insn prepare-insn drop ;
|
|
|
|
|
2010-05-14 18:18:29 -04:00
|
|
|
M: vreg-insn prepare-insn
|
2010-04-28 05:03:12 -04:00
|
|
|
[ temp-vregs [ leader-map get conjoin ] each ]
|
|
|
|
[
|
2010-07-13 07:40:14 -04:00
|
|
|
[ defs-vregs ] [ uses-vregs ] bi
|
|
|
|
2dup [ empty? not ] both? [
|
|
|
|
[ first ] bi@
|
2010-04-30 18:18:30 -04:00
|
|
|
2dup [ rep-of reg-class-of ] bi@ eq?
|
2010-05-17 05:49:41 -04:00
|
|
|
[ maybe-eliminate-copy-later ] [ 2drop ] if
|
2010-04-28 05:03:12 -04:00
|
|
|
] [ 2drop ] if
|
|
|
|
] bi ;
|
2009-09-27 18:17:26 -04:00
|
|
|
|
2009-08-07 18:44:50 -04:00
|
|
|
M: ##copy prepare-insn
|
2010-05-17 05:49:41 -04:00
|
|
|
[ dst>> ] [ src>> ] bi maybe-eliminate-copy-later ;
|
2009-08-02 11:26:52 -04:00
|
|
|
|
2011-01-17 18:16:17 -05:00
|
|
|
M: ##parallel-copy prepare-insn
|
|
|
|
values>> [ first2 maybe-eliminate-copy-later ] each ;
|
|
|
|
|
|
|
|
: leaders ( vreg1 vreg2 -- vreg1' vreg2' )
|
|
|
|
[ leader ] bi@ ;
|
|
|
|
|
|
|
|
: vregs-interfere? ( vreg1 vreg2 -- merged/f ? )
|
|
|
|
[ class-elements ] bi@ sets-interfere? ;
|
|
|
|
|
|
|
|
ERROR: vregs-shouldn't-interfere vreg1 vreg2 ;
|
|
|
|
|
|
|
|
:: must-eliminate-copy ( vreg1 vreg2 -- )
|
|
|
|
! Eliminate a copy.
|
|
|
|
vreg1 vreg2 eq? [
|
|
|
|
vreg1 vreg2 vregs-interfere?
|
|
|
|
[ vreg1 vreg2 vregs-shouldn't-interfere ]
|
|
|
|
[ vreg1 vreg2 coalesce-vregs ]
|
|
|
|
if
|
|
|
|
] unless ;
|
|
|
|
|
2010-04-28 05:03:12 -04:00
|
|
|
M: ##tagged>integer prepare-insn
|
2011-01-17 18:16:17 -05:00
|
|
|
[ dst>> ] [ src>> ] bi leaders must-eliminate-copy ;
|
2010-04-28 05:03:12 -04:00
|
|
|
|
2009-08-02 11:26:52 -04:00
|
|
|
M: ##phi prepare-insn
|
|
|
|
[ dst>> ] [ inputs>> values ] bi
|
2011-01-17 18:16:17 -05:00
|
|
|
[ leaders must-eliminate-copy ] with each ;
|
2009-08-02 11:26:52 -04:00
|
|
|
|
|
|
|
: prepare-coalescing ( cfg -- )
|
|
|
|
init-coalescing
|
2010-07-27 12:40:31 -04:00
|
|
|
[ [ prepare-insn ] each ] simple-analysis ;
|
2009-08-02 11:26:52 -04:00
|
|
|
|
2011-01-17 18:16:17 -05:00
|
|
|
:: maybe-eliminate-copy ( vreg1 vreg2 -- )
|
|
|
|
! Eliminate a copy if possible.
|
|
|
|
vreg1 vreg2 eq? [
|
|
|
|
vreg1 vreg2 vregs-interfere?
|
|
|
|
[ drop ] [ vreg1 vreg2 coalesce-vregs ] if
|
|
|
|
] unless ;
|
|
|
|
|
2009-08-02 11:26:52 -04:00
|
|
|
: process-copies ( -- )
|
2011-01-17 18:16:17 -05:00
|
|
|
copies get [ leaders maybe-eliminate-copy ] assoc-each ;
|
2009-07-26 22:11:26 -04:00
|
|
|
|
2011-01-17 18:16:17 -05:00
|
|
|
GENERIC: cleanup-insn ( insn -- )
|
2010-04-27 10:51:00 -04:00
|
|
|
|
2010-04-28 05:03:12 -04:00
|
|
|
: useful-copy? ( insn -- ? )
|
2011-01-17 18:16:17 -05:00
|
|
|
[ dst>> ] [ src>> ] bi leaders eq? not ; inline
|
|
|
|
|
|
|
|
M: ##copy cleanup-insn
|
|
|
|
dup useful-copy? [ , ] [ drop ] if ;
|
2010-04-27 10:51:00 -04:00
|
|
|
|
2011-01-17 18:16:17 -05:00
|
|
|
M: ##parallel-copy cleanup-insn
|
|
|
|
values>>
|
|
|
|
[ first2 leaders 2array ] map [ first2 eq? not ] filter
|
|
|
|
[ parallel-copy-rep ] unless-empty ;
|
2010-04-27 10:51:00 -04:00
|
|
|
|
2011-01-17 18:16:17 -05:00
|
|
|
M: ##tagged>integer cleanup-insn
|
|
|
|
dup useful-copy? [ , ] [ drop ] if ;
|
2010-04-28 04:47:38 -04:00
|
|
|
|
2011-01-17 18:16:17 -05:00
|
|
|
M: ##phi cleanup-insn drop ;
|
2010-04-27 10:51:00 -04:00
|
|
|
|
2011-01-17 18:16:17 -05:00
|
|
|
M: insn cleanup-insn , ;
|
2010-04-27 10:51:00 -04:00
|
|
|
|
2010-04-28 05:03:12 -04:00
|
|
|
: cleanup-cfg ( cfg -- )
|
2011-01-17 18:16:17 -05:00
|
|
|
[ [ [ cleanup-insn ] each ] V{ } make ] simple-optimization ;
|
2010-04-28 04:47:38 -04:00
|
|
|
|
2010-04-28 05:03:12 -04:00
|
|
|
PRIVATE>
|
2009-07-26 22:11:26 -04:00
|
|
|
|
2009-07-28 13:56:33 -04:00
|
|
|
: destruct-ssa ( cfg -- cfg' )
|
2009-08-08 21:02:56 -04:00
|
|
|
needs-dominance
|
|
|
|
|
2009-08-05 19:57:46 -04:00
|
|
|
dup construct-cssa
|
|
|
|
dup compute-defs
|
2010-05-17 05:49:41 -04:00
|
|
|
dup compute-insns
|
2010-09-25 17:36:58 -04:00
|
|
|
dup compute-live-sets
|
2009-08-05 19:57:46 -04:00
|
|
|
dup compute-live-ranges
|
|
|
|
dup prepare-coalescing
|
|
|
|
process-copies
|
2011-01-17 18:16:17 -05:00
|
|
|
dup cleanup-cfg
|
|
|
|
dup compute-live-sets ;
|