compiler.cfg.gvn: try to clean up availability issues (untested)

db4
Alex Vondrak 2011-06-28 17:58:55 -07:00 committed by John Benediktsson
parent a0c569c876
commit b90187a7b0
6 changed files with 67 additions and 42 deletions

View File

@ -9,6 +9,7 @@ compiler.cfg.registers
compiler.cfg.instructions compiler.cfg.instructions
compiler.cfg.gvn.math compiler.cfg.gvn.math
compiler.cfg.gvn.graph compiler.cfg.gvn.graph
compiler.cfg.gvn.avail
compiler.cfg.gvn.rewrite ; compiler.cfg.gvn.rewrite ;
IN: compiler.cfg.gvn.alien IN: compiler.cfg.gvn.alien
@ -34,12 +35,17 @@ M: ##box-displaced-alien rewrite
##add ##add
] { } make ; ] { } make ;
! XXX the vregs that src>> vreg>insn uses are not necessarily available
: rewrite-unbox-any-c-ptr ( insn -- insn/f ) : rewrite-unbox-any-c-ptr ( insn -- insn/f )
dup src>> vreg>insn dup src>> vreg>insn
{ {
{ [ dup ##box-alien? ] [ rewrite-unbox-alien ] } {
{ [ dup ##box-displaced-alien? ] [ rewrite-unbox-displaced-alien ] } [ dup [ ##box-alien? ] with-available-uses? ]
[ rewrite-unbox-alien ]
}
{
[ dup [ ##box-displaced-alien? ] with-available-uses? ]
[ rewrite-unbox-displaced-alien ]
}
[ 2drop f ] [ 2drop f ]
} cond ; } cond ;
@ -50,9 +56,8 @@ M: ##unbox-alien rewrite rewrite-unbox-any-c-ptr ;
! Fuse ##add-imm into ##load-memory(-imm) and ##store-memory(-imm) ! Fuse ##add-imm into ##load-memory(-imm) and ##store-memory(-imm)
! just update the offset in the instruction ! just update the offset in the instruction
: fuse-base-offset? ( insn -- ? ) : fuse-base-offset? ( insn -- ? )
base>> vreg>insn ##add-imm? ; base>> vreg>insn [ ##add-imm? ] with-available-uses? ;
! XXX base>> vreg>insn src1>> not necessarily available
: fuse-base-offset ( insn -- insn' ) : fuse-base-offset ( insn -- insn' )
clone dup base>> vreg>insn clone dup base>> vreg>insn
[ src1>> ] [ src2>> ] bi [ src1>> ] [ src2>> ] bi
@ -61,9 +66,11 @@ M: ##unbox-alien rewrite rewrite-unbox-any-c-ptr ;
! Fuse ##add-imm into ##load-memory and ##store-memory ! Fuse ##add-imm into ##load-memory and ##store-memory
! just update the offset in the instruction ! just update the offset in the instruction
: fuse-displacement-offset? ( insn -- ? ) : fuse-displacement-offset? ( insn -- ? )
{ [ scale>> 0 = ] [ displacement>> vreg>insn ##add-imm? ] } 1&& ; {
[ scale>> 0 = ]
[ displacement>> vreg>insn [ ##add-imm? ] with-available-uses? ]
} 1&& ;
! XXX displacement>> vreg>insn src1>> not necessarily available
: fuse-displacement-offset ( insn -- insn' ) : fuse-displacement-offset ( insn -- insn' )
clone dup displacement>> vreg>insn clone dup displacement>> vreg>insn
[ src1>> ] [ src2>> ] bi [ src1>> ] [ src2>> ] bi
@ -75,7 +82,7 @@ M: ##unbox-alien rewrite rewrite-unbox-any-c-ptr ;
: fuse-displacement? ( insn -- ? ) : fuse-displacement? ( insn -- ? )
{ {
[ offset>> 0 = complex-addressing? or ] [ offset>> 0 = complex-addressing? or ]
[ base>> vreg>insn ##add? ] [ base>> vreg>insn [ ##add? ] with-available-uses? ]
} 1&& ; } 1&& ;
GENERIC: alien-insn-value ( insn -- value ) GENERIC: alien-insn-value ( insn -- value )
@ -88,8 +95,6 @@ GENERIC: new-alien-insn ( value base displacement scale offset rep c-type insn -
M: ##load-memory-imm new-alien-insn drop \ ##load-memory new-insn ; M: ##load-memory-imm new-alien-insn drop \ ##load-memory new-insn ;
M: ##store-memory-imm new-alien-insn drop \ ##store-memory new-insn ; M: ##store-memory-imm new-alien-insn drop \ ##store-memory new-insn ;
! XXX base>> vreg>insn src1>> & src2>> not necessarily
! available
: fuse-displacement ( insn -- insn' ) : fuse-displacement ( insn -- insn' )
{ {
[ alien-insn-value ] [ alien-insn-value ]
@ -106,9 +111,11 @@ M: ##store-memory-imm new-alien-insn drop \ ##store-memory new-insn ;
{ [ ##shl-imm? ] [ src2>> { 1 2 3 } member? ] } 1&& ; { [ ##shl-imm? ] [ src2>> { 1 2 3 } member? ] } 1&& ;
: fuse-scale? ( insn -- ? ) : fuse-scale? ( insn -- ? )
{ [ scale>> 0 = ] [ displacement>> vreg>insn scale-insn? ] } 1&& ; {
[ scale>> 0 = ]
[ displacement>> vreg>insn [ scale-insn? ] with-available-uses? ]
} 1&& ;
! XXX displacement>> vreg>insn src1>> not necessarily available
: fuse-scale ( insn -- insn' ) : fuse-scale ( insn -- insn' )
clone dup displacement>> vreg>insn clone dup displacement>> vreg>insn
[ src1>> ] [ src2>> ] bi [ src1>> ] [ src2>> ] bi

View File

@ -1,10 +1,13 @@
! Copyright (C) 2011 Alex Vondrak. ! Copyright (C) 2011 Alex Vondrak.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs compiler.cfg USING: accessors assocs hashtables kernel namespaces sequences
sets
compiler.cfg
compiler.cfg.dataflow-analysis compiler.cfg.dataflow-analysis
compiler.cfg.def-use compiler.cfg.def-use
compiler.cfg.predecessors compiler.cfg.rpo deques dlists compiler.cfg.gvn.graph
hashtables kernel locals namespaces sequences sets ; compiler.cfg.predecessors
compiler.cfg.rpo ;
FROM: namespaces => set ; FROM: namespaces => set ;
IN: compiler.cfg.gvn.avail IN: compiler.cfg.gvn.avail
@ -18,8 +21,20 @@ FORWARD-ANALYSIS: avail
M: avail-analysis transfer-set drop defined assoc-union ; M: avail-analysis transfer-set drop defined assoc-union ;
! Strict idea of availability, for now. Would like to see if
! searching the VN congruence classes for the smallest
! available vn would work at all / better.
: available? ( vn -- ? ) : available? ( vn -- ? )
basic-block get avail-ins get at key? ; final-iteration? get [
basic-block get avail-ins get at key?
] [ drop t ] if ;
: available-uses? ( insn -- ? )
uses-vregs [ available? ] all? ;
: with-available-uses? ( quot -- ? )
[ available-uses? ] bi and ; inline
: make-available ( insn -- insn ) : make-available ( insn -- insn )
dup dst>> dup dst>>

View File

@ -9,6 +9,7 @@ compiler.cfg.instructions
compiler.cfg.registers compiler.cfg.registers
compiler.cfg.gvn.math compiler.cfg.gvn.math
compiler.cfg.gvn.graph compiler.cfg.gvn.graph
compiler.cfg.gvn.avail
compiler.cfg.gvn.rewrite ; compiler.cfg.gvn.rewrite ;
IN: compiler.cfg.gvn.comparisons IN: compiler.cfg.gvn.comparisons
@ -212,12 +213,11 @@ M: ##compare-integer rewrite
: rewrite-redundant-comparison? ( insn -- ? ) : rewrite-redundant-comparison? ( insn -- ? )
{ {
[ src1>> vreg>insn scalar-compare-insn? ] [ src1>> vreg>insn [ scalar-compare-insn? ] with-available-uses? ]
[ src2>> not ] [ src2>> not ]
[ cc>> { cc= cc/= } member? ] [ cc>> { cc= cc/= } member? ]
} 1&& ; inline } 1&& ; inline
! XXX the vregs that src1>> vreg>insn uses are not necessarily available
: rewrite-redundant-comparison ( insn -- insn' ) : rewrite-redundant-comparison ( insn -- insn' )
[ cc>> ] [ dst>> ] [ src1>> vreg>insn ] tri { [ cc>> ] [ dst>> ] [ src1>> vreg>insn ] tri {
{ [ dup ##compare? ] [ >compare< next-vreg \ ##compare new-insn ] } { [ dup ##compare? ] [ >compare< next-vreg \ ##compare new-insn ] }
@ -255,8 +255,9 @@ M: ##compare-integer-imm rewrite
[ drop f ] [ drop f ]
} cond ; } cond ;
! XXX the vregs (src1>> and src2>>) that src1>> vreg>insn uses : simplify-test? ( insn -- ? )
! are not necessarily available src1>> vreg>insn [ ##and? ] with-available-uses? ;
: (simplify-test) ( insn -- src1 src2 cc ) : (simplify-test) ( insn -- src1 src2 cc )
[ src1>> vreg>insn [ src1>> ] [ src2>> ] bi ] [ cc>> ] bi ; inline [ src1>> vreg>insn [ src1>> ] [ src2>> ] bi ] [ cc>> ] bi ; inline
@ -266,8 +267,9 @@ M: ##compare-integer-imm rewrite
: simplify-test-branch ( insn -- insn ) : simplify-test-branch ( insn -- insn )
(simplify-test) \ ##test-branch new-insn ; inline (simplify-test) \ ##test-branch new-insn ; inline
! XXX the vregs (src1>> and src2>>) that src1>> vreg>insn uses : simplify-test-imm? ( insn -- ? )
! are not necessarily available src1>> vreg>insn [ ##and-imm? ] with-available-uses? ;
: (simplify-test-imm) ( insn -- src1 src2 cc ) : (simplify-test-imm) ( insn -- src1 src2 cc )
[ src1>> vreg>insn [ src1>> ] [ src2>> ] bi ] [ cc>> ] bi ; inline [ src1>> vreg>insn [ src1>> ] [ src2>> ] bi ] [ cc>> ] bi ; inline
@ -291,8 +293,8 @@ M: ##test rewrite
{ [ dup src2>> vreg-immediate-comparand? ] [ f >test-imm ] } { [ dup src2>> vreg-immediate-comparand? ] [ f >test-imm ] }
{ [ dup diagonal? ] [ { [ dup diagonal? ] [
{ {
{ [ dup src1>> vreg>insn ##and? ] [ simplify-test ] } { [ dup simplify-test? ] [ simplify-test ] }
{ [ dup src1>> vreg>insn ##and-imm? ] [ simplify-test-imm ] } { [ dup simplify-test-imm? ] [ simplify-test-imm ] }
[ drop f ] [ drop f ]
} cond } cond
] } ] }
@ -305,8 +307,8 @@ M: ##test-branch rewrite
{ [ dup src2>> vreg-immediate-comparand? ] [ f >test-imm-branch ] } { [ dup src2>> vreg-immediate-comparand? ] [ f >test-imm-branch ] }
{ [ dup diagonal? ] [ { [ dup diagonal? ] [
{ {
{ [ dup src1>> vreg>insn ##and? ] [ simplify-test-branch ] } { [ dup simplify-test? ] [ simplify-test-branch ] }
{ [ dup src1>> vreg>insn ##and-imm? ] [ simplify-test-imm-branch ] } { [ dup simplify-test-imm? ] [ simplify-test-imm-branch ] }
[ drop f ] [ drop f ]
} cond } cond
] } ] }

View File

@ -7,6 +7,7 @@ compiler.cfg.registers
compiler.cfg.utilities compiler.cfg.utilities
compiler.cfg.gvn.folding compiler.cfg.gvn.folding
compiler.cfg.gvn.graph compiler.cfg.gvn.graph
compiler.cfg.gvn.avail
compiler.cfg.gvn.rewrite ; compiler.cfg.gvn.rewrite ;
IN: compiler.cfg.gvn.math IN: compiler.cfg.gvn.math
@ -23,7 +24,9 @@ M: ##tagged>integer rewrite
[ 2drop f ] [ 2drop f ]
} cond ; } cond ;
! XXX src>> vreg>insn src>> not necessarily available : self-inverse? ( insn quot -- ? )
[ src>> vreg>insn ] dip with-available-uses? ; inline
: self-inverse ( insn -- insn' ) : self-inverse ( insn -- insn' )
[ dst>> ] [ src>> vreg>insn src>> ] bi <copy> ; [ dst>> ] [ src>> vreg>insn src>> ] bi <copy> ;
@ -32,14 +35,14 @@ M: ##tagged>integer rewrite
M: ##neg rewrite M: ##neg rewrite
{ {
{ [ dup src>> vreg>insn ##neg? ] [ self-inverse ] } { [ dup [ ##neg? ] self-inverse? ] [ self-inverse ] }
{ [ dup unary-constant-fold? ] [ unary-constant-fold ] } { [ dup unary-constant-fold? ] [ unary-constant-fold ] }
[ drop f ] [ drop f ]
} cond ; } cond ;
M: ##not rewrite M: ##not rewrite
{ {
{ [ dup src>> vreg>insn ##not? ] [ self-inverse ] } { [ dup [ ##not? ] self-inverse? ] [ self-inverse ] }
{ [ dup unary-constant-fold? ] [ unary-constant-fold ] } { [ dup unary-constant-fold? ] [ unary-constant-fold ] }
[ drop f ] [ drop f ]
} cond ; } cond ;
@ -102,7 +105,6 @@ M: ##sub-imm rewrite sub-imm>add-imm ;
: mul-to-shl ( insn -- insn' ) : mul-to-shl ( insn -- insn' )
[ [ dst>> ] [ src1>> ] bi ] [ src2>> log2 ] bi \ ##shl-imm new-insn ; [ [ dst>> ] [ src1>> ] bi ] [ src2>> log2 ] bi \ ##shl-imm new-insn ;
! XXX not sure if availability is an issue
! Distribution converts ! Distribution converts
! ##+-imm 2 1 X ! ##+-imm 2 1 X
! ##*-imm 3 2 Y ! ##*-imm 3 2 Y
@ -120,10 +122,10 @@ M: ##sub-imm rewrite sub-imm>add-imm ;
] [ f ] if ; inline ] [ f ] if ; inline
: distribute-over-add? ( insn -- ? ) : distribute-over-add? ( insn -- ? )
src1>> vreg>insn ##add-imm? ; src1>> vreg>insn [ ##add-imm? ] with-available-uses? ;
: distribute-over-sub? ( insn -- ? ) : distribute-over-sub? ( insn -- ? )
src1>> vreg>insn ##sub-imm? ; src1>> vreg>insn [ ##sub-imm? ] with-available-uses? ;
: distribute ( insn add-op mul-op -- new-insns/f ) : distribute ( insn add-op mul-op -- new-insns/f )
[ [

View File

@ -12,15 +12,13 @@ compiler.cfg.comparisons
compiler.cfg.instructions compiler.cfg.instructions
compiler.cfg.gvn.math compiler.cfg.gvn.math
compiler.cfg.gvn.graph compiler.cfg.gvn.graph
compiler.cfg.gvn.avail
compiler.cfg.gvn.rewrite ; compiler.cfg.gvn.rewrite ;
IN: compiler.cfg.gvn.simd IN: compiler.cfg.gvn.simd
! Some lame constant folding for SIMD intrinsics. Eventually this ! Some lame constant folding for SIMD intrinsics. Eventually this
! should be redone completely. ! should be redone completely.
! XXX pretty much all of these rely on the vregs used by some
! vreg>insn, but they aren't necessarily available
: useless-shuffle-vector-imm? ( insn -- ? ) : useless-shuffle-vector-imm? ( insn -- ? )
[ shuffle>> ] [ rep>> rep-length iota ] bi sequence= ; [ shuffle>> ] [ rep>> rep-length iota ] bi sequence= ;
@ -42,7 +40,7 @@ IN: compiler.cfg.gvn.simd
M: ##shuffle-vector-imm rewrite M: ##shuffle-vector-imm rewrite
dup src>> vreg>insn { dup src>> vreg>insn {
{ [ over useless-shuffle-vector-imm? ] [ drop [ dst>> ] [ src>> ] bi <copy> ] } { [ over useless-shuffle-vector-imm? ] [ drop [ dst>> ] [ src>> ] bi <copy> ] }
{ [ dup ##shuffle-vector-imm? ] [ compose-shuffle-vector-imm ] } { [ dup [ ##shuffle-vector-imm? ] with-available-uses? ] [ compose-shuffle-vector-imm ] }
{ [ dup ##load-reference? ] [ fold-shuffle-vector-imm ] } { [ dup ##load-reference? ] [ fold-shuffle-vector-imm ] }
[ 2drop f ] [ 2drop f ]
} cond ; } cond ;
@ -64,7 +62,7 @@ M: ##shuffle-vector-imm rewrite
M: ##scalar>vector rewrite M: ##scalar>vector rewrite
dup src>> vreg>insn { dup src>> vreg>insn {
{ [ dup literal-insn? ] [ fold-scalar>vector ] } { [ dup literal-insn? ] [ fold-scalar>vector ] }
{ [ dup ##vector>scalar? ] [ [ dst>> ] [ src>> ] bi* <copy> ] } { [ dup [ ##vector>scalar? ] with-available-uses? ] [ [ dst>> ] [ src>> ] bi* <copy> ] }
[ 2drop f ] [ 2drop f ]
} cond ; } cond ;
@ -119,9 +117,9 @@ M: ##xor-vector rewrite
: vector-not? ( insn -- ? ) : vector-not? ( insn -- ? )
{ {
[ ##not-vector? ] [ [ ##not-vector? ] with-available-uses? ]
[ { [ {
[ ##xor-vector? ] [ [ ##xor-vector? ] with-available-uses? ]
[ [ src1>> ] [ src2>> ] bi [ vreg>insn ##fill-vector? ] either? ] [ [ src1>> ] [ src2>> ] bi [ vreg>insn ##fill-vector? ] either? ]
} 1&& ] } 1&& ]
} 1|| ; } 1|| ;

View File

@ -4,14 +4,15 @@ USING: accessors combinators.short-circuit cpu.architecture fry
kernel math kernel math
compiler.cfg.instructions compiler.cfg.instructions
compiler.cfg.gvn.graph compiler.cfg.gvn.graph
compiler.cfg.gvn.avail
compiler.cfg.gvn.rewrite ; compiler.cfg.gvn.rewrite ;
IN: compiler.cfg.gvn.slots IN: compiler.cfg.gvn.slots
: simplify-slot-addressing? ( insn -- ? ) : simplify-slot-addressing? ( insn -- ? )
complex-addressing? complex-addressing? [
[ slot>> vreg>insn ##add-imm? ] [ drop f ] if ; slot>> vreg>insn [ ##add-imm? ] with-available-uses?
] [ drop f ] if ;
! XXX the vregs that slot>> vreg>insn uses are not necessarily available
: simplify-slot-addressing ( insn -- insn/f ) : simplify-slot-addressing ( insn -- insn/f )
dup simplify-slot-addressing? [ dup simplify-slot-addressing? [
clone dup slot>> vreg>insn clone dup slot>> vreg>insn