Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cranelift: rewrite x*-1 to ineg(x) #6052

Merged
merged 2 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cranelift/codegen/src/opts/algebraic.isle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@
_))
(subsume zero))

;; x*-1 == -1*x == ineg(x).
(rule (simplify (imul ty x (iconst ty c)))
(if-let -1 (i64_sextend_imm64 ty c))
(ineg ty x))
(rule (simplify (imul ty (iconst ty c) x))
(if-let -1 (i64_sextend_imm64 ty c))
(ineg ty x))

;; x/1 == x.
(rule (simplify (sdiv ty
x
Expand Down
21 changes: 21 additions & 0 deletions cranelift/filetests/filetests/egraph/algebraic.clif
Kmeakin marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,27 @@ block0(v0: i32):
; check: return v2
}

function %mul_minus_one(i32) -> i32 {
block0(v0: i32):
v1 = iconst.i32 0xffff_ffff ; -1
v2 = imul v0, v1
return v2
; check: v3 = ineg v0
; check: v4 -> v3
; check: return v3
}

function %mul_minus_one_commuted(i32) -> i32 {
block0(v0: i32):
v1 = iconst.i32 0xffff_ffff ; -1
v2 = imul v1, v0
return v2
; check: v3 = ineg v0
; check: v5 -> v3
; check: v6 -> v3
; check: return v3
}

function %or_and_y_with_not_y_i8(i8, i8) -> i8 {
block0(v0: i8, v1: i8):
v2 = band v0, v1
Expand Down