Skip to content

Commit

Permalink
Cranelift: Remove some unneeded arithmetic.isle patterns (bytecodea…
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmcm authored Nov 29, 2023
1 parent 4d22446 commit d2887ab
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 41 deletions.
30 changes: 8 additions & 22 deletions cranelift/codegen/src/opts/arithmetic.isle
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
;; rewrites for integer and floating-point arithmetic
;; eg: `iadd`, `isub`, `ineg`, `imul`, `fadd`, `fsub`, `fmul`

;; x+0 == 0+x == x.
;; For commutative instructions, we depend on cprop.isle pushing immediates to
;; the right, and thus only simplify patterns like `x+0`, not `0+x`.

;; x+0 == x.
(rule (simplify (iadd ty
x
(iconst ty (u64_from_imm64 0))))
(subsume x))
(rule (simplify (iadd ty
(iconst ty (u64_from_imm64 0))
x))
(subsume x))
;; x-0 == x.
(rule (simplify (isub ty
x
Expand Down Expand Up @@ -39,33 +38,22 @@
;; x-x == 0.
(rule (simplify (isub (fits_in_64 (ty_int ty)) x x)) (subsume (iconst ty (imm64 0))))

;; x*1 == 1*x == x.
;; x*1 == x.
(rule (simplify (imul ty
x
(iconst ty (u64_from_imm64 1))))
(subsume x))
(rule (simplify (imul ty
(iconst ty (u64_from_imm64 1))
x))
(subsume x))

;; x*0 == 0*x == 0.
;; x*0 == 0.
(rule (simplify (imul ty
_
zero @ (iconst ty (u64_from_imm64 0))))
(subsume zero))
(rule (simplify (imul ty
zero @ (iconst ty (u64_from_imm64 0))
_))
(subsume zero))

;; x*-1 == -1*x == ineg(x).
;; x*-1 == 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 == 1 + (!x) == !(x) - (-1) == ineg(x)
(rule (simplify (iadd ty (bnot ty x) (iconst ty (u64_from_imm64 1))))
Expand Down Expand Up @@ -99,11 +87,9 @@
;; TODO: strength reduction: div to shifts
;; TODO: div/rem by constants -> magic multiplications

;; x*2 == 2*x == x+x.
;; x*2 == x+x.
(rule (simplify (imul ty x (iconst _ (simm32 2))))
(iadd ty x x))
(rule (simplify (imul ty (iconst _ (simm32 2)) x))
(iadd ty x x))

;; x*c == x<<log2(c) when c is a power of two.
;; Note that the type of `iconst` must be the same as the type of `imul`,
Expand Down
37 changes: 35 additions & 2 deletions cranelift/filetests/filetests/egraph/arithmetic.clif
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,39 @@ block0(v0: i32):
; check: return v2
}

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

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

function %one_times(i32) -> i32 {
block0(v0: i32):
v1 = iconst.i32 1
v2 = imul v1, v0
return v2
; check: return v0
}

function %two_times(i32) -> i32 {
block0(v0: i32):
v1 = iconst.i32 2
v2 = imul v1, v0
return v2
; check: v6 = iadd v0, v0
; check: return v6
}

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

function %ineg_not_plus_one(i32) -> i32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ block0(v0: i32, v1: i32):
v6 = iadd v4, v5
return v6
; check: v7 = iadd v0, v1
; nextln: iconst.i32 78
; nextln: v16 = iadd v7, v14 ; v14 = 78
; check: return v16
; nextln: v9 = iconst.i32 78
; nextln: v15 = iadd v7, v9 ; v9 = 78
; check: return v15
}

function %imul_shallow_and_wide(i32, i32, i32, i32) -> i32 {
Expand Down
4 changes: 2 additions & 2 deletions cranelift/filetests/filetests/egraph/cprop.clif
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ block0:
return v2
}

; check: v9 = iconst.i8 41
; nextln: return v9
; check: v3 = iconst.i8 41
; nextln: return v3

function %f1() -> i16 {
block0:
Expand Down
11 changes: 5 additions & 6 deletions cranelift/filetests/filetests/egraph/remat.clif
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ block2:
; check: v2 = iadd v0, v1
; check: brif v2, block1, block2
; check: block1:
; check: v11 = iconst.i32 126
; check: v13 = iadd.i32 v0, v11
; check: return v13
; check: v6 = iconst.i32 126
; check: v12 = iadd.i32 v0, v6
; check: return v12
; check: block2:
; check: v15 = iadd.i32 v0, v1
; check: return v15

; check: v14 = iadd.i32 v0, v1
; check: return v14
12 changes: 6 additions & 6 deletions cranelift/filetests/filetests/egraph/shifts.clif
Original file line number Diff line number Diff line change
Expand Up @@ -651,9 +651,9 @@ block0(v0: i8):
return v4
}

; check: v11 = iconst.i8 2
; check: v13 = rotr v0, v11 ; v11 = 2
; check: return v13
; check: v6 = iconst.i8 2
; check: v12 = rotr v0, v6 ; v6 = 2
; check: return v12

function %rotr_rotr_add(i8, i8, i8) -> i8 {
block0(v0: i8, v1: i8, v2: i8):
Expand All @@ -675,9 +675,9 @@ block0(v0: i8):
return v4
}

; check: v11 = iconst.i8 2
; check: v13 = rotl v0, v11 ; v11 = 2
; check: return v13
; check: v6 = iconst.i8 2
; check: v12 = rotl v0, v6 ; v6 = 2
; check: return v12

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

0 comments on commit d2887ab

Please sign in to comment.