Make dead code elimination work for non case lambdas.
Related to PR14138.
This commit is contained in:
parent
210aa98d6c
commit
dd01d9932d
|
@ -37,9 +37,12 @@
|
||||||
(pattern ((~and kw if) tst:contradiction thn:expr els:opt-expr)
|
(pattern ((~and kw if) tst:contradiction thn:expr els:opt-expr)
|
||||||
#:do [(log-optimization "dead then branch" "Unreachable then branch elimination." #'thn)]
|
#:do [(log-optimization "dead then branch" "Unreachable then branch elimination." #'thn)]
|
||||||
#:with opt (syntax/loc/origin this-syntax #'kw (if tst.opt thn els.opt)))
|
#:with opt (syntax/loc/origin this-syntax #'kw (if tst.opt thn els.opt)))
|
||||||
|
(pattern ((~and kw lambda) formals . bodies)
|
||||||
|
#:when (dead-lambda-branch? #'formals)
|
||||||
|
#:with opt this-syntax)
|
||||||
(pattern ((~and kw case-lambda) (formals . bodies) ...)
|
(pattern ((~and kw case-lambda) (formals . bodies) ...)
|
||||||
#:when (for/or ((formals (in-syntax #'(formals ...))))
|
#:when (for/or ((formals (in-syntax #'(formals ...))))
|
||||||
(dead-case-lambda-branch? formals))
|
(dead-lambda-branch? formals))
|
||||||
#:with opt
|
#:with opt
|
||||||
(quasisyntax/loc/origin
|
(quasisyntax/loc/origin
|
||||||
this-syntax #'kw
|
this-syntax #'kw
|
||||||
|
@ -47,13 +50,13 @@
|
||||||
(case-lambda
|
(case-lambda
|
||||||
#,@(for/list ((formals (in-syntax #'(formals ...)))
|
#,@(for/list ((formals (in-syntax #'(formals ...)))
|
||||||
(bodies (in-syntax #'(bodies ...)))
|
(bodies (in-syntax #'(bodies ...)))
|
||||||
#:unless (dead-case-lambda-branch? formals))
|
#:unless (dead-lambda-branch? formals))
|
||||||
(cons formals (stx-map (optimize) bodies))))
|
(cons formals (stx-map (optimize) bodies))))
|
||||||
;; We need to keep the syntax objects around in the generated code with the correct bindings
|
;; We need to keep the syntax objects around in the generated code with the correct bindings
|
||||||
;; so that CheckSyntax displays the arrows correctly
|
;; so that CheckSyntax displays the arrows correctly
|
||||||
#,@(for/list ((formals (in-syntax #'(formals ...)))
|
#,@(for/list ((formals (in-syntax #'(formals ...)))
|
||||||
(bodies (in-syntax #'(bodies ...)))
|
(bodies (in-syntax #'(bodies ...)))
|
||||||
#:when (dead-case-lambda-branch? formals))
|
#:when (dead-lambda-branch? formals))
|
||||||
(log-optimization
|
(log-optimization
|
||||||
"dead case-lambda branch"
|
"dead case-lambda branch"
|
||||||
"Unreachable case-lambda branch elimination."
|
"Unreachable case-lambda branch elimination."
|
||||||
|
|
|
@ -334,7 +334,7 @@
|
||||||
[(or (arities-seen-seen-before? arities-seen arity)
|
[(or (arities-seen-seen-before? arities-seen arity)
|
||||||
(and expected-type (null? (find-matching-arities formal))))
|
(and expected-type (null? (find-matching-arities formal))))
|
||||||
(warn-unreachable body)
|
(warn-unreachable body)
|
||||||
(add-dead-case-lambda-branch (formals-syntax formal))
|
(add-dead-lambda-branch (formals-syntax formal))
|
||||||
(if (check-unreachable-code?)
|
(if (check-unreachable-code?)
|
||||||
(cons formal+body formals+bodies*)
|
(cons formal+body formals+bodies*)
|
||||||
formals+bodies*)]
|
formals+bodies*)]
|
||||||
|
|
|
@ -56,16 +56,16 @@
|
||||||
(eq? t? (hash-ref tautology-contradiction-table e 'not-there)))
|
(eq? t? (hash-ref tautology-contradiction-table e 'not-there)))
|
||||||
(values (mk 'tautology) (mk 'contradiction) (mk 'neither))))
|
(values (mk 'tautology) (mk 'contradiction) (mk 'neither))))
|
||||||
|
|
||||||
;; keeps track of case-lambda branches that never get evaluated, so that the
|
;; keeps track of lambda branches that never get evaluated, so that the
|
||||||
;; optimizer can eliminate dead code. The key is the formals syntax object.
|
;; optimizer can eliminate dead code. The key is the formals syntax object.
|
||||||
;; 1 possible value: #t
|
;; 1 possible value: #t
|
||||||
(define case-lambda-dead-table (make-hasheq))
|
(define lambda-dead-table (make-hasheq))
|
||||||
|
|
||||||
(define (add-dead-case-lambda-branch formals)
|
(define (add-dead-lambda-branch formals)
|
||||||
(when (optimize?)
|
(when (optimize?)
|
||||||
(hash-set! case-lambda-dead-table formals #t)))
|
(hash-set! lambda-dead-table formals #t)))
|
||||||
(define (dead-case-lambda-branch? formals)
|
(define (dead-lambda-branch? formals)
|
||||||
(hash-ref case-lambda-dead-table formals #f))
|
(hash-ref lambda-dead-table formals #f))
|
||||||
|
|
||||||
|
|
||||||
(provide/cond-contract
|
(provide/cond-contract
|
||||||
|
@ -78,5 +78,5 @@
|
||||||
[tautology? (syntax? . -> . boolean?)]
|
[tautology? (syntax? . -> . boolean?)]
|
||||||
[contradiction? (syntax? . -> . boolean?)]
|
[contradiction? (syntax? . -> . boolean?)]
|
||||||
[neither? (syntax? . -> . boolean?)]
|
[neither? (syntax? . -> . boolean?)]
|
||||||
[add-dead-case-lambda-branch (syntax? . -> . any)]
|
[add-dead-lambda-branch (syntax? . -> . any)]
|
||||||
[dead-case-lambda-branch? (syntax? . -> . boolean?)])
|
[dead-lambda-branch? (syntax? . -> . boolean?)])
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
#lang typed/racket
|
||||||
|
(: f (case->))
|
||||||
|
(define (f x) (add1 "foo"))
|
Loading…
Reference in New Issue
Block a user