Skip to content

Commit

Permalink
ISLE: rewrite and/or of icmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Kmeakin committed Mar 28, 2023
1 parent cdd0cae commit 43842f7
Show file tree
Hide file tree
Showing 5 changed files with 3,117 additions and 8 deletions.
14 changes: 14 additions & 0 deletions cranelift/codegen/src/ir/condcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ impl IntCC {
]
}

/// Get the corresponding IntCC with the equal component added.
/// For conditions with a zero component, this is a no-op.
pub fn with_equal(self) -> Self {
use self::IntCC::*;
match self {
SignedGreaterThan => SignedGreaterThanOrEqual,
SignedLessThan => SignedLessThanOrEqual,
UnsignedGreaterThan => UnsignedGreaterThanOrEqual,
UnsignedLessThan => UnsignedLessThanOrEqual,
NotEqual => Equal,
_ => self,
}
}

/// Get the corresponding IntCC with the equal component removed.
/// For conditions without a zero component, this is a no-op.
pub fn without_equal(self) -> Self {
Expand Down
43 changes: 35 additions & 8 deletions cranelift/codegen/src/isle_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,16 +721,33 @@ macro_rules! isle_common_prelude_methods {
#[inline]
fn signed_cond_code(&mut self, cc: &condcodes::IntCC) -> Option<condcodes::IntCC> {
match cc {
IntCC::Equal
| IntCC::UnsignedGreaterThanOrEqual
| IntCC::UnsignedGreaterThan
| IntCC::UnsignedLessThanOrEqual
| IntCC::UnsignedLessThan
| IntCC::NotEqual => None,
IntCC::SignedGreaterThanOrEqual
IntCC::SignedLessThan
| IntCC::SignedLessThanOrEqual
| IntCC::SignedGreaterThan
| IntCC::SignedGreaterThanOrEqual => Some(*cc),
_ => None,
}
}

#[inline]
fn intcc_lt_or_gt(&mut self, cc: &IntCC) -> Option<IntCC> {
match cc {
IntCC::UnsignedLessThan
| IntCC::SignedLessThan
| IntCC::UnsignedGreaterThan
| IntCC::SignedGreaterThan => Some(*cc),
_ => None,
}
}

#[inline]
fn intcc_le_or_ge(&mut self, cc: &IntCC) -> Option<IntCC> {
match cc {
IntCC::UnsignedLessThanOrEqual
| IntCC::SignedLessThanOrEqual
| IntCC::SignedLessThan => Some(*cc),
| IntCC::UnsignedGreaterThanOrEqual
| IntCC::SignedGreaterThanOrEqual => Some(*cc),
_ => None,
}
}

Expand All @@ -744,6 +761,16 @@ macro_rules! isle_common_prelude_methods {
cc.inverse()
}

#[inline]
fn intcc_with_equal(&mut self, cc: &IntCC) -> IntCC {
cc.with_equal()
}

#[inline]
fn intcc_without_equal(&mut self, cc: &IntCC) -> IntCC {
cc.without_equal()
}

#[inline]
fn floatcc_reverse(&mut self, cc: &FloatCC) -> FloatCC {
cc.reverse()
Expand Down
50 changes: 50 additions & 0 deletions cranelift/codegen/src/opts/algebraic.isle
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,56 @@
(iconst _ (u64_from_imm64 0))))
(iconst ty (imm64 1)))

(decl pure decompose_intcc (IntCC) u64)
(rule (decompose_intcc (IntCC.Equal)) 1)
(rule (decompose_intcc (IntCC.UnsignedLessThan)) 2)
(rule (decompose_intcc (IntCC.SignedLessThan)) 2)
(rule (decompose_intcc (IntCC.UnsignedLessThanOrEqual)) 3)
(rule (decompose_intcc (IntCC.SignedLessThanOrEqual)) 3)
(rule (decompose_intcc (IntCC.UnsignedGreaterThan)) 4)
(rule (decompose_intcc (IntCC.SignedGreaterThan)) 4)
(rule (decompose_intcc (IntCC.UnsignedGreaterThanOrEqual)) 5)
(rule (decompose_intcc (IntCC.SignedGreaterThanOrEqual)) 5)
(rule (decompose_intcc (IntCC.NotEqual)) 6)

(decl compose_icmp (Type u64 bool Value Value) Value)
(rule (compose_icmp ty 0 _ _ _) (subsume (iconst ty (imm64 0))))
(rule (compose_icmp ty 1 _ x y) (icmp ty (IntCC.Equal) x y))
(rule (compose_icmp ty 2 $false x y) (icmp ty (IntCC.UnsignedLessThan) x y))
(rule (compose_icmp ty 2 $true x y) (icmp ty (IntCC.SignedLessThan) x y))
(rule (compose_icmp ty 3 $false x y) (icmp ty (IntCC.UnsignedLessThanOrEqual) x y))
(rule (compose_icmp ty 3 $true x y) (icmp ty (IntCC.SignedLessThanOrEqual) x y))
(rule (compose_icmp ty 4 $false x y) (icmp ty (IntCC.UnsignedGreaterThan) x y))
(rule (compose_icmp ty 4 $true x y) (icmp ty (IntCC.SignedGreaterThan) x y))
(rule (compose_icmp ty 5 $false x y) (icmp ty (IntCC.UnsignedGreaterThanOrEqual) x y))
(rule (compose_icmp ty 5 $true x y) (icmp ty (IntCC.SignedGreaterThanOrEqual) x y))
(rule (compose_icmp ty 6 _ x y) (icmp ty (IntCC.NotEqual) x y))
(rule (compose_icmp ty 7 _ _ _) (subsume (iconst ty (imm64 1))))

(decl pure intcc_class (IntCC) u64)
(rule (intcc_class (IntCC.UnsignedLessThan)) 1)
(rule (intcc_class (IntCC.UnsignedLessThanOrEqual)) 1)
(rule (intcc_class (IntCC.UnsignedGreaterThan)) 1)
(rule (intcc_class (IntCC.UnsignedGreaterThanOrEqual)) 1)
(rule (intcc_class (IntCC.SignedLessThan)) 2)
(rule (intcc_class (IntCC.SignedLessThanOrEqual)) 2)
(rule (intcc_class (IntCC.SignedGreaterThan)) 2)
(rule (intcc_class (IntCC.SignedGreaterThanOrEqual)) 2)
(rule (intcc_class (IntCC.Equal)) 3)
(rule (intcc_class (IntCC.NotEqual)) 3)

(decl pure partial intcc_comparable (IntCC IntCC) bool)
(rule (intcc_comparable x y)
(if-let (u64_nonzero class) (u64_and (intcc_class x) (intcc_class y)))
(u64_eq 2 class))

(rule (simplify (band ty (icmp ty cc1 x y) (icmp ty cc2 x y)))
(if-let signed (intcc_comparable cc1 cc2))
(compose_icmp ty (u64_and (decompose_intcc cc1) (decompose_intcc cc2)) signed x y))

(rule (simplify (bor ty (icmp ty cc1 x y) (icmp ty cc2 x y)))
(if-let signed (intcc_comparable cc1 cc2))
(compose_icmp ty (u64_or (decompose_intcc cc1) (decompose_intcc cc2)) signed x y))

;; Transform select-of-icmp into {u,s}{min,max} instructions where possible.
(rule (simplify (select ty (sgt _ x y) x y)) (smax ty x y))
Expand Down
14 changes: 14 additions & 0 deletions cranelift/codegen/src/prelude.isle
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@
(decl intcc_inverse (IntCC) IntCC)
(extern constructor intcc_inverse intcc_inverse)

;; Add equal component to an IntCC
(decl intcc_with_equal (IntCC) IntCC)
(extern constructor intcc_with_equal intcc_with_equal)

;; Remove equal component from an IntCC
(decl intcc_without_equal (IntCC) IntCC)
(extern constructor intcc_without_equal intcc_without_equal)

;; Reverse an FloatCC flag.
(decl floatcc_reverse (FloatCC) FloatCC)
(extern constructor floatcc_reverse floatcc_reverse)
Expand Down Expand Up @@ -315,6 +323,12 @@
(decl sge (Type Value Value) Value)
(extractor (sge ty x y) (icmp ty (IntCC.SignedGreaterThanOrEqual) x y))

(decl intcc_le_or_ge (IntCC) IntCC)
(extern extractor intcc_le_or_ge intcc_le_or_ge)

(decl intcc_lt_or_gt (IntCC) IntCC)
(extern extractor intcc_lt_or_gt intcc_lt_or_gt)

;; An extractor that only matches types that can fit in 16 bits.
(decl fits_in_16 (Type) Type)
(extern extractor fits_in_16 fits_in_16)
Expand Down
Loading

0 comments on commit 43842f7

Please sign in to comment.