working on block optimization

This commit is contained in:
Danny Yoo 2011-07-30 18:28:32 -04:00
parent 27b5821898
commit a8af8dc9b6
4 changed files with 56 additions and 27 deletions

View File

@ -1,9 +1,13 @@
#lang typed/racket/base #lang typed/racket/base
;; Does some basic optimizations at the level of basic blocks.
(require "assemble-structs.rkt" (require "assemble-structs.rkt"
"../compiler/il-structs.rkt" "../compiler/il-structs.rkt"
racket/list) racket/list)
(require/typed "../logger.rkt" [log-debug (String -> Void)]) (require/typed "../logger.rkt" [log-debug (String -> Void)])
@ -11,18 +15,29 @@
(define-type Blockht (HashTable Symbol BasicBlock)) (define-type Blockht (HashTable Symbol BasicBlock))
(define-type Bodyht (HashTable (Listof UnlabeledStatement) Symbol))
(: optimize-basic-blocks ((Listof BasicBlock) -> (Listof BasicBlock))) (: optimize-basic-blocks ((Listof BasicBlock) -> (Listof BasicBlock)))
(define (optimize-basic-blocks blocks) (define (optimize-basic-blocks blocks)
(let: ([blockht : Blockht (make-hasheq)])
(for-each (lambda: ([b : BasicBlock])
(hash-set! blockht (BasicBlock-name b) b))
blocks)
(: blockht : Blockht)
(define blockht (make-hasheq))
(: bodyht : Blockht)
(define bodyht (make-hasheq))
(for ([b blocks])
(hash-set! blockht (BasicBlock-name b) b))
(define inlined-blocks
(map (lambda: ([b : BasicBlock]) (map (lambda: ([b : BasicBlock])
(optimize-block b blockht)) (optimize-block b blockht))
blocks))) blocks))
inlined-blocks)
@ -31,13 +46,13 @@
;; Simple optimization: optimize away single-statement goto blocks with their ;; Simple optimization: optimize away single-statement goto blocks with their
;; immediate contents. ;; immediate contents.
(define (optimize-block b blocks) (define (optimize-block b blocks)
(let ([stmts (BasicBlock-stmts b)]) (define stmts (BasicBlock-stmts b))
(cond (cond
[(= (length stmts) 1) [(= (length stmts) 1)
(let ([first-stmt (first stmts)]) (define first-stmt (first stmts))
(cond (cond
[(GotoStatement? first-stmt) [(GotoStatement? first-stmt)
(let ([target (GotoStatement-target first-stmt)]) (define target (GotoStatement-target first-stmt))
(cond (cond
[(Label? target) [(Label? target)
(log-debug (format "inlining basic block ~a" (BasicBlock-name b))) (log-debug (format "inlining basic block ~a" (BasicBlock-name b)))
@ -46,8 +61,8 @@
(hash-ref blocks (Label-name target)))) (hash-ref blocks (Label-name target))))
blocks)] blocks)]
[else [else
b]))] b])]
[else [else
b]))] b])]
[else [else
b]))) b]))

View File

@ -0,0 +1,3 @@
1
2
68

View File

@ -0,0 +1,10 @@
#lang planet dyoo/whalesong
(define (f x)
(* x x))
(define (g x)
(+ x x))
(f 1)
(g 1)
(+ (f 2) (f (g (g 2))))

View File

@ -7,7 +7,7 @@
;; type replaced with .expected. ;; type replaced with .expected.
(test "more-tests/hello.rkt") (test "more-tests/hello.rkt")
(test "more-tests/conform.rkt") (test "more-tests/simple-functions.rkt")
(test "more-tests/sk-generator.rkt") (test "more-tests/sk-generator.rkt")
(test "more-tests/sk-generator-2.rkt") (test "more-tests/sk-generator-2.rkt")
(test "more-tests/simple-structs.rkt") (test "more-tests/simple-structs.rkt")
@ -15,6 +15,7 @@
(test "more-tests/colors.rkt") (test "more-tests/colors.rkt")
(test "more-tests/images.rkt") (test "more-tests/images.rkt")
(test "more-tests/lists.rkt") (test "more-tests/lists.rkt")
(test "more-tests/earley.rkt")
(test "more-tests/hello-bf.rkt")
(test "more-tests/simple-apply.rkt") (test "more-tests/simple-apply.rkt")
(test "more-tests/hello-bf.rkt")
(test "more-tests/conform.rkt")
(test "more-tests/earley.rkt")