1.0.24.40: fix regression from 1.0.24.37
[sbcl.git] / src / compiler / x86-64 / pred.lisp
1 ;;;; predicate VOPs for the x86 VM
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!VM")
13 \f
14 ;;;; the branch VOP
15
16 ;;; The unconditional branch, emitted when we can't drop through to the desired
17 ;;; destination. Dest is the continuation we transfer control to.
18 (define-vop (branch)
19   (:info dest)
20   (:generator 5
21     (inst jmp dest)))
22
23 \f
24 ;;;; Generic conditional VOPs
25
26 ;;; The generic conditional branch, emitted immediately after test
27 ;;; VOPs that only set flags.
28 ;;;
29 ;;; FLAGS is a list of condition descriptors. If the first descriptor
30 ;;; is CL:NOT, the test was true if all the remaining conditions are
31 ;;; false. Otherwise, the test was true if any of the conditions is.
32 ;;;
33 ;;; NOT-P flips the meaning of the test, as with regular :CONDITIONAL
34 ;;; VOP. If NOT-P is true, the code must branch to dest if the test was
35 ;;; false. Otherwise, the code must branch to dest if the test was true.
36
37 (define-vop (branch-if)
38   (:info dest flags not-p)
39   (:generator 0
40      (when (eq (car flags) 'not)
41        (pop flags)
42        (setf not-p (not not-p)))
43      (flet ((negate-condition (name)
44               (let ((code (logxor 1 (conditional-opcode name))))
45                 (aref *condition-name-vec* code))))
46        (cond ((null (rest flags))
47               (inst jmp
48                     (if not-p
49                         (negate-condition (first flags))
50                         (first flags))
51                     dest))
52              (not-p
53               (let ((not-lab (gen-label))
54                     (last    (car (last flags))))
55                 (dolist (flag (butlast flags))
56                   (inst jmp flag not-lab))
57                 (inst jmp (negate-condition last) dest)
58                 (emit-label not-lab)))
59              (t
60               (dolist (flag flags)
61                 (inst jmp flag dest)))))))
62
63 (defvar *cmov-ptype-representation-vop*
64   (mapcan (lambda (entry)
65             (destructuring-bind (ptypes &optional sc vop)
66                 entry
67               (unless (listp ptypes)
68                 (setf ptypes (list ptypes)))
69               (mapcar (if (and vop sc)
70                           (lambda (ptype)
71                             (list ptype sc vop))
72                           #'list)
73                       ptypes)))
74           '((t descriptor-reg move-if/t)
75
76             ((fixnum positive-fixnum)
77              any-reg move-if/fx)
78             ((unsigned-byte-64 unsigned-byte-63)
79              unsigned-reg move-if/unsigned)
80             (signed-byte-64 signed-reg move-if/signed)
81             (character character-reg move-if/char)
82
83             ((single-float complex-single-float
84               double-float complex-double-float))
85
86             (system-area-pointer sap-reg move-if/sap)))
87   "Alist of primitive type -> (storage-class-name VOP-name)
88    if values of such a type should be cmoved, and NIL otherwise.
89
90    storage-class-name is the name of the storage class to use for
91    the values, and VOP-name the name of the VOP that will be used
92    to execute the conditional move.")
93
94 (!def-vm-support-routine
95     convert-conditional-move-p (node dst-tn x-tn y-tn)
96   (declare (ignore node))
97   (let* ((ptype (sb!c::tn-primitive-type dst-tn))
98          (name  (sb!c::primitive-type-name ptype))
99          (param (cdr (or (assoc name *cmov-ptype-representation-vop*)
100                          '(t descriptor-reg move-if/t)))))
101     (when param
102       (destructuring-bind (representation vop) param
103         (let ((scn (sc-number-or-lose representation)))
104           (labels ((make-tn ()
105                      (make-representation-tn ptype scn))
106                    (frob-tn (tn)
107                      (if (immediate-tn-p tn)
108                          tn
109                          (make-tn))))
110             (values vop
111                     (frob-tn x-tn) (frob-tn y-tn)
112                     (make-tn)
113                     nil)))))))
114
115 (define-vop (move-if)
116   (:args (then) (else))
117   (:results (res))
118   (:info flags)
119   (:generator 0
120      (let ((not-p (eq (first flags) 'not)))
121        (when not-p (pop flags))
122        (flet ((negate-condition (name)
123                 (let ((code (logxor 1 (conditional-opcode name))))
124                   (aref *condition-name-vec* code)))
125               (load-immediate (dst constant-tn
126                                    &optional (sc (sc-name (tn-sc dst))))
127                 (let ((val (tn-value constant-tn)))
128                   (etypecase val
129                     (integer
130                        (if (memq sc '(any-reg descriptor-reg))
131                            (inst mov dst (fixnumize val))
132                            (inst mov dst val)))
133                     (symbol
134                        (aver (eq sc 'descriptor-reg))
135                        (load-symbol dst val))
136                     (character
137                        (if (eq sc 'descriptor-reg)
138                            (inst mov dst (logior (ash (char-code val) n-widetag-bits)
139                                                  character-widetag))
140                            (inst mov dst (char-code val))))))))
141          (cond ((null (rest flags))
142                 (if (sc-is else immediate)
143                     (load-immediate res else)
144                     (move res else))
145                 (when (sc-is then immediate)
146                   (load-immediate temp-reg-tn then (sc-name (tn-sc res)))
147                   (setf then temp-reg-tn))
148                 (inst cmov (if not-p
149                                (negate-condition (first flags))
150                                (first flags))
151                       res
152                       then))
153                (not-p
154                 (cond ((sc-is then immediate)
155                        (when (location= else res)
156                          (inst mov temp-reg-tn else)
157                          (setf else temp-reg-tn))
158                        (load-immediate res then))
159                       ((location= else res)
160                        (inst xchg else then)
161                        (rotatef else then))
162                       (t
163                        (move res then)))
164                 (when (sc-is else immediate)
165                   (load-immediate temp-reg-tn else (sc-name (tn-sc res)))
166                   (setf else temp-reg-tn))
167                 (dolist (flag flags)
168                   (inst cmov flag res else)))
169                (t
170                 (if (sc-is else immediate)
171                     (load-immediate res else)
172                     (move res else))
173                 (when (sc-is then immediate)
174                   (load-immediate temp-reg-tn then (sc-name (tn-sc res)))
175                   (setf then temp-reg-tn))
176                 (dolist (flag flags)
177                   (inst cmov flag res then))))))))
178
179 (macrolet ((def-move-if (name type reg &optional stack)
180                (when stack (setf stack (list stack)))
181
182                `(define-vop (,name move-if)
183                   (:args (then :scs (immediate ,reg ,@stack) :to :eval
184                                :load-if (not (or (sc-is then immediate)
185                                                  (and (sc-is then ,@stack)
186                                                       (not (location= else res))))))
187                          (else :scs (immediate ,reg ,@stack) :target res
188                                :load-if (not (sc-is else immediate ,@stack))))
189                   (:arg-types ,type ,type)
190                   (:results (res :scs (,reg)
191                                  :from (:argument 1)))
192                   (:result-types ,type))))
193   (def-move-if move-if/t
194       t descriptor-reg control-stack)
195   (def-move-if move-if/fx
196       tagged-num any-reg control-stack)
197   (def-move-if move-if/unsigned
198       unsigned-num unsigned-reg unsigned-stack)
199   (def-move-if move-if/signed
200       signed-num signed-reg signed-stack)
201   (def-move-if move-if/char
202       character character-reg character-stack)
203   (def-move-if move-if/sap
204       system-area-pointer sap-reg sap-stack))
205
206 \f
207 ;;;; conditional VOPs
208
209 ;;; Note: a constant-tn is allowed in CMP; it uses an EA displacement,
210 ;;; not immediate data.
211 (define-vop (if-eq)
212   (:args (x :scs (any-reg descriptor-reg control-stack constant)
213             :load-if (not (and (sc-is x immediate)
214                                (sc-is y any-reg descriptor-reg
215                                       control-stack constant))))
216          (y :scs (any-reg descriptor-reg immediate)
217             :load-if (not (and (sc-is x any-reg descriptor-reg immediate)
218                                (sc-is y control-stack constant)))))
219   (:temporary (:sc descriptor-reg) temp)
220   (:conditional :e)
221   (:policy :fast-safe)
222   (:translate eq)
223   (:generator 3
224     (cond
225      ((sc-is y immediate)
226       (let ((val (tn-value y)))
227         (etypecase val
228           (integer
229            (if (and (zerop val) (sc-is x any-reg descriptor-reg))
230                (inst test x x) ; smaller
231              (let ((fixnumized (fixnumize val)))
232                (if (typep fixnumized
233                           '(or (signed-byte 32) (unsigned-byte 31)))
234                    (inst cmp x fixnumized)
235                  (progn
236                    (inst mov temp fixnumized)
237                    (inst cmp x temp))))))
238           (symbol
239            (inst cmp x (+ nil-value (static-symbol-offset val))))
240           (character
241            (inst cmp x (logior (ash (char-code val) n-widetag-bits)
242                                character-widetag))))))
243      ((sc-is x immediate) ; and y not immediate
244       ;; Swap the order to fit the compare instruction.
245       (let ((val (tn-value x)))
246         (etypecase val
247           (integer
248            (if (and (zerop val) (sc-is y any-reg descriptor-reg))
249                (inst test y y) ; smaller
250              (let ((fixnumized (fixnumize val)))
251                (if (typep fixnumized
252                           '(or (signed-byte 32) (unsigned-byte 31)))
253                    (inst cmp y fixnumized)
254                  (progn
255                    (inst mov temp fixnumized)
256                    (inst cmp y temp))))))
257           (symbol
258            (inst cmp y (+ nil-value (static-symbol-offset val))))
259           (character
260            (inst cmp y (logior (ash (char-code val) n-widetag-bits)
261                                character-widetag))))))
262       (t
263        (inst cmp x y)))))