1 /*
2  * Copyright (c) 2018 Helmut Neemann.
3  * Use of this source code is governed by the GPL v3 license
4  * that can be found in the LICENSE file.
5  */
6 package de.neemann.digital.hdl.model2.optimizations;
7 
8 import de.neemann.digital.hdl.model2.*;
9 
10 import java.util.Iterator;
11 
12 /**
13  * Inlines the inputs of the HDLNodeSplitterManyToOne.
14  */
15 public class InlineManyToOne implements Optimization {
16     @Override
optimize(HDLCircuit circuit)17     public void optimize(HDLCircuit circuit) {
18         Iterator<HDLNode> it = circuit.iterator();
19         while (it.hasNext()) {
20             HDLNode node = it.next();
21             if (node instanceof HDLNodeAssignment) {
22                 HDLNodeAssignment assign = (HDLNodeAssignment) node;
23                 final HDLNet net = assign.getTargetNet();
24                 if (net != null && net.getInputs().size() == 1) {
25                     HDLNode receiver = net.getInputs().get(0).getParent();
26                     if (receiver instanceof HDLNodeSplitterManyToOne) {
27                         HDLNodeSplitterManyToOne mto = (HDLNodeSplitterManyToOne) receiver;
28                         mto.replaceNetByExpression(net, assign.getExpression());
29                         it.remove();
30                         circuit.removeNet(net);
31                     }
32                 }
33             }
34         }
35     }
36 }
37