From 1927dc6574b9eee315c8f24441df390f2ce793c7 Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Thu, 23 Jul 2020 11:14:35 -0700 Subject: mask bits when propagating bitwise ops (#1745) * ConstProp: test bitwise op of signed literals * ConstProp: use bit mask for FoldOr/FoldXor * handle and also * add UIntLiteral.masked helper Co-authored-by: Jack Koenig --- .../scala/firrtl/transforms/ConstantPropagation.scala | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src/main/scala/firrtl/transforms') diff --git a/src/main/scala/firrtl/transforms/ConstantPropagation.scala b/src/main/scala/firrtl/transforms/ConstantPropagation.scala index 000adc15..8ad3489f 100644 --- a/src/main/scala/firrtl/transforms/ConstantPropagation.scala +++ b/src/main/scala/firrtl/transforms/ConstantPropagation.scala @@ -207,7 +207,10 @@ class ConstantPropagation extends Transform with DependencyAPIMigration with Res } object FoldAND extends FoldCommutativeOp { - def fold(c1: Literal, c2: Literal) = UIntLiteral(c1.value & c2.value, c1.width max c2.width) + def fold(c1: Literal, c2: Literal) = { + val width = (c1.width max c2.width).asInstanceOf[IntWidth] + UIntLiteral.masked(c1.value & c2.value, width) + } def simplify(e: Expression, lhs: Literal, rhs: Expression) = lhs match { case UIntLiteral(v, w) if v == BigInt(0) => UIntLiteral(0, w) case SIntLiteral(v, w) if v == BigInt(0) => UIntLiteral(0, w) @@ -218,7 +221,10 @@ class ConstantPropagation extends Transform with DependencyAPIMigration with Res } object FoldOR extends FoldCommutativeOp { - def fold(c1: Literal, c2: Literal) = UIntLiteral(c1.value | c2.value, c1.width max c2.width) + def fold(c1: Literal, c2: Literal) = { + val width = (c1.width max c2.width).asInstanceOf[IntWidth] + UIntLiteral.masked((c1.value | c2.value), width) + } def simplify(e: Expression, lhs: Literal, rhs: Expression) = lhs match { case UIntLiteral(v, _) if v == BigInt(0) => rhs case SIntLiteral(v, _) if v == BigInt(0) => asUInt(rhs, e.tpe) @@ -229,7 +235,10 @@ class ConstantPropagation extends Transform with DependencyAPIMigration with Res } object FoldXOR extends FoldCommutativeOp { - def fold(c1: Literal, c2: Literal) = UIntLiteral(c1.value ^ c2.value, c1.width max c2.width) + def fold(c1: Literal, c2: Literal) = { + val width = (c1.width max c2.width).asInstanceOf[IntWidth] + UIntLiteral.masked((c1.value ^ c2.value), width) + } def simplify(e: Expression, lhs: Literal, rhs: Expression) = lhs match { case UIntLiteral(v, _) if v == BigInt(0) => rhs case SIntLiteral(v, _) if v == BigInt(0) => asUInt(rhs, e.tpe) -- cgit v1.2.3