package org.graalvm.compiler.nodes.util;
import static org.graalvm.compiler.graph.Graph.Options.VerifyGraalGraphEdges;
import static org.graalvm.compiler.nodes.util.GraphUtil.Options.VerifyKillCFGUnusedNodes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import org.graalvm.compiler.bytecode.Bytecode;
import org.graalvm.compiler.code.SourceStackTraceBailoutException;
import org.graalvm.compiler.core.common.CollectionsFactory;
import org.graalvm.compiler.core.common.spi.ConstantFieldProvider;
import org.graalvm.compiler.core.common.type.StampFactory;
import org.graalvm.compiler.debug.Debug;
import org.graalvm.compiler.graph.Graph;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeClass;
import org.graalvm.compiler.graph.NodeWorkList;
import org.graalvm.compiler.graph.Position;
import org.graalvm.compiler.graph.iterators.NodeIterable;
import org.graalvm.compiler.graph.spi.SimplifierTool;
import org.graalvm.compiler.nodeinfo.InputType;
import org.graalvm.compiler.nodeinfo.NodeInfo;
import org.graalvm.compiler.nodes.AbstractBeginNode;
import org.graalvm.compiler.nodes.AbstractEndNode;
import org.graalvm.compiler.nodes.AbstractMergeNode;
import org.graalvm.compiler.nodes.FixedNode;
import org.graalvm.compiler.nodes.FixedWithNextNode;
import org.graalvm.compiler.nodes.FrameState;
import org.graalvm.compiler.nodes.LoopBeginNode;
import org.graalvm.compiler.nodes.LoopEndNode;
import org.graalvm.compiler.nodes.LoopExitNode;
import org.graalvm.compiler.nodes.PhiNode;
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.calc.FloatingNode;
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
import org.graalvm.compiler.nodes.spi.ArrayLengthProvider;
import org.graalvm.compiler.nodes.spi.LimitedValueProxy;
import org.graalvm.compiler.nodes.spi.LoweringProvider;
import org.graalvm.compiler.nodes.spi.ValueProxy;
import org.graalvm.compiler.options.Option;
import org.graalvm.compiler.options.OptionType;
import org.graalvm.compiler.options.OptionValue;
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.MetaAccessProvider;
import jdk.vm.ci.meta.ResolvedJavaMethod;
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 OptionValue<Boolean> VerifyKillCFGUnusedNodes = new OptionValue<>(false);
}
@SuppressWarnings("try")
public static void killCFG(FixedNode node, SimplifierTool tool) {
try (Debug.Scope scope = Debug.scope("KillCFG", node)) {
Set<Node> unusedNodes = null;
Set<Node> unsafeNodes = null;
Graph.NodeEventScope nodeEventScope = null;
if (VerifyGraalGraphEdges.getValue()) {
unsafeNodes = collectUnsafeNodes(node.graph());
}
if (VerifyKillCFGUnusedNodes.getValue()) {
Set<Node> collectedUnusedNodes = unusedNodes = CollectionsFactory.newSet();
nodeEventScope = node.graph().trackNodeEvents(new Graph.NodeEventListener() {
@Override
public void event(Graph.NodeEvent e, Node n) {
if (e == Graph.NodeEvent.ZERO_USAGES && isFloatingNode(n)) {
collectedUnusedNodes.add(n);
}
}
});
}
Debug.dump(Debug.VERY_DETAILED_LOG_LEVEL, node.graph(), "Before killCFG %s", node);
NodeWorkList worklist = killCFG(node, tool, null);
if (worklist != null) {
for (Node n : worklist) {
killCFG(n, tool, worklist);
}
}
if (VerifyGraalGraphEdges.getValue()) {
Set<Node> newUnsafeNodes = collectUnsafeNodes(node.graph());
newUnsafeNodes.removeAll(unsafeNodes);
assert newUnsafeNodes.isEmpty() : "New unsafe nodes: " + newUnsafeNodes;
}
if (VerifyKillCFGUnusedNodes.getValue()) {
nodeEventScope.close();
unusedNodes.removeIf(n -> n.isDeleted());
assert unusedNodes.isEmpty() : "New unused nodes: " + unusedNodes;
}
} catch (Throwable t) {
throw Debug.handle(t);
}
}
private static Set<Node> collectUnsafeNodes(Graph graph) {
Set<Node> unsafeNodes = CollectionsFactory.newSet();
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;
}
private static NodeWorkList killCFG(Node node, SimplifierTool tool, NodeWorkList worklist) {
NodeWorkList newWorklist = worklist;
if (node instanceof FixedNode) {
newWorklist = killCFGLinear((FixedNode) node, newWorklist, tool);
} else {
newWorklist = propagateKill(node, newWorklist);
Debug.dump(Debug.VERY_DETAILED_LOG_LEVEL, node.graph(), "killCFG (Floating) %s", node);
}
return newWorklist;
}
private static NodeWorkList killCFGLinear(FixedNode in, NodeWorkList worklist, SimplifierTool tool) {
NodeWorkList newWorklist = worklist;
FixedNode current = in;
while (current != null) {
FixedNode next = null;
assert current.isAlive();
if (current instanceof AbstractEndNode) {
AbstractEndNode end = (AbstractEndNode) current;
newWorklist = killEnd(end, newWorklist, tool);
} else if (current instanceof FixedWithNextNode) {
FixedWithNextNode fixedWithNext = (FixedWithNextNode) current;
assert fixedWithNext.successors().count() == 1 || fixedWithNext.successors().count() == 0;
assert fixedWithNext.successors().first() == fixedWithNext.next();
next = fixedWithNext.next();
} else {
Iterator<Node> successors = current.successors().iterator();
if (successors.hasNext()) {
Node first = successors.next();
if (!successors.hasNext()) {
next = (FixedNode) first;
} else {
if (newWorklist == null) {
newWorklist = in.graph().createNodeWorkList();
}
for (Node successor : current.successors()) {
newWorklist.add(successor);
if (successor instanceof LoopExitNode) {
LoopExitNode exit = (LoopExitNode) successor;
exit.replaceFirstInput(exit.loopBegin(), null);
}
}
}
}
}
current.replaceAtPredecessor(null);
newWorklist = propagateKill(current, newWorklist);
Debug.dump(Debug.VERY_DETAILED_LOG_LEVEL, current.graph(), "killCFGLinear %s", current);
current = next;
}
Debug.dump(Debug.DETAILED_LOG_LEVEL, in.graph(), "killCFGLinear %s", in);
return newWorklist;
}
public static void killCFG(FixedNode node) {
killCFG(node, null);
}
@NodeInfo(allowedUsageTypes = InputType.Unchecked)
private static final class PoisonNode extends FloatingNode {
public static final NodeClass<PoisonNode> TYPE = NodeClass.create(PoisonNode.class);
protected PoisonNode() {
super(TYPE, StampFactory.forVoid());
}
}
private static NodeWorkList killEnd(AbstractEndNode end, NodeWorkList worklist, SimplifierTool tool) {
NodeWorkList newWorklist = worklist;
AbstractMergeNode merge = end.merge();
if (merge != null) {
merge.removeEnd(end);
StructuredGraph graph = end.graph();
if (merge instanceof LoopBeginNode && merge.forwardEndCount() == 0) {
LoopBeginNode begin = (LoopBeginNode) merge;
for (LoopEndNode loopend : begin.loopEnds().snapshot()) {
loopend.predecessor().replaceFirstSuccessor(loopend, null);
loopend.safeDelete();
}
for (LoopExitNode exit : begin.loopExits()) {
for (ProxyNode vpn : exit.proxies().snapshot()) {
tryKillUnused(vpn);
}
}
begin.removeExits();
PoisonNode poison = null;
if (merge.phis().isNotEmpty()) {
poison = graph.unique(new PoisonNode());
for (PhiNode phi : merge.phis()) {
phi.replaceAtUsages(poison);
}
for (PhiNode phi : merge.phis().snapshot()) {
killWithUnusedFloatingInputs(phi);
}
}
FixedNode loopBody = begin.next();
Debug.dump(Debug.VERY_DETAILED_LOG_LEVEL, end.graph(), "killEnd (Loop) %s after initial loop cleanup", end);
if (loopBody != null) {
newWorklist = killCFG(loopBody, tool, worklist);
}
FrameState frameState = begin.stateAfter();
begin.safeDelete();
if (frameState != null) {
tryKillUnused(frameState);
}
if (poison != null && poison.isAlive()) {
if (newWorklist == null) {
newWorklist = graph.createNodeWorkList();
}
for (Node n : newWorklist) {
killCFG(n, tool, newWorklist);
}
if (poison.isAlive()) {
newWorklist.add(poison);
}
}
} else if (merge instanceof LoopBeginNode && ((LoopBeginNode) merge).loopEnds().isEmpty()) {
if (tool != null) {
for (PhiNode phi : merge.phis()) {
tool.addToWorkList(phi.usages());
}
}
graph.reduceDegenerateLoopBegin((LoopBeginNode) merge);
} else if (merge.phiPredecessorCount() == 1) {
if (tool != null) {
for (PhiNode phi : merge.phis()) {
tool.addToWorkList(phi.usages());
}
}
graph.reduceTrivialMerge(merge);
}
}
return newWorklist;
}
public static boolean isFloatingNode(Node n) {
return !(n instanceof FixedNode);
}
private static NodeWorkList propagateKill(Node node, NodeWorkList workList) {
NodeWorkList newWorkList = workList;
if (node != null && node.isAlive()) {
for (Node usage : node.usages().snapshot()) {
assert usage.isAlive();
if (isFloatingNode(usage)) {
boolean addUsage = false;
if (usage instanceof PhiNode) {
PhiNode phi = (PhiNode) usage;
assert phi.merge() != null;
if (phi.merge() == node) {
addUsage = true;
} else {
assert phi.values().contains(node);
}
} else {
addUsage = true;
}
if (addUsage) {
if (newWorkList == null) {
newWorkList = node.graph().createNodeWorkList();
}
newWorkList.add(usage);
}
}
usage.replaceFirstInput(node, null);
}
killWithUnusedFloatingInputs(node);
}
return newWorkList;
}
private static boolean checkKill(Node 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) {
assert checkKill(node);
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()) {
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 checkRedundantPhi(PhiNode phiNode) {
if (phiNode.isDeleted() || phiNode.valueCount() == 1) {
return;
}
ValueNode singleValue = phiNode.singleValue();
if (singleValue != PhiNode.MULTIPLE_VALUES) {
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) {
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) {
ValueNode result = value;
while (result instanceof ValueProxy) {
result = ((ValueProxy) result).getOriginalNode();
}
return result;
}
public static ValueNode arrayLength(ValueNode value) {
ValueNode current = value;
do {
if (current instanceof ArrayLengthProvider) {
ValueNode length = ((ArrayLengthProvider) current).length();
if (length != null) {
return length;
}
}
if (current instanceof ValueProxy) {
current = ((ValueProxy) current).getOriginalNode();
} else {
break;
}
} while (true);
return null;
}
public static ValueNode originalValue(ValueNode proxy) {
ValueNode v = proxy;
do {
if (v instanceof LimitedValueProxy) {
v = ((LimitedValueProxy) v).getOriginalNode();
} else if (v instanceof PhiNode) {
v = ((PhiNode) v).singleValue();
if (v == PhiNode.MULTIPLE_VALUES) {
v = null;
}
} else {
break;
}
} while (v != null);
if (v == null) {
v = new OriginalValueSearch(proxy).result;
}
return v;
}
public static boolean tryKillUnused(Node node) {
if (node.isAlive() && isFloatingNode(node) && node.hasNoUsages()) {
killWithUnusedFloatingInputs(node);
return true;
}
return false;
}
static class OriginalValueSearch {
ValueNode result;
OriginalValueSearch(ValueNode proxy) {
NodeWorkList worklist = proxy.graph().createNodeWorkList();
worklist.add(proxy);
for (Node node : worklist) {
if (node instanceof LimitedValueProxy) {
ValueNode originalValue = ((LimitedValueProxy) node).getOriginalNode();
if (!process(originalValue, worklist)) {
return;
}
} else if (node instanceof PhiNode) {
for (Node value : ((PhiNode) node).values()) {
if (!process((ValueNode) value, worklist)) {
return;
}
}
} else {
if (!process((ValueNode) node, null)) {
return;
}
}
}
}
private boolean process(ValueNode node, NodeWorkList worklist) {
if (node.isAlive()) {
if (worklist == null) {
if (result == null) {
result = node;
} else if (result != node) {
result = null;
return false;
}
} else {
worklist.add(node);
}
}
return true;
}
}
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 implements SimplifierTool {
private final MetaAccessProvider metaAccess;
private final ConstantReflectionProvider constantReflection;
private final ConstantFieldProvider constantFieldProvider;
private final boolean canonicalizeReads;
private final Assumptions assumptions;
private final LoweringProvider loweringProvider;
DefaultSimplifierTool(MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ConstantFieldProvider constantFieldProvider, boolean canonicalizeReads,
Assumptions assumptions, LoweringProvider loweringProvider) {
this.metaAccess = metaAccess;
this.constantReflection = constantReflection;
this.constantFieldProvider = constantFieldProvider;
this.canonicalizeReads = canonicalizeReads;
this.assumptions = assumptions;
this.loweringProvider = loweringProvider;
}
@Override
public MetaAccessProvider getMetaAccess() {
return metaAccess;
}
@Override
public ConstantReflectionProvider getConstantReflection() {
return constantReflection;
}
@Override
public ConstantFieldProvider getConstantFieldProvider() {
return constantFieldProvider;
}
@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, this);
}
@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 boolean supportSubwordCompare(int bits) {
if (loweringProvider != null) {
return loweringProvider.supportSubwordCompare(bits);
} else {
return false;
}
}
}
public static SimplifierTool getDefaultSimplifier(MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ConstantFieldProvider constantFieldProvider,
boolean canonicalizeReads, Assumptions assumptions) {
return getDefaultSimplifier(metaAccess, constantReflection, constantFieldProvider, canonicalizeReads, assumptions, null);
}
public static SimplifierTool getDefaultSimplifier(MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ConstantFieldProvider constantFieldProvider,
boolean canonicalizeReads, Assumptions assumptions, LoweringProvider loweringProvider) {
return new DefaultSimplifierTool(metaAccess, constantReflection, constantFieldProvider, canonicalizeReads, assumptions, loweringProvider);
}
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;
}
}