Skip to content

Commit

Permalink
aarch64: verify bitselect, bnot rules (avanhatt#100)
Browse files Browse the repository at this point in the history
Quick implementation of `bitselect`, `bnot` basically worked
out-of-the-box!

Updates avanhatt#34
  • Loading branch information
avanhatt authored Oct 1, 2024
1 parent b83145b commit 44cc02c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
14 changes: 14 additions & 0 deletions cranelift/codegen/src/inst_specs.isle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
(default true)
)

;;;; Common Term Forms ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(form
bv_ternary_8_to_64
((args (bv 8) (bv 8) (bv 8)) (ret (bv 8)))
((args (bv 16) (bv 16) (bv 16)) (ret (bv 16)))
((args (bv 32) (bv 32) (bv 32)) (ret (bv 32)))
((args (bv 64) (bv 64) (bv 64)) (ret (bv 64)))
)

;;;; CLIF Instruction Specifications ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(spec (iadd x y)
Expand Down Expand Up @@ -97,3 +107,7 @@
((args (bv 32)) (ret (bv 32)))
((args (bv 64)) (ret (bv 64)))
)

(spec (bitselect c x y)
(provide (= result (bvor (bvand c x) (bvand (bvnot c) y)))))
(instantiate bitselect bv_ternary_8_to_64)
1 change: 1 addition & 0 deletions cranelift/codegen/src/isa/aarch64/inst.isle
Original file line number Diff line number Diff line change
Expand Up @@ -2162,6 +2162,7 @@
;; Instruction creation helpers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Helper for creating the zero register.
(spec (zero_reg) (provide (= (zero_ext (widthof result) #b0) result)))
(decl zero_reg () Reg)
(extern constructor zero_reg zero_reg)

Expand Down
6 changes: 3 additions & 3 deletions cranelift/codegen/src/isa/aarch64/lower.isle
Original file line number Diff line number Diff line change
Expand Up @@ -1252,12 +1252,12 @@
;; Note that bitwise negation is implemented here as
;;
;; NOT rd, rm ==> ORR_NOT rd, zero, rm
(rule -1 (lower (has_type (fits_in_64 ty) (bnot x)))
(rule bnot_base_case -1 (lower (has_type (fits_in_64 ty) (bnot x)))
(orr_not ty (zero_reg) x))

;; Special case to use `orr_not_shift` if it's a `bnot` of a const-left-shifted
;; value.
(rule 1 (lower (has_type (fits_in_64 ty)
(rule bnot_ishl 1 (lower (has_type (fits_in_64 ty)
(bnot (ishl x (iconst k)))))
(if-let amt (lshl_from_imm64 ty k))
(orr_not_shift ty (zero_reg) x amt))
Expand Down Expand Up @@ -1885,7 +1885,7 @@

;;;; Rules for `bitselect` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(rule (lower (has_type ty (bitselect c x y)))
(rule bitselect (lower (has_type ty (bitselect c x y)))
(if (ty_int_ref_scalar_64 ty))
(let ((tmp1 Reg (and_reg ty x c))
(tmp2 Reg (bic ty y c)))
Expand Down
8 changes: 8 additions & 0 deletions cranelift/codegen/src/prelude.isle
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,14 @@

;; A pure constructor/extractor that only matches scalar integers, and
;; references that can fit in 64 bits.
(spec (ty_int_ref_scalar_64 ty)
(provide (= result ty))
(require
(or
(= ty 8)
(= ty 16)
(= ty 32)
(= ty 64))))
(decl pure partial ty_int_ref_scalar_64 (Type) Type)
(extern constructor ty_int_ref_scalar_64 ty_int_ref_scalar_64)
(extern extractor ty_int_ref_scalar_64 ty_int_ref_scalar_64_extract)
Expand Down

0 comments on commit 44cc02c

Please sign in to comment.