|
4 | 4 | // author: Kevin Laeufer <laeufer@cornell.edu> |
5 | 5 |
|
6 | 6 | use super::{ |
7 | | - BVLitValue, Context, Expr, ExprMap, ExprRef, SparseExprMap, TypeCheck, WidthInt, |
8 | | - do_transform_expr, |
| 7 | + BVLitValue, Context, Expr, ExprMap, ExprRef, SerializableIrNode, SparseExprMap, TypeCheck, |
| 8 | + WidthInt, do_transform_expr, find_symbols, |
9 | 9 | }; |
10 | 10 | use crate::expr::meta::get_fixed_point; |
11 | 11 | use crate::expr::transform::ExprTransformMode; |
| 12 | +use crate::smt::{CheckSatResponse, SolverContext}; |
12 | 13 | use baa::BitVecOps; |
13 | 14 | use smallvec::{SmallVec, smallvec}; |
14 | 15 |
|
@@ -38,6 +39,68 @@ impl<T: ExprMap<Option<ExprRef>>> Simplifier<T> { |
38 | 39 | ); |
39 | 40 | get_fixed_point(&mut self.cache, e).unwrap() |
40 | 41 | } |
| 42 | + |
| 43 | + /// Uses an SMT solver to check all simplification steps that have been made with this Simplifier. |
| 44 | + /// Returns the number of incorrect simplifications. |
| 45 | + pub fn verify_simplification( |
| 46 | + &self, |
| 47 | + ctx: &mut Context, |
| 48 | + solver: &mut impl SolverContext, |
| 49 | + ) -> crate::smt::Result<usize> { |
| 50 | + let mut incorrect = 0; |
| 51 | + let mut correct = 0; |
| 52 | + for (key, &value) in self.cache.iter() { |
| 53 | + if let Some(simplified) = value { |
| 54 | + let key_symbols = find_symbols(ctx, key); |
| 55 | + let simpl_symbols = find_symbols(ctx, simplified); |
| 56 | + let symbols: Vec<_> = key_symbols.union(&simpl_symbols).cloned().collect(); |
| 57 | + solver.push()?; |
| 58 | + for &sym in symbols.iter() { |
| 59 | + solver.declare_const(ctx, sym)?; |
| 60 | + } |
| 61 | + let not_eq = ctx.distinct(key, simplified); |
| 62 | + solver.assert(ctx, not_eq)?; |
| 63 | + match solver.check_sat()? { |
| 64 | + CheckSatResponse::Sat => { |
| 65 | + let key_value = solver.get_value(ctx, key)?; |
| 66 | + let simplified_value = solver.get_value(ctx, simplified)?; |
| 67 | + println!( |
| 68 | + "{} ({}) =/= ({}) {}", |
| 69 | + key.serialize_to_str(ctx), |
| 70 | + key_value.serialize_to_str(ctx), |
| 71 | + simplified_value.serialize_to_str(ctx), |
| 72 | + simplified.serialize_to_str(ctx) |
| 73 | + ); |
| 74 | + let mut syms = vec![]; |
| 75 | + for &sym in symbols.iter() { |
| 76 | + let value = solver.get_value(ctx, sym)?; |
| 77 | + syms.push(format!( |
| 78 | + "{}={}", |
| 79 | + sym.serialize_to_str(ctx), |
| 80 | + value.serialize_to_str(ctx) |
| 81 | + )); |
| 82 | + } |
| 83 | + println!(" w/ {}", syms.join(", ")); |
| 84 | + incorrect += 1; |
| 85 | + } |
| 86 | + CheckSatResponse::Unsat => { |
| 87 | + correct += 1; |
| 88 | + } // OK |
| 89 | + CheckSatResponse::Unknown => {} // OK |
| 90 | + } |
| 91 | + |
| 92 | + solver.pop()?; |
| 93 | + } |
| 94 | + } |
| 95 | + if incorrect > 0 { |
| 96 | + println!( |
| 97 | + "{incorrect} / {} simplifications were incorrect. See log.", |
| 98 | + incorrect + correct |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + Ok(incorrect) |
| 103 | + } |
41 | 104 | } |
42 | 105 |
|
43 | 106 | /// Simplifies one expression (not its children) |
@@ -254,8 +317,11 @@ fn simplify_bv_and(ctx: &mut Context, a: ExprRef, b: ExprRef) -> Option<ExprRef> |
254 | 317 | // a & !a -> 0 |
255 | 318 | (Expr::BVNot(inner, w), _) if *inner == b => Some(ctx.zero(*w)), |
256 | 319 | (_, Expr::BVNot(inner, w)) if *inner == a => Some(ctx.zero(*w)), |
257 | | - // !a & !b -> a | b |
258 | | - (Expr::BVNot(a, _), Expr::BVNot(b, _)) => Some(ctx.or(*a, *b)), |
| 320 | + // !a & !b -> !(a | b) |
| 321 | + (Expr::BVNot(a, _), Expr::BVNot(b, _)) => { |
| 322 | + let or = ctx.or(*a, *b); |
| 323 | + Some(ctx.not(or)) |
| 324 | + } |
259 | 325 | _ => None, |
260 | 326 | } |
261 | 327 | } |
@@ -290,8 +356,11 @@ fn simplify_bv_or(ctx: &mut Context, a: ExprRef, b: ExprRef) -> Option<ExprRef> |
290 | 356 | // a | !a -> 1 |
291 | 357 | (Expr::BVNot(inner, w), _) if *inner == b => Some(ctx.ones(*w)), |
292 | 358 | (_, Expr::BVNot(inner, w)) if *inner == a => Some(ctx.ones(*w)), |
293 | | - // !a | !b -> a & b |
294 | | - (Expr::BVNot(a, _), Expr::BVNot(b, _)) => Some(ctx.and(*a, *b)), |
| 359 | + // !a | !b -> !(a & b) |
| 360 | + (Expr::BVNot(a, _), Expr::BVNot(b, _)) => { |
| 361 | + let and = ctx.and(*a, *b); |
| 362 | + Some(ctx.not(and)) |
| 363 | + } |
295 | 364 | _ => None, |
296 | 365 | } |
297 | 366 | } |
|
0 commit comments