diff options
| author | Albert Chen | 2020-07-23 11:14:35 -0700 |
|---|---|---|
| committer | GitHub | 2020-07-23 18:14:35 +0000 |
| commit | 1927dc6574b9eee315c8f24441df390f2ce793c7 (patch) | |
| tree | 8f942d8d4c7c46c59db476a4329d119d0ac77c79 /src/main/scala/firrtl/transforms/ConstantPropagation.scala | |
| parent | ea558ad79ed0e65df73b5a01ceea690e5b0479ca (diff) | |
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 <koenig@sifive.com>
Diffstat (limited to 'src/main/scala/firrtl/transforms/ConstantPropagation.scala')
| -rw-r--r-- | src/main/scala/firrtl/transforms/ConstantPropagation.scala | 15 |
1 files changed, 12 insertions, 3 deletions
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) |
