Optimize (- (* x constant)) into (* x (- constant))
authorPaul Khuong <pvk@pvk.ca>
Wed, 22 May 2013 04:50:01 +0000 (00:50 -0400)
committerPaul Khuong <pvk@pvk.ca>
Wed, 22 May 2013 05:13:22 +0000 (01:13 -0400)
Another marginal lvar-fun-name/splice-fun-arg hack.

Closes lp#1065770.

src/compiler/srctran.lisp

index 90d1077..22d9f48 100644 (file)
   (splice-fun-args x '%negate 1)
   '(the rational x))
 
+(deftransform %negate ((x) (number))
+  "Combine %negate/*"
+  (let ((use (lvar-uses x))
+        arg)
+    (unless (and (combination-p use)
+                 (eql '* (lvar-fun-name (combination-fun use)))
+                 (constant-lvar-p (setf arg (second (combination-args use))))
+                 (numberp (setf arg (lvar-value arg))))
+      (give-up-ir1-transform))
+    (splice-fun-args x '* 2)
+    `(lambda (x y)
+       (declare (ignore y))
+       (* x ,(- arg)))))
+
 ;;; Return T if in an arithmetic op including lvars X and Y, the
 ;;; result type is not affected by the type of X. That is, Y is at
 ;;; least as contagious as X.