Skip to content

Commit

Permalink
cranelift: Reassociate constants out of nested shifts (bytecodeallian…
Browse files Browse the repository at this point in the history
…ce#7450)

This allows for more constant propagation.

Co-authored-by: Trevor Elliott <telliott@fastly.com>
  • Loading branch information
fitzgen and elliottt authored Nov 1, 2023
1 parent c56cdb3 commit e0bfa73
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cranelift/codegen/src/opts/cprop.isle
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,13 @@
(rule (splat32 n) (splat64 (u64_or n (u64_shl n 32))))
(decl splat64 (u64) Constant)
(extern constructor splat64 splat64)

;; Reassociate nested shifts of constants to put constants together for cprop.
;;
;; ((A shift b) shift C) ==> ((A shift C) shift b)
(rule (simplify (ishl ty (ishl ty a@(iconst _ _) b) c@(iconst _ _)))
(ishl ty (ishl ty a c) b))
(rule (simplify (ushr ty (ushr ty a@(iconst _ _) b) c@(iconst _ _)))
(ushr ty (ushr ty a c) b))
(rule (simplify (sshr ty (sshr ty a@(iconst _ _) b) c@(iconst _ _)))
(sshr ty (sshr ty a c) b))
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
test optimize
set opt_level=speed
target x86_64

;; Test egraph rewrite rules that reassociate constants out of nested shifts.

function %a(i32) -> i32 {
block0(v0: i32):
v1 = iconst.i32 1
v2 = ishl v1, v0
v3 = iconst.i32 2
v4 = ishl v2, v3
return v4
; check: v6 = iconst.i32 4
; nextln: v7 = ishl v6, v0
; check: return v7
}

function %b(i32) -> i32 {
block0(v0: i32):
v1 = iconst.i32 8
v2 = ushr v1, v0
v3 = iconst.i32 2
v4 = ushr v2, v3
return v4
; check: v3 = iconst.i32 2
; nextln: v6 = ushr v3, v0
; check: return v6
}

function %c(i32) -> i32 {
block0(v0: i32):
v1 = iconst.i32 8
v2 = sshr v1, v0
v3 = iconst.i32 2
v4 = sshr v2, v3
return v4
; check: v3 = iconst.i32 2
; nextln: v6 = sshr v3, v0
; check: return v6
}

0 comments on commit e0bfa73

Please sign in to comment.