<commit_before>! Copyright (C) 2008, 2009 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel namespaces assocs accessors sequences
compiler.cfg.rpo compiler.cfg.renaming compiler.cfg.instructions ;
IN: compiler.cfg.copy-prop

! The first three definitions are also used in compiler.cfg.alias-analysis.
SYMBOL: copies

: resolve ( vreg -- vreg )
    [ copies get at ] keep or ;

: record-copy ( insn -- )
    [ src>> resolve ] [ dst>> ] bi copies get set-at ; inline

: collect-copies ( cfg -- )
    H{ } clone copies set
    [
        instructions>>
        [ dup ##copy? [ record-copy ] [ drop ] if ] each
    ] each-basic-block ;

: rename-copies ( cfg -- )
    copies get dup assoc-empty? [ 2drop ] [
        renamings set
        [
            instructions>>
            [ dup ##copy? [ drop f ] [ rename-insn-uses t ] if ] filter-here
        ] each-basic-block
    ] if ;

: copy-propagation ( cfg -- cfg' )
    [ collect-copies ]
    [ rename-copies ]
    [ ]
    tri ;<commit_msg>compiler.cfg.copy-prop: work in progress<commit_after>! Copyright (C) 2008, 2009 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel namespaces assocs accessors sequences
compiler.cfg.rpo compiler.cfg.renaming compiler.cfg.instructions ;
IN: compiler.cfg.copy-prop

! The first three definitions are also used in compiler.cfg.alias-analysis.
SYMBOL: copies

: resolve ( vreg -- vreg )
    [ copies get at ] keep or ;

: record-copy ( ##copy -- )
    [ src>> resolve ] [ dst>> ] bi copies get set-at ; inline

<PRIVATE

GENERIC: visit-insn ( insn -- )

M: ##copy visit-insn record-copy ;

M: ##phi visit-insn inputs>> values [ resolve ] map all-equal? [ "BLAH!" print ] when ;

M: insn visit-insn drop ;

: collect-copies ( cfg -- )
    H{ } clone copies set
    [
        instructions>>
        [ visit-insn ] each
    ] each-basic-block ;

GENERIC: update-insn ( insn -- keep? )

M: ##copy update-insn drop f ;

M: insn update-insn rename-insn-uses t ;

: rename-copies ( cfg -- )
    copies get dup assoc-empty? [ 2drop ] [
        renamings set
        [
            instructions>>
            [ update-insn ] filter-here
        ] each-basic-block
    ] if ;

PRIVATE>

: copy-propagation ( cfg -- cfg' )
    [ collect-copies ]
    [ rename-copies ]
    [ ]
    tri ;
{'fraction_non_alphanumeric': 0.13180639585133966, 'fraction_numerical': 0.007778738115816767, 'mean_word_length': 2.3697234352256187, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2002833', 'n_tokens_mistral': 768, 'n_tokens_neox': 759, 'n_words': 213}