Implement EQ of unboxed characters and small integers on x86oids
[sbcl.git] / src / compiler / x86 / 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      (flet ((negate-condition (name)
41               (let ((code (logxor 1 (conditional-opcode name))))
42                 (aref *condition-name-vec* code))))
43        (aver (null (rest flags)))
44        (inst jmp
45              (if not-p
46                  (negate-condition (first flags))
47                  (first flags))
48              dest))))
49
50 (defvar *cmov-ptype-representation-vop*
51   (mapcan (lambda (entry)
52             (destructuring-bind (ptypes &optional sc vop)
53                 entry
54               (unless (listp ptypes)
55                 (setf ptypes (list ptypes)))
56               (mapcar (if (and vop sc)
57                           (lambda (ptype)
58                             (list ptype sc vop))
59                           #'list)
60                       ptypes)))
61           '((t descriptor-reg move-if/t)
62
63             ((fixnum positive-fixnum)
64              any-reg move-if/fx)
65             ((unsigned-byte-32 unsigned-byte-31)
66              unsigned-reg move-if/unsigned)
67             (signed-byte-32 signed-reg move-if/signed)
68             ;; FIXME: Can't use CMOV with byte registers, and characters live
69             ;; in such outside of unicode builds. A better solution then just
70             ;; disabling MOVE-IF/CHAR should be possible, though.
71             #!+sb-unicode
72             (character character-reg move-if/char)
73
74             ((single-float complex-single-float
75               double-float complex-double-float))
76
77             (system-area-pointer sap-reg move-if/sap)))
78   "Alist of primitive type -> (storage-class-name VOP-name)
79    if values of such a type should be cmoved, and NIL otherwise.
80
81    storage-class-name is the name of the storage class to use for
82    the values, and VOP-name the name of the VOP that will be used
83    to execute the conditional move.")
84
85 (!def-vm-support-routine
86     convert-conditional-move-p (node dst-tn x-tn y-tn)
87   (declare (ignore node))
88   (let* ((ptype (sb!c::tn-primitive-type dst-tn))
89          (name  (sb!c::primitive-type-name ptype))
90          (param (and (memq :cmov *backend-subfeatures*)
91                      (cdr (or (assoc name *cmov-ptype-representation-vop*)
92                               '(t descriptor-reg move-if/t))))))
93     (when param
94       (destructuring-bind (representation vop) param
95         (let ((scn (sc-number-or-lose representation)))
96           (labels ((make-tn ()
97                      (make-representation-tn ptype scn))
98                    (frob-tn (tn)
99                      (if (immediate-tn-p tn)
100                          tn
101                          (make-tn))))
102             (values vop
103                     (frob-tn x-tn) (frob-tn y-tn)
104                     (make-tn)
105                     nil)))))))
106
107 (define-vop (move-if)
108   (:args (then) (else))
109   (:temporary (:sc unsigned-reg :from :eval) temp)
110   (:results (res))
111   (:info flags)
112   (:generator 0
113      (flet ((load-immediate (dst constant-tn
114                                  &optional (sc (sc-name (tn-sc dst))))
115               (let ((val (tn-value constant-tn)))
116                 (etypecase val
117                   (integer
118                      (if (memq sc '(any-reg descriptor-reg))
119                          (inst mov dst (fixnumize val))
120                          (inst mov dst val)))
121                   (symbol
122                      (aver (eq sc 'descriptor-reg))
123                      (load-symbol dst val))
124                   (character
125                      (cond ((memq sc '(any-reg descriptor-reg))
126                             (inst mov dst
127                                   (logior (ash (char-code val) n-widetag-bits)
128                                           character-widetag)))
129                            (t
130                             (aver (eq sc 'character-reg))
131                             (inst mov dst (char-code val)))))))))
132        (aver (null (rest flags)))
133        (if (sc-is else immediate)
134            (load-immediate res else)
135            (move res else))
136        (when (sc-is then immediate)
137          (load-immediate temp then (sc-name (tn-sc res)))
138          (setf then temp))
139        (inst cmov (first flags) res then))))
140
141 (macrolet ((def-move-if (name type reg stack)
142                `(define-vop (,name move-if)
143                   (:args (then :scs (immediate ,reg ,stack) :to :eval
144                                :target temp
145                                :load-if (not (or (sc-is then immediate)
146                                                  (and (sc-is then ,stack)
147                                                       (not (location= else res))))))
148                          (else :scs (immediate ,reg ,stack) :target res
149                                :load-if (not (sc-is else immediate ,stack))))
150                   (:arg-types ,type ,type)
151                   (:results (res :scs (,reg)
152                                  :from (:argument 1)))
153                   (:result-types ,type))))
154   (def-move-if move-if/t t descriptor-reg control-stack)
155   (def-move-if move-if/fx tagged-num any-reg control-stack)
156   (def-move-if move-if/unsigned unsigned-num unsigned-reg unsigned-stack)
157   (def-move-if move-if/signed signed-num signed-reg signed-stack)
158   ;; FIXME: See *CMOV-PTYPE-REPRESENTATION-VOP* above.
159   #!+sb-unicode
160   (def-move-if move-if/char character character-reg character-stack)
161   (def-move-if move-if/sap system-area-pointer sap-reg sap-stack))
162
163 \f
164 ;;;; conditional VOPs
165
166 ;;; Note: a constant-tn is allowed in CMP; it uses an EA displacement,
167 ;;; not immediate data.
168 (define-vop (if-eq)
169   (:args (x :scs (any-reg descriptor-reg control-stack constant)
170             :load-if (not (and (sc-is x immediate)
171                                (sc-is y any-reg descriptor-reg
172                                       control-stack constant))))
173          (y :scs (any-reg descriptor-reg immediate)
174             :load-if (not (and (sc-is x any-reg descriptor-reg immediate)
175                                (sc-is y control-stack constant)))))
176   (:conditional :e)
177   (:info)
178   (:policy :fast-safe)
179   (:translate eq)
180   (:generator 8
181     (let ((x-val (encode-value-if-immediate x))
182           (y-val (encode-value-if-immediate y)))
183       (cond
184         ;; Shorter instruction sequences for these two cases.
185         ((and (eql 0 y-val) (sc-is x any-reg descriptor-reg)) (inst test x x))
186         ((and (eql 0 x-val) (sc-is y any-reg descriptor-reg)) (inst test y y))
187
188         ;; An encoded value (literal integer) has to be the second argument.
189         ((sc-is x immediate) (inst cmp y x-val))
190
191         (t (inst cmp x y-val))))))
192
193 (macrolet ((def (eq-name eql-name)
194              `(define-vop (,eq-name ,eql-name)
195                 (:translate eq))))
196   (def fast-if-eq-character fast-char=/character)
197   (def fast-if-eq-character/c fast-char=/character/c)
198   (def fast-if-eq/signed fast-if-eql/signed)
199   (def fast-if-eq-c/signed fast-if-eql-c/signed)
200   (def fast-if-eq/unsigned fast-if-eql/unsigned)
201   (def fast-if-eq-c/unsigned fast-if-eql-c/unsigned))