From 2063511bfdd04f20505ca28a7b9b18b52e5d6f25 Mon Sep 17 00:00:00 2001 From: Gustavo Massaccesi Date: Sun, 8 Jun 2014 13:44:59 -0300 Subject: [PATCH] optimizer: local known to satisfy predicate => #t in a boolean context For example, `(if (pair? x) (if x e1 e2) e3)` => `(if (pair? x) e1 e3)`. --- pkgs/racket-pkgs/racket-test/tests/racket/optimize.rktl | 9 +++++++++ racket/src/racket/src/optimize.c | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/pkgs/racket-pkgs/racket-test/tests/racket/optimize.rktl b/pkgs/racket-pkgs/racket-test/tests/racket/optimize.rktl index 35fd0e5bcf..5cbe430ba6 100644 --- a/pkgs/racket-pkgs/racket-test/tests/racket/optimize.rktl +++ b/pkgs/racket-pkgs/racket-test/tests/racket/optimize.rktl @@ -1340,6 +1340,15 @@ (test-comp '(if (if (list? (cons 1 null)) #t #t) 1 2) '1) +(test-comp '(let ([x '(7)]) + (list x x (if (if (list? (cons 1 null)) x 3) 0 1))) + '(let ([x '(7)]) + (list x x 0))) +(test-comp '(lambda (x) + (cons (car x) (if x 1 2))) + '(lambda (x) + (cons (car x) 1))) + (test-comp '(lambda (y) (let ([f (lambda (x) x)]) (if f diff --git a/racket/src/racket/src/optimize.c b/racket/src/racket/src/optimize.c index c913239d4b..3c595cc1a3 100644 --- a/racket/src/racket/src/optimize.c +++ b/racket/src/racket/src/optimize.c @@ -6298,6 +6298,12 @@ Scheme_Object *scheme_optimize_expr(Scheme_Object *expr, Optimize_Info *info, in } } else if (is_mutated) { info->vclock += 1; + } else if (context & OPT_CONTEXT_BOOLEAN) { + Scheme_Object *pred; + pred = optimize_get_predicate(pos, info); + if (pred) + return scheme_true; + /* all predicates recognize non-#f things */ } delta = optimize_info_get_shift(info, pos);