| 
									
										
										
										
											2010-01-14 10:10:13 -05:00
										 |  |  | ! Copyright (C) 2008, 2010 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  | USING: kernel accessors words assocs sequences arrays namespaces | 
					
						
							| 
									
										
										
										
											2011-11-06 22:20:33 -05:00
										 |  |  | fry locals definitions classes classes.algebra generic math | 
					
						
							|  |  |  | combinators math.private | 
					
						
							| 
									
										
										
										
											2009-11-08 21:34:46 -05:00
										 |  |  | stack-checker.dependencies | 
					
						
							| 
									
										
										
										
											2008-08-28 23:28:34 -04:00
										 |  |  | stack-checker.backend | 
					
						
							| 
									
										
										
										
											2008-08-22 18:38:23 -04:00
										 |  |  | compiler.tree | 
					
						
							|  |  |  | compiler.tree.propagation.info | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | compiler.tree.dead-code.liveness ;
 | 
					
						
							|  |  |  | IN: compiler.tree.dead-code.simple | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-22 18:38:23 -04:00
										 |  |  | : flushable-call? ( #call -- ? )
 | 
					
						
							|  |  |  |     dup word>> dup flushable? [ | 
					
						
							|  |  |  |         "input-classes" word-prop dup [ | 
					
						
							|  |  |  |             [ node-input-infos ] dip
 | 
					
						
							|  |  |  |             [ [ class>> ] dip class<= ] 2all?
 | 
					
						
							|  |  |  |         ] [ 2drop t ] if
 | 
					
						
							|  |  |  |     ] [ 2drop f ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | M: #call mark-live-values* | 
					
						
							| 
									
										
										
										
											2008-08-22 18:38:23 -04:00
										 |  |  |     dup flushable-call? [ drop ] [ look-at-inputs ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 22:06:07 -05:00
										 |  |  | M: #alien-node mark-live-values* look-at-inputs ;
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: #return mark-live-values* look-at-inputs ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : look-at-mapping ( value inputs outputs -- )
 | 
					
						
							|  |  |  |     [ index ] dip over [ nth look-at-value ] [ 2drop ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #copy compute-live-values* | 
					
						
							|  |  |  |     #! If the output of a copy is live, then the corresponding | 
					
						
							|  |  |  |     #! input is live also. | 
					
						
							|  |  |  |     [ out-d>> ] [ in-d>> ] bi look-at-mapping ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #call compute-live-values* nip look-at-inputs ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #shuffle compute-live-values* | 
					
						
							|  |  |  |     mapping>> at look-at-value ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 22:06:07 -05:00
										 |  |  | M: #alien-node compute-live-values* nip look-at-inputs ;
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  | : filter-mapping ( assoc -- assoc' )
 | 
					
						
							| 
									
										
										
										
											2008-09-10 23:11:40 -04:00
										 |  |  |     live-values get '[ drop _ key? ] assoc-filter ;
 | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-22 04:12:15 -04:00
										 |  |  | : filter-corresponding ( new old -- old' )
 | 
					
						
							|  |  |  |     #! Remove elements from 'old' if the element with the same | 
					
						
							|  |  |  |     #! index in 'new' is dead. | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  |     zip filter-mapping values ;
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : filter-live ( values -- values' )
 | 
					
						
							| 
									
										
										
										
											2008-11-11 19:46:31 -05:00
										 |  |  |     dup empty? [ [ live-value? ] filter ] unless ;
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-22 19:09:48 -04:00
										 |  |  | :: drop-values ( inputs outputs mapping-keys mapping-values -- #shuffle )
 | 
					
						
							|  |  |  |     inputs | 
					
						
							|  |  |  |     outputs | 
					
						
							|  |  |  |     outputs | 
					
						
							|  |  |  |     mapping-keys | 
					
						
							|  |  |  |     mapping-values | 
					
						
							| 
									
										
										
										
											2011-11-06 23:41:31 -05:00
										 |  |  |     filter-corresponding zip <#data-shuffle> ; inline
 | 
					
						
							| 
									
										
										
										
											2008-08-22 19:09:48 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | :: drop-dead-values ( outputs -- #shuffle )
 | 
					
						
							| 
									
										
										
										
											2010-01-14 10:10:13 -05:00
										 |  |  |     outputs length make-values :> new-outputs | 
					
						
							| 
									
										
										
										
											2009-10-27 22:50:31 -04:00
										 |  |  |     outputs filter-live :> live-outputs | 
					
						
							|  |  |  |     new-outputs | 
					
						
							|  |  |  |     live-outputs | 
					
						
							|  |  |  |     outputs | 
					
						
							|  |  |  |     new-outputs | 
					
						
							|  |  |  |     drop-values ;
 | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-01 03:04:42 -04:00
										 |  |  | : drop-dead-outputs ( node -- #shuffle )
 | 
					
						
							| 
									
										
										
										
											2009-02-02 14:43:54 -05:00
										 |  |  |     dup out-d>> drop-dead-values [ in-d>> >>out-d drop ] keep ;
 | 
					
						
							| 
									
										
										
										
											2008-08-22 19:09:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-01 03:04:42 -04:00
										 |  |  | : some-outputs-dead? ( #call -- ? )
 | 
					
						
							| 
									
										
										
										
											2009-01-29 23:19:07 -05:00
										 |  |  |     out-d>> [ live-value? not ] any? ;
 | 
					
						
							| 
									
										
										
										
											2008-09-01 03:04:42 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : maybe-drop-dead-outputs ( node -- nodes )
 | 
					
						
							|  |  |  |     dup some-outputs-dead? [ | 
					
						
							|  |  |  |         dup drop-dead-outputs 2array
 | 
					
						
							|  |  |  |     ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  | M: #introduce remove-dead-code* ( #introduce -- nodes )
 | 
					
						
							| 
									
										
										
										
											2008-09-01 03:04:42 -04:00
										 |  |  |     maybe-drop-dead-outputs ;
 | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | M: #push remove-dead-code* | 
					
						
							|  |  |  |     dup out-d>> first live-value? [ drop f ] unless ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : dead-flushable-call? ( #call -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-08-22 18:38:23 -04:00
										 |  |  |     dup flushable-call? [ | 
					
						
							|  |  |  |         out-d>> [ live-value? not ] all?
 | 
					
						
							|  |  |  |     ] [ drop f ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : remove-flushable-call ( #call -- node )
 | 
					
						
							| 
									
										
										
										
											2012-06-21 02:55:24 -04:00
										 |  |  |     [ word>> add-depends-on-flushable ] | 
					
						
							| 
									
										
										
										
											2011-11-06 23:41:31 -05:00
										 |  |  |     [ in-d>> <#drop> remove-dead-code* ] | 
					
						
							| 
									
										
										
										
											2008-08-28 23:28:34 -04:00
										 |  |  |     bi ;
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-06 22:20:33 -05:00
										 |  |  | : define-simplifications ( word seq -- )
 | 
					
						
							|  |  |  |     "simplifications" set-word-prop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ /mod { | 
					
						
							|  |  |  |     { { f t } /i } | 
					
						
							|  |  |  |     { { t f } mod } | 
					
						
							|  |  |  | } define-simplifications | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ fixnum/mod { | 
					
						
							|  |  |  |     { { f t } fixnum/i } | 
					
						
							|  |  |  |     { { t f } fixnum-mod } | 
					
						
							|  |  |  | } define-simplifications | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ bignum/mod { | 
					
						
							|  |  |  |     { { f t } bignum/i } | 
					
						
							|  |  |  |     { { t f } bignum-mod } | 
					
						
							|  |  |  | } define-simplifications | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : out-d-matches? ( out-d seq -- ? )
 | 
					
						
							|  |  |  |     [ [ live-value? ] [ drop t ] if ] 2all? not ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (simplify-call) ( #call -- new-word/f )
 | 
					
						
							|  |  |  |     [ out-d>> ] [ word>> "simplifications" word-prop ] bi
 | 
					
						
							|  |  |  |     [ first out-d-matches? ] with find nip dup [ second ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : simplify-call ( #call -- nodes )
 | 
					
						
							|  |  |  |     dup (simplify-call) [ | 
					
						
							|  |  |  |         >>word [ filter-live ] change-out-d | 
					
						
							|  |  |  |     ] when* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | M: #call remove-dead-code* | 
					
						
							| 
									
										
										
										
											2011-11-06 22:20:33 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |         { [ dup dead-flushable-call? ] [ remove-flushable-call ] } | 
					
						
							|  |  |  |         { [ dup word>> "simplifications" word-prop ] [ simplify-call ] } | 
					
						
							|  |  |  |         [ maybe-drop-dead-outputs ] | 
					
						
							|  |  |  |     } cond ;
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: #shuffle remove-dead-code* | 
					
						
							|  |  |  |     [ filter-live ] change-in-d | 
					
						
							|  |  |  |     [ filter-live ] change-out-d | 
					
						
							| 
									
										
										
										
											2008-11-11 19:46:31 -05:00
										 |  |  |     [ filter-live ] change-in-r | 
					
						
							|  |  |  |     [ filter-live ] change-out-r | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  |     [ filter-mapping ] change-mapping | 
					
						
							| 
									
										
										
										
											2008-11-11 19:46:31 -05:00
										 |  |  |     dup [ in-d>> empty? ] [ in-r>> empty? ] bi and [ drop f ] when ;
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: #copy remove-dead-code* | 
					
						
							|  |  |  |     [ in-d>> ] [ out-d>> ] bi
 | 
					
						
							| 
									
										
										
										
											2011-11-06 23:41:31 -05:00
										 |  |  |     2dup swap zip <#data-shuffle> | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  |     remove-dead-code* ;
 | 
					
						
							| 
									
										
										
										
											2008-08-15 00:35:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: #terminate remove-dead-code* | 
					
						
							|  |  |  |     [ filter-live ] change-in-d | 
					
						
							|  |  |  |     [ filter-live ] change-in-r ;
 | 
					
						
							| 
									
										
										
										
											2008-09-01 03:04:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 22:06:07 -05:00
										 |  |  | M: #alien-node remove-dead-code* | 
					
						
							| 
									
										
										
										
											2008-09-01 03:04:42 -04:00
										 |  |  |     maybe-drop-dead-outputs ;
 | 
					
						
							| 
									
										
										
										
											2010-07-28 00:49:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: #alien-callback remove-dead-code* | 
					
						
							|  |  |  |     [ (remove-dead-code) ] change-child ;
 |