what each x86-64 form costs under the selected cost model.
(defparameter *x86-form-classes*) source(defparameter *x86-form-classes*
'(;; scalar integer
(:alu add sub and or xor cmp test inc dec neg not mov movzx movsx movsxd
cbw cwde cdqe cwd cdq cqo bswap bt btc btr bts clc stc cmc xchg nop)
(:lea lea)
(:shift shl shr sar sal rol ror)
(:shiftcl rcl rcr shld shrd)
(:shiftx shlx shrx sarx rorx)
(:imul imul)
(:mulx mulx mul)
(:div32 div idiv)
(:bitcnt popcnt lzcnt tzcnt bsf bsr)
(:bmi andn blsr blsi blsmsk bzhi bextr)
(:pdep pdep pext)
(:adc adc sbb adcx adox)
(:load movbe)
(:store movnti movdiri)
(:crc32 crc32)
(:lock xadd cmpxchg)
(:cvti2f cvtsi2ss cvtsi2sd)
(:cvtf2i cvttss2si cvttsd2si cvtss2si cvtsd2si)
;; vector integer
(:valu paddb paddw paddd paddq psubb psubw psubd psubq pand pandn por pxor
andps andpd andnps andnpd orps orpd xorps xorpd
movdqa movdqu movaps movups movapd movupd movd movq)
(:valu01 pcmpeqb pcmpeqw pcmpeqd pcmpeqq pcmpgtb pcmpgtw pcmpgtd
pminsb pminsw pminsd pminub pminuw pminud pmaxsb pmaxsw pmaxsd pmaxub pmaxuw pmaxud
pabsb pabsw pabsd pavgb pavgw paddsb paddsw paddusb paddusw
psubsb psubsw psubusb psubusw psignb psignw psignd)
(:vshiftx psllw pslld psllq psrlw psrld psrlq psraw psrad)
(:vshift vpsllvd vpsllvq vpsrlvd vpsrlvq vpsravd)
(:vshuf pshufb pshufd pshuflw pshufhw palignr pslldq psrldq
punpcklbw punpcklwd punpckldq punpcklqdq punpckhbw punpckhwd punpckhdq punpckhqdq
packsswb packssdw packuswb packusdw shufps shufpd unpcklps unpckhps unpcklpd unpckhpd
pblendw insertps movhlps movlhps movss movsd movddup movshdup movsldup
movhps movlps movhpd movlpd vpermilps vpermilpd)
(:vperm vpermd vpermq vpermps vpermpd vperm2i128 vperm2f128 vinserti128 vinsertf128
vextracti128 vextractf128 vpbroadcastb vpbroadcastw vpbroadcastd vpbroadcastq
vbroadcastss vbroadcastsd vbroadcasti128 vbroadcastf128)
(:vblend vpblendd blendps blendpd)
(:vblendv pblendvb blendvps blendvpd)
(:vmul pmullw pmulhw pmulhuw pmulhrsw pmuludq pmuldq pmaddwd pmaddubsw)
(:vmulld pmulld)
(:vsad psadbw)
(:vpcmpq pcmpgtq)
(:vmovmsk pmovmskb movmskps movmskpd)
(:vtest ptest vtestps vtestpd comiss comisd ucomiss ucomisd)
(:vextr pextrb pextrw pextrd pextrq extractps)
(:vinsr pinsrb pinsrw pinsrd pinsrq)
(:vpmovx pmovsxbw pmovsxbd pmovsxbq pmovsxwd pmovsxwq pmovsxdq
pmovzxbw pmovzxbd pmovzxbq pmovzxwd pmovzxwq pmovzxdq)
(:vhadd phaddw phaddd phaddsw phsubw phsubd phsubsw haddps haddpd hsubps hsubpd)
;; vector float (the FMA families are prefixes, *X86-FORM-CLASS-PREFIXES*)
(:vfadd addps addpd addss addsd subps subpd subss subsd addsubps addsubpd)
(:vfmul mulps mulpd mulss mulsd)
(:vfdivs divps divss)
(:vfdivd divpd divsd)
(:vfsqrts sqrtps sqrtss)
(:vfsqrtd sqrtpd sqrtsd)
(:vrcp rcpps rcpss rsqrtps rsqrtss)
...16 more lines: the source.
(defparameter *x86-cc*) source(defparameter *x86-cc*
'("o" "no" "b" "nb" "c" "nc" "ae" "nae" "e" "z" "ne" "nz" "be" "nbe" "a" "na"
"s" "ns" "p" "np" "pe" "po" "l" "nl" "ge" "nge" "le" "nle" "g" "ng")
"the condition codes of Jcc, SETcc and CMOVcc")(defparameter *x86-form-class-prefixes*) source(defparameter *x86-form-class-prefixes*
'((:cmov "cmov" :cc) (:setcc "set" :cc) (:br "j" :cc)
(:vfma "vfmadd") (:vfma "vfmsub") (:vfma "vfnmadd") (:vfma "vfnmsub"))
"(class prefix [:cc]): a mnemonic that is the prefix and (with :cc) a
condition code, or anything (vfmadd132ps, vfmaddsub213pd)")(defparameter *x86-unpriced-class*) source(defparameter *x86-unpriced-class* :libm
"the class a form with none is priced as")(defparameter *x86-cpuid-features*) source(defparameter *x86-cpuid-features*
'((:sse2 . "sse2") (:sse3 . "sse3") (:ssse3 . "ssse3") (:sse4-1 . "sse4.1") (:sse4-2 . "sse4.2")
(:popcnt . "popcnt") (:lzcnt . "lzcnt") (:bmi1 . "bmi") (:bmi2 . "bmi2") (:avx . "avx")
(:avx2 . "avx2") (:fma . "fma") (:f16c . "f16c") (:pclmulqdq . "pclmul") (:aes . "aes")
(:adx . "adx") (:movbe . "movbe") (:gfni . "gfni") (:vaes . "vaes"))
"a form's CPUID features (:cpuid) that ucas tracks, by ucas's name")(defvar *x86-mnemonic-classes*) source(defvar *x86-mnemonic-classes* (make-hash-table :test 'equal))(defun x86-mnemonic) source(defun x86-mnemonic (form)
"a form's mnemonic, lower case: ADD.R/M64.R64 -> \"add\", LEAVE~2 -> \"leave\""
(let ((s (symbol-name (x86f-name form))))
(string-downcase (subseq s 0 (or (position-if (lambda (c) (member c '(#\. #\~))) s)
(length s))))))(defun x86-prefix-class) source(defun x86-prefix-class (m)
(dolist (p *x86-form-class-prefixes*)
(let ((prefix (second p)))
(when (and (> (length m) (length prefix)) (string= prefix (subseq m 0 (length prefix)))
(or (not (third p)) (member (subseq m (length prefix)) *x86-cc* :test #'string=)))
(return (first p))))))(defun x86-mnemonic-class) source(defun x86-mnemonic-class (m)
"the class of mnemonic M (a lower-case string), or nil"
(or (gethash m *x86-mnemonic-classes*)
(and (> (length m) 1) (char= (char m 0) #\v) (gethash (subseq m 1) *x86-mnemonic-classes*))
(x86-prefix-class m)))(defun x86-vector-class-p) source(defun x86-vector-class-p (class)
"a class from ucas's vector integer ones on (:valu ...)"
(let ((names (mapcar #'car *x86-cost-classes*)))
(>= (or (position class names) -1) (position :valu names))))(defun x86-refine-class) source(defun x86-refine-class (class form)
"CLASS made exact for FORM's operands (the table in the file's header)"
(let* ((ops (x86f-operands form))
(dest (first ops)))
(flet ((has-class (c) (find c ops :key #'third))
(cl-p (o) (and (eq (second o) :fixed) (eq (third o) 'gpr) (eql (getf (nthcdr 5 o) :fixed) 1))))
(case class
(:shift (if (some #'cl-p ops) :shiftcl :shift))
(:imul (if (= (count-if #'x86-explicit-p ops) 1) :mulx :imul))
(:div32 (if (= (x86f-opsize form) 64) :div64 :div32))
(:vshiftx (if (has-class 'imm) :vshift :vshiftx))
(:valu (cond ((not (has-class 'gpr)) :valu)
((eq (third dest) 'gpr) :movx2g)
(t :movg2x)))
(:vmaskld (if (eq (third dest) 'mem) :vmaskst :vmaskld))
(:load (if (eq (third dest) 'mem) :store :load))
(t class)))))(defun x86-form-class) source(defun x86-form-class (form)
"-> FORM's cost class (nil: none) and width (:x or :y); FORM is a form or its name"
(let* ((f (if (x86-form-p form) form (find-x86-form form)))
(class (x86-mnemonic-class (x86-mnemonic f)))
(width (if (= (x86f-vlen f) 256) :y :x)))
(when (and class (x86-vector-class-p class) (not (find 'vec (x86f-operands f) :key #'third)))
(setq class nil))
(values (and class (x86-refine-class class f)) width)))(defun x86-form-runs-p) source(defun x86-form-runs-p (form model)
"does MODEL's core have the CPUID features FORM needs (of those ucas tracks)?"
(let ((features (getf (costm-props (find-cost-model model)) :features)))
(every (lambda (k)
(let ((name (cdr (assoc k *x86-cpuid-features*))))
(or (null name) (member name features :test #'string=))))
(x86f-cpuid form))))(defun x86-form-cost) source(defun x86-form-cost (form &optional (model *cost-cpu*))
"-> the cost-entry of FORM (a form or its name) under MODEL, its class (nil:
none, the entry is *X86-UNPRICED-CLASS*'s) and width. The entry is nil
when MODEL's machine cannot run the form: it lacks the class or a feature."
(let ((f (if (x86-form-p form) form (find-x86-form form))))
(multiple-value-bind (class width) (x86-form-class f)
(values (cond ((not (x86-form-runs-p f model)) nil)
(class (cost-class-entry model class width))
(t (cost-class-entry model *x86-unpriced-class*)))
class width))))(defun x86-form-cost-units) source(defun x86-form-cost-units (form &optional (model *cost-cpu*))
"FORM's price as the matcher sums it (COST-UNITS); nil when MODEL can't run it"
(cost-units (x86-form-cost form model)))(defun x86-mnemonic-source) source(defun x86-mnemonic-source (m class)
"-> :ucas if ucas's description of CLASS names mnemonic M (a stem equal to it,
with or without its v, one of 4 letters or more it begins with, or a stem
ending in cc that it is with a condition code: cmovcc, setcc, jcc), else :added"
(let ((stems (fourth (assoc class *x86-cost-classes*)))
(bare (if (and (> (length m) 1) (char= (char m 0) #\v)) (subseq m 1) m)))
(flet ((names (s x)
(let ((n (length s)))
(or (string= s x)
(and (>= n 4) (> (length x) n) (string= s (subseq x 0 n)))
(and (> n 2) (string= "cc" (subseq s (- n 2)))
(> (length x) (- n 2)) (string= (subseq s 0 (- n 2)) (subseq x 0 (- n 2)))
(member (subseq x (- n 2)) *x86-cc* :test #'string=))))))
(if (some (lambda (s) (or (names s m) (names s bare))) stems) :ucas :added))))(defun x86-cost-coverage) source(defun x86-cost-coverage (&optional (model *cost-cpu*))
"-> forms with a class, forms; those with semantics with a class, with
semantics; forms with a class that MODEL's machine cannot run"
(let ((priced 0) (total 0) (sem-priced 0) (sem 0) (lacking 0))
(dolist (f (x86-form-list))
(let ((class (x86-form-class f)))
(incf total)
(unless (x86f-unread f) (incf sem))
(when class
(incf priced)
(unless (x86f-unread f) (incf sem-priced))
(unless (x86-form-cost f model) (incf lacking)))))
(values priced total sem-priced sem lacking)))Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.