From 2034e1ef05b1bc9d789e75ac2787f1f1f3f1270a Mon Sep 17 00:00:00 2001 From: Daniel Ehrenberg Date: Wed, 24 Feb 2010 14:20:43 -0600 Subject: [PATCH] Fewer unnecessary dependence edges between stack operations, implemented by reviving stack height normalization. Now, 40% of spills and reloads are eliminated in total --- .../compiler/cfg/dependence/dependence.factor | 17 +----- basis/compiler/cfg/height/height.factor | 55 +++++++++++++++++++ basis/compiler/cfg/height/summary.txt | 1 + basis/compiler/cfg/optimizer/optimizer.factor | 2 + .../compiler/cfg/scheduling/scheduling.factor | 5 +- 5 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 basis/compiler/cfg/height/height.factor create mode 100644 basis/compiler/cfg/height/summary.txt diff --git a/basis/compiler/cfg/dependence/dependence.factor b/basis/compiler/cfg/dependence/dependence.factor index 48f8da7b53..947e9e3bd1 100644 --- a/basis/compiler/cfg/dependence/dependence.factor +++ b/basis/compiler/cfg/dependence/dependence.factor @@ -53,16 +53,6 @@ M: node hashcode* nip number>> ; UNION: stack-read-write ##peek ##replace ; -PREDICATE: ds-read-write < stack-read-write - loc>> ds-loc? ; -UNION: data-stack-insn - ##inc-d ds-read-write ; - -PREDICATE: rs-read-write < stack-read-write - loc>> rs-loc? ; -UNION: retain-stack-insn - ##inc-r rs-read-write ; - UNION: ##alien-read ##alien-double ##alien-float ##alien-cell ##alien-vector ##alien-signed-1 ##alien-signed-2 ##alien-signed-4 @@ -92,11 +82,8 @@ UNION: alien-call-insn GENERIC: add-control-edge ( node insn -- ) -M: data-stack-insn add-control-edge - drop data-stack-insn chain ; - -M: retain-stack-insn add-control-edge - drop retain-stack-insn chain ; +M: stack-read-write add-control-edge + loc>> chain ; M: alien-memory-insn add-control-edge drop alien-memory-insn chain ; diff --git a/basis/compiler/cfg/height/height.factor b/basis/compiler/cfg/height/height.factor new file mode 100644 index 0000000000..a782d2d4bc --- /dev/null +++ b/basis/compiler/cfg/height/height.factor @@ -0,0 +1,55 @@ +! Copyright (C) 2008, 2009 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors math namespaces sequences kernel fry +compiler.cfg compiler.cfg.registers compiler.cfg.instructions +compiler.cfg.rpo ; +IN: compiler.cfg.height + +! Combine multiple stack height changes into one at the +! start of the basic block. + +SYMBOL: ds-height +SYMBOL: rs-height + +GENERIC: compute-heights ( insn -- ) + +M: ##inc-d compute-heights n>> ds-height [ + ] change ; +M: ##inc-r compute-heights n>> rs-height [ + ] change ; +M: insn compute-heights drop ; + +GENERIC: normalize-height* ( insn -- insn' ) + +: normalize-inc-d/r ( insn stack -- insn' ) + swap n>> '[ _ - ] change f ; inline + +M: ##inc-d normalize-height* ds-height normalize-inc-d/r ; +M: ##inc-r normalize-height* rs-height normalize-inc-d/r ; + +GENERIC: loc-stack ( loc -- stack ) + +M: ds-loc loc-stack drop ds-height ; +M: rs-loc loc-stack drop rs-height ; + +GENERIC: ( n stack -- loc ) + +M: ds-loc drop ; +M: rs-loc drop ; + +: normalize-peek/replace ( insn -- insn' ) + [ [ [ n>> ] [ loc-stack get ] bi + ] keep ] change-loc ; inline + +M: ##peek normalize-height* normalize-peek/replace ; +M: ##replace normalize-height* normalize-peek/replace ; + +M: insn normalize-height* ; + +: height-step ( insns -- insns' ) + 0 ds-height set + 0 rs-height set + [ [ compute-heights ] each ] + [ [ [ normalize-height* ] map sift ] with-scope ] bi + ds-height get dup 0 = [ drop ] [ \ ##inc-d new-insn prefix ] if + rs-height get dup 0 = [ drop ] [ \ ##inc-r new-insn prefix ] if ; + +: normalize-height ( cfg -- cfg' ) + [ height-step ] local-optimization ; diff --git a/basis/compiler/cfg/height/summary.txt b/basis/compiler/cfg/height/summary.txt new file mode 100644 index 0000000000..ce1974ad60 --- /dev/null +++ b/basis/compiler/cfg/height/summary.txt @@ -0,0 +1 @@ +Stack height normalization coalesces height changes at start of basic block diff --git a/basis/compiler/cfg/optimizer/optimizer.factor b/basis/compiler/cfg/optimizer/optimizer.factor index b556514174..bdd1ff34f4 100644 --- a/basis/compiler/cfg/optimizer/optimizer.factor +++ b/basis/compiler/cfg/optimizer/optimizer.factor @@ -5,6 +5,7 @@ compiler.cfg.tco compiler.cfg.useless-conditionals compiler.cfg.branch-splitting compiler.cfg.block-joining +compiler.cfg.height compiler.cfg.ssa.construction compiler.cfg.alias-analysis compiler.cfg.value-numbering @@ -30,6 +31,7 @@ SYMBOL: check-optimizer? delete-useless-conditionals split-branches join-blocks + normalize-height construct-ssa alias-analysis value-numbering diff --git a/basis/compiler/cfg/scheduling/scheduling.factor b/basis/compiler/cfg/scheduling/scheduling.factor index fe0cf5cef9..2f08ad96cf 100644 --- a/basis/compiler/cfg/scheduling/scheduling.factor +++ b/basis/compiler/cfg/scheduling/scheduling.factor @@ -58,8 +58,11 @@ ERROR: bad-delete-at key assoc ; : cut-by ( seq quot -- before after ) dupd find drop [ cut ] [ f ] if* ; inline +UNION: initial-insn + ##phi ##inc-d ##inc-r ; + : split-3-ways ( insns -- first middle last ) - [ ##phi? not ] cut-by unclip-last ; + [ initial-insn? not ] cut-by unclip-last ; : reorder ( insns -- insns' ) split-3-ways [