Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simprocs for reducing x >>> i and x <<< i where i is a bittvector literal #4193

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/BitVec.lean
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ Helper function for reducing bitvector functions such as `shiftLeft` and `rotate
let some i ← Nat.fromExpr? e.appArg! | return .continue
return .done <| toExpr (op v.value i)

/--
Helper function for reducing `x <<< i` and `x >>> i` where `i` is a bitvector literal,
into one that is a natural number literal.
-/
@[inline] def reduceShiftWithBitVecLit (declName : Name) (e : Expr) : SimpM DStep := do
unless e.isAppOfArity declName 6 do return .continue
let v := e.appFn!.appArg!
let some i ← fromExpr? e.appArg! | return .continue
return .visit (← mkAppM declName #[v, toExpr i.value.toNat])

/--
Helper function for reducing bitvector predicates.
-/
Expand Down Expand Up @@ -158,9 +168,15 @@ builtin_dsimproc [simp, seval] reduceSShiftRight (BitVec.sshiftRight _ _) :=
/-- Simplification procedure for shift left on `BitVec`. -/
builtin_dsimproc [simp, seval] reduceHShiftLeft ((_ <<< _ : BitVec _)) :=
reduceShift ``HShiftLeft.hShiftLeft 6 (· <<< ·)
/-- Simplification procedure for converting a shift with a bit-vector literal into a natural number literal. -/
builtin_dsimproc [simp, seval] reduceHShiftLeft' ((_ <<< (_ : BitVec _) : BitVec _)) :=
reduceShiftWithBitVecLit ``HShiftLeft.hShiftLeft
/-- Simplification procedure for shift right on `BitVec`. -/
builtin_dsimproc [simp, seval] reduceHShiftRight ((_ >>> _ : BitVec _)) :=
reduceShift ``HShiftRight.hShiftRight 6 (· >>> ·)
/-- Simplification procedure for converting a shift with a bit-vector literal into a natural number literal. -/
builtin_dsimproc [simp, seval] reduceHShiftRight' ((_ >>> (_ : BitVec _) : BitVec _)) :=
reduceShiftWithBitVecLit ``HShiftRight.hShiftRight
/-- Simplification procedure for rotate left on `BitVec`. -/
builtin_dsimproc [simp, seval] reduceRotateLeft (BitVec.rotateLeft _ _) :=
reduceShift ``BitVec.rotateLeft 3 BitVec.rotateLeft
Expand Down
8 changes: 8 additions & 0 deletions tests/lean/run/bitvec_simproc.lean
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,11 @@ example (h : -5#10 = x) : signExtend 10 (-5#8) = x := by
simp; guard_target =ₛ1019#10 = x; assumption
example (h : 5#10 = x) : -signExtend 10 (-5#8) = x := by
simp; guard_target =ₛ5#10 = x; assumption
example (h : 40#32 = b) : 10#32 <<< 2#32 = b := by
simp; guard_target =ₛ 40#32 = b; assumption
example (h : 3#32 = b) : 12#32 >>> 2#32 = b := by
simp; guard_target =ₛ 3#32 = b; assumption
example (a : BitVec 32) (h : a >>> 2 = b) : a >>> 2#32 = b := by
simp; guard_target =ₛ a >>> 2 = b; assumption
example (a : BitVec 32) (h : a <<< 2 = b) : a <<< 2#32 = b := by
simp; guard_target =ₛ a <<< 2 = b; assumption
Loading