1 ;;;; predicate VOPs for the x86 VM
3 ;;;; This software is part of the SBCL system. See the README file for
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.
16 ;;; The unconditional branch, emitted when we can't drop through to the desired
17 ;;; destination. Dest is the continuation we transfer control to.
24 ;;;; Generic conditional VOPs
26 ;;; The generic conditional branch, emitted immediately after test
27 ;;; VOPs that only set flags.
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.
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.
37 (define-vop (branch-if)
38 (:info dest flags not-p)
40 (when (eq (car flags) 'not)
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))
49 (negate-condition (first flags))
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)))
61 (inst jmp flag dest)))))))
63 (defvar *cmov-ptype-representation-vop*
64 (mapcan (lambda (entry)
65 (destructuring-bind (ptypes &optional sc vop)
67 (unless (listp ptypes)
68 (setf ptypes (list ptypes)))
69 (mapcar (if (and vop sc)
74 '((t descriptor-reg move-if/t)
76 ((fixnum positive-fixnum)
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)
83 ((single-float complex-single-float
84 double-float complex-double-float))
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.
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.")
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)))))
102 (destructuring-bind (representation vop) param
103 (let ((scn (sc-number-or-lose representation)))
105 (make-representation-tn ptype scn))
107 (if (immediate-tn-p tn)
111 (frob-tn x-tn) (frob-tn y-tn)
115 (define-vop (move-if)
116 (:args (then) (else))
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)))
130 (if (memq sc '(any-reg descriptor-reg))
131 (inst mov dst (fixnumize val))
134 (aver (eq sc 'descriptor-reg))
135 (load-symbol dst val))
137 (if (eq sc 'descriptor-reg)
138 (inst mov dst (logior (ash (char-code val) n-widetag-bits)
140 (inst mov dst (char-code val))))))))
141 (cond ((null (rest flags))
142 (if (sc-is else immediate)
143 (load-immediate 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))
149 (negate-condition (first flags))
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)
164 (when (sc-is else immediate)
165 (load-immediate temp-reg-tn else (sc-name (tn-sc res)))
166 (setf else temp-reg-tn))
168 (inst cmov flag res else)))
170 (if (sc-is else immediate)
171 (load-immediate 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))
177 (inst cmov flag res then))))))))
179 (macrolet ((def-move-if (name type reg &optional stack)
180 (when stack (setf stack (list stack)))
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))
207 ;;;; conditional VOPs
209 ;;; Note: a constant-tn is allowed in CMP; it uses an EA displacement,
210 ;;; not immediate data.
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)
226 (let ((val (tn-value y)))
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)
236 (inst mov temp fixnumized)
237 (inst cmp x temp))))))
239 (inst cmp x (+ nil-value (static-symbol-offset val))))
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)))
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)
255 (inst mov temp fixnumized)
256 (inst cmp y temp))))))
258 (inst cmp y (+ nil-value (static-symbol-offset val))))
260 (inst cmp y (logior (ash (char-code val) n-widetag-bits)
261 character-widetag))))))