diff --git a/module/language/tree-il/compile-bytecode.scm b/module/language/tree-il/compile-bytecode.scm index b8d432f3c..59bed8d27 100644 --- a/module/language/tree-il/compile-bytecode.scm +++ b/module/language/tree-il/compile-bytecode.scm @@ -422,6 +422,20 @@ (($ src '>= (a b)) (reify-branch src '<= (list b a))) (($ src '> (a b)) (reify-branch src '< (list b a))) + ;; Specialize eq?. + (($ src 'eq? (a b)) + (let ((a (if (const? b) a b)) + (b (if (const? b) b a))) + (define (simplify test) (reify-branch src test (list a))) + (match b + (($ _ '()) (simplify 'eq-null?)) + (($ _ #f) (simplify 'eq-false?)) + (($ _ #t) (simplify 'eq-true?)) + (($ _ #nil) (simplify 'eq-nil?)) + (($ _ (? unspecified?)) (simplify 'unspecified?)) + (($ _ (? eof-object?)) (simplify 'eof-object?)) + (_ (reify-branch src 'eq? (list a b)))))) + ;; Simplify "not". (($ src 'not (x)) (finish-conditional diff --git a/module/language/tree-il/compile-cps.scm b/module/language/tree-il/compile-cps.scm index 334b4ce70..9484e84dc 100644 --- a/module/language/tree-il/compile-cps.scm +++ b/module/language/tree-il/compile-cps.scm @@ -2365,6 +2365,25 @@ integer." (make-const src #t) (make-const src #f))) + ;; Specialize eq?. + (($ src 'eq? (a b)) + (define (reify-branch test args) + ;; No need to reduce as test is a branching primitive. + (make-conditional src (make-primcall src test args) + (make-const src #t) + (make-const src #f))) + (let ((a (if (const? b) a b)) + (b (if (const? b) b a))) + (define (simplify test) (reify-branch test (list a))) + (match b + (($ _ '()) (simplify 'eq-null?)) + (($ _ #f) (simplify 'eq-false?)) + (($ _ #t) (simplify 'eq-true?)) + (($ _ #nil) (simplify 'eq-nil?)) + (($ _ (? unspecified?)) (simplify 'unspecified?)) + (($ _ (? eof-object?)) (simplify 'eof-object?)) + (_ (reify-branch 'eq? (list a b)))))) + (($ src (? branching-primitive? name) args) ;; No need to reduce because test is not reducible: reifying ;; #t/#f is the right thing.