1 ! Copyright (C) 2008 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays combinators compiler.tree
4 compiler.tree.combinators compiler.tree.propagation.info fry
5 hashtables kernel math math.partial-dispatch sequences words ;
6 IN: compiler.tree.identities
8 : define-identities ( word identities -- )
9 [ integer-derived-ops dup empty? f assert= ] dip
10 '[ _ "identities" set-word-prop ] each ;
54 : matches? ( pattern infos -- ? )
55 [ over X eq? [ 2drop t ] [ literal>> = ] if ] 2all? ;
57 : find-identity ( patterns infos -- result )
58 '[ first _ matches? ] find swap [ second ] when ;
60 GENERIC: apply-identities* ( node -- node )
62 : simplify-to-constant ( #call constant -- nodes )
63 [ [ in-d>> <#drop> ] [ out-d>> first ] bi ] dip swap <#push>
66 : select-input ( node n -- #shuffle )
67 [ [ in-d>> ] [ out-d>> ] bi ] dip
68 pick nth over first associate <#data-shuffle> ;
70 M: #call apply-identities*
71 dup word>> "identities" word-prop [
72 over node-input-infos find-identity [
74 { \ drop [ 0 select-input ] }
75 { \ nip [ 1 select-input ] }
76 [ simplify-to-constant ]
81 M: node apply-identities* ;
83 : apply-identities ( nodes -- nodes' )
84 [ apply-identities* ] map-nodes ;