diff options
| author | Jack Koenig | 2019-11-29 23:08:38 -0800 |
|---|---|---|
| committer | Jack Koenig | 2020-01-07 18:35:42 -0800 |
| commit | df48d61c3e1cb476f51762b1f009ecc9391221c6 (patch) | |
| tree | 4991282dac9f3511c71af9435936a29551de6617 /src | |
| parent | 66f354558a21cd0d339968b3665b44c17c2c16e8 (diff) | |
Remove unnecessary casts in Constant Propagation
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/scala/firrtl/transforms/ConstantPropagation.scala | 31 | ||||
| -rw-r--r-- | src/test/scala/firrtlTests/ConstantPropagationTests.scala | 26 |
2 files changed, 48 insertions, 9 deletions
diff --git a/src/main/scala/firrtl/transforms/ConstantPropagation.scala b/src/main/scala/firrtl/transforms/ConstantPropagation.scala index a008a4d3..20b24e60 100644 --- a/src/main/scala/firrtl/transforms/ConstantPropagation.scala +++ b/src/main/scala/firrtl/transforms/ConstantPropagation.scala @@ -286,16 +286,33 @@ class ConstantPropagation extends Transform with ResolvedAnnotationPaths { case UIntLiteral(v, IntWidth(w)) => UIntLiteral(v ^ ((BigInt(1) << w.toInt) - 1), IntWidth(w)) case _ => e } - case AsUInt => e.args.head match { - case SIntLiteral(v, IntWidth(w)) => UIntLiteral(v + (if (v < 0) BigInt(1) << w.toInt else 0), IntWidth(w)) - case u: UIntLiteral => u - case _ => e - } + case AsUInt => + e.args.head match { + case SIntLiteral(v, IntWidth(w)) => UIntLiteral(v + (if (v < 0) BigInt(1) << w.toInt else 0), IntWidth(w)) + case arg => arg.tpe match { + case _: UIntType => arg + case _ => e + } + } case AsSInt => e.args.head match { case UIntLiteral(v, IntWidth(w)) => SIntLiteral(v - ((v >> (w.toInt-1)) << w.toInt), IntWidth(w)) - case s: SIntLiteral => s - case _ => e + case arg => arg.tpe match { + case _: SIntType => arg + case _ => e + } } + case AsClock => + val arg = e.args.head + arg.tpe match { + case ClockType => arg + case _ => e + } + case AsAsyncReset => + val arg = e.args.head + arg.tpe match { + case AsyncResetType => arg + case _ => e + } case Pad => e.args.head match { case UIntLiteral(v, IntWidth(w)) => UIntLiteral(v, IntWidth(e.consts.head max w)) case SIntLiteral(v, IntWidth(w)) => SIntLiteral(v, IntWidth(e.consts.head max w)) diff --git a/src/test/scala/firrtlTests/ConstantPropagationTests.scala b/src/test/scala/firrtlTests/ConstantPropagationTests.scala index 71709255..af186cda 100644 --- a/src/test/scala/firrtlTests/ConstantPropagationTests.scala +++ b/src/test/scala/firrtlTests/ConstantPropagationTests.scala @@ -3,8 +3,6 @@ package firrtlTests import firrtl._ -import firrtl.ir.Circuit -import firrtl.Parser.IgnoreInfo import firrtl.passes._ import firrtl.transforms._ @@ -824,6 +822,30 @@ class ConstantPropagationSingleModule extends ConstantPropagationSpec { """.stripMargin (parse(exec(input))) should be(parse(check)) } + + def castCheck(tpe: String, cast: String): Unit = { + val input = + s"""circuit Top : + | module Top : + | input x : $tpe + | output z : $tpe + | z <= $cast(x) + """.stripMargin + val check = + s"""circuit Top : + | module Top : + | input x : $tpe + | output z : $tpe + | z <= x + """.stripMargin + (parse(exec(input)).serialize) should be (parse(check).serialize) + } + it should "optimize unnecessary casts" in { + castCheck("UInt<4>", "asUInt") + castCheck("SInt<4>", "asSInt") + castCheck("Clock", "asClock") + castCheck("AsyncReset", "asAsyncReset") + } } // More sophisticated tests of the full compiler |
