Skip to content

Commit

Permalink
cranelift: simplify x-x to 0 (#6032)
Browse files Browse the repository at this point in the history
* cranelift: simplify `x-x` to `0`

* Guard `x-x => 0` rewrite with `fits_in_64`
  • Loading branch information
Kmeakin authored Mar 17, 2023
1 parent 28371bf commit b53d66e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cranelift/codegen/src/opts/algebraic.isle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
;; ineg(ineg(x)) == x.
(rule (simplify (ineg ty (ineg ty x))) (subsume x))

;; x-x == 0.
(rule (simplify (isub (fits_in_64 (ty_int ty)) x x)) (subsume (iconst ty (imm64 0))))

;; x*1 == 1*x == x.
(rule (simplify (imul ty
x
Expand Down
8 changes: 8 additions & 0 deletions cranelift/filetests/filetests/egraph/algebraic.clif
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ block0(v0: i32):
; check: return v0
}

function %isub_self(i32) -> i32 {
block0(v0: i32):
v1 = isub v0, v0
return v1
; check: v2 = iconst.i32 0
; check: return v2
}

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 b53d66e

Please sign in to comment.