From ca115092ff418c495d65a9a8d187b9653a533659 Mon Sep 17 00:00:00 2001 From: Vincent St-Amour Date: Wed, 1 Feb 2012 15:51:02 -0500 Subject: [PATCH] Only optimize fixnum comparisons in the binary case. Closes PR 12479. original commit: e3be06117116bb622a410f44ee5e06f8c3f4f713 --- .../optimizer/tests/fixnum-comparison.rkt | 2 +- .../typed-racket/optimizer/tests/ternary-equality.rkt | 11 +++++++++++ .../tests/typed-racket/optimizer/tests/vector-sum.rkt | 4 ++-- collects/typed-racket/optimizer/fixnum.rkt | 11 ++++++++++- 4 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 collects/tests/typed-racket/optimizer/tests/ternary-equality.rkt diff --git a/collects/tests/typed-racket/optimizer/tests/fixnum-comparison.rkt b/collects/tests/typed-racket/optimizer/tests/fixnum-comparison.rkt index 9a154c53..4d73af88 100644 --- a/collects/tests/typed-racket/optimizer/tests/fixnum-comparison.rkt +++ b/collects/tests/typed-racket/optimizer/tests/fixnum-comparison.rkt @@ -1,6 +1,6 @@ #; ( -TR opt: fixnum-comparison.rkt 12:0 (< (vector-length (quote #(1 2 3))) (string-length "asdf")) -- binary fixnum +TR opt: fixnum-comparison.rkt 12:0 (< (vector-length (quote #(1 2 3))) (string-length "asdf")) -- binary fixnum comp TR opt: fixnum-comparison.rkt 12:3 (vector-length (quote #(1 2 3))) -- vector-length TR opt: fixnum-comparison.rkt 12:29 (string-length "asdf") -- string-length #t diff --git a/collects/tests/typed-racket/optimizer/tests/ternary-equality.rkt b/collects/tests/typed-racket/optimizer/tests/ternary-equality.rkt new file mode 100644 index 00000000..8dc188f8 --- /dev/null +++ b/collects/tests/typed-racket/optimizer/tests/ternary-equality.rkt @@ -0,0 +1,11 @@ +#; +( + #t +) + +#lang typed/racket/base +#:optimize + +;; PR 12479 +;; was incorrectly optimized in the same way as fixnum bitwise-and and co +(= 1 1 1) diff --git a/collects/tests/typed-racket/optimizer/tests/vector-sum.rkt b/collects/tests/typed-racket/optimizer/tests/vector-sum.rkt index 0738cd61..adb807c4 100644 --- a/collects/tests/typed-racket/optimizer/tests/vector-sum.rkt +++ b/collects/tests/typed-racket/optimizer/tests/vector-sum.rkt @@ -1,9 +1,9 @@ #; ( TR opt: vector-sum.rkt 41:2 (for/fold: ((sum : Float 0.0)) ((i : Nonnegative-Fixnum (in-range l))) (+ sum (vector-ref v i))) -- fixnum bounded expr -TR opt: vector-sum.rkt 41:2 (for/fold: ((sum : Float 0.0)) ((i : Nonnegative-Fixnum (in-range l))) (+ sum (vector-ref v i))) -- binary fixnum +TR opt: vector-sum.rkt 41:2 (for/fold: ((sum : Float 0.0)) ((i : Nonnegative-Fixnum (in-range l))) (+ sum (vector-ref v i))) -- binary fixnum comp TR opt: vector-sum.rkt 39:2 (for: ((i : Nonnegative-Fixnum (in-range l))) (vector-set! v i (sin (exact->inexact i)))) -- fixnum bounded expr -TR opt: vector-sum.rkt 39:2 (for: ((i : Nonnegative-Fixnum (in-range l))) (vector-set! v i (sin (exact->inexact i)))) -- binary fixnum +TR opt: vector-sum.rkt 39:2 (for: ((i : Nonnegative-Fixnum (in-range l))) (vector-set! v i (sin (exact->inexact i)))) -- binary fixnum comp TR opt: vector-sum.rkt 29:0 #%module-begin -- dead else branch TR opt: vector-sum.rkt 29:0 #%module-begin -- dead else branch TR opt: vector-sum.rkt 29:0 #%module-begin -- dead else branch diff --git a/collects/typed-racket/optimizer/fixnum.rkt b/collects/typed-racket/optimizer/fixnum.rkt index f541386e..a02b98c5 100644 --- a/collects/typed-racket/optimizer/fixnum.rkt +++ b/collects/typed-racket/optimizer/fixnum.rkt @@ -23,18 +23,22 @@ (dict-set (dict-set (dict-set - (mk-fixnum-tbl (list #'= #'<= #'< #'> #'>= #'min #'max)) + (mk-fixnum-tbl (list #'min #'max)) #'bitwise-and #'unsafe-fxand) #'fxand #'unsafe-fxand) #'bitwise-ior #'unsafe-fxior) #'fxior #'unsafe-fxior) #'bitwise-xor #'unsafe-fxxor) #'fxxor #'unsafe-fxxor)) + +(define binary-fixnum-comps (mk-fixnum-tbl (list #'= #'<= #'< #'> #'>=))) + (define-syntax-class fixnum-unary-op #:commit (pattern (~or (~literal bitwise-not) (~literal fxnot)) #:with unsafe (begin (add-disappeared-use this-syntax) #'unsafe-fxnot))) + ;; closed on fixnums, but 2nd argument must not be 0 (define-syntax-class nonzero-fixnum-binary-op #:commit @@ -106,6 +110,11 @@ #:with opt (begin (log-optimization "binary fixnum" fixnum-opt-msg this-syntax) (n-ary->binary #'op.unsafe #'n1.opt #'n2.opt #'(ns.opt ...)))) + (pattern (#%plain-app (~var op (fixnum-op binary-fixnum-comps)) + n1:fixnum-expr n2:fixnum-expr) + #:with opt + (begin (log-optimization "binary fixnum comp" fixnum-opt-msg this-syntax) + #'(op.unsafe n1.opt n2.opt))) (pattern (#%plain-app op:nonzero-fixnum-binary-op n1:fixnum-expr n2:nonzero-fixnum-expr)