0.8.3.94:
[sbcl.git] / src / compiler / sparc / arith.lisp
index cadcdab..1887090 100644 (file)
@@ -1,4 +1,4 @@
-;;;; the VM definition arithmetic VOPs for the Alpha
+;;;; the VM definition arithmetic VOPs for the SPARC
 
 ;;;; This software is part of the SBCL system. See the README file for
 ;;;; more information.
 
 (eval-when (:compile-toplevel :load-toplevel :execute)
 
-(defmacro define-binop (translate untagged-penalty op)
+(defmacro define-binop (translate untagged-penalty op
+                       &optional arg-swap restore-fixnum-mask)
   `(progn
-     (define-vop (,(symbolicate "FAST-" translate "/FIXNUM=>FIXNUM")
+     (define-vop (,(symbolicate 'fast translate '/fixnum=>fixnum)
                  fast-fixnum-binop)
+       ,@(when restore-fixnum-mask
+          `((:temporary (:sc non-descriptor-reg) temp)))
        (:translate ,translate)
        (:generator 2
-        (inst ,op r x y)))
-     (define-vop (,(symbolicate 'fast- translate '-c/fixnum=>fixnum)
-                 fast-fixnum-binop-c)
-       (:translate ,translate)
-       (:generator 1
-        (inst ,op r x (fixnumize y))))
-     (define-vop (,(symbolicate "FAST-" translate "/SIGNED=>SIGNED")
+        ,(if arg-swap
+             `(inst ,op ,(if restore-fixnum-mask 'temp 'r) y x)
+             `(inst ,op ,(if restore-fixnum-mask 'temp 'r) x y))
+        ,@(when restore-fixnum-mask
+            `((inst andn r temp fixnum-tag-mask)))))
+     ,@(unless arg-swap
+        `((define-vop (,(symbolicate 'fast- translate '-c/fixnum=>fixnum)
+                       fast-fixnum-binop-c)
+            ,@(when restore-fixnum-mask
+                `((:temporary (:sc non-descriptor-reg) temp)))
+            (:translate ,translate)
+            (:generator 1
+              (inst ,op ,(if restore-fixnum-mask 'temp 'r) x (fixnumize y))
+              ,@(when restore-fixnum-mask
+                  `((inst andn r temp fixnum-tag-mask)))))))
+     (define-vop (,(symbolicate 'fast- translate '/signed=>signed)
                  fast-signed-binop)
        (:translate ,translate)
        (:generator ,(1+ untagged-penalty)
-        (inst ,op r x y)))
-     (define-vop (,(symbolicate 'fast- translate '-c/signed=>signed)
-                 fast-signed-binop-c)
-       (:translate ,translate)
-       (:generator ,untagged-penalty
-        (inst ,op r x y)))
-     (define-vop (,(symbolicate "FAST-" translate "/UNSIGNED=>UNSIGNED")
+        ,(if arg-swap
+             `(inst ,op r y x)
+             `(inst ,op r x y))))
+     ,@(unless arg-swap
+        `((define-vop (,(symbolicate 'fast- translate '-c/signed=>signed)
+                       fast-signed-binop-c)
+            (:translate ,translate)
+            (:generator ,untagged-penalty
+              (inst ,op r x y)))))
+     (define-vop (,(symbolicate 'fast- translate '/unsigned=>unsigned)
                  fast-unsigned-binop)
        (:translate ,translate)
        (:generator ,(1+ untagged-penalty)
-        (inst ,op r x y)))
-     (define-vop (,(symbolicate 'fast- translate '-c/unsigned=>unsigned)
-                 fast-unsigned-binop-c)
-       (:translate ,translate)
-       (:generator ,untagged-penalty
-        (inst ,op r x y)))))
+        ,(if arg-swap
+             `(inst ,op r y x)
+             `(inst ,op r x y))))
+     ,@(unless arg-swap
+        `((define-vop (,(symbolicate 'fast- translate '-c/unsigned=>unsigned)
+                       fast-unsigned-binop-c)
+            (:translate ,translate)
+            (:generator ,untagged-penalty
+              (inst ,op r x y)))))))
 
 ); eval-when
 
 (define-binop + 4 add)
 (define-binop - 4 sub)
 (define-binop logand 2 and)
+(define-binop logandc1 2 andn t)
 (define-binop logandc2 2 andn)
 (define-binop logior 2 or)
-(define-binop logorc2 2 orn)
+(define-binop logorc1 2 orn t t)
+(define-binop logorc2 2 orn nil t)
 (define-binop logxor 2 xor)
-(define-binop logeqv 2 xnor)
+(define-binop logeqv 2 xnor nil t)
 
-;;; Special logand cases: (logand signed unsigned) => unsigned
-
-(define-vop (fast-logand/signed-unsigned=>unsigned
-            fast-logand/unsigned=>unsigned)
-    (:args (x :target r :scs (signed-reg))
-          (y :scs (unsigned-reg unsigned-stack)))
-  (:arg-types signed-num unsigned-num))
-
-(define-vop (fast-logand/unsigned-signed=>unsigned
-            fast-logand/unsigned=>unsigned)
-    (:args (x :target r :scs (unsigned-reg))
-          (y :scs (signed-reg signed-stack)))
-  (:arg-types unsigned-num signed-num))
-    
 ;;; Special case fixnum + and - that trap on overflow.  Useful when we
 ;;; don't know that the output type is a fixnum.
 
-;;; I (toy@rtp.ericsson.se) took these out.  They don't seem to be
-;;; used anywhere at all.
+;;; I (Raymond Toy) took these out. They don't seem to be used
+;;; anywhere at all.
 #+nil
 (progn
 (define-vop (+/fixnum fast-+/fixnum=>fixnum)
 
 ;;; Shifting
 
-(macrolet
-    ((frob (name sc-type type shift-right-inst)
-       `(define-vop (,name)
-         (:note "inline ASH")
-         (:args (number :scs (,sc-type) :to :save)
-                (amount :scs (signed-reg immediate)))
-         (:arg-types ,type signed-num)
-         (:results (result :scs (,sc-type)))
-         (:result-types ,type)
-         (:translate ash)
-         (:policy :fast-safe)
-         (:temporary (:sc non-descriptor-reg) ndesc)
-         (:generator 5
-           (sc-case amount
-            (signed-reg
-             (cond
-               ;; FIXME: These two don't look different enough.
-               ((member :sparc-v9 *backend-subfeatures*)
-                (let ((done (gen-label))
-                      (positive (gen-label)))
-                  (inst cmp amount)
-                  (inst b :ge positive)
-                  (inst neg ndesc amount)
-                  ;; ndesc = max(-amount, 31)
-                  (inst cmp ndesc 31)
-                  (inst cmove :ge ndesc 31)
-                  (inst b done)
-                  (inst ,shift-right-inst result number ndesc)
-                  (emit-label positive)
-                  ;; The result-type assures us that this shift will
-                  ;; not overflow.
-                  (inst sll result number amount)
-                  ;; We want a right shift of the appropriate size.
-                  (emit-label done)))
-               (t
-                (let ((positive (gen-label))
-                      (done (gen-label)))
-                  (inst cmp amount)
-                  (inst b :ge positive)
-                  (inst neg ndesc amount)
-                  (inst cmp ndesc 31)
-                  (inst b :le done)
-                  (inst ,shift-right-inst result number ndesc)
-                  (inst b done)
-                  (inst ,shift-right-inst result number 31)
-                  (emit-label positive)
-                  ;; The result-type assures us that this shift will
-                  ;; not overflow.
-                  (inst sll result number amount)
-                  (emit-label done)))))
-            (immediate
-             (let ((amount (tn-value amount)))
-               (if (minusp amount)
-                   (let ((amount (min 31 (- amount))))
-                     (inst ,shift-right-inst result number amount))
-                   (inst sll result number amount)))))))))
-  (frob fast-ash/signed=>signed signed-reg signed-num sra)
-  (frob fast-ash/unsigned=>unsigned unsigned-reg unsigned-num srl))
+(define-vop (fast-ash/signed=>signed)
+  (:note "inline ASH")
+  (:args (number :scs (signed-reg) :to :save)
+        (amount :scs (signed-reg immediate) :to :save))
+  (:arg-types signed-num signed-num)
+  (:results (result :scs (signed-reg)))
+  (:result-types signed-num)
+  (:translate ash)
+  (:policy :fast-safe)
+  (:temporary (:sc non-descriptor-reg) ndesc)
+  (:generator 5
+    (sc-case amount
+      (signed-reg
+       (let ((done (gen-label)))
+        (inst cmp amount)
+        (inst b :ge done)
+        ;; The result-type assures us that this shift will not
+        ;; overflow.
+        (inst sll result number amount)
+        (inst neg ndesc amount)
+        (inst cmp ndesc 31)
+        (if (member :sparc-v9 *backend-subfeatures*)
+            (progn
+              (inst cmove :ge ndesc 31)
+              (inst sra result number ndesc))
+            (progn
+              (inst b :le done)
+              (inst sra result number ndesc)
+              (inst sra result number 31)))
+        (emit-label done)))
+      (immediate
+       (bug "IMMEDIATE case in ASH VOP; should have been transformed")))))
+
+(define-vop (fast-ash/unsigned=>unsigned)
+  (:note "inline ASH")
+  (:args (number :scs (unsigned-reg) :to :save)
+        (amount :scs (signed-reg immediate) :to :save))
+  (:arg-types unsigned-num signed-num)
+  (:results (result :scs (unsigned-reg)))
+  (:result-types unsigned-num)
+  (:translate ash)
+  (:policy :fast-safe)
+  (:temporary (:sc non-descriptor-reg) ndesc)
+  (:generator 5
+    (sc-case amount
+      (signed-reg
+       (let ((done (gen-label)))
+        (inst cmp amount)
+        (inst b :ge done)
+        ;; The result-type assures us that this shift will not
+        ;; overflow.
+        (inst sll result number amount)
+        (inst neg ndesc amount)
+        (inst cmp ndesc 32)
+        (if (member :sparc-v9 *backend-subfeatures*)
+            (progn
+              (inst srl result number ndesc)
+              (inst cmove :ge result zero-tn))
+            (progn
+              (inst b :lt done)
+              (inst srl result number ndesc)
+              (move result zero-tn)))
+        (emit-label done)))
+      (immediate
+       (bug "IMMEDIATE case in ASH VOP; should have been transformed")))))
 
 ;; Some special cases where we know we want a left shift.  Just do the
 ;; shift, instead of checking for the sign of the shift.
         (:policy :fast-safe)
         (:generator ,cost
          ;; The result-type assures us that this shift will not
-         ;; overflow. And for fixnum's, the zero bits that get
+         ;; overflow. And for fixnums, the zero bits that get
          ;; shifted in are just fine for the fixnum tag.
          (sc-case amount
           ((signed-reg unsigned-reg)
     (inst sra temp y n-fixnum-tag-bits)
     (inst smul r x temp)))
 
+(define-vop (fast-v8-*-c/fixnum=>fixnum fast-safe-arith-op)
+  (:args (x :target r :scs (any-reg zero)))
+  (:info y)
+  (:arg-types tagged-num
+             (:constant (and (signed-byte 13) (not (integer 0 0)))))
+  (:results (r :scs (any-reg)))
+  (:result-types tagged-num)
+  (:note "inline fixnum arithmetic")
+  (:translate *)
+  (:guard (or (member :sparc-v8 *backend-subfeatures*)
+             (and (member :sparc-v9 *backend-subfeatures*)
+                  (not (member :sparc-64 *backend-subfeatures*)))))
+  (:generator 1
+    (inst smul r x y)))
+
 (define-vop (fast-v8-*/signed=>signed fast-signed-binop)
   (:translate *)
   (:guard (or (member :sparc-v8 *backend-subfeatures*)
   (:generator 3
     (inst smul r x y)))
 
+(define-vop (fast-v8-*-c/signed=>signed fast-signed-binop-c)
+  (:translate *)
+  (:guard (or (member :sparc-v8 *backend-subfeatures*)
+             (and (member :sparc-v9 *backend-subfeatures*)
+                  (not (member :sparc-64 *backend-subfeatures*)))))
+  (:generator 2
+    (inst smul r x y)))
+         
 (define-vop (fast-v8-*/unsigned=>unsigned fast-unsigned-binop)
   (:translate *)
   (:guard (or (member :sparc-v8 *backend-subfeatures*)
   (:generator 3
     (inst umul r x y)))
 
+(define-vop (fast-v8-*-c/unsigned=>unsigned fast-unsigned-binop-c)
+  (:translate *)
+  (:guard (or (member :sparc-v8 *backend-subfeatures*)
+             (and (member :sparc-v9 *backend-subfeatures*)
+                  (not (member :sparc-64 *backend-subfeatures*)))))
+  (:generator 2
+    (inst umul r x y)))
+
 ;; The smul and umul instructions are deprecated on the Sparc V9.  Use
 ;; mulx instead.
 (define-vop (fast-v9-*/fixnum=>fixnum fast-fixnum-binop)
     (inst mulx r x y)))
 
 \f
+;;;; Modular functions:
+(define-modular-fun lognot-mod32 (x) lognot 32)
+(define-vop (lognot-mod32/unsigned=>unsigned)
+  (:translate lognot-mod32)
+  (:args (x :scs (unsigned-reg)))
+  (:arg-types unsigned-num)
+  (:results (res :scs (unsigned-reg)))
+  (:result-types unsigned-num)
+  (:policy :fast-safe)
+  (:generator 1
+    (inst not res x)))
+
+(macrolet
+    ((define-modular-backend (fun &optional constantp)
+       (let ((mfun-name (symbolicate fun '-mod32))
+             (modvop (symbolicate 'fast- fun '-mod32/unsigned=>unsigned))
+             (modcvop (symbolicate 'fast- fun 'mod32-c/unsigned=>unsigned))
+             (vop (symbolicate 'fast- fun '/unsigned=>unsigned))
+             (cvop (symbolicate 'fast- fun '-c/unsigned=>unsigned)))
+         `(progn
+            (define-modular-fun ,mfun-name (x y) ,fun 32)
+            (define-vop (,modvop ,vop)
+              (:translate ,mfun-name))
+           ,@(when constantp
+               `((define-vop (,modcvop ,cvop)
+                   (:translate ,mfun-name))))))))
+  (define-modular-backend + t)
+  (define-modular-backend logxor t)
+  (define-modular-backend logeqv t)
+  (define-modular-backend logandc1)
+  (define-modular-backend logandc2 t)
+  (define-modular-backend logorc1)
+  (define-modular-backend logorc2 t))
+
+(define-source-transform lognand (x y)
+  `(lognot (logand ,x ,y)))
+(define-source-transform lognor (x y)
+  `(lognot (logior ,x ,y)))
+\f
 ;;;; Binary conditional VOPs:
 
 (define-vop (fast-conditional)
   (:affected)
   (:policy :fast-safe))
 
-(deftype integer-with-a-bite-out (s bite)
-  (cond ((eq s '*) 'integer)
-       ((and (integerp s) (> s 1))
-        (let ((bound (ash 1 (1- s))))
-          `(integer ,(- bound) ,(- bound bite 1))))
-       (t
-        (error "Bad size specified for SIGNED-BYTE type specifier: ~S." s))))
-
 (define-vop (fast-conditional/fixnum fast-conditional)
   (:args (x :scs (any-reg zero))
         (y :scs (any-reg zero)))
       (emit-label done)
       (move result res))))
 
+(define-source-transform 32bit-logical-not (x)
+  `(logand (lognot (the (unsigned-byte 32) ,x)) #.(1- (ash 1 32))))
 
-(define-vop (32bit-logical)
-  (:args (x :scs (unsigned-reg zero))
-        (y :scs (unsigned-reg zero)))
-  (:arg-types unsigned-num unsigned-num)
-  (:results (r :scs (unsigned-reg)))
-  (:result-types unsigned-num)
-  (:policy :fast-safe))
-
-(define-vop (32bit-logical-not 32bit-logical)
-  (:translate 32bit-logical-not)
-  (:args (x :scs (unsigned-reg zero)))
-  (:arg-types unsigned-num)
-  (:generator 1
-    (inst not r x)))
-
-(define-vop (32bit-logical-and 32bit-logical)
-  (:translate 32bit-logical-and)
-  (:generator 1
-    (inst and r x y)))
-
-(deftransform 32bit-logical-nand ((x y) (* *))
-  '(32bit-logical-not (32bit-logical-and x y)))
+(deftransform 32bit-logical-and ((x y))
+  '(logand x y))
 
-(define-vop (32bit-logical-or 32bit-logical)
-  (:translate 32bit-logical-or)
-  (:generator 1
-    (inst or r x y)))
+(deftransform 32bit-logical-nand ((x y))
+  '(logand (lognand x y) #.(1- (ash 1 32))))
 
-(deftransform 32bit-logical-nor ((x y) (* *))
-  '(32bit-logical-not (32bit-logical-or x y)))
+(deftransform 32bit-logical-or ((x y))
+  '(logior x y))
 
-(define-vop (32bit-logical-xor 32bit-logical)
-  (:translate 32bit-logical-xor)
-  (:generator 1
-    (inst xor r x y)))
+(deftransform 32bit-logical-nor ((x y))
+  '(logand (lognor x y) #.(1- (ash 1 32))))
 
-(define-vop (32bit-logical-eqv 32bit-logical)
-  (:translate 32bit-logical-eqv)
-  (:generator 1
-    (inst xnor r x y)))
+(deftransform 32bit-logical-xor ((x y))
+  '(logxor x y))
 
-(define-vop (32bit-logical-orc2 32bit-logical)
-  (:translate 32bit-logical-orc2)
-  (:generator 1
-    (inst orn r x y)))
+(deftransform 32bit-logical-eqv ((x y))
+  '(logand (logeqv x y) #.(1- (ash 1 32))))
 
-(deftransform 32bit-logical-orc1 ((x y) (* *))
-  '(32bit-logical-orc2 y x))
+(deftransform 32bit-logical-orc1 ((x y))
+  '(logand (logorc1 x y) #.(1- (ash 1 32))))
 
-(define-vop (32bit-logical-andc2 32bit-logical)
-  (:translate 32bit-logical-andc2)
-  (:generator 1
-    (inst andn r x y)))
+(deftransform 32bit-logical-orc2 ((x y))
+  '(logand (logorc2 x y) #.(1- (ash 1 32))))
 
-(deftransform 32bit-logical-andc1 ((x y) (* *))
-  '(32bit-logical-andc2 y x))
+(deftransform 32bit-logical-andc1 ((x y))
+  '(logand (logandc1 x y) #.(1- (ash 1 32))))
 
+(deftransform 32bit-logical-andc2 ((x y))
+  '(logand (logandc2 x y) #.(1- (ash 1 32))))
 
 (define-vop (shift-towards-someplace)
   (:policy :fast-safe)
   (:note "shift-towards-end")
   (:generator 1
     (inst srl r num amount)))
-
-
-
 \f
 ;;;; Bignum stuff.
 
   (:generator 40
     (emit-multiply x y hi lo)))
 
-(define-vop (bignum-lognot)
-  (:translate sb!bignum::%lognot)
-  (:policy :fast-safe)
-  (:args (x :scs (unsigned-reg)))
-  (:arg-types unsigned-num)
-  (:results (r :scs (unsigned-reg)))
-  (:result-types unsigned-num)
-  (:generator 1
-    (inst not r x)))
+(define-vop (bignum-lognot lognot-mod32/unsigned=>unsigned)
+  (:translate sb!bignum::%lognot))
 
 (define-vop (fixnum-to-digit)
   (:translate sb!bignum::%fixnum-to-digit)
 (define-static-fun two-arg-and (x y) :translate logand)
 (define-static-fun two-arg-ior (x y) :translate logior)
 (define-static-fun two-arg-xor (x y) :translate logxor)
+(define-static-fun two-arg-eqv (x y) :translate logeqv)
 
 \f
 ;; Need these so constant folding works with the deftransform.
 
-(defun ash-right-signed (num shift)
-  (declare (type (signed-byte #.sb!vm:n-word-bits) num)
-          (type (integer 0 #.(1- sb!vm:n-word-bits)) shift))
-  (ash-right-signed num shift))
+;; FIXME KLUDGE ew yuk.
+#-sb-xc-host
+(progn
+  (defun ash-right-signed (num shift)
+    (ash-right-signed num shift))
+
+  (defun ash-right-unsigned (num shuft)
+    (ash-right-unsigned num shift)))
+
+(in-package "SB!C")
 
-(defun ash-right-unsigned (num shift)
-  (declare (type (unsigned-byte #.sb!vm:n-word-bits) num)
-          (type (integer 0 #.(1- sb!vm:n-word-bits)) shift))
-  (ash-right-unsigned num shift))
+(deftransform * ((x y)
+                ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
+                (unsigned-byte 32))
+  "recode as shifts and adds"
+  (let ((y (lvar-value y)))
+    (multiple-value-bind (result adds shifts)
+       (ub32-strength-reduce-constant-multiply 'x y)
+      (cond
+        ;; we assume, perhaps foolishly, that good SPARCs don't have an
+        ;; issue with multiplications.  (Remember that there's a
+        ;; different transform for converting x*2^k to a shift).
+        ((member :sparc-64 *backend-subfeatures*) (give-up-ir1-transform))
+        ((or (member :sparc-v9 *backend-subfeatures*)
+            (member :sparc-v8 *backend-subfeatures*))
+        ;; breakeven point as measured by Raymond Toy
+        (when (> (+ adds shifts) 9)
+          (give-up-ir1-transform))))
+      (or result 0))))
 
 ;; If we can prove that we have a right shift, just do the right shift
 ;; instead of calling the inline ASH which has to check for the
 ;; direction of the shift at run-time.
-(in-package "SB!C")
-
 (deftransform ash ((num shift) (integer integer))
-  (let ((num-type (continuation-type num))
-       (shift-type (continuation-type shift)))
+  (let ((num-type (lvar-type num))
+       (shift-type (lvar-type shift)))
     ;; Can only handle right shifts
     (unless (csubtypep shift-type (specifier-type '(integer * 0)))
       (give-up-ir1-transform))