Skip to content

Commit

Permalink
Add constant folding of ifs.
Browse files Browse the repository at this point in the history
  • Loading branch information
stamourv committed Oct 29, 2011
1 parent fc4aee2 commit 204e428
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions compiler/front-end.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@
(substitute-child! p node res)))]
[else
(fail!)]))]
[(if* _ cs) ; if result of test is known, keep only the used branch
(for-each constant-fold! cs) ; fold each branch
(match node
[(if* p `(,(cst _ '() val) ,thn ,els))
(substitute-child! p node (if val thn els))]
[_
(void)])] ; nothing to do, we folded children already
[_
(for-each constant-fold! (node-children node))]))

Expand Down
3 changes: 3 additions & 0 deletions tests/succeed/constant-folding.expected
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
14
1
1
2
4 changes: 4 additions & 0 deletions tests/succeed/constant-folding.scm
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
;; Also, the current test harness cannot detect if constant folding does
;; not happen. This test will only fail if there's an error somewhere.
(displayln (#%+ 2 (#%mul-non-neg 3 4)))

(displayln (if #t 1 2)) ; all these should be a single constant, no if
(displayln (if 3 1 2))
(displayln (if #f 1 2))

0 comments on commit 204e428

Please sign in to comment.