Optimization passes =================== Yosys employs a number of optimizations to generate better and cleaner results. This chapter outlines these optimizations. .. todo:: "outlines these optimizations" or "outlines *some*.."? The :cmd:ref:`opt` macro command -------------------------------- The Yosys pass :cmd:ref:`opt` runs a number of simple optimizations. This includes removing unused signals and cells and const folding. It is recommended to run this pass after each major step in the synthesis script. As listed in :doc:`/cmd/opt`, this macro command calls the following ``opt_*`` commands: .. literalinclude:: /cmd/opt.rst :language: yoscrypt :start-after: following order: :end-at: while :dedent: :caption: Passes called by :cmd:ref:`opt` .. _adv_opt_expr: Constant folding and simple expression rewriting - :cmd:ref:`opt_expr` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This pass performs constant folding on the internal combinational cell types described in :doc:`/yosys_internals/formats/cell_library`. This means a cell with all constant inputs is replaced with the constant value this cell drives. In some cases this pass can also optimize cells with some constant inputs. .. table:: Const folding rules for ``$_AND_`` cells as used in :cmd:ref:`opt_expr`. :name: tab:opt_expr_and :align: center ========= ========= =========== A-Input B-Input Replacement ========= ========= =========== any 0 0 0 any 0 1 1 1 --------- --------- ----------- X/Z X/Z X 1 X/Z X X/Z 1 X --------- --------- ----------- any X/Z 0 X/Z any 0 --------- --------- ----------- :math:`a` 1 :math:`a` 1 :math:`b` :math:`b` ========= ========= =========== :numref:`Table %s ` shows the replacement rules used for optimizing an ``$_AND_`` gate. The first three rules implement the obvious const folding rules. Note that 'any' might include dynamic values calculated by other parts of the circuit. The following three lines propagate undef (X) states. These are the only three cases in which it is allowed to propagate an undef according to Sec. 5.1.10 of IEEE Std. 1364-2005 :cite:p:`Verilog2005`. The next two lines assume the value 0 for undef states. These two rules are only used if no other substitutions are possible in the current module. If other substitutions are possible they are performed first, in the hope that the 'any' will change to an undef value or a 1 and therefore the output can be set to undef. The last two lines simply replace an ``$_AND_`` gate with one constant-1 input with a buffer. Besides this basic const folding the :cmd:ref:`opt_expr` pass can replace 1-bit wide ``$eq`` and ``$ne`` cells with buffers or not-gates if one input is constant. Equality checks may also be reduced in size if there are redundant bits in the arguments (i.e. bits which are constant on both inputs). This can, for example, result in a 32-bit wide constant like ``255`` being reduced to the 8-bit value of ``8'11111111`` if the signal being compared is only 8-bit as in :ref:`addr_gen_clean` of :doc:`/getting_started/example_synth`. The :cmd:ref:`opt_expr` pass is very conservative regarding optimizing ``$mux`` cells, as these cells are often used to model decision-trees and breaking these trees can interfere with other optimizations. .. literalinclude:: /code_examples/opt/opt_expr.ys :language: Verilog :start-after: read_verilog <