- (let ((y (continuation-value y))
- (adds 0)
- (shifts 0)
- (result nil)
- (first-one nil))
- (labels ((tub32 (x) `(truly-the (unsigned-byte 32) ,x))
- (add (next-factor)
- (setf result
- (tub32
- (if result
- (progn (incf adds) `(+ ,result ,(tub32 next-factor)))
- next-factor)))))
- (declare (inline add))
- (dotimes (bitpos 32)
- (if first-one
- (when (not (logbitp bitpos y))
- (add (if (= (1+ first-one) bitpos)
- ;; There is only a single bit in the string.
- (progn (incf shifts) `(ash x ,first-one))
- ;; There are at least two.
- (progn
- (incf adds)
- (incf shifts 2)
- `(- ,(tub32 `(ash x ,bitpos))
- ,(tub32 `(ash x ,first-one))))))
- (setf first-one nil))
- (when (logbitp bitpos y)
- (setf first-one bitpos))))
- (when first-one
- (cond ((= first-one 31))
- ((= first-one 30) (incf shifts) (add '(ash x 30)))
- (t
- (incf shifts 2)
- (incf adds)
- (add `(- ,(tub32 '(ash x 31)) ,(tub32 `(ash x ,first-one))))))
- (incf shifts)
- (add '(ash x 31))))
-
- (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.
-(deftransform ash ((num shift) (integer integer))
- (let ((num-type (continuation-type num))
- (shift-type (continuation-type shift)))
- ;; Can only handle right shifts
- (unless (csubtypep shift-type (specifier-type '(integer * 0)))
- (give-up-ir1-transform))
-
- ;; If we can prove the shift is so large that all bits are shifted
- ;; out, return the appropriate constant. If the shift is small
- ;; enough, call the VOP. Otherwise, check for the shift size and
- ;; do the appropriate thing. (Hmm, could we just leave the IF
- ;; s-expr and depend on other parts of the compiler to delete the
- ;; unreachable parts, if any?)
- (cond ((csubtypep num-type (specifier-type '(signed-byte #.sb!vm:n-word-bits)))
- ;; A right shift by 31 is the same as a right shift by
- ;; larger amount. We get just the sign.
- (if (csubtypep shift-type (specifier-type '(integer #.(- 1 sb!vm:n-word-bits) 0)))
- ;; FIXME: ash-right-{un,}signed package problems
- `(sb!vm::ash-right-signed num (- shift))
- `(sb!vm::ash-right-signed num (min (- shift) #.(1- sb!vm:n-word-bits)))))
- ((csubtypep num-type (specifier-type '(unsigned-byte #.sb!vm:n-word-bits)))
- (if (csubtypep shift-type (specifier-type '(integer #.(- 1 sb!vm:n-word-bits) 0)))
- `(sb!vm::ash-right-unsigned num (- shift))
- `(if (<= shift #.(- sb!vm:n-word-bits))
- 0
- (sb!vm::ash-right-unsigned num (- shift)))))
- (t
- (give-up-ir1-transform)))))
-
+ (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))))