Skip to content

Commit

Permalink
x64: add additional select lowerings
Browse files Browse the repository at this point in the history
  • Loading branch information
abrown committed Jan 11, 2022
1 parent f08c995 commit 3c8ca00
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 108 deletions.
3 changes: 3 additions & 0 deletions cranelift/codegen/src/isa/x64/inst.isle
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,9 @@ src1 src2)
(ConsumesFlags.ConsumesFlags (MInst.XmmCmove size cc consequent alternative dst)
(writable_reg_to_reg dst))))

;; TODO Need another version of cmove to handle multiple-register values (i.e.,
;; I128)

;; Helper for creating `MInst.MovzxRmR` instructions.
(decl movzx (Type ExtMode RegMem) Reg)
(rule (movzx ty mode src)
Expand Down
49 changes: 49 additions & 0 deletions cranelift/codegen/src/isa/x64/lower.isle
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,55 @@
(value_reg (pminud (put_in_reg x) (put_in_reg_mem y))))

;;;; Rules for `select` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; CLIF `select` instructions receive a testable argument (i.e. boolean or
;; integer) that determines which of the other two arguments is selected as
;; output. Since Cranelift booleans are typically generated by a comparison, the
;; lowerings in this section "look upwards in the tree" to emit the proper
;; sequence of "selection" instructions.

(rule (lower (has_type ty (select (def_inst (fcmp (FloatCC.Ordered) a b)) x y)))
(value_reg (with_flags_1 (fpcmp ty (put_in_reg_mem a) (put_in_reg b))
(cmove ty (CC.NP) (put_in_reg_mem x) (put_in_reg y)))))

(rule (lower (has_type ty (select (def_inst (fcmp (FloatCC.Unordered) a b)) x y)))
(value_reg (with_flags_1 (fpcmp ty (put_in_reg_mem a) (put_in_reg b))
(cmove ty (CC.P) (put_in_reg_mem x) (put_in_reg y)))))

(rule (lower (has_type ty (select (def_inst (fcmp (FloatCC.GreaterThan) a b)) x y)))
(value_reg (with_flags_1 (fpcmp ty (put_in_reg_mem a) (put_in_reg b))
(cmove ty (CC.NBE) (put_in_reg_mem x) (put_in_reg y)))))

(rule (lower (has_type ty (select (def_inst (fcmp (FloatCC.GreaterThanOrEqual) a b)) x y)))
(value_reg (with_flags_1 (fpcmp ty (put_in_reg_mem a) (put_in_reg b))
(cmove ty (CC.NB) (put_in_reg_mem x) (put_in_reg y)))))

(rule (lower (has_type ty (select (def_inst (fcmp (FloatCC.UnorderedOrLessThan) a b)) x y)))
(value_reg (with_flags_1 (fpcmp ty (put_in_reg_mem a) (put_in_reg b))
(cmove ty (CC.B) (put_in_reg_mem x) (put_in_reg y)))))

(rule (lower (has_type ty (select (def_inst (fcmp (FloatCC.UnorderedOrLessThanOrEqual) a b)) x y)))
(value_reg (with_flags_1 (fpcmp ty (put_in_reg_mem a) (put_in_reg b))
(cmove ty (CC.BE) (put_in_reg_mem x) (put_in_reg y)))))

;; Certain FloatCC variants are implemented by flipping the operands of the
;; comparison (e.g., "greater than" is lowered the same as "less than" but the
;; comparison is reversed).

(rule (lower (has_type ty (select (def_inst (fcmp (FloatCC.LessThan) a b)) x y)))
(value_reg (with_flags_1 (fpcmp ty (put_in_reg_mem b) (put_in_reg a))
(cmove ty (CC.NBE) (put_in_reg_mem x) (put_in_reg y)))))

(rule (lower (has_type ty (select (def_inst (fcmp (FloatCC.LessThanOrEqual) a b)) x y)))
(value_reg (with_flags_1 (fpcmp ty (put_in_reg_mem b) (put_in_reg a))
(cmove ty (CC.NB) (put_in_reg_mem x) (put_in_reg y)))))

(rule (lower (has_type ty (select (def_inst (fcmp (FloatCC.UnorderedOrGreaterThan) a b)) x y)))
(value_reg (with_flags_1 (fpcmp ty (put_in_reg_mem b) (put_in_reg a))
(cmove ty (CC.B) (put_in_reg_mem x) (put_in_reg y)))))

(rule (lower (has_type ty (select (def_inst (fcmp (FloatCC.UnorderedOrGreaterThanOrEqual) a b)) x y)))
(value_reg (with_flags_1 (fpcmp ty (put_in_reg_mem b) (put_in_reg a))
(cmove ty (CC.BE) (put_in_reg_mem x) (put_in_reg y)))))

;; TODO `FloatCC.Equal` and `FloatCC.NotEqual` are only implemented with
;; multiple flag checks (e.g., `CC::P`, `CC::NZ`)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
src/clif.isle f176ef3bba99365
src/prelude.isle babc931e5dc5b4cf
src/isa/x64/inst.isle c4ccb93c53d849e1
src/isa/x64/lower.isle 40f29a8eab1763ce
src/isa/x64/inst.isle e31f127d75178da6
src/isa/x64/lower.isle 6e7f2eb81eb7f76
Loading

0 comments on commit 3c8ca00

Please sign in to comment.