1 ! Copyright (C) 2009, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs combinators fry kernel layouts locals
4 math make namespaces sequences cpu.architecture
10 compiler.cfg.comparisons
11 compiler.cfg.instructions
12 compiler.cfg.predecessors
14 compiler.cfg.liveness.ssa
15 compiler.cfg.stacks.uninitialized ;
16 IN: compiler.cfg.gc-checks
20 ! Garbage collection check insertion. This pass runs after
21 ! representation selection, since it needs to know which vregs
22 ! can contain tagged pointers.
24 : insert-gc-check? ( bb -- ? )
26 [ drop f ] [ instructions>> [ ##allocation? ] any? ] if ;
28 : blocks-with-gc ( cfg -- bbs )
29 post-order [ insert-gc-check? ] filter ;
31 ! A GC check for bb consists of two new basic blocks, gc-check
40 ! Any ##phi instructions at the start of bb are transplanted
41 ! into the gc-check block.
43 : <gc-check> ( phis size -- bb )
44 [ <basic-block> ] 2dip
48 cc<= int-rep next-vreg-rep int-rep next-vreg-rep
49 ##check-nursery-branch
51 ] V{ } make >>instructions ;
53 : scrubbed ( uninitialized-locs -- scrub-d scrub-r )
54 [ ds-loc? ] partition [ [ n>> ] map ] bi@ ;
56 : <gc-call> ( uninitialized-locs gc-roots -- bb )
57 [ <basic-block> ] 2dip
58 [ [ scrubbed ] dip ##gc-map ##call-gc ##branch ] V{ } make
59 >>instructions t >>unlikely? ;
61 :: insert-guard ( body check bb -- )
62 bb predecessors>> check predecessors<<
63 V{ bb body } check successors<<
65 V{ check } body predecessors<<
66 V{ bb } body successors<<
68 V{ check body } bb predecessors<<
70 check predecessors>> [ bb check update-successors ] each ;
72 : (insert-gc-check) ( uninitialized-locs gc-roots phis size bb -- )
73 [ [ <gc-call> ] 2dip <gc-check> ] dip insert-guard ;
75 GENERIC: allocation-size* ( insn -- n )
77 M: ##allot allocation-size* size>> ;
79 M: ##box-alien allocation-size* drop 5 cells ;
81 M: ##box-displaced-alien allocation-size* drop 5 cells ;
83 : allocation-size ( bb -- n )
85 [ ##allocation? ] filter
86 [ allocation-size* data-alignment get align ] map-sum ;
88 : gc-live-in ( bb -- vregs )
89 [ live-in keys ] [ instructions>> [ ##phi? ] filter [ dst>> ] map ] bi
92 : live-tagged ( bb -- vregs )
93 gc-live-in [ rep-of tagged-rep? ] filter ;
95 : remove-phis ( bb -- phis )
96 [ [ ##phi? ] partition ] change-instructions drop ;
98 : insert-gc-check ( bb -- )
100 [ uninitialized-locs ]
110 : insert-gc-checks ( cfg -- cfg' )
114 dup compute-ssa-live-sets
115 dup compute-uninitialized-sets
117 [ insert-gc-check ] each