From a8e01d870755d5ea823d93b51a8228456b368f43 Mon Sep 17 00:00:00 2001 From: Eric Dobson Date: Sat, 22 Feb 2014 13:08:09 -0800 Subject: [PATCH] Add more optimizations for projections. original commit: 262fad814afc33dbac8e927409dca225faf00686 --- .../typed-racket/optimizer/float.rkt | 1 - .../typed-racket/optimizer/number.rkt | 32 +++++++++++++- .../optimizer/missed-optimizations/fixnum.rkt | 2 +- .../optimizer/tests/fixnum-bounded-expr.rkt | 2 +- .../typed-racket/optimizer/tests/pr13468.rkt | 37 ++++++++++++---- .../optimizer/tests/projections.rkt | 42 +++++++++++++++++++ 6 files changed, 105 insertions(+), 11 deletions(-) create mode 100644 pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/optimizer/tests/projections.rkt diff --git a/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/optimizer/float.rkt b/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/optimizer/float.rkt index 577e72e6..4c59f212 100644 --- a/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/optimizer/float.rkt +++ b/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/optimizer/float.rkt @@ -34,7 +34,6 @@ (define unary-float-ops (dict-set* unary-float-math-ops - #'real-part #'#%expression #'flreal-part #'#%expression #'unsafe-flreal-part #'#%experession)) diff --git a/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/optimizer/number.rkt b/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/optimizer/number.rkt index 94d2ffeb..42952439 100644 --- a/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/optimizer/number.rkt +++ b/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/optimizer/number.rkt @@ -1,18 +1,48 @@ #lang racket/base (require syntax/parse + syntax/parse/experimental/specialize (for-template racket/base) "../utils/utils.rkt" (utils tc-utils) + (types numeric-tower union) (optimizer utils logging)) (provide number-opt-expr) (define-literal-syntax-class unary-op (+ * min max)) +(define-literal-syntax-class real-part) +(define-literal-syntax-class imag-part) +(define-literal-syntax-class abs) +(define-literal-syntax-class magnitude) +(define-literal-syntax-class angle) + +(define-syntax-class/specialize real-expr + (subtyped-expr -Real)) +;; We don't want -0.0 here +(define-syntax-class/specialize non-neg-real-expr + (subtyped-expr (Un -PosReal -Zero -InexactRealPosZero))) + (define-syntax-class number-opt-expr #:commit ;; these cases are all identity (pattern (#%plain-app op:unary-op f:opt-expr) #:do [(log-opt "unary number" "Identity elimination.")] - #:with opt #'f.opt)) + #:with opt #'f.opt) + + (pattern (#%plain-app op:real-part^ f:real-expr) + #:do [(log-opt "unary number" "Projection elimination.")] + #:with opt #'f.opt) + + (pattern (#%plain-app op:imag-part^ f:real-expr) + #:do [(log-opt "unary number" "Projection elimination.")] + #:with opt #'0) + + (pattern (#%plain-app (~or op:abs^ op:magnitude^) f:non-neg-real-expr) + #:do [(log-opt "unary number" "Projection elimination.")] + #:with opt #'f.opt) + + (pattern (#%plain-app op:angle^ f:non-neg-real-expr) + #:do [(log-opt "unary number" "Projection elimination.")] + #:with opt #'0)) diff --git a/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/optimizer/missed-optimizations/fixnum.rkt b/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/optimizer/missed-optimizations/fixnum.rkt index 7324f153..82a5a0a4 100644 --- a/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/optimizer/missed-optimizations/fixnum.rkt +++ b/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/optimizer/missed-optimizations/fixnum.rkt @@ -20,7 +20,7 @@ TR opt: fixnum.rkt 15:17 (+ 301 302) -- fixnum bounded expr TR opt: fixnum.rkt 15:5 (+ 300 301) -- fixnum bounded expr TR opt: fixnum.rkt 28:0 (add1 min-fixnum) -- fixnum add1 TR opt: fixnum.rkt 30:0 (- max-fixnum) -- unary fixnum -TR opt: fixnum.rkt 31:0 (abs max-fixnum) -- fixnum fxabs +TR opt: fixnum.rkt 31:0 (abs max-fixnum) -- unary number TR opt: fixnum.rkt 32:0 (sub1 max-fixnum) -- fixnum sub1 TR opt: fixnum.rkt 35:0 (= (- max-fixnum max-fixnum) 0) -- binary fixnum comp TR opt: fixnum.rkt 35:3 (- max-fixnum max-fixnum) -- fixnum bounded expr diff --git a/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/optimizer/tests/fixnum-bounded-expr.rkt b/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/optimizer/tests/fixnum-bounded-expr.rkt index 224584cf..83cd2fc8 100644 --- a/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/optimizer/tests/fixnum-bounded-expr.rkt +++ b/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/optimizer/tests/fixnum-bounded-expr.rkt @@ -9,7 +9,7 @@ TR opt: fixnum-bounded-expr.rkt 12:2 (- x (* y y)) -- fixnum bounded expr TR opt: fixnum-bounded-expr.rkt 12:7 (* y y) -- fixnum bounded expr TR opt: fixnum-bounded-expr.rkt 19:2 (+ x y) -- fixnum bounded expr TR opt: fixnum-bounded-expr.rkt 22:2 (+ x y) -- fixnum bounded expr -TR opt: fixnum-bounded-expr.rkt 27:0 (abs 45) -- fixnum fxabs +TR opt: fixnum-bounded-expr.rkt 27:0 (abs 45) -- unary number TR opt: fixnum-bounded-expr.rkt 30:0 (fx+ 5 2) -- fixnum fx+ TR opt: fixnum-bounded-expr.rkt 31:0 (fx+ (+ 34 231) (* 24 25)) -- fixnum fx+ TR opt: fixnum-bounded-expr.rkt 31:16 (* 24 25) -- fixnum bounded expr diff --git a/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/optimizer/tests/pr13468.rkt b/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/optimizer/tests/pr13468.rkt index 91f70d94..56873496 100644 --- a/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/optimizer/tests/pr13468.rkt +++ b/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/optimizer/tests/pr13468.rkt @@ -2,24 +2,47 @@ #<