Skip to content

Commit

Permalink
Rename cc helpers (bytecodealliance#6862)
Browse files Browse the repository at this point in the history
* Rename "reverse" to "flip"

* Rename "inverse" to "negate"

* Correct grammar in comment: change "an" to "a"

* Rename "flip" to "swap_args"

* Rename "negate" to "complement"
  • Loading branch information
gurry authored and eduardomourar committed Sep 6, 2023
1 parent e7c4844 commit d393299
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 51 deletions.
48 changes: 24 additions & 24 deletions cranelift/codegen/src/ir/condcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ use serde::{Deserialize, Serialize};

/// Common traits of condition codes.
pub trait CondCode: Copy {
/// Get the inverse condition code of `self`.
/// Get the complemented condition code of `self`.
///
/// The inverse condition code produces the opposite result for all comparisons.
/// That is, `cmp CC, x, y` is true if and only if `cmp CC.inverse(), x, y` is false.
/// The complemented condition code produces the opposite result for all comparisons.
/// That is, `cmp CC, x, y` is true if and only if `cmp CC.complement(), x, y` is false.
#[must_use]
fn inverse(self) -> Self;
fn complement(self) -> Self;

/// Get the reversed condition code for `self`.
/// Get the swapped args condition code for `self`.
///
/// The reversed condition code produces the same result as swapping `x` and `y` in the
/// comparison. That is, `cmp CC, x, y` is the same as `cmp CC.reverse(), y, x`.
/// The swapped args condition code produces the same result as swapping `x` and `y` in the
/// comparison. That is, `cmp CC, x, y` is the same as `cmp CC.swap_args(), y, x`.
#[must_use]
fn reverse(self) -> Self;
fn swap_args(self) -> Self;
}

/// Condition code for comparing integers.
Expand Down Expand Up @@ -58,7 +58,7 @@ pub enum IntCC {
}

impl CondCode for IntCC {
fn inverse(self) -> Self {
fn complement(self) -> Self {
use self::IntCC::*;
match self {
Equal => NotEqual,
Expand All @@ -74,7 +74,7 @@ impl CondCode for IntCC {
}
}

fn reverse(self) -> Self {
fn swap_args(self) -> Self {
use self::IntCC::*;
match self {
Equal => Equal,
Expand Down Expand Up @@ -254,7 +254,7 @@ impl FloatCC {
}

impl CondCode for FloatCC {
fn inverse(self) -> Self {
fn complement(self) -> Self {
use self::FloatCC::*;
match self {
Ordered => Unordered,
Expand All @@ -273,7 +273,7 @@ impl CondCode for FloatCC {
UnorderedOrGreaterThanOrEqual => LessThan,
}
}
fn reverse(self) -> Self {
fn swap_args(self) -> Self {
use self::FloatCC::*;
match self {
Ordered => Ordered,
Expand Down Expand Up @@ -347,21 +347,21 @@ mod tests {
use std::string::ToString;

#[test]
fn int_inverse() {
fn int_complement() {
for r in IntCC::all() {
let cc = *r;
let inv = cc.inverse();
let inv = cc.complement();
assert!(cc != inv);
assert_eq!(inv.inverse(), cc);
assert_eq!(inv.complement(), cc);
}
}

#[test]
fn int_reverse() {
fn int_swap_args() {
for r in IntCC::all() {
let cc = *r;
let rev = cc.reverse();
assert_eq!(rev.reverse(), cc);
let rev = cc.swap_args();
assert_eq!(rev.swap_args(), cc);
}
}

Expand All @@ -375,21 +375,21 @@ mod tests {
}

#[test]
fn float_inverse() {
fn float_complement() {
for r in FloatCC::all() {
let cc = *r;
let inv = cc.inverse();
let inv = cc.complement();
assert!(cc != inv);
assert_eq!(inv.inverse(), cc);
assert_eq!(inv.complement(), cc);
}
}

#[test]
fn float_reverse() {
fn float_swap_args() {
for r in FloatCC::all() {
let cc = *r;
let rev = cc.reverse();
assert_eq!(rev.reverse(), cc);
let rev = cc.swap_args();
assert_eq!(rev.swap_args(), cc);
}
}

Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/isa/riscv64/inst.isle
Original file line number Diff line number Diff line change
Expand Up @@ -2617,7 +2617,7 @@
(if-let $true (floatcc_unordered cc))
(let ((then BranchTarget (label_to_br_target (vec_label_get targets 0)))
(else BranchTarget (label_to_br_target (vec_label_get targets 1))))
(emit_side_effect (cond_br (emit_fcmp (floatcc_inverse cc) ty a b) else then))))
(emit_side_effect (cond_br (emit_fcmp (floatcc_complement cc) ty a b) else then))))

(rule 1
(lower_branch (brif (maybe_uextend (fcmp cc a @ (value_type ty) b)) _ _) targets)
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/isa/riscv64/inst/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl IntegerCompare {

pub(crate) fn inverse(self) -> Self {
Self {
kind: self.kind.inverse(),
kind: self.kind.complement(),
..self
}
}
Expand Down
6 changes: 3 additions & 3 deletions cranelift/codegen/src/isa/x64/inst.isle
Original file line number Diff line number Diff line change
Expand Up @@ -4554,12 +4554,12 @@
(let ((size OperandSize (raw_operand_size_of_type ty)))
(icmp_cond_result (x64_cmp size b a) cc)))

;; As a special case, reverse the arguments to the comparison when the LHS is a
;; As a special case, swap the arguments to the comparison when the LHS is a
;; constant. This ensures that we avoid moving the constant into a register when
;; performing the comparison.
(rule 1 (emit_cmp cc (and (simm32_from_value a) (value_type ty)) b)
(let ((size OperandSize (raw_operand_size_of_type ty)))
(icmp_cond_result (x64_cmp size a b) (intcc_reverse cc))))
(icmp_cond_result (x64_cmp size a b) (intcc_swap_args cc))))

;; Special case: use the test instruction for comparisons with 0.
(rule 2 (emit_cmp cc a @ (value_type ty) (u64_from_iconst 0))
Expand All @@ -4570,7 +4570,7 @@
(rule 3 (emit_cmp cc (u64_from_iconst 0) b @ (value_type ty))
(let ((size OperandSize (raw_operand_size_of_type ty))
(b Gpr (put_in_reg b)))
(icmp_cond_result (x64_test size b b) (intcc_reverse cc))))
(icmp_cond_result (x64_test size b b) (intcc_swap_args cc))))

;; For I128 values (held in two GPRs), the instruction sequences depend on what
;; kind of condition is tested.
Expand Down
16 changes: 8 additions & 8 deletions cranelift/codegen/src/isle_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,23 +818,23 @@ macro_rules! isle_common_prelude_methods {
}

#[inline]
fn intcc_reverse(&mut self, cc: &IntCC) -> IntCC {
cc.reverse()
fn intcc_swap_args(&mut self, cc: &IntCC) -> IntCC {
cc.swap_args()
}

#[inline]
fn intcc_inverse(&mut self, cc: &IntCC) -> IntCC {
cc.inverse()
fn intcc_complement(&mut self, cc: &IntCC) -> IntCC {
cc.complement()
}

#[inline]
fn floatcc_reverse(&mut self, cc: &FloatCC) -> FloatCC {
cc.reverse()
fn floatcc_swap_args(&mut self, cc: &FloatCC) -> FloatCC {
cc.swap_args()
}

#[inline]
fn floatcc_inverse(&mut self, cc: &FloatCC) -> FloatCC {
cc.inverse()
fn floatcc_complement(&mut self, cc: &FloatCC) -> FloatCC {
cc.complement()
}

fn floatcc_unordered(&mut self, cc: &FloatCC) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/opts/cprop.isle
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@

(rule (simplify
(icmp ty cc k @ (iconst _ _) x))
(icmp ty (intcc_reverse cc) x k))
(icmp ty (intcc_swap_args cc) x k))

;; Canonicalize via associativity: reassociate to a right-heavy tree
;; for constants.
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/opts/icmp.isle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
(rule (simplify (eq ty
(uextend _ (icmp ty cc x y))
(iconst _ (u64_from_imm64 0))))
(subsume (icmp ty (intcc_inverse cc) x y)))
(subsume (icmp ty (intcc_complement cc) x y)))

;; Optimize select-of-uextend-of-icmp to select-of-icmp, because
;; select can take an I8 condition too.
Expand Down
24 changes: 12 additions & 12 deletions cranelift/codegen/src/prelude.isle
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,21 @@

;;;; Helpers for Working with Flags ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Reverse an IntCC flag.
(decl intcc_reverse (IntCC) IntCC)
(extern constructor intcc_reverse intcc_reverse)
;; Swap args of an IntCC flag.
(decl intcc_swap_args (IntCC) IntCC)
(extern constructor intcc_swap_args intcc_swap_args)

;; Invert an IntCC flag.
(decl intcc_inverse (IntCC) IntCC)
(extern constructor intcc_inverse intcc_inverse)
;; Complement an IntCC flag.
(decl intcc_complement (IntCC) IntCC)
(extern constructor intcc_complement intcc_complement)

;; Reverse an FloatCC flag.
(decl floatcc_reverse (FloatCC) FloatCC)
(extern constructor floatcc_reverse floatcc_reverse)
;; Swap args of a FloatCC flag.
(decl floatcc_swap_args (FloatCC) FloatCC)
(extern constructor floatcc_swap_args floatcc_swap_args)

;; Invert an FloatCC flag.
(decl floatcc_inverse (FloatCC) FloatCC)
(extern constructor floatcc_inverse floatcc_inverse)
;; Complement a FloatCC flag.
(decl floatcc_complement (FloatCC) FloatCC)
(extern constructor floatcc_complement floatcc_complement)

;; True when this FloatCC involves an unordered comparison.
(decl pure floatcc_unordered (FloatCC) bool)
Expand Down

0 comments on commit d393299

Please sign in to comment.