package org.graalvm.compiler.nodes.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.function.BiFunction;
import jdk.internal.vm.compiler.collections.EconomicMap;
import jdk.internal.vm.compiler.collections.EconomicSet;
import jdk.internal.vm.compiler.collections.Equivalence;
import jdk.internal.vm.compiler.collections.MapCursor;
import org.graalvm.compiler.bytecode.Bytecode;
import org.graalvm.compiler.code.SourceStackTraceBailoutException;
import org.graalvm.compiler.core.common.type.ObjectStamp;
import org.graalvm.compiler.debug.DebugContext;
import org.graalvm.compiler.graph.Graph;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeBitMap;
import org.graalvm.compiler.graph.NodeSourcePosition;
import org.graalvm.compiler.graph.NodeStack;
import org.graalvm.compiler.graph.Position;
import org.graalvm.compiler.graph.iterators.NodeIterable;
import org.graalvm.compiler.graph.spi.SimplifierTool;
import org.graalvm.compiler.nodes.AbstractBeginNode;
import org.graalvm.compiler.nodes.AbstractEndNode;
import org.graalvm.compiler.nodes.AbstractMergeNode;
import org.graalvm.compiler.nodes.ConstantNode;
import org.graalvm.compiler.nodes.ControlSinkNode;
import org.graalvm.compiler.nodes.ControlSplitNode;
import org.graalvm.compiler.nodes.FixedNode;
import org.graalvm.compiler.nodes.FixedWithNextNode;
import org.graalvm.compiler.nodes.FrameState;
import org.graalvm.compiler.nodes.GuardNode;
import org.graalvm.compiler.nodes.IfNode;
import org.graalvm.compiler.nodes.LoopBeginNode;
import org.graalvm.compiler.nodes.LoopEndNode;
import org.graalvm.compiler.nodes.LoopExitNode;
import org.graalvm.compiler.nodes.NodeView;
import org.graalvm.compiler.nodes.PhiNode;
import org.graalvm.compiler.nodes.PiNode;
import org.graalvm.compiler.nodes.ProxyNode;
import org.graalvm.compiler.nodes.StateSplit;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.ValuePhiNode;
import org.graalvm.compiler.nodes.ValueProxyNode;
import org.graalvm.compiler.nodes.WithExceptionNode;
import org.graalvm.compiler.nodes.java.LoadIndexedNode;
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
import org.graalvm.compiler.nodes.java.MonitorIdNode;
import org.graalvm.compiler.nodes.spi.ArrayLengthProvider;
import org.graalvm.compiler.nodes.spi.ArrayLengthProvider.FindLengthMode;
import org.graalvm.compiler.nodes.spi.CoreProviders;
import org.graalvm.compiler.nodes.spi.CoreProvidersDelegate;
import org.graalvm.compiler.nodes.spi.LimitedValueProxy;
import org.graalvm.compiler.nodes.spi.ValueProxy;
import org.graalvm.compiler.nodes.spi.VirtualizerTool;
import org.graalvm.compiler.nodes.type.StampTool;
import org.graalvm.compiler.nodes.virtual.VirtualArrayNode;
import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
import org.graalvm.compiler.options.Option;
import org.graalvm.compiler.options.OptionKey;
import org.graalvm.compiler.options.OptionType;
import org.graalvm.compiler.options.OptionValues;
import jdk.vm.ci.code.BailoutException;
import jdk.vm.ci.code.BytecodePosition;
import jdk.vm.ci.meta.Assumptions;
import jdk.vm.ci.meta.Constant;
import jdk.vm.ci.meta.ConstantReflectionProvider;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
public class GraphUtil {
public static class Options {
@Option(help = "Verify that there are no new unused nodes when performing killCFG", type = OptionType.Debug)
public static final OptionKey<Boolean> VerifyKillCFGUnusedNodes = new OptionKey<>(false);
}
public static final int MAX_FRAMESTATE_SEARCH_DEPTH = 4;
private static void killCFGInner(FixedNode node) {
EconomicSet<Node> markedNodes = EconomicSet.create();
EconomicMap<AbstractMergeNode, List<AbstractEndNode>> unmarkedMerges = EconomicMap.create();
node.replaceAtPredecessor(null);
markFixedNodes(node, markedNodes, unmarkedMerges);
fixSurvivingAffectedMerges(markedNodes, unmarkedMerges);
DebugContext debug = node.getDebug();
debug.dump(DebugContext.DETAILED_LEVEL, node.graph(), "After fixing merges (killCFG %s)", node);
markUsages(markedNodes);
for (Node marked : markedNodes) {
for (Node input : marked.inputs()) {
if (!markedNodes.contains(input)) {
marked.replaceFirstInput(input, null);
tryKillUnused(input);
}
}
}
debug.dump(DebugContext.VERY_DETAILED_LEVEL, node.graph(), "After disconnecting non-marked inputs (killCFG %s)", node);
for (Node marked : markedNodes) {
if (marked.isAlive()) {
marked.markDeleted();
}
}
}
private static void markFixedNodes(FixedNode node, EconomicSet<Node> markedNodes, EconomicMap<AbstractMergeNode, List<AbstractEndNode>> unmarkedMerges) {
NodeStack workStack = new NodeStack();
workStack.push(node);
while (!workStack.isEmpty()) {
Node fixedNode = workStack.pop();
markedNodes.add(fixedNode);
if (fixedNode instanceof AbstractMergeNode) {
unmarkedMerges.removeKey((AbstractMergeNode) fixedNode);
}
while (fixedNode instanceof FixedWithNextNode) {
fixedNode = ((FixedWithNextNode) fixedNode).next();
if (fixedNode != null) {
markedNodes.add(fixedNode);
}
}
if (fixedNode instanceof ControlSplitNode) {
for (Node successor : fixedNode.successors()) {
workStack.push(successor);
}
} else if (fixedNode instanceof AbstractEndNode) {
AbstractEndNode end = (AbstractEndNode) fixedNode;
AbstractMergeNode merge = end.merge();
if (merge != null) {
assert !markedNodes.contains(merge) || (merge instanceof LoopBeginNode && end instanceof LoopEndNode) : merge;
if (merge instanceof LoopBeginNode) {
if (end == ((LoopBeginNode) merge).forwardEnd()) {
workStack.push(merge);
continue;
}
if (markedNodes.contains(merge)) {
continue;
}
}
List<AbstractEndNode> endsSeen = unmarkedMerges.get(merge);
if (endsSeen == null) {
endsSeen = new ArrayList<>(merge.forwardEndCount());
unmarkedMerges.put(merge, endsSeen);
}
endsSeen.add(end);
if (!(end instanceof LoopEndNode) && endsSeen.size() == merge.forwardEndCount()) {
assert merge.forwardEnds().filter(n -> !markedNodes.contains(n)).isEmpty();
workStack.push(merge);
}
}
}
}
}
private static void fixSurvivingAffectedMerges(EconomicSet<Node> markedNodes, EconomicMap<AbstractMergeNode, List<AbstractEndNode>> unmarkedMerges) {
MapCursor<AbstractMergeNode, List<AbstractEndNode>> cursor = unmarkedMerges.getEntries();
while (cursor.advance()) {
AbstractMergeNode merge = cursor.getKey();
for (AbstractEndNode end : cursor.getValue()) {
merge.removeEnd(end);
}
if (merge.phiPredecessorCount() == 1) {
if (merge instanceof LoopBeginNode) {
LoopBeginNode loopBegin = (LoopBeginNode) merge;
assert merge.forwardEndCount() == 1;
for (LoopExitNode loopExit : loopBegin.loopExits().snapshot()) {
if (markedNodes.contains(loopExit)) {
loopExit.replaceFirstInput(loopBegin, null);
}
}
merge.graph().reduceDegenerateLoopBegin(loopBegin);
} else {
merge.graph().reduceTrivialMerge(merge);
}
} else {
assert merge.phiPredecessorCount() > 1 : merge;
}
}
}
private static void markUsages(EconomicSet<Node> markedNodes) {
NodeStack workStack = new NodeStack(markedNodes.size() + 4);
for (Node marked : markedNodes) {
workStack.push(marked);
}
while (!workStack.isEmpty()) {
Node marked = workStack.pop();
for (Node usage : marked.usages()) {
if (!markedNodes.contains(usage)) {
workStack.push(usage);
markedNodes.add(usage);
}
}
}
}
@SuppressWarnings("try")
public static void killCFG(FixedNode node) {
DebugContext debug = node.getDebug();
try (DebugContext.Scope scope = debug.scope("KillCFG", node)) {
EconomicSet<Node> unusedNodes = null;
EconomicSet<Node> unsafeNodes = null;
Graph.NodeEventScope nodeEventScope = null;
OptionValues options = node.getOptions();
boolean verifyGraalGraphEdges = Graph.Options.VerifyGraalGraphEdges.getValue(options);
boolean verifyKillCFGUnusedNodes = GraphUtil.Options.VerifyKillCFGUnusedNodes.getValue(options);
if (verifyGraalGraphEdges) {
unsafeNodes = collectUnsafeNodes(node.graph());
}
if (verifyKillCFGUnusedNodes) {
EconomicSet<Node> collectedUnusedNodes = unusedNodes = EconomicSet.create(Equivalence.IDENTITY);
nodeEventScope = node.graph().trackNodeEvents(new Graph.NodeEventListener() {
@Override
public void changed(Graph.NodeEvent e, Node n) {
if (e == Graph.NodeEvent.ZERO_USAGES && isFloatingNode(n) && !(n instanceof GuardNode)) {
collectedUnusedNodes.add(n);
}
}
});
}
debug.dump(DebugContext.VERY_DETAILED_LEVEL, node.graph(), "Before killCFG %s", node);
killCFGInner(node);
debug.dump(DebugContext.VERY_DETAILED_LEVEL, node.graph(), "After killCFG %s", node);
if (verifyGraalGraphEdges) {
EconomicSet<Node> newUnsafeNodes = collectUnsafeNodes(node.graph());
newUnsafeNodes.removeAll(unsafeNodes);
assert newUnsafeNodes.isEmpty() : "New unsafe nodes: " + newUnsafeNodes;
}
if (verifyKillCFGUnusedNodes) {
nodeEventScope.close();
Iterator<Node> iterator = unusedNodes.iterator();
while (iterator.hasNext()) {
Node curNode = iterator.next();
if (curNode.isDeleted()) {
iterator.remove();
}
}
assert unusedNodes.isEmpty() : "New unused nodes: " + unusedNodes;
}
} catch (Throwable t) {
throw debug.handle(t);
}
}
private static EconomicSet<Node> collectUnsafeNodes(Graph graph) {
EconomicSet<Node> unsafeNodes = EconomicSet.create(Equivalence.IDENTITY);
for (Node n : graph.getNodes()) {
for (Position pos : n.inputPositions()) {
Node input = pos.get(n);
if (input == null) {
if (!pos.isInputOptional()) {
unsafeNodes.add(n);
}
}
}
}
return unsafeNodes;
}
public static boolean isFloatingNode(Node n) {
return !(n instanceof FixedNode);
}
private static boolean checkKill(Node node, boolean mayKillGuard) {
node.assertTrue(mayKillGuard || !(node instanceof GuardNode), "must not be a guard node %s", node);
node.assertTrue(node.isAlive(), "must be alive");
node.assertTrue(node.hasNoUsages(), "cannot kill node %s because of usages: %s", node, node.usages());
node.assertTrue(node.predecessor() == null, "cannot kill node %s because of predecessor: %s", node, node.predecessor());
return true;
}
public static void killWithUnusedFloatingInputs(Node node) {
killWithUnusedFloatingInputs(node, false);
}
public static void killWithUnusedFloatingInputs(Node node, boolean mayKillGuard) {
assert checkKill(node, mayKillGuard);
node.markDeleted();
outer: for (Node in : node.inputs()) {
if (in.isAlive()) {
in.removeUsage(node);
if (in.hasNoUsages()) {
node.maybeNotifyZeroUsages(in);
}
if (isFloatingNode(in)) {
if (in.hasNoUsages()) {
if (in instanceof GuardNode) {
} else {
killWithUnusedFloatingInputs(in);
}
} else if (in instanceof PhiNode) {
for (Node use : in.usages()) {
if (use != in) {
continue outer;
}
}
in.replaceAtUsages(null);
killWithUnusedFloatingInputs(in);
}
}
}
}
}
public static void removeNewNodes(Graph graph, Graph.Mark mark) {
assert checkNoOldToNewEdges(graph, mark);
for (Node n : graph.getNewNodes(mark)) {
n.markDeleted();
for (Node in : n.inputs()) {
in.removeUsage(n);
}
}
}
private static boolean checkNoOldToNewEdges(Graph graph, Graph.Mark mark) {
for (Node old : graph.getNodes()) {
if (graph.isNew(mark, old)) {
break;
}
for (Node n : old.successors()) {
assert !graph.isNew(mark, n) : old + " -> " + n;
}
for (Node n : old.inputs()) {
assert !graph.isNew(mark, n) : old + " -> " + n;
}
}
return true;
}
public static void removeFixedWithUnusedInputs(FixedWithNextNode fixed) {
if (fixed instanceof StateSplit) {
FrameState stateAfter = ((StateSplit) fixed).stateAfter();
if (stateAfter != null) {
((StateSplit) fixed).setStateAfter(null);
if (stateAfter.hasNoUsages()) {
killWithUnusedFloatingInputs(stateAfter);
}
}
}
unlinkFixedNode(fixed);
killWithUnusedFloatingInputs(fixed);
}
public static void unlinkFixedNode(FixedWithNextNode fixed) {
assert fixed.next() != null && fixed.predecessor() != null && fixed.isAlive() : fixed;
FixedNode next = fixed.next();
fixed.setNext(null);
fixed.replaceAtPredecessor(next);
}
public static void unlinkAndKillExceptionEdge(WithExceptionNode withException) {
assert withException.next() != null && withException.predecessor() != null && withException.isAlive() : withException;
FixedNode next = withException.next();
withException.setNext(null);
withException.replaceAtPredecessor(next);
withException.killExceptionEdge();
}
public static void checkRedundantPhi(PhiNode phiNode) {
if (phiNode.isDeleted() || phiNode.valueCount() == 1) {
return;
}
ValueNode singleValue = phiNode.singleValueOrThis();
if (singleValue != phiNode) {
Collection<PhiNode> phiUsages = phiNode.usages().filter(PhiNode.class).snapshot();
Collection<ProxyNode> proxyUsages = phiNode.usages().filter(ProxyNode.class).snapshot();
phiNode.replaceAtUsagesAndDelete(singleValue);
for (PhiNode phi : phiUsages) {
checkRedundantPhi(phi);
}
for (ProxyNode proxy : proxyUsages) {
checkRedundantProxy(proxy);
}
}
}
public static void checkRedundantProxy(ProxyNode vpn) {
if (vpn.isDeleted()) {
return;
}
AbstractBeginNode proxyPoint = vpn.proxyPoint();
if (proxyPoint instanceof LoopExitNode) {
LoopExitNode exit = (LoopExitNode) proxyPoint;
LoopBeginNode loopBegin = exit.loopBegin();
Node vpnValue = vpn.value();
for (ValueNode v : loopBegin.stateAfter().values()) {
ValueNode v2 = v;
if (loopBegin.isPhiAtMerge(v2)) {
v2 = ((PhiNode) v2).valueAt(loopBegin.forwardEnd());
}
if (vpnValue == v2) {
Collection<PhiNode> phiUsages = vpn.usages().filter(PhiNode.class).snapshot();
Collection<ProxyNode> proxyUsages = vpn.usages().filter(ProxyNode.class).snapshot();
vpn.replaceAtUsagesAndDelete(vpnValue);
for (PhiNode phi : phiUsages) {
checkRedundantPhi(phi);
}
for (ProxyNode proxy : proxyUsages) {
checkRedundantProxy(proxy);
}
return;
}
}
}
}
public static void normalizeLoops(StructuredGraph graph) {
boolean loopRemoved = false;
for (LoopBeginNode begin : graph.getNodes(LoopBeginNode.TYPE)) {
if (begin.loopEnds().isEmpty()) {
assert begin.forwardEndCount() == 1;
graph.reduceDegenerateLoopBegin(begin);
loopRemoved = true;
} else {
normalizeLoopBegin(begin);
}
}
if (loopRemoved) {
for (Node node : graph.getNodes()) {
if (node instanceof PhiNode) {
checkRedundantPhi((PhiNode) node);
}
}
}
}
private static void normalizeLoopBegin(LoopBeginNode begin) {
for (PhiNode phi : begin.phis().snapshot()) {
GraphUtil.checkRedundantPhi(phi);
}
for (LoopExitNode exit : begin.loopExits()) {
for (ProxyNode vpn : exit.proxies().snapshot()) {
GraphUtil.checkRedundantProxy(vpn);
}
}
}
public static StackTraceElement[] approxSourceStackTraceElement(Node node) {
NodeSourcePosition position = node.getNodeSourcePosition();
if (position != null) {
return approxSourceStackTraceElement(position);
}
ArrayList<StackTraceElement> elements = new ArrayList<>();
Node n = node;
while (n != null) {
if (n instanceof MethodCallTargetNode) {
elements.add(((MethodCallTargetNode) n).targetMethod().asStackTraceElement(-1));
n = ((MethodCallTargetNode) n).invoke().asNode();
}
if (n instanceof StateSplit) {
FrameState state = ((StateSplit) n).stateAfter();
elements.addAll(Arrays.asList(approxSourceStackTraceElement(state)));
break;
}
n = n.predecessor();
}
return elements.toArray(new StackTraceElement[elements.size()]);
}
public static StackTraceElement[] approxSourceStackTraceElement(FrameState frameState) {
ArrayList<StackTraceElement> elements = new ArrayList<>();
FrameState state = frameState;
while (state != null) {
Bytecode code = state.getCode();
if (code != null) {
elements.add(code.asStackTraceElement(state.bci - 1));
}
state = state.outerFrameState();
}
return elements.toArray(new StackTraceElement[0]);
}
public static StackTraceElement[] approxSourceStackTraceElement(BytecodePosition bytecodePosition) {
ArrayList<StackTraceElement> elements = new ArrayList<>();
BytecodePosition position = bytecodePosition;
while (position != null) {
ResolvedJavaMethod method = position.getMethod();
if (method != null) {
elements.add(method.asStackTraceElement(position.getBCI()));
}
position = position.getCaller();
}
return elements.toArray(new StackTraceElement[0]);
}
public static RuntimeException approxSourceException(Node node, Throwable cause) {
final StackTraceElement[] elements = approxSourceStackTraceElement(node);
return createBailoutException(cause == null ? "" : cause.getMessage(), cause, elements);
}
public static BailoutException createBailoutException(String message, Throwable cause, StackTraceElement[] elements) {
return SourceStackTraceBailoutException.create(cause, message, elements);
}
public static String approxSourceLocation(Node node) {
StackTraceElement[] stackTraceElements = approxSourceStackTraceElement(node);
if (stackTraceElements != null && stackTraceElements.length > 0) {
StackTraceElement top = stackTraceElements[0];
if (top.getFileName() != null && top.getLineNumber() >= 0) {
return top.getFileName() + ":" + top.getLineNumber();
}
}
return null;
}
public static String toString(Iterable<?> objects) {
StringBuilder str = new StringBuilder();
str.append("[");
for (Object o : objects) {
str.append(o).append(", ");
}
if (str.length() > 1) {
str.setLength(str.length() - 2);
}
str.append("]");
return str.toString();
}
public static ValueNode unproxify(ValueNode value) {
if (value instanceof ValueProxy) {
return unproxify((ValueProxy) value);
} else {
return value;
}
}
public static ValueNode unproxify(ValueProxy value) {
if (value != null) {
ValueNode result = value.getOriginalNode();
while (result instanceof ValueProxy) {
result = ((ValueProxy) result).getOriginalNode();
}
return result;
} else {
return null;
}
}
public static ValueNode skipPi(ValueNode node) {
ValueNode n = node;
while (n instanceof PiNode) {
PiNode piNode = (PiNode) n;
n = piNode.getOriginalNode();
}
return n;
}
public static ValueNode skipPiWhileNonNull(ValueNode node) {
ValueNode n = node;
while (n instanceof PiNode) {
PiNode piNode = (PiNode) n;
ObjectStamp originalStamp = (ObjectStamp) piNode.getOriginalNode().stamp(NodeView.DEFAULT);
if (originalStamp.nonNull()) {
n = piNode.getOriginalNode();
} else {
break;
}
}
return n;
}
public static ValueNode arrayLength(ValueNode value, FindLengthMode mode, ConstantReflectionProvider constantReflection) {
Objects.requireNonNull(mode);
ValueNode current = value;
do {
if (current instanceof ArrayLengthProvider) {
return ((ArrayLengthProvider) current).findLength(mode, constantReflection);
} else if (current instanceof ValuePhiNode) {
return phiArrayLength((ValuePhiNode) current, mode, constantReflection);
} else if (current instanceof ValueProxyNode) {
ValueProxyNode proxy = (ValueProxyNode) current;
ValueNode length = arrayLength(proxy.getOriginalNode(), mode, constantReflection);
if (mode == ArrayLengthProvider.FindLengthMode.CANONICALIZE_READ && length != null && !length.isConstant()) {
length = new ValueProxyNode(length, proxy.proxyPoint());
}
return length;
} else if (current instanceof ValueProxy) {
current = ((ValueProxy) current).getOriginalNode();
} else {
return null;
}
} while (true);
}
private static ValueNode phiArrayLength(ValuePhiNode phi, ArrayLengthProvider.FindLengthMode mode, ConstantReflectionProvider constantReflection) {
if (phi.merge() instanceof LoopBeginNode) {
return null;
}
ValueNode singleLength = null;
for (int i = 0; i < phi.values().count(); i++) {
ValueNode input = phi.values().get(i);
ValueNode length = arrayLength(input, mode, constantReflection);
if (length == null) {
return null;
}
assert length.stamp(NodeView.DEFAULT).getStackKind() == JavaKind.Int;
if (i == 0) {
assert singleLength == null;
singleLength = length;
} else if (singleLength == length) {
} else {
return null;
}
}
return singleLength;
}
public static ValueNode originalValue(ValueNode value, boolean abortOnLoopPhi) {
ValueNode result = originalValueSimple(value, abortOnLoopPhi);
assert result != null;
return result;
}
private static ValueNode originalValueSimple(ValueNode value, boolean abortOnLoopPhi) {
ValueNode cur = originalValueForProxy(value);
while (cur instanceof PhiNode) {
PhiNode phi = (PhiNode) cur;
if (abortOnLoopPhi && phi.isLoopPhi()) {
return value;
}
ValueNode phiSingleValue = null;
int count = phi.valueCount();
for (int i = 0; i < count; ++i) {
ValueNode phiCurValue = originalValueForProxy(phi.valueAt(i));
if (phiCurValue == phi) {
} else if (phiSingleValue == null) {
phiSingleValue = phiCurValue;
} else if (phiSingleValue != phiCurValue) {
if (phiSingleValue instanceof PhiNode || phiCurValue instanceof PhiNode) {
return originalValueForComplicatedPhi(value, phi, new NodeBitMap(value.graph()), abortOnLoopPhi);
} else {
return phi;
}
}
}
assert phiSingleValue != null;
cur = phiSingleValue;
}
assert !(cur instanceof LimitedValueProxy) && !(cur instanceof PhiNode);
return cur;
}
private static ValueNode originalValueForProxy(ValueNode value) {
ValueNode cur = value;
while (cur instanceof LimitedValueProxy) {
cur = ((LimitedValueProxy) cur).getOriginalNode();
}
return cur;
}
private static ValueNode originalValueForComplicatedPhi(ValueNode value, PhiNode phi, NodeBitMap visited, boolean abortOnLoopPhi) {
if (visited.isMarked(phi)) {
return null;
}
visited.mark(phi);
ValueNode phiSingleValue = null;
int count = phi.valueCount();
for (int i = 0; i < count; ++i) {
ValueNode phiCurValue = originalValueForProxy(phi.valueAt(i));
if (phiCurValue instanceof PhiNode) {
PhiNode curPhi = (PhiNode) phiCurValue;
if (abortOnLoopPhi && curPhi.isLoopPhi()) {
return value;
}
phiCurValue = originalValueForComplicatedPhi(value, curPhi, visited, abortOnLoopPhi);
if (phiCurValue == value) {
assert abortOnLoopPhi;
return value;
}
}
if (phiCurValue == null) {
} else if (phiSingleValue == null) {
phiSingleValue = phiCurValue;
} else if (phiCurValue != phiSingleValue) {
return phi;
}
}
return phiSingleValue;
}
public static boolean tryKillUnused(Node node) {
if (node.isAlive() && isFloatingNode(node) && node.hasNoUsages() && !(node instanceof GuardNode)) {
killWithUnusedFloatingInputs(node);
return true;
}
return false;
}
public static NodeIterable<FixedNode> predecessorIterable(final FixedNode start) {
return new NodeIterable<FixedNode>() {
@Override
public Iterator<FixedNode> iterator() {
return new Iterator<FixedNode>() {
public FixedNode current = start;
@Override
public boolean hasNext() {
return current != null;
}
@Override
public FixedNode next() {
try {
return current;
} finally {
current = (FixedNode) current.predecessor();
}
}
};
}
};
}
private static final class DefaultSimplifierTool extends CoreProvidersDelegate implements SimplifierTool {
private final boolean canonicalizeReads;
private final Assumptions assumptions;
private final OptionValues options;
DefaultSimplifierTool(CoreProviders providers, boolean canonicalizeReads, Assumptions assumptions, OptionValues options) {
super(providers);
this.canonicalizeReads = canonicalizeReads;
this.assumptions = assumptions;
this.options = options;
}
@Override
public boolean canonicalizeReads() {
return canonicalizeReads;
}
@Override
public boolean allUsagesAvailable() {
return true;
}
@Override
public void deleteBranch(Node branch) {
FixedNode fixedBranch = (FixedNode) branch;
fixedBranch.predecessor().replaceFirstSuccessor(fixedBranch, null);
GraphUtil.killCFG(fixedBranch);
}
@Override
public void removeIfUnused(Node node) {
GraphUtil.tryKillUnused(node);
}
@Override
public void addToWorkList(Node node) {
}
@Override
public void addToWorkList(Iterable<? extends Node> nodes) {
}
@Override
public Assumptions getAssumptions() {
return assumptions;
}
@Override
public OptionValues getOptions() {
return options;
}
@Override
public Integer smallestCompareWidth() {
if (getLowerer() != null) {
return getLowerer().smallestCompareWidth();
} else {
return null;
}
}
}
public static SimplifierTool getDefaultSimplifier(CoreProviders providers, boolean canonicalizeReads, Assumptions assumptions, OptionValues options) {
return new DefaultSimplifierTool(providers, canonicalizeReads, assumptions, options);
}
public static Constant foldIfConstantAndRemove(ValueNode node, ValueNode constant) {
assert node.inputs().contains(constant);
if (constant.isConstant()) {
node.replaceFirstInput(constant, null);
Constant result = constant.asConstant();
tryKillUnused(constant);
return result;
}
return null;
}
public static void virtualizeArrayCopy(VirtualizerTool tool, ValueNode source, ValueNode sourceLength, ValueNode newLength, ValueNode from, ResolvedJavaType newComponentType, JavaKind elementKind,
StructuredGraph graph, BiFunction<ResolvedJavaType, Integer, VirtualArrayNode> virtualArrayProvider) {
ValueNode sourceAlias = tool.getAlias(source);
ValueNode replacedSourceLength = tool.getAlias(sourceLength);
ValueNode replacedNewLength = tool.getAlias(newLength);
ValueNode replacedFrom = tool.getAlias(from);
if (!replacedNewLength.isConstant() || !replacedFrom.isConstant() || !replacedSourceLength.isConstant()) {
return;
}
assert newComponentType != null : "An array copy can be virtualized only if the real type of the resulting array is known statically.";
int fromInt = replacedFrom.asJavaConstant().asInt();
int newLengthInt = replacedNewLength.asJavaConstant().asInt();
int sourceLengthInt = replacedSourceLength.asJavaConstant().asInt();
if (sourceAlias instanceof VirtualObjectNode) {
VirtualObjectNode sourceVirtual = (VirtualObjectNode) sourceAlias;
assert sourceLengthInt == sourceVirtual.entryCount();
}
if (fromInt < 0 || newLengthInt < 0 || fromInt > sourceLengthInt) {
return;
}
if (newLengthInt > tool.getMaximumEntryCount()) {
return;
}
ValueNode[] newEntryState = new ValueNode[newLengthInt];
int readLength = Math.min(newLengthInt, sourceLengthInt - fromInt);
if (sourceAlias instanceof VirtualObjectNode) {
VirtualObjectNode sourceVirtual = (VirtualObjectNode) sourceAlias;
boolean alwaysAssignable = newComponentType.getJavaKind() == JavaKind.Object && newComponentType.isJavaLangObject();
for (int i = 0; i < readLength; i++) {
ValueNode entry = tool.getEntry(sourceVirtual, fromInt + i);
if (!alwaysAssignable) {
ResolvedJavaType entryType = StampTool.typeOrNull(entry, tool.getMetaAccess());
if (entryType == null) {
return;
}
if (!newComponentType.isAssignableFrom(entryType)) {
return;
}
}
newEntryState[i] = entry;
}
} else {
ResolvedJavaType sourceType = StampTool.typeOrNull(sourceAlias, tool.getMetaAccess());
if (sourceType == null || !sourceType.isArray() || !newComponentType.isAssignableFrom(sourceType.getElementalType())) {
return;
}
for (int i = 0; i < readLength; i++) {
LoadIndexedNode load = new LoadIndexedNode(null, sourceAlias, ConstantNode.forInt(i + fromInt, graph), null, elementKind);
tool.addNode(load);
newEntryState[i] = load;
}
}
if (readLength < newLengthInt) {
ValueNode defaultValue = ConstantNode.defaultForKind(elementKind, graph);
for (int i = readLength; i < newLengthInt; i++) {
newEntryState[i] = defaultValue;
}
}
VirtualArrayNode newVirtualArray = virtualArrayProvider.apply(newComponentType, newLengthInt);
tool.createVirtualObject(newVirtualArray, newEntryState, Collections.<MonitorIdNode> emptyList(), false);
tool.replaceWithVirtual(newVirtualArray);
}
public static boolean checkFrameState(FixedNode start, int maxDepth) {
if (maxDepth == 0) {
return false;
}
FixedNode node = start;
while (true) {
if (node instanceof AbstractMergeNode) {
AbstractMergeNode mergeNode = (AbstractMergeNode) node;
if (mergeNode.stateAfter() == null) {
return false;
} else {
return true;
}
} else if (node instanceof StateSplit) {
StateSplit stateSplitNode = (StateSplit) node;
if (stateSplitNode.stateAfter() != null) {
return true;
}
}
if (node instanceof ControlSplitNode) {
ControlSplitNode controlSplitNode = (ControlSplitNode) node;
for (Node succ : controlSplitNode.cfgSuccessors()) {
if (checkFrameState((FixedNode) succ, maxDepth - 1)) {
return true;
}
}
return false;
} else if (node instanceof FixedWithNextNode) {
FixedWithNextNode fixedWithNextNode = (FixedWithNextNode) node;
node = fixedWithNextNode.next();
} else if (node instanceof AbstractEndNode) {
AbstractEndNode endNode = (AbstractEndNode) node;
node = endNode.merge();
} else if (node instanceof ControlSinkNode) {
return true;
} else {
assert false : "unexpected node";
return false;
}
}
}
public static boolean mayRemoveSplit(IfNode ifNode) {
return GraphUtil.checkFrameState(ifNode.trueSuccessor(), MAX_FRAMESTATE_SEARCH_DEPTH) && GraphUtil.checkFrameState(ifNode.falseSuccessor(), MAX_FRAMESTATE_SEARCH_DEPTH);
}
}