| 
									
										
										
										
											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. | 
					
						
							| 
									
										
										
										
											2014-12-13 19:10:21 -05:00
										 |  |  | USING: accessors assocs columns combinators compiler.tree | 
					
						
							|  |  |  | compiler.tree.dead-code.liveness compiler.tree.dead-code.simple | 
					
						
							|  |  |  | fry kernel namespaces sequences stack-checker.backend | 
					
						
							|  |  |  | stack-checker.branches ;
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | IN: compiler.tree.dead-code.branches | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #if mark-live-values* look-at-inputs ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #dispatch mark-live-values* look-at-inputs ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  | : look-at-phi ( value outputs inputs -- )
 | 
					
						
							| 
									
										
										
										
											2015-07-21 01:24:30 -04:00
										 |  |  |     [ index ] dip swap [ <column> look-at-values ] [ drop ] if* ;
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: #phi compute-live-values* | 
					
						
							| 
									
										
										
										
											2015-09-08 19:15:10 -04:00
										 |  |  |     ! If any of the outputs of a #phi are live, then the | 
					
						
							|  |  |  |     ! corresponding inputs are live too. | 
					
						
							| 
									
										
										
										
											2008-08-18 21:49:03 -04:00
										 |  |  |     [ out-d>> ] [ phi-in-d>> ] bi look-at-phi ;
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-18 16:47:49 -04:00
										 |  |  | SYMBOL: if-node | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-15 00:35:19 -04:00
										 |  |  | M: #branch remove-dead-code* | 
					
						
							| 
									
										
										
										
											2008-08-18 16:47:49 -04:00
										 |  |  |     [ [ [ (remove-dead-code) ] map ] change-children ] | 
					
						
							|  |  |  |     [ if-node set ] | 
					
						
							|  |  |  |     bi ;
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  | : remove-phi-inputs ( #phi -- )
 | 
					
						
							| 
									
										
										
										
											2008-08-18 22:30:10 -04:00
										 |  |  |     if-node get children>> | 
					
						
							| 
									
										
										
										
											2009-05-25 17:38:33 -04:00
										 |  |  |     [ dup ends-with-terminate? [ drop f ] [ last out-d>> ] if ] map
 | 
					
						
							| 
									
										
										
										
											2008-08-18 22:30:10 -04:00
										 |  |  |     pad-with-bottom >>phi-in-d drop ;
 | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-18 16:47:49 -04:00
										 |  |  | : live-value-indices ( values -- indices )
 | 
					
						
							| 
									
										
										
										
											2009-06-10 10:45:48 -04:00
										 |  |  |     [ length iota ] keep live-values get
 | 
					
						
							| 
									
										
										
										
											2008-09-10 23:11:40 -04:00
										 |  |  |     '[ _ nth _ key? ] filter ; inline
 | 
					
						
							| 
									
										
										
										
											2008-08-18 16:47:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-22 19:09:48 -04:00
										 |  |  | : drop-indexed-values ( values indices -- node )
 | 
					
						
							| 
									
										
										
										
											2008-09-12 19:08:38 -04:00
										 |  |  |     [ drop filter-live ] [ swap nths ] 2bi
 | 
					
						
							| 
									
										
										
										
											2010-01-14 10:10:13 -05:00
										 |  |  |     [ length make-values ] keep
 | 
					
						
							| 
									
										
										
										
											2008-08-18 16:47:49 -04:00
										 |  |  |     [ drop ] [ zip ] 2bi
 | 
					
						
							| 
									
										
										
										
											2011-11-06 23:41:31 -05:00
										 |  |  |     <#data-shuffle> ;
 | 
					
						
							| 
									
										
										
										
											2008-08-18 16:47:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-18 21:49:03 -04:00
										 |  |  | : insert-drops ( nodes values indices -- nodes' )
 | 
					
						
							| 
									
										
										
										
											2008-08-18 22:30:10 -04:00
										 |  |  |     '[ | 
					
						
							|  |  |  |         over ends-with-terminate? | 
					
						
							| 
									
										
										
										
											2008-09-10 23:11:40 -04:00
										 |  |  |         [ drop ] [ _ drop-indexed-values suffix ] if
 | 
					
						
							| 
									
										
										
										
											2008-08-18 22:30:10 -04:00
										 |  |  |     ] 2map ;
 | 
					
						
							| 
									
										
										
										
											2008-08-18 16:47:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : hoist-drops ( #phi -- )
 | 
					
						
							|  |  |  |     if-node get swap
 | 
					
						
							| 
									
										
										
										
											2008-08-18 21:49:03 -04:00
										 |  |  |     [ phi-in-d>> ] [ out-d>> live-value-indices ] bi
 | 
					
						
							| 
									
										
										
										
											2008-09-10 23:11:40 -04:00
										 |  |  |     '[ _ _ insert-drops ] change-children drop ;
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : remove-phi-outputs ( #phi -- )
 | 
					
						
							| 
									
										
										
										
											2008-08-18 22:30:10 -04:00
										 |  |  |     [ filter-live ] change-out-d drop ;
 | 
					
						
							| 
									
										
										
										
											2008-08-13 19:56:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: #phi remove-dead-code* | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-08-18 16:47:49 -04:00
										 |  |  |         [ hoist-drops ] | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  |         [ remove-phi-inputs ] | 
					
						
							|  |  |  |         [ remove-phi-outputs ] | 
					
						
							|  |  |  |         [ ] | 
					
						
							|  |  |  |     } cleave ;
 |