use of org.graalvm.compiler.nodes.PhiNode in project graal by oracle.
the class ValueMergeUtil method mergeValueProducers.
public static <T> ValueNode mergeValueProducers(AbstractMergeNode merge, List<? extends T> valueProducers, Function<T, FixedWithNextNode> lastInstrFunction, Function<T, ValueNode> valueFunction) {
ValueNode singleResult = null;
PhiNode phiResult = null;
for (T valueProducer : valueProducers) {
ValueNode result = valueFunction.apply(valueProducer);
if (result != null) {
if (phiResult == null && (singleResult == null || singleResult == result)) {
/* Only one result value, so no need yet for a phi node. */
singleResult = result;
} else if (phiResult == null) {
/* Found a second result value, so create phi node. */
phiResult = merge.graph().addWithoutUnique(new ValuePhiNode(result.stamp(NodeView.DEFAULT).unrestricted(), merge));
for (int i = 0; i < merge.forwardEndCount(); i++) {
phiResult.addInput(singleResult);
}
phiResult.addInput(result);
} else {
/* Multiple return values, just add to existing phi node. */
phiResult.addInput(result);
}
}
// create and wire up a new EndNode
EndNode endNode = merge.graph().add(new EndNode());
merge.addForwardEnd(endNode);
if (lastInstrFunction == null) {
assert valueProducer instanceof ReturnNode || valueProducer instanceof UnwindNode;
((ControlSinkNode) valueProducer).replaceAndDelete(endNode);
} else {
FixedWithNextNode lastInstr = lastInstrFunction.apply(valueProducer);
lastInstr.setNext(endNode);
}
}
if (phiResult != null) {
assert phiResult.verify();
phiResult.inferStamp();
return phiResult;
} else {
return singleResult;
}
}
Aggregations