From f9695951a072e689d5591798e06a1dd88d6b369c Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 1 Oct 2009 19:53:30 -0500 Subject: [PATCH] fold test-vector/branch sequences into a test-vector-branch instruction --- .../cfg/comparisons/comparisons.factor | 10 +++++- .../cfg/instructions/instructions.factor | 13 +++++++- .../cfg/linearization/linearization.factor | 23 ++++++++++--- .../value-numbering/rewrite/rewrite.factor | 10 ++++++ .../value-numbering-tests.factor | 18 +++++++++++ basis/compiler/codegen/codegen.factor | 1 + basis/cpu/architecture/architecture.factor | 3 +- basis/cpu/x86/x86.factor | 32 ++++++++++++++----- 8 files changed, 95 insertions(+), 15 deletions(-) diff --git a/basis/compiler/cfg/comparisons/comparisons.factor b/basis/compiler/cfg/comparisons/comparisons.factor index d538ee818c..0b4a6f2f02 100644 --- a/basis/compiler/cfg/comparisons/comparisons.factor +++ b/basis/compiler/cfg/comparisons/comparisons.factor @@ -10,7 +10,7 @@ SYMBOLS: cc/< cc/<= cc/= cc/> cc/>= cc/<> cc/<>= ; SYMBOLS: - vcc-all vcc-any vcc-none ; + vcc-all vcc-notall vcc-any vcc-none ; : negate-cc ( cc -- cc' ) H{ @@ -30,6 +30,14 @@ SYMBOLS: { cc/<>= cc<>= } } at ; +: negate-vcc ( cc -- cc' ) + H{ + { vcc-all vcc-notall } + { vcc-any vcc-none } + { vcc-none vcc-any } + { vcc-notall vcc-all } + } at ; + : swap-cc ( cc -- cc' ) H{ { cc< cc> } diff --git a/basis/compiler/cfg/instructions/instructions.factor b/basis/compiler/cfg/instructions/instructions.factor index 09e7736235..d40aabcb19 100644 --- a/basis/compiler/cfg/instructions/instructions.factor +++ b/basis/compiler/cfg/instructions/instructions.factor @@ -304,7 +304,18 @@ literal: rep cc ; PURE-INSN: ##test-vector def: dst/int-rep -use: src +use: src1 +temp: temp/int-rep +literal: rep vcc ; + +INSN: ##test-vector-branch +use: src1 +temp: temp/int-rep +literal: rep vcc ; + +INSN: _test-vector-branch +literal: label +use: src1 temp: temp/int-rep literal: rep vcc ; diff --git a/basis/compiler/cfg/linearization/linearization.factor b/basis/compiler/cfg/linearization/linearization.factor index 66ac1addb0..31a4247206 100755 --- a/basis/compiler/cfg/linearization/linearization.factor +++ b/basis/compiler/cfg/linearization/linearization.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: kernel math accessors sequences namespaces make combinators assocs arrays locals layouts hashtables -cpu.architecture +cpu.architecture generalizations compiler.cfg compiler.cfg.comparisons compiler.cfg.stack-frame @@ -42,14 +42,26 @@ M: ##branch linearize-insn : successors ( bb -- first second ) successors>> first2 ; inline +:: conditional ( bb insn n conditional-quot negate-cc-quot -- bb successor label ... ) + bb insn + conditional-quot + [ drop dup successors>> second useless-branch? ] 2bi + [ [ swap block-number ] n ndip ] + [ [ block-number ] n ndip negate-cc-quot call ] if ; inline + : (binary-conditional) ( bb insn -- bb successor1 successor2 src1 src2 cc ) [ dup successors ] [ [ src1>> ] [ src2>> ] [ cc>> ] tri ] bi* ; inline : binary-conditional ( bb insn -- bb successor label2 src1 src2 cc ) - [ (binary-conditional) ] - [ drop dup successors>> second useless-branch? ] 2bi - [ [ swap block-number ] 3dip ] [ [ block-number ] 3dip negate-cc ] if ; + 3 [ (binary-conditional) ] [ negate-cc ] conditional ; + +: (test-vector-conditional) ( bb insn -- bb successor1 successor2 src1 temp rep vcc ) + [ dup successors ] + [ { [ src1>> ] [ temp>> ] [ rep>> ] [ vcc>> ] } cleave ] bi* ; inline + +: test-vector-conditional ( bb insn -- bb successor label src1 temp rep vcc ) + 4 [ (test-vector-conditional) ] [ negate-vcc ] conditional ; M: ##compare-branch linearize-insn binary-conditional _compare-branch emit-branch ; @@ -63,6 +75,9 @@ M: ##compare-float-ordered-branch linearize-insn M: ##compare-float-unordered-branch linearize-insn binary-conditional _compare-float-unordered-branch emit-branch ; +M: ##test-vector-branch linearize-insn + test-vector-conditional _test-vector-branch emit-branch ; + : overflow-conditional ( bb insn -- bb successor label2 dst src1 src2 ) [ dup successors block-number ] [ [ dst>> ] [ src1>> ] [ src2>> ] tri ] bi* ; inline diff --git a/basis/compiler/cfg/value-numbering/rewrite/rewrite.factor b/basis/compiler/cfg/value-numbering/rewrite/rewrite.factor index 8e5e013606..9827e02bf5 100755 --- a/basis/compiler/cfg/value-numbering/rewrite/rewrite.factor +++ b/basis/compiler/cfg/value-numbering/rewrite/rewrite.factor @@ -40,6 +40,7 @@ M: insn rewrite drop f ; [ compare-imm-expr? ] [ compare-float-unordered-expr? ] [ compare-float-ordered-expr? ] + [ test-vector-expr? ] } 1|| ; : rewrite-boolean-comparison? ( insn -- ? ) @@ -53,12 +54,21 @@ M: insn rewrite drop f ; : >compare-imm-expr< ( expr -- in1 in2 cc ) [ src1>> vn>vreg ] [ src2>> vn>constant ] [ cc>> ] tri ; inline +: >test-vector-expr< ( expr -- src1 temp rep vcc ) + { + [ src1>> vn>vreg ] + [ drop next-vreg ] + [ rep>> ] + [ vcc>> ] + } cleave ; inline + : rewrite-boolean-comparison ( expr -- insn ) src1>> vreg>expr { { [ dup compare-expr? ] [ >compare-expr< \ ##compare-branch new-insn ] } { [ dup compare-imm-expr? ] [ >compare-imm-expr< \ ##compare-imm-branch new-insn ] } { [ dup compare-float-unordered-expr? ] [ >compare-expr< \ ##compare-float-unordered-branch new-insn ] } { [ dup compare-float-ordered-expr? ] [ >compare-expr< \ ##compare-float-ordered-branch new-insn ] } + { [ dup test-vector-expr? ] [ >test-vector-expr< \ ##test-vector-branch new-insn ] } } cond ; : tag-fixnum-expr? ( expr -- ? ) diff --git a/basis/compiler/cfg/value-numbering/value-numbering-tests.factor b/basis/compiler/cfg/value-numbering/value-numbering-tests.factor index b2750da3fa..ba33885aac 100644 --- a/basis/compiler/cfg/value-numbering/value-numbering-tests.factor +++ b/basis/compiler/cfg/value-numbering/value-numbering-tests.factor @@ -14,6 +14,8 @@ IN: compiler.cfg.value-numbering.tests [ ##compare-imm? ] [ ##compare-float-unordered? ] [ ##compare-float-ordered? ] + [ ##test-vector? ] + [ ##test-vector-branch? ] } 1|| [ f >>temp ] when ] map ; @@ -141,6 +143,22 @@ IN: compiler.cfg.value-numbering.tests } value-numbering-step trim-temps ] unit-test +[ + { + T{ ##peek f 1 D -1 } + T{ ##unbox-vector f 1111 1 float-4-rep } + T{ ##test-vector f 1 1111 f float-4-rep vcc-any } + T{ ##test-vector-branch f 1111 f float-4-rep vcc-any } + } +] [ + { + T{ ##peek f 1 D -1 } + T{ ##unbox-vector f 1111 1 float-4-rep } + T{ ##test-vector f 1 1111 2 float-4-rep vcc-any } + T{ ##compare-imm-branch f 1 5 cc/= } + } value-numbering-step trim-temps +] unit-test + ! Immediate operand conversion [ { diff --git a/basis/compiler/codegen/codegen.factor b/basis/compiler/codegen/codegen.factor index 7c4c593d16..839e1aef05 100755 --- a/basis/compiler/codegen/codegen.factor +++ b/basis/compiler/codegen/codegen.factor @@ -235,6 +235,7 @@ CODEGEN: _compare-branch %compare-branch CODEGEN: _compare-imm-branch %compare-imm-branch CODEGEN: _compare-float-ordered-branch %compare-float-ordered-branch CODEGEN: _compare-float-unordered-branch %compare-float-unordered-branch +CODEGEN: _test-vector-branch %test-vector-branch CODEGEN: _dispatch %dispatch CODEGEN: _spill %spill CODEGEN: _reload %reload diff --git a/basis/cpu/architecture/architecture.factor b/basis/cpu/architecture/architecture.factor index ffe77671a7..41b3fed08d 100644 --- a/basis/cpu/architecture/architecture.factor +++ b/basis/cpu/architecture/architecture.factor @@ -224,7 +224,8 @@ HOOK: %gather-vector-2 cpu ( dst src1 src2 rep -- ) HOOK: %gather-vector-4 cpu ( dst src1 src2 src3 src4 rep -- ) HOOK: %shuffle-vector cpu ( dst src shuffle rep -- ) HOOK: %compare-vector cpu ( dst src1 src2 rep cc -- ) -HOOK: %test-vector cpu ( dst src temp rep vcc -- ) +HOOK: %test-vector cpu ( dst src1 temp rep vcc -- ) +HOOK: %test-vector-branch cpu ( label src1 temp rep vcc -- ) HOOK: %add-vector cpu ( dst src1 src2 rep -- ) HOOK: %saturated-add-vector cpu ( dst src1 src2 rep -- ) HOOK: %add-sub-vector cpu ( dst src1 src2 rep -- ) diff --git a/basis/cpu/x86/x86.factor b/basis/cpu/x86/x86.factor index 877afaa390..cca8e617ca 100644 --- a/basis/cpu/x86/x86.factor +++ b/basis/cpu/x86/x86.factor @@ -747,20 +747,36 @@ M: x86 %compare-vector-reps { sse4.1? { longlong-2-rep ulonglong-2-rep } } } available-reps ; -:: (%test-vector) ( dst temp mask vcc -- ) +:: %test-vector-mask ( dst temp mask vcc -- ) vcc { - { vcc-any [ dst dst TEST dst temp \ CMOVNE %boolean ] } - { vcc-none [ dst dst TEST dst temp \ CMOVE %boolean ] } - { vcc-all [ dst mask CMP dst temp \ CMOVE %boolean ] } + { vcc-any [ dst dst TEST dst temp \ CMOVNE %boolean ] } + { vcc-none [ dst dst TEST dst temp \ CMOVE %boolean ] } + { vcc-all [ dst mask CMP dst temp \ CMOVE %boolean ] } + { vcc-notall [ dst mask CMP dst temp \ CMOVNE %boolean ] } } case ; -M:: x86 %test-vector ( dst src temp rep vcc -- ) - dst src rep { +: %move-vector-mask ( dst src rep -- mask ) + { { double-2-rep [ MOVMSKPD HEX: 3 ] } { float-4-rep [ MOVMSKPS HEX: f ] } [ drop PMOVMSKB HEX: ffff ] - } case :> mask - dst temp mask vcc (%test-vector) ; + } case ; + +M:: x86 %test-vector ( dst src temp rep vcc -- ) + dst src rep %move-vector-mask :> mask + dst temp mask vcc %test-vector-mask ; + +:: %test-vector-mask-branch ( label temp mask vcc -- ) + vcc { + { vcc-any [ temp temp TEST label JNE ] } + { vcc-none [ temp temp TEST label JE ] } + { vcc-all [ temp mask CMP label JE ] } + { vcc-notall [ temp mask CMP label JNE ] } + } case ; + +M:: x86 %test-vector-branch ( label src temp rep vcc -- ) + temp src rep %move-vector-mask :> mask + label temp mask vcc %test-vector-mask-branch ; M: x86 %test-vector-reps {