From e19c4db19df8a59552f1b84bd24999347caf036d Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Mon, 7 Nov 2016 18:20:19 -0500 Subject: [PATCH] Issue #130 - Inline constant values --- CHANGELOG.md | 1 + scheme/cyclone/cps-optimizations.sld | 29 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42aa0dfa..3040906f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Features: - Thanks to ecraven, added R7RS function `exact-integer-sqrt` to `(scheme base)`. - Allow the reader to recognize `+inf.0`, `-inf.0`, `+nan.0`, and `-nan.0`. - Allow `cond-expand` to test for whether a library exists using the form `(library {library name})`. +- Reduce size of compiled code by inlining constant values. This reduced the code size of various cyclone libraries by approximately 33%. Bug Fixes diff --git a/scheme/cyclone/cps-optimizations.sld b/scheme/cyclone/cps-optimizations.sld index 86c3014a..dfe73c3f 100644 --- a/scheme/cyclone/cps-optimizations.sld +++ b/scheme/cyclone/cps-optimizations.sld @@ -470,6 +470,33 @@ ;; TODO: check for more than one arg?? (equal? (length (cdr exp)) (length (ast:lambda-formals->list (car exp)))) + (or + ;; This "and" is not for primitives, but rather checking + ;; for constants to optimize out. This just happens to be + ;; a convenient place since the optimization is the same. + (and + ;; Check each parameter + (every + (lambda (param) + (with-var param (lambda (var) + (and + ;; At least for now, do not replace if referenced by multiple functions + (<= (length (adbv:ref-by var)) 1) + ;; Need to keep variable because it is mutated + (not (adbv:reassigned? var)) + )))) + (ast:lambda-formals->list (car exp))) + ;; Args are all constants + (every + (lambda (arg) + (and + arg ;; #f is a special value for init, so do not optimize it for now + (or (const? arg) + (quote? arg)))) + (cdr exp)) + ) + ;; Check for primitive calls that can be optimized out + (and ;; Double-check parameter can be optimized-out (every (lambda (param) @@ -498,7 +525,7 @@ (inline-prim-call? (ast:lambda-body (car exp)) (prim-calls->arg-variables (cdr exp)) - (ast:lambda-formals->list (car exp))) + (ast:lambda-formals->list (car exp))))) ) (let ((args (cdr exp))) (for-each