Skip to content

Commit

Permalink
Merge pull request #1975 from MaxGraey/more-peephole-opts
Browse files Browse the repository at this point in the history
peepmatic: add reminder by a power of two rule
  • Loading branch information
fitzgen authored Jul 6, 2020
2 parents 2286576 + 3056594 commit 62530e4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cranelift/codegen/src/preopt.peepmatic
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,9 @@
(=> (when (udiv_imm $C $x)
(is-power-of-two $C))
(ushr_imm $(log2 $C) $x))

;; Remainder by a power of two -> bitwise and with decreased by one constant.
(=> (when (urem_imm $C $x)
(is-power-of-two $C)
(fits-in-native-word $C))
(band_imm $(isub $C 1) $x))
Binary file modified cranelift/codegen/src/preopt.serialized
Binary file not shown.
4 changes: 4 additions & 0 deletions cranelift/peepmatic/crates/runtime/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ pub enum UnquoteOperator {
#[peepmatic(params(iNN, iNN), result(iNN))]
Imul,

/// Compile-time `isub` of two constant values.
#[peepmatic(params(iNN, iNN), result(iNN))]
Isub,

/// Take the base-2 log of a power of two integer.
#[peepmatic(params(iNN), result(iNN))]
Log2,
Expand Down
4 changes: 3 additions & 1 deletion cranelift/peepmatic/crates/runtime/src/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ where
| UnquoteOperator::Bor
| UnquoteOperator::Bxor
| UnquoteOperator::Iadd
| UnquoteOperator::Imul => unreachable!("not a unary unquote operator: {:?}", operator),
| UnquoteOperator::Imul
| UnquoteOperator::Isub => unreachable!("not a unary unquote operator: {:?}", operator),
}
}

Expand All @@ -99,6 +100,7 @@ where
UnquoteOperator::Bxor => fold_ints!(a, b, |x, y| x ^ y),
UnquoteOperator::Iadd => fold_ints!(a, b, |x, y| x.wrapping_add(y)),
UnquoteOperator::Imul => fold_ints!(a, b, |x, y| x.wrapping_mul(y)),
UnquoteOperator::Isub => fold_ints!(a, b, |x, y| x.wrapping_sub(y)),
UnquoteOperator::Log2 | UnquoteOperator::Neg => {
unreachable!("not a binary unquote operator: {:?}", operator)
}
Expand Down

0 comments on commit 62530e4

Please sign in to comment.