title: Does the Chisel/FIRRTL toolchain do boolean expression optimization?
body: <p>I am generating input to be compiled by Chisel.  Doing it the easy way might result in suboptimal boolean expressions.  For example, I tend to generate chains of nested Mux()-es, like this:</p>
  <pre><code>  x :=
      Mux(!a &amp;&amp; !b &amp;&amp; !c &amp;&amp; d, 13,
          Mux(!a &amp;&amp; !b &amp;&amp; c, 3,
              Mux(!a &amp;&amp; !b, 2,
                  Mux(!a &amp;&amp; b, temp1,
                      Mux(a &amp;&amp; e, 11,
                          Mux(a, 0,
                              -1))))))
  </code></pre>
  <p>As you can see,</p>
  <ol>
  <li><p>some of the boolean expressions are repeated, such as &quot;!a&quot;, so likely some optimization could be done to express the same function using fewer evaluations, such as common sub-expression elimination,</p>
  </li>
  <li><p>tests are repeated, again, such as &quot;!a&quot;, so likely some optimization could be done to factor that out and test it once instead, and</p>
  </li>
  <li><p>similar to point 2 above, the expression is very deep, so likely some optimization could be done to make it more like a tree and less like a linear sequence of Mux-es.</p>
  </li>
  </ol>
  <p>One thing I do not do is have complex predicate expressions: every predicate is just a conjunction of terms and each term is just a var or its negation.</p>
  <p>I could try to implement these kind of transforms in my code generator, but in doing so I would end up writing my own optimizing compiler for boolean expressions.  Instead can I just generate the above and rely on the Chisel/FIRRTL toolchain to optimize boolean expressions of this level of complexity?  Such expressions are likely to be about the size of the one above or up to maybe twice its size.</p>