| 
									
										
										
										
											2010-02-10 22:43:30 -05:00
										 |  |  | ! Copyright (C) 2009, 2010 Daniel Ehrenberg. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2014-11-07 18:49:59 -05:00
										 |  |  | USING: accessors arrays assocs compiler.cfg.def-use compiler.cfg.dependence | 
					
						
							|  |  |  | compiler.cfg.instructions compiler.cfg.linear-scan.numbering compiler.cfg.rpo | 
					
						
							|  |  |  | cpu.architecture fry kernel make math namespaces sequences sets splitting ;
 | 
					
						
							| 
									
										
										
										
											2014-11-09 01:34:31 -05:00
										 |  |  | FROM: namespaces => set ;
 | 
					
						
							| 
									
										
										
										
											2010-02-10 22:43:30 -05:00
										 |  |  | IN: compiler.cfg.scheduling | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Instruction scheduling to reduce register pressure, from: | 
					
						
							|  |  |  | ! "Register-sensitive selection, duplication, and | 
					
						
							|  |  |  | !  sequencing of instructions" | 
					
						
							|  |  |  | ! by Vivek Sarkar, et al. | 
					
						
							|  |  |  | ! http://portal.acm.org/citation.cfm?id=377849 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : set-parent-indices ( node -- )
 | 
					
						
							|  |  |  |     children>> building get length
 | 
					
						
							|  |  |  |     '[ _ >>parent-index drop ] each ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-09 01:34:31 -05:00
										 |  |  | : ready? ( node -- ? ) precedes>> assoc-empty? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 20:22:50 -05:00
										 |  |  | ! Remove the node and unregister it from all nodes precedes links. | 
					
						
							|  |  |  | : remove-node ( nodes node -- )
 | 
					
						
							|  |  |  |     [ swap remove! ] keep '[ precedes>> _ swap delete-at ] each ;
 | 
					
						
							| 
									
										
										
										
											2010-02-10 22:43:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-08 13:01:00 -05:00
										 |  |  | : score ( node -- n )
 | 
					
						
							| 
									
										
										
										
											2010-02-12 00:12:17 -05:00
										 |  |  |     [ parent-index>> ] [ registers>> neg ] [ insn>> insn#>> ] tri 3array ;
 | 
					
						
							| 
									
										
										
										
											2010-02-10 22:43:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 20:22:50 -05:00
										 |  |  | : select-instruction ( nodes -- insn/f )
 | 
					
						
							| 
									
										
										
										
											2014-11-10 19:34:29 -05:00
										 |  |  |     [ f ] [ | 
					
						
							| 
									
										
										
										
											2014-11-10 20:22:50 -05:00
										 |  |  |         ! select one among the ready nodes (roots) | 
					
						
							|  |  |  |         dup [ ready? ] filter [ score ] supremum-by
 | 
					
						
							| 
									
										
										
										
											2014-11-10 19:34:29 -05:00
										 |  |  |         [ remove-node ] keep
 | 
					
						
							|  |  |  |         [ insn>> ] [ set-parent-indices ] bi
 | 
					
						
							| 
									
										
										
										
											2010-02-10 22:43:30 -05:00
										 |  |  |     ] if-empty ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 20:22:50 -05:00
										 |  |  | : (reorder) ( nodes -- )
 | 
					
						
							| 
									
										
										
										
											2014-11-10 19:34:29 -05:00
										 |  |  |     dup select-instruction [ , (reorder) ] [ drop ] if* ;
 | 
					
						
							| 
									
										
										
										
											2010-02-10 22:43:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-11 05:48:44 -05:00
										 |  |  | UNION: initial-insn | 
					
						
							|  |  |  |     ##phi ##inc-d ##inc-r ##callback-inputs | 
					
						
							|  |  |  |     ! See #1187 | 
					
						
							|  |  |  |     ##peek ;
 | 
					
						
							| 
									
										
										
										
											2010-02-10 22:43:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-15 17:38:34 -04:00
										 |  |  | UNION: final-insn | 
					
						
							|  |  |  | ##branch | 
					
						
							| 
									
										
										
										
											2010-07-15 20:55:31 -04:00
										 |  |  | ##dispatch | 
					
						
							| 
									
										
										
										
											2010-07-15 17:38:34 -04:00
										 |  |  | conditional-branch-insn | 
					
						
							| 
									
										
										
										
											2011-10-18 01:43:19 -04:00
										 |  |  | ##safepoint | 
					
						
							| 
									
										
										
										
											2010-07-15 17:38:34 -04:00
										 |  |  | ##epilogue ##return | 
					
						
							|  |  |  | ##callback-outputs ;
 | 
					
						
							| 
									
										
										
										
											2010-02-24 15:20:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-15 17:38:34 -04:00
										 |  |  | : initial-insn-end ( insns -- n )
 | 
					
						
							|  |  |  |     [ initial-insn? not ] find drop 0 or ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : final-insn-start ( insns -- n )
 | 
					
						
							|  |  |  |     [ final-insn? not ] find-last drop [ 1 + ] [ 0 ] if* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-08 13:01:00 -05:00
										 |  |  | : split-insns ( insns -- pre/body/post )
 | 
					
						
							| 
									
										
										
										
											2014-11-08 19:46:04 -05:00
										 |  |  |     dup [ initial-insn-end ] [ final-insn-start ] bi 2array split-indices ;
 | 
					
						
							| 
									
										
										
										
											2014-11-08 16:18:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 20:22:50 -05:00
										 |  |  | : setup-nodes ( insns -- nodes )
 | 
					
						
							|  |  |  |     [ <node> ] V{ } map-as
 | 
					
						
							|  |  |  |     [ build-dependence-graph ] [ build-fan-in-trees ] [ ] tri ;
 | 
					
						
							| 
									
										
										
										
											2014-11-09 01:34:31 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : reorder-body ( body -- body' )
 | 
					
						
							| 
									
										
										
										
											2014-11-10 20:22:50 -05:00
										 |  |  |     setup-nodes [ (reorder) ] V{ } make reverse ;
 | 
					
						
							| 
									
										
										
										
											2010-02-10 22:43:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : reorder ( insns -- insns' )
 | 
					
						
							| 
									
										
										
										
											2014-11-08 16:18:02 -05:00
										 |  |  |     split-insns first3 [ reorder-body ] dip 3append ;
 | 
					
						
							| 
									
										
										
										
											2010-02-10 22:43:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : schedule-block ( bb -- )
 | 
					
						
							| 
									
										
										
										
											2014-11-07 18:49:59 -05:00
										 |  |  |     [ reorder ] change-instructions drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! TODO: stack effect should be ( cfg -- ) | 
					
						
							|  |  |  | : schedule-instructions ( cfg --  cfg' )
 | 
					
						
							|  |  |  |     dup number-instructions | 
					
						
							| 
									
										
										
										
											2014-11-08 22:34:10 -05:00
										 |  |  |     dup reverse-post-order [ kill-block?>> not ] filter
 | 
					
						
							|  |  |  |     [ schedule-block ] each ;
 |