Skip to content

Commit

Permalink
cranelift: rewrite x*-1 to ineg(x) (#6052)
Browse files Browse the repository at this point in the history
* cranelift: rewrite `x*-1` to `ineg(x)`

* Add commuted test
  • Loading branch information
Kmeakin authored Mar 17, 2023
1 parent c3f5b71 commit 208d09e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cranelift/codegen/src/opts/algebraic.isle
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,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
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,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

0 comments on commit 208d09e

Please sign in to comment.