| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | ! Copyright (C) 2009 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2015-04-21 16:45:38 -04:00
										 |  |  | USING: accessors assocs combinators.short-circuit compiler.cfg.predecessors | 
					
						
							|  |  |  | compiler.cfg.rpo compiler.cfg.utilities deques dlists functors kernel lexer | 
					
						
							|  |  |  | locals namespaces sequences ;
 | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | IN: compiler.cfg.dataflow-analysis | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-13 00:52:29 -04:00
										 |  |  | GENERIC: join-sets ( sets bb dfa -- set )
 | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | GENERIC: transfer-set ( in-set bb dfa -- out-set )
 | 
					
						
							|  |  |  | GENERIC: block-order ( cfg dfa -- bbs )
 | 
					
						
							|  |  |  | GENERIC: successors ( bb dfa -- seq )
 | 
					
						
							|  |  |  | GENERIC: predecessors ( bb dfa -- seq )
 | 
					
						
							| 
									
										
										
										
											2014-08-13 04:55:44 -04:00
										 |  |  | GENERIC: ignore-block? ( bb dfa -- ? )
 | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MIXIN: dataflow-analysis | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <dfa-worklist> ( cfg dfa -- queue )
 | 
					
						
							|  |  |  |     block-order <hashed-dlist> [ push-all-front ] keep ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 21:36:52 -04:00
										 |  |  | :: compute-in-set ( bb out-sets dfa -- set )
 | 
					
						
							| 
									
										
										
										
											2009-08-20 19:15:41 -04:00
										 |  |  |     ! Only consider initialized sets. | 
					
						
							| 
									
										
										
										
											2014-08-13 04:55:44 -04:00
										 |  |  |     bb dfa ignore-block? [ f ] [ | 
					
						
							| 
									
										
										
										
											2010-05-09 21:36:52 -04:00
										 |  |  |         bb dfa predecessors | 
					
						
							|  |  |  |         [ out-sets key? ] filter
 | 
					
						
							|  |  |  |         [ out-sets at ] map
 | 
					
						
							|  |  |  |         bb dfa join-sets | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | :: update-in-set ( bb in-sets out-sets dfa -- ? )
 | 
					
						
							|  |  |  |     bb out-sets dfa compute-in-set | 
					
						
							|  |  |  |     bb in-sets maybe-set-at ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 21:36:52 -04:00
										 |  |  | :: compute-out-set ( bb in-sets dfa -- set )
 | 
					
						
							| 
									
										
										
										
											2014-08-13 04:55:44 -04:00
										 |  |  |     bb dfa ignore-block? [ f ] [ bb in-sets at bb dfa transfer-set ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | :: update-out-set ( bb in-sets out-sets dfa -- ? )
 | 
					
						
							|  |  |  |     bb in-sets dfa compute-out-set | 
					
						
							|  |  |  |     bb out-sets maybe-set-at ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-21 16:45:38 -04:00
										 |  |  | : update-in/out-set ( bb in-sets out-sets dfa -- ? )
 | 
					
						
							|  |  |  |     { [ update-in-set ] [ update-out-set ] } 4 n&& ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: dfa-step ( bb in-sets out-sets dfa -- bbs )
 | 
					
						
							|  |  |  |     bb in-sets out-sets dfa update-in/out-set bb dfa successors { } ? ;
 | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | :: run-dataflow-analysis ( cfg dfa -- in-sets out-sets )
 | 
					
						
							|  |  |  |     H{ } clone :> in-sets | 
					
						
							|  |  |  |     H{ } clone :> out-sets | 
					
						
							| 
									
										
										
										
											2015-04-21 16:45:38 -04:00
										 |  |  |     cfg needs-predecessors | 
					
						
							|  |  |  |     cfg dfa <dfa-worklist> | 
					
						
							|  |  |  |     [ in-sets out-sets dfa dfa-step ] slurp/replenish-deque | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  |     in-sets | 
					
						
							|  |  |  |     out-sets ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-13 00:52:29 -04:00
										 |  |  | M: dataflow-analysis join-sets 2drop assoc-refine ;
 | 
					
						
							| 
									
										
										
										
											2014-08-13 04:55:44 -04:00
										 |  |  | M: dataflow-analysis ignore-block? drop kill-block?>> ;
 | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-05 21:41:19 -04:00
										 |  |  | <FUNCTOR: define-analysis ( name -- )
 | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-16 09:37:33 -04:00
										 |  |  | name DEFINES-CLASS ${name} | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | name-ins DEFINES ${name}-ins | 
					
						
							|  |  |  | name-outs DEFINES ${name}-outs | 
					
						
							|  |  |  | name-in DEFINES ${name}-in | 
					
						
							|  |  |  | name-out DEFINES ${name}-out | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | WHERE | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-16 09:37:33 -04:00
										 |  |  | SINGLETON: name | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: name-ins | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : name-in ( bb -- set ) name-ins get at ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: name-outs | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : name-out ( bb -- set ) name-outs get at ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-05 21:41:19 -04:00
										 |  |  | ;FUNCTOR> | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! ! ! Forward dataflow analysis | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MIXIN: forward-analysis | 
					
						
							|  |  |  | INSTANCE: forward-analysis dataflow-analysis | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: forward-analysis block-order  drop reverse-post-order ;
 | 
					
						
							|  |  |  | M: forward-analysis successors   drop successors>> ;
 | 
					
						
							|  |  |  | M: forward-analysis predecessors drop predecessors>> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-05 21:41:19 -04:00
										 |  |  | <FUNCTOR: define-forward-analysis ( name -- )
 | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-16 09:37:33 -04:00
										 |  |  | name IS ${name} | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | name-ins IS ${name}-ins | 
					
						
							|  |  |  | name-outs IS ${name}-outs | 
					
						
							|  |  |  | compute-name-sets DEFINES compute-${name}-sets | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | WHERE | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-16 09:37:33 -04:00
										 |  |  | INSTANCE: name forward-analysis | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : compute-name-sets ( cfg -- )
 | 
					
						
							| 
									
										
										
										
											2016-03-16 09:37:33 -04:00
										 |  |  |     name run-dataflow-analysis | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  |     [ name-ins set ] [ name-outs set ] bi* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-05 21:41:19 -04:00
										 |  |  | ;FUNCTOR> | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! ! ! Backward dataflow analysis | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MIXIN: backward-analysis | 
					
						
							|  |  |  | INSTANCE: backward-analysis dataflow-analysis | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: backward-analysis block-order  drop post-order ;
 | 
					
						
							|  |  |  | M: backward-analysis successors   drop predecessors>> ;
 | 
					
						
							|  |  |  | M: backward-analysis predecessors drop successors>> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-05 21:41:19 -04:00
										 |  |  | <FUNCTOR: define-backward-analysis ( name -- )
 | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-16 09:37:33 -04:00
										 |  |  | name IS ${name} | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | name-ins IS ${name}-ins | 
					
						
							|  |  |  | name-outs IS ${name}-outs | 
					
						
							|  |  |  | compute-name-sets DEFINES compute-${name}-sets | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | WHERE | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-16 09:37:33 -04:00
										 |  |  | INSTANCE: name backward-analysis | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : compute-name-sets ( cfg -- )
 | 
					
						
							| 
									
										
										
										
											2016-03-16 09:37:33 -04:00
										 |  |  |     \ name run-dataflow-analysis | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  |     [ name-outs set ] [ name-ins set ] bi* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-05 21:41:19 -04:00
										 |  |  | ;FUNCTOR> | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYNTAX: FORWARD-ANALYSIS: | 
					
						
							| 
									
										
										
										
											2011-09-27 16:20:07 -04:00
										 |  |  |     scan-token [ define-analysis ] [ define-forward-analysis ] bi ;
 | 
					
						
							| 
									
										
										
										
											2009-07-22 03:06:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | SYNTAX: BACKWARD-ANALYSIS: | 
					
						
							| 
									
										
										
										
											2011-09-27 16:20:07 -04:00
										 |  |  |     scan-token [ define-analysis ] [ define-backward-analysis ] bi ;
 |