Skip to content

Commit a36672d

Browse files
committed
fixes
1 parent ceaf862 commit a36672d

File tree

4 files changed

+66
-20
lines changed

4 files changed

+66
-20
lines changed

cranelift/codegen/src/isle_prelude.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ macro_rules! isle_common_prelude_methods {
4040
#[inline]
4141
fn imm64_sdiv(&mut self, ty: Type, x: Imm64, y: Imm64) -> Option<Imm64> {
4242
// Sign extend `x` and `y`.
43-
assert!(ty.bits() <= 64);
44-
let shift = 64 - ty.bits();
45-
let x = (x.bits() << shift) >> shift;
46-
let y = (y.bits() << shift) >> shift;
43+
let type_width = ty.bits();
44+
assert!(type_width <= 64);
45+
let x = x.sign_extend_from_width(type_width).bits();
46+
let y = y.sign_extend_from_width(type_width).bits();
47+
let shift = 64 - type_width;
4748

4849
// NB: We can't rely on `checked_div` to detect `ty::MIN / -1`
4950
// (which overflows and should trap) because we are working with
@@ -55,20 +56,20 @@ macro_rules! isle_common_prelude_methods {
5556
return None;
5657
}
5758

58-
let ty_mask = self.ty_mask(ty) as i64;
59-
let result = x.checked_div(y)? & ty_mask;
60-
Some(Imm64::new(result))
59+
let result = x.checked_div(y)?;
60+
Some(Imm64::new(result).mask_to_width(type_width))
6161
}
6262

6363
#[inline]
6464
fn imm64_srem(&mut self, ty: Type, x: Imm64, y: Imm64) -> Option<Imm64> {
6565
// Sign extend `x` and `y`.
66-
assert!(ty.bits() <= 64);
67-
let shift = 64 - ty.bits();
68-
let x = (x.bits() << shift) >> shift;
69-
let y = (y.bits() << shift) >> shift;
66+
let type_width = ty.bits();
67+
assert!(type_width <= 64);
68+
let x = x.sign_extend_from_width(type_width).bits();
69+
let y = y.sign_extend_from_width(type_width).bits();
70+
let shift = 64 - type_width;
7071

71-
// NB: We can't rely on `checked_rem` to detect `ty::MIN / -1`
72+
// NB: We can't rely on `checked_div` to detect `ty::MIN / -1`
7273
// (which overflows and should trap) because we are working with
7374
// `i64` values here, and `i32::MIN != i64::MIN`, for
7475
// example. Therefore, we have to explicitly check for this case
@@ -78,9 +79,8 @@ macro_rules! isle_common_prelude_methods {
7879
return None;
7980
}
8081

81-
let ty_mask = self.ty_mask(ty) as i64;
82-
let result = x.checked_rem(y)? & ty_mask;
83-
Some(Imm64::new(result))
82+
let result = x.checked_rem(y)?;
83+
Some(Imm64::new(result).mask_to_width(type_width))
8484
}
8585

8686
#[inline]

cranelift/codegen/src/opts/arithmetic.isle

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@
125125
;; x % 1 == 0
126126
(rule (simplify_skeleton (urem x (iconst_u ty 1))) (iconst_u ty 0))
127127
(rule (simplify_skeleton (srem x (iconst_u ty 1))) (iconst_u ty 0))
128-
(rule (simplify_skeleton (srem x (iconst_s ty -1))) (iconst_u ty 0))
129128

130129
;; Unsigned `x % d == x & ((1 << ilog2(d)) - 1)` when `d` is a power of two.
131130
(rule (simplify_skeleton (urem x (iconst_u ty (u64_extract_power_of_two d))))
@@ -339,5 +338,3 @@
339338
;; (x + y) - y --> x
340339
(rule (simplify (isub ty (iadd ty x y) x)) y)
341340
(rule (simplify (isub ty (iadd ty x y) y)) x)
342-
343-

cranelift/codegen/src/opts/cprop.isle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131

3232
(rule (simplify_skeleton
3333
(sdiv (iconst ty k1)
34-
(iconst _ k2)))
34+
(iconst ty k2)))
3535
(if-let d (imm64_sdiv ty k1 k2))
3636
(iconst ty d))
3737

3838
(rule (simplify_skeleton
3939
(srem (iconst ty k1)
40-
(iconst _ k2)))
40+
(iconst ty k2)))
4141
(if-let d (imm64_srem ty k1 k2))
4242
(iconst ty d))
4343

cranelift/filetests/filetests/egraph/skeleton.clif

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,54 @@ block0:
125125
; return v11 ; v11 = -1
126126
; }
127127

128+
function %cprop_sdiv_i8_min() -> i8 {
129+
block0:
130+
v0 = iconst.i8 -128
131+
v1 = iconst.i8 -1
132+
v2 = sdiv v0, v1
133+
return v2
134+
}
135+
136+
;function %cprop_sdiv_i8_min() -> i8 fast {
137+
;block0:
138+
; v0 = iconst.i8 -128
139+
; v1 = iconst.i8 -1
140+
; v2 = sdiv v0, v1 ; v0 = -128, v1 = -1
141+
; return v2
142+
;}
143+
144+
function %cprop_srem_i8_min() -> i8 {
145+
block0:
146+
v0 = iconst.i8 -128
147+
v1 = iconst.i8 -1
148+
v2 = srem v0, v1
149+
return v2
150+
}
151+
152+
;function %cprop_srem_i8_min() -> i8 fast {
153+
;block0:
154+
; v0 = iconst.i8 -128
155+
; v1 = iconst.i8 -1
156+
; v2 = srem v0, v1 ; v0 = -128, v1 = -1
157+
; return v2
158+
;}
159+
160+
function %cprop_srem_i64_min() -> i64 {
161+
block0:
162+
v0 = iconst.i64 -9223372036854775808
163+
v1 = iconst.i64 -1
164+
v2 = srem v0, v1
165+
return v2
166+
}
167+
168+
;function %cprop_srem_i64_min() -> i64 fast {
169+
;block0:
170+
; v0 = iconst.i64 -9223372036854775808
171+
; v1 = iconst.i64 -1
172+
; v2 = srem v0, v1 ; v0 = -9223372036854775808, v1 = -1
173+
; return v2
174+
;}
175+
128176
function %cprop_srem() -> i32 {
129177
block0:
130178
v0 = iconst.i32 -17
@@ -140,6 +188,7 @@ block0:
140188
; return v28 ; v28 = -3
141189
; }
142190

191+
143192
function %udiv_by_one(i32) -> i32 {
144193
block0(v0: i32):
145194
v1 = iconst.i32 1

0 commit comments

Comments
 (0)