Skip to content

Commit 7aadd9c

Browse files
committed
fixes
1 parent a36672d commit 7aadd9c

File tree

5 files changed

+434
-4
lines changed

5 files changed

+434
-4
lines changed

cranelift/codegen/src/isle_prelude.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ macro_rules! isle_common_prelude_methods {
3737
Imm64::new(i64::from(lz))
3838
}
3939

40+
#[inline]
41+
fn imm64_ctz(&mut self, ty: Type, a: Imm64) -> Imm64 {
42+
let bits = ty.bits();
43+
assert!(bits <= 64);
44+
let a_v: u64 = a.bits().cast_unsigned();
45+
if a_v == 0 {
46+
// ctz(0) is defined to be the number of bits in the type.
47+
return Imm64::new(i64::from(bits));
48+
}
49+
let lz = a_v.trailing_zeros();
50+
Imm64::new(i64::from(lz))
51+
}
52+
4053
#[inline]
4154
fn imm64_sdiv(&mut self, ty: Type, x: Imm64, y: Imm64) -> Option<Imm64> {
4255
// Sign extend `x` and `y`.

cranelift/codegen/src/opts/cprop.isle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
;; Constant propagation.
22

33
(rule (simplify
4-
(clz (fits_in_64 ty)
5-
(iconst ty kx)))
4+
(clz (fits_in_64 ty)
5+
(iconst ty kx)))
66
(subsume (iconst ty (imm64_clz ty kx))))
77

88

99
(rule (simplify
1010
(ctz (fits_in_64 ty)
11-
(iconst ty (u64_from_imm64 kx))))
12-
(subsume (iconst ty (imm64_masked ty (u64_trailing_zeros kx)))))
11+
(iconst ty kx)))
12+
(subsume (iconst ty (imm64_ctz ty kx))))
1313

1414
(rule (simplify
1515
(iadd (fits_in_64 ty)

cranelift/codegen/src/prelude.isle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
(decl pure imm64_clz (Type Imm64) Imm64)
103103
(extern constructor imm64_clz imm64_clz)
104104

105+
(decl pure imm64_ctz (Type Imm64) Imm64)
106+
(extern constructor imm64_ctz imm64_ctz)
105107

106108
;; Each of these extractors tests whether the upper half of the input equals the
107109
;; lower half of the input

cranelift/filetests/filetests/egraph/cprop.clif

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ block0:
4949
; check: v3 = iconst.i16 4
5050
; nextln: return v3
5151

52+
function %f0() -> i16 {
53+
block0:
54+
v1 = iconst.i16 0
55+
v2 = ctz.i16 v1
56+
return v2
57+
}
58+
59+
; check: v3 = iconst.i16 16
60+
; nextln: return v3
61+
5262
function %ishl() -> i8 {
5363
block0:
5464
v0 = iconst.i8 1

0 commit comments

Comments
 (0)