Skip to content

Commit

Permalink
feat: simprocs for reducing x >>> i and x <<< i where i is a bi…
Browse files Browse the repository at this point in the history
…ttvector literal (#4193)
  • Loading branch information
leodemoura authored May 16, 2024
1 parent f2a304e commit e8c4540
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
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

0 comments on commit e8c4540

Please sign in to comment.