use of org.graalvm.compiler.graph.NodeMap in project graal by oracle.
the class InliningUtil method fixFrameStates.
private static void fixFrameStates(StructuredGraph graph, MergeNode originalMerge, PhiNode returnPhi) {
// It is possible that some of the frame states that came from AFTER_BCI reference a Phi
// node that was created to merge multiple returns. This can create cycles
// (see GR-3949 and GR-3957).
// To detect this, we follow the control paths starting from the merge node,
// split the Phi node inputs at merges and assign the proper input to each frame state.
NodeMap<Node> seen = new NodeMap<>(graph);
ArrayDeque<Node> workList = new ArrayDeque<>();
ArrayDeque<ValueNode> valueList = new ArrayDeque<>();
workList.push(originalMerge);
valueList.push(returnPhi);
while (!workList.isEmpty()) {
Node current = workList.pop();
ValueNode currentValue = valueList.pop();
if (seen.containsKey(current)) {
continue;
}
seen.put(current, current);
if (current instanceof StateSplit && current != originalMerge) {
StateSplit stateSplit = (StateSplit) current;
FrameState state = stateSplit.stateAfter();
if (state != null && state.values().contains(returnPhi)) {
int index = 0;
FrameState duplicate = state.duplicate();
for (ValueNode value : state.values()) {
if (value == returnPhi) {
duplicate.values().set(index, currentValue);
}
index++;
}
stateSplit.setStateAfter(duplicate);
GraphUtil.tryKillUnused(state);
}
}
if (current instanceof AbstractMergeNode) {
AbstractMergeNode currentMerge = (AbstractMergeNode) current;
for (EndNode pred : currentMerge.cfgPredecessors()) {
ValueNode newValue = currentValue;
if (currentMerge.isPhiAtMerge(currentValue)) {
PhiNode currentPhi = (PhiNode) currentValue;
newValue = currentPhi.valueAt(pred);
}
workList.push(pred);
valueList.push(newValue);
}
} else if (current.predecessor() != null) {
workList.push(current.predecessor());
valueList.push(currentValue);
}
}
}
use of org.graalvm.compiler.graph.NodeMap in project graal by oracle.
the class BinaryGraphPrinter method nodeProperties.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void nodeProperties(GraphInfo info, Node node, Map<String, Object> props) {
node.getDebugProperties((Map) props);
Graph graph = info.graph;
ControlFlowGraph cfg = info.cfg;
NodeMap<Block> nodeToBlocks = info.nodeToBlocks;
if (cfg != null && DebugOptions.PrintGraphProbabilities.getValue(graph.getOptions()) && node instanceof FixedNode) {
try {
props.put("probability", cfg.blockFor(node).probability());
} catch (Throwable t) {
props.put("probability", 0.0);
props.put("probability-exception", t);
}
}
try {
props.put("NodeCost-Size", node.estimatedNodeSize());
props.put("NodeCost-Cycles", node.estimatedNodeCycles());
} catch (Throwable t) {
props.put("node-cost-exception", t.getMessage());
}
if (nodeToBlocks != null) {
Object block = getBlockForNode(node, nodeToBlocks);
if (block != null) {
props.put("node-to-block", block);
}
}
if (node instanceof ControlSinkNode) {
props.put("category", "controlSink");
} else if (node instanceof ControlSplitNode) {
props.put("category", "controlSplit");
} else if (node instanceof AbstractMergeNode) {
props.put("category", "merge");
} else if (node instanceof AbstractBeginNode) {
props.put("category", "begin");
} else if (node instanceof AbstractEndNode) {
props.put("category", "end");
} else if (node instanceof FixedNode) {
props.put("category", "fixed");
} else if (node instanceof VirtualState) {
props.put("category", "state");
} else if (node instanceof PhiNode) {
props.put("category", "phi");
} else if (node instanceof ProxyNode) {
props.put("category", "proxy");
} else {
if (node instanceof ConstantNode) {
ConstantNode cn = (ConstantNode) node;
updateStringPropertiesForConstant((Map) props, cn);
}
props.put("category", "floating");
}
if (getSnippetReflectionProvider() != null) {
for (Map.Entry<String, Object> prop : props.entrySet()) {
if (prop.getValue() instanceof JavaConstantFormattable) {
props.put(prop.getKey(), ((JavaConstantFormattable) prop.getValue()).format(this));
}
}
}
}
use of org.graalvm.compiler.graph.NodeMap in project graal by oracle.
the class GraphComparison method verifyGraphsEqual.
public static boolean verifyGraphsEqual(StructuredGraph expectedGraph, StructuredGraph actualGraph) {
NodeMap<Node> nodeMapping = new NodeMap<>(expectedGraph);
Deque<Pair<Node, Node>> workList = new ArrayDeque<>();
pushToWorklist(expectedGraph.start(), actualGraph.start(), nodeMapping, workList);
while (!workList.isEmpty()) {
Pair<Node, Node> pair = workList.removeFirst();
Node expectedNode = pair.getLeft();
Node actualNode = pair.getRight();
assert expectedNode.getClass() == actualNode.getClass();
NodeClass<?> nodeClass = expectedNode.getNodeClass();
assert nodeClass == actualNode.getNodeClass();
if (expectedNode instanceof MergeNode) {
/* The order of the ends can be different, so ignore them. */
verifyNodesEqual(expectedNode.inputs(), actualNode.inputs(), nodeMapping, workList, true);
} else if (expectedNode instanceof PhiNode) {
verifyPhi((PhiNode) expectedNode, (PhiNode) actualNode, nodeMapping, workList);
} else {
verifyNodesEqual(expectedNode.inputs(), actualNode.inputs(), nodeMapping, workList, false);
}
verifyNodesEqual(expectedNode.successors(), actualNode.successors(), nodeMapping, workList, false);
if (expectedNode instanceof LoopEndNode) {
LoopEndNode actualLoopEnd = (LoopEndNode) actualNode;
assert actualLoopEnd.loopBegin().loopEnds().snapshot().indexOf(actualLoopEnd) == actualLoopEnd.endIndex();
} else {
for (int i = 0; i < nodeClass.getData().getCount(); i++) {
Object expectedProperty = nodeClass.getData().get(expectedNode, i);
Object actualProperty = nodeClass.getData().get(actualNode, i);
assert Objects.equals(expectedProperty, actualProperty);
}
}
if (expectedNode instanceof EndNode) {
/* Visit the merge node, which is the one and only usage of the EndNode. */
assert expectedNode.usages().count() == 1;
assert actualNode.usages().count() == 1;
verifyNodesEqual(expectedNode.usages(), actualNode.usages(), nodeMapping, workList, false);
}
if (expectedNode instanceof AbstractEndNode) {
/* Visit the input values of the merge phi functions for this EndNode. */
verifyPhis((AbstractEndNode) expectedNode, (AbstractEndNode) actualNode, nodeMapping, workList);
}
}
return true;
}
use of org.graalvm.compiler.graph.NodeMap in project graal by oracle.
the class ReplaceConstantNodesPhase method tryToReplaceWithExisting.
/**
* Try to find dominating node doing the resolution that can be reused.
*
* @param graph
* @param node {@link ConstantNode} containing a {@link HotSpotResolvedJavaType} that needs
* resolution.
*/
private static void tryToReplaceWithExisting(StructuredGraph graph, ConstantNode node) {
ScheduleResult schedule = graph.getLastSchedule();
NodeMap<Block> nodeToBlock = schedule.getNodeToBlockMap();
BlockMap<List<Node>> blockToNodes = schedule.getBlockToNodesMap();
EconomicMap<Block, Node> blockToExisting = EconomicMap.create();
for (Node n : node.usages().filter(n -> isReplacementNode(n))) {
blockToExisting.put(nodeToBlock.get(n), n);
}
for (Node use : node.usages().filter(n -> !isReplacementNode(n)).snapshot()) {
boolean replaced = false;
Block b = nodeToBlock.get(use);
Node e = blockToExisting.get(b);
if (e != null) {
// the use is scheduled after it.
for (Node n : blockToNodes.get(b)) {
if (n.equals(use)) {
// Usage is before initialization, can't use it
break;
}
if (n.equals(e)) {
use.replaceFirstInput(node, e);
replaced = true;
break;
}
}
}
if (!replaced) {
// Look for dominating blocks that have existing nodes
for (Block d : blockToExisting.getKeys()) {
if (strictlyDominates(d, b)) {
use.replaceFirstInput(node, blockToExisting.get(d));
break;
}
}
}
}
}
Aggregations