optimizer: reduce constants to #t/#f in boolean contexts

While something like `(if 7 e1 e2)` was already reduced to `e1`,
this improvement makes `(if (let ([x (random)]) 7) e1 e2)`
reduce to `(if (let ([x (random)]) #t) e1 e2)`.
This commit is contained in:
Gustavo Massaccesi 2014-05-31 19:02:48 -03:00 committed by Matthew Flatt
parent e658e48b01
commit 7cb37d1bc8
2 changed files with 18 additions and 1 deletions

View File

@ -1328,6 +1328,18 @@
(a1)
(a2))))
(test-comp '(if (let ([z (random)]) null) 1 2)
'(if (let ([z (random)]) #t) 1 2))
(test-comp '(if (if (list? (cons 1 null)) null (void)) 1 2)
'1)
(test-comp '(if (if (list? (cons 1 null)) 7 8) 1 2)
'1)
(test-comp '(if (if (list? (cons 1 null)) #t #t) 1 2)
'1)
(test-comp '(lambda (y)
(let ([f (lambda (x) x)])
(if f

View File

@ -6409,7 +6409,12 @@ Scheme_Object *scheme_optimize_expr(Scheme_Object *expr, Optimize_Info *info, in
return module_optimize(expr, info, context);
default:
info->size += 1;
return expr;
if ((context & OPT_CONTEXT_BOOLEAN)
&& (SCHEME_TYPE(expr) > _scheme_compiled_values_types_)
&& SCHEME_TRUEP(expr))
return scheme_true;
else
return expr;
}
}