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 : wipe-locs ( uninitialized-locs -- )
57 [ '[ [ _ ] dip ##replace ] each ] bi
60 : <gc-call> ( uninitialized-locs gc-roots -- bb )
61 [ <basic-block> ] 2dip
62 [ [ wipe-locs ] [ ##call-gc ] bi* ##branch ] V{ } make
63 >>instructions t >>unlikely? ;
65 :: insert-guard ( body check bb -- )
66 bb predecessors>> check predecessors<<
67 V{ bb body } check successors<<
69 V{ check } body predecessors<<
70 V{ bb } body successors<<
72 V{ check body } bb predecessors<<
74 check predecessors>> [ bb check update-successors ] each ;
76 : (insert-gc-check) ( uninitialized-locs gc-roots phis size bb -- )
77 [ [ <gc-call> ] 2dip <gc-check> ] dip insert-guard ;
79 GENERIC: allocation-size* ( insn -- n )
81 M: ##allot allocation-size* size>> ;
83 M: ##box-alien allocation-size* drop 5 cells ;
85 M: ##box-displaced-alien allocation-size* drop 5 cells ;
87 : allocation-size ( bb -- n )
89 [ ##allocation? ] filter
90 [ allocation-size* data-alignment get align ] map-sum ;
92 : gc-live-in ( bb -- vregs )
93 [ live-in keys ] [ instructions>> [ ##phi? ] filter [ dst>> ] map ] bi
96 : live-tagged ( bb -- vregs )
97 gc-live-in [ rep-of tagged-rep? ] filter ;
99 : remove-phis ( bb -- phis )
100 [ [ ##phi? ] partition ] change-instructions drop ;
102 : insert-gc-check ( bb -- )
104 [ uninitialized-locs ]
114 : insert-gc-checks ( cfg -- cfg' )
118 dup compute-ssa-live-sets
119 dup compute-uninitialized-sets
121 [ insert-gc-check ] each