factor/library/compiler/optimizer/kill-literals.factor

75 lines
2.0 KiB
Factor
Raw Normal View History

2005-08-07 00:00:57 -04:00
! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2005-09-09 00:17:19 -04:00
IN: optimizer
USING: arrays generic hashtables inference kernel math
2006-03-02 01:12:32 -05:00
namespaces sequences words ;
2006-08-15 16:29:35 -04:00
: node-union ( node quot -- hash )
2005-09-17 04:15:05 -04:00
[
swap [ swap call [ dup set ] each ] each-node-with
] make-hash ; inline
2005-08-07 00:00:57 -04:00
2005-09-17 04:15:05 -04:00
GENERIC: literals* ( node -- seq )
2005-08-07 00:00:57 -04:00
: literals ( node -- hash )
[ literals* ] node-union ;
2005-09-17 04:15:05 -04:00
GENERIC: live-values* ( node -- seq )
2005-08-07 00:00:57 -04:00
: live-values ( node -- hash )
#! All values that are returned or passed to calls.
[ live-values* ] node-union ;
2005-09-07 17:21:11 -04:00
2006-03-02 01:12:32 -05:00
: kill-node* ( values node -- )
2dup [ node-in-d remove-all ] keep set-node-in-d
2dup [ node-out-d remove-all ] keep set-node-out-d
2dup [ node-in-r remove-all ] keep set-node-in-r
[ node-out-r remove-all ] keep set-node-out-r ;
2005-08-07 00:00:57 -04:00
: kill-node ( values node -- )
2005-11-29 23:49:59 -05:00
over hash-empty?
2006-03-02 01:12:32 -05:00
[ 2drop ] [ [ kill-node* ] each-node-with ] if ;
: kill-values ( node -- )
dup live-values over literals hash-diff swap kill-node ;
2005-08-07 00:00:57 -04:00
! Generic nodes
M: node literals* drop { } ;
2005-08-07 00:00:57 -04:00
M: node live-values*
2006-05-10 03:40:03 -04:00
node-in-d [ value? ] subset ;
2006-04-17 17:17:34 -04:00
! #push
M: #push literals* node-out-d ;
2006-03-02 01:12:32 -05:00
2005-09-07 17:21:11 -04:00
! #return
M: #return live-values*
#! Values returned by local labels can be killed.
dup node-param [ drop { } ] [ delegate live-values* ] if ;
2006-04-17 17:17:34 -04:00
! nodes that don't use their values directly
UNION: #killable
#push #shuffle #call-label #merge #values #entry ;
2005-09-17 04:15:05 -04:00
M: #killable live-values* drop { } ;
: purge-invariants ( stacks -- seq )
#! Output a sequence of values which are not present in the
#! same position in each sequence of the stacks sequence.
unify-lengths flip [ all-eq? not ] subset concat ;
! #label
M: #label live-values*
2006-03-04 18:46:49 -05:00
dup node-child node-in-d over node-in-d 2array
2006-03-05 19:42:14 -05:00
swap collect-recursion append purge-invariants ;
! branching
2005-09-24 15:21:17 -04:00
UNION: #branch #if #dispatch ;
M: #branch live-values*
#! This assumes that the last element of each branch is a
#! #return node.
2006-02-28 00:26:45 -05:00
dup delegate live-values* >r
node-children [ last-node node-in-d ] map purge-invariants
r> append ;