| 
									
										
										
										
											2015-06-15 04:44:53 -04:00
										 |  |  | USING: accessors arrays assocs compiler.cfg.def-use | 
					
						
							|  |  |  | compiler.cfg.instructions compiler.cfg.linearization | 
					
						
							|  |  |  | compiler.cfg.registers compiler.cfg.ssa.destruction.leaders | 
					
						
							|  |  |  | compiler.cfg.ssa.interference cpu.architecture fry kernel make | 
					
						
							|  |  |  | namespaces sequences sets sorting ;
 | 
					
						
							|  |  |  | IN: compiler.cfg.ssa.destruction.coalescing | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : zip-scalar ( scalar seq -- pairs )
 | 
					
						
							|  |  |  |     [ 2array ] with map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: class-element-map | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : value-of ( vreg -- value )
 | 
					
						
							|  |  |  |     dup insn-of dup ##tagged>integer? [ nip src>> ] [ drop ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : coalesce-elements ( merged follower leader -- )
 | 
					
						
							|  |  |  |     class-element-map get [ delete-at ] [ set-at ] bi-curry bi* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : coalesce-vregs ( merged follower leader -- )
 | 
					
						
							|  |  |  |     2dup swap leader-map get set-at coalesce-elements ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : vregs-interfere? ( vreg1 vreg2 -- merged/f ? )
 | 
					
						
							| 
									
										
										
										
											2015-06-15 12:11:16 -04:00
										 |  |  |     class-element-map get '[ _ at ] bi@ sets-interfere? ;
 | 
					
						
							| 
									
										
										
										
											2015-06-15 04:44:53 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ERROR: vregs-shouldn't-interfere vreg1 vreg2 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : try-eliminate-copy ( follower leader must? -- )
 | 
					
						
							|  |  |  |     -rot leaders 2dup = [ 3drop ] [ | 
					
						
							|  |  |  |         2dup vregs-interfere? [ | 
					
						
							| 
									
										
										
										
											2015-08-13 06:20:39 -04:00
										 |  |  |             drop rot [ throw-vregs-shouldn't-interfere ] [ 2drop ] if
 | 
					
						
							| 
									
										
										
										
											2015-06-15 04:44:53 -04:00
										 |  |  |         ] [ -rot coalesce-vregs drop ] if
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : try-eliminate-copies ( pairs must? -- )
 | 
					
						
							|  |  |  |     '[ first2 _ try-eliminate-copy ] each ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 17:18:01 -04:00
										 |  |  | : initial-leaders ( insns -- leaders )
 | 
					
						
							|  |  |  |     [ [ defs-vregs ] [ temp-vregs ] bi append ] map concat unique ;
 | 
					
						
							| 
									
										
										
										
											2015-06-15 04:44:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 17:18:01 -04:00
										 |  |  | : initial-class-elements ( -- class-elements )
 | 
					
						
							|  |  |  |     defs get [ [ dup dup value-of ] dip <vreg-info> 1array ] assoc-map ;
 | 
					
						
							| 
									
										
										
										
											2015-06-15 04:44:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 17:18:01 -04:00
										 |  |  | : init-coalescing ( insns -- )
 | 
					
						
							|  |  |  |     initial-leaders leader-map set
 | 
					
						
							|  |  |  |     initial-class-elements class-element-map set ;
 | 
					
						
							| 
									
										
										
										
											2015-06-15 04:44:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 17:18:01 -04:00
										 |  |  | GENERIC: coalesce-now ( insn -- )
 | 
					
						
							| 
									
										
										
										
											2015-06-15 04:44:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 17:18:01 -04:00
										 |  |  | M: insn coalesce-now drop ;
 | 
					
						
							| 
									
										
										
										
											2015-06-15 04:44:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 17:18:01 -04:00
										 |  |  | M: ##tagged>integer coalesce-now | 
					
						
							| 
									
										
										
										
											2015-06-15 04:44:53 -04:00
										 |  |  |     [ dst>> ] [ src>> ] bi t try-eliminate-copy ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 17:18:01 -04:00
										 |  |  | M: ##phi coalesce-now | 
					
						
							| 
									
										
										
										
											2015-06-15 04:44:53 -04:00
										 |  |  |     [ dst>> ] [ inputs>> values ] bi zip-scalar | 
					
						
							|  |  |  |     natural-sort t try-eliminate-copies ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 17:18:01 -04:00
										 |  |  | GENERIC: coalesce-later ( insn -- )
 | 
					
						
							| 
									
										
										
										
											2015-07-28 14:39:51 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 17:18:01 -04:00
										 |  |  | M: insn coalesce-later drop ;
 | 
					
						
							| 
									
										
										
										
											2015-07-28 14:39:51 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 17:18:01 -04:00
										 |  |  | M: alien-call-insn coalesce-later drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: vreg-insn coalesce-later | 
					
						
							|  |  |  |     [ defs-vregs ] [ uses-vregs ] bi zip ?first [ , ] when* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: ##copy coalesce-later | 
					
						
							|  |  |  |     [ dst>> ] [ src>> ] bi 2array , ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: ##parallel-copy coalesce-later | 
					
						
							|  |  |  |     values>> % ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : eliminatable-copy? ( vreg1 vreg2 -- ? )
 | 
					
						
							|  |  |  |     [ rep-of ] bi@ [ [ reg-class-of ] same? ] [ [ rep-size ] same? ] 2bi and ;
 | 
					
						
							| 
									
										
										
										
											2015-07-28 14:39:51 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-15 04:44:53 -04:00
										 |  |  | : coalesce-cfg ( cfg -- )
 | 
					
						
							| 
									
										
										
										
											2015-07-28 17:18:01 -04:00
										 |  |  |     cfg>insns-rpo dup init-coalescing | 
					
						
							|  |  |  |     [ [ [ coalesce-now ] [ coalesce-later ] bi ] each ] { } make | 
					
						
							|  |  |  |     [ first2 eliminatable-copy? ] filter f try-eliminate-copies ;
 |