aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/passes/RemoveValidIf.scala
blob: dc4e70ff3548ccb673f264aa2ee8739b764b6697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// SPDX-License-Identifier: Apache-2.0

package firrtl
package passes

import firrtl.Mappers._
import firrtl.ir._
import Utils.throwInternalError
import firrtl.options.Dependency

/** Remove [[firrtl.ir.ValidIf ValidIf]] and replace [[firrtl.ir.IsInvalid IsInvalid]] with a connection to zero */
object RemoveValidIf extends Pass {

  val UIntZero = Utils.zero
  val SIntZero = SIntLiteral(BigInt(0), IntWidth(1))
  val ClockZero = DoPrim(PrimOps.AsClock, Seq(UIntZero), Seq.empty, ClockType)
  val FixedZero = FixedLiteral(BigInt(0), IntWidth(1), IntWidth(0))
  val AsyncZero = DoPrim(PrimOps.AsAsyncReset, Seq(UIntZero), Nil, AsyncResetType)

  /** Returns an [[firrtl.ir.Expression Expression]] equal to zero for a given [[firrtl.ir.GroundType GroundType]]
    * @note Accepts [[firrtl.ir.Type Type]] but dyanmically expects [[firrtl.ir.GroundType GroundType]]
    */
  def getGroundZero(tpe: Type): Expression = tpe match {
    case g: GroundType => Utils.getGroundZero(g)
    case other => throwInternalError(s"Unexpected type $other")
  }

  override def prerequisites = firrtl.stage.Forms.LowForm

  override def optionalPrerequisiteOf =
    Seq(Dependency[SystemVerilogEmitter], Dependency[VerilogEmitter])

  override def invalidates(a: Transform): Boolean = a match {
    case _: firrtl.transforms.ConstantPropagation => true // switching out the validifs allows for more constant prop
    case _ => false
  }

  // Recursive. Removes ValidIfs
  private def onExp(e: Expression): Expression = {
    e.map(onExp) match {
      case ValidIf(_, value, _) => value
      case x                    => x
    }
  }

  // Recursive. Replaces IsInvalid with connecting zero
  private def onStmt(s: Statement): Statement = s.map(onStmt).map(onExp) match {
    case invalid @ IsInvalid(info, loc) =>
      loc.tpe match {
        case _: AnalogType => EmptyStmt
        case tpe => Connect(info, loc, getGroundZero(tpe))
      }
    case other => other
  }

  private def onModule(m: DefModule): DefModule = {
    m match {
      case m: Module    => Module(m.info, m.name, m.ports, onStmt(m.body))
      case m: ExtModule => m
    }
  }

  def run(c: Circuit): Circuit = Circuit(c.info, c.modules.map(onModule), c.main)
}