use of org.graalvm.compiler.nodes.ParameterNode in project graal by oracle.
the class MethodTypeFlow method computeReturnedParamter.
private ParameterNode computeReturnedParamter() {
if (graphRef == null) {
// Some methods, e.g., native ones, don't have a graph.
return null;
}
ParameterNode retParam = null;
for (ParameterNode param : graphRef.getNodes(ParameterNode.TYPE)) {
if (param.stamp(NodeView.DEFAULT) instanceof ObjectStamp) {
boolean returnsParameter = true;
NodeIterable<ReturnNode> retIterable = graphRef.getNodes(ReturnNode.TYPE);
returnsParameter &= retIterable.count() > 0;
for (ReturnNode ret : retIterable) {
returnsParameter &= ret.result() == param;
}
if (returnsParameter) {
retParam = param;
}
}
}
return retParam;
}
use of org.graalvm.compiler.nodes.ParameterNode in project graal by oracle.
the class MethodTypeFlowBuilder method apply.
protected void apply() {
// assert method.getAnnotation(Fold.class) == null : method;
if (method.getAnnotation(NodeIntrinsic.class) != null) {
graph.getDebug().log("apply MethodTypeFlow on node intrinsic %s", method);
AnalysisType returnType = (AnalysisType) method.getSignature().getReturnType(method.getDeclaringClass());
if (returnType.getJavaKind() == JavaKind.Object) {
/*
* This is a method used in a snippet, so most likely the return value does not
* matter at all. However, some methods return an object, and the snippet continues
* to work with the object. So pretend that this method returns an object of the
* exact return type.
*/
TypeFlow<?> returnTypeFlow = methodFlow.getResultFlow().getDeclaredType().getTypeFlow(this.bb, true);
returnTypeFlow = new ProxyTypeFlow(null, returnTypeFlow);
FormalReturnTypeFlow resultFlow = new FormalReturnTypeFlow(null, returnType, method);
returnTypeFlow.addOriginalUse(this.bb, resultFlow);
methodFlow.addMiscEntry(returnTypeFlow);
methodFlow.setResult(resultFlow);
}
return;
}
if (!parse()) {
return;
}
this.bb.getUnsupportedFeatures().checkMethod(method, graph);
processedNodes = new NodeBitMap(graph);
TypeFlowsOfNodes typeFlows = new TypeFlowsOfNodes();
for (Node n : graph.getNodes()) {
if (n instanceof ParameterNode) {
ParameterNode node = (ParameterNode) n;
if (node.getStackKind() == JavaKind.Object) {
TypeFlowBuilder<?> paramBuilder = TypeFlowBuilder.create(bb, node, FormalParamTypeFlow.class, () -> {
boolean isStatic = Modifier.isStatic(method.getModifiers());
int index = node.index();
FormalParamTypeFlow parameter;
if (!isStatic && index == 0) {
AnalysisType paramType = method.getDeclaringClass();
parameter = new FormalReceiverTypeFlow(node, paramType, method);
} else {
int offset = isStatic ? 0 : 1;
AnalysisType paramType = (AnalysisType) method.getSignature().getParameterType(index - offset, method.getDeclaringClass());
parameter = new FormalParamTypeFlow(node, paramType, method, index);
}
methodFlow.setParameter(index, parameter);
return parameter;
});
typeFlowGraphBuilder.checkFormalParameterBuilder(paramBuilder);
typeFlows.add(node, paramBuilder);
}
} else if (n instanceof BoxNode) {
BoxNode node = (BoxNode) n;
Object key = uniqueKey(node);
BytecodeLocation boxSite = bb.analysisPolicy().createAllocationSite(bb, key, methodFlow.getMethod());
AnalysisType type = (AnalysisType) StampTool.typeOrNull(node);
TypeFlowBuilder<?> boxBuilder = TypeFlowBuilder.create(bb, node, BoxTypeFlow.class, () -> {
BoxTypeFlow boxFlow = new BoxTypeFlow(node, type, boxSite);
methodFlow.addAllocation(boxFlow);
return boxFlow;
});
typeFlows.add(node, boxBuilder);
}
for (Node input : n.inputs()) {
/*
* TODO change the handling of constants so that the SourceTypeFlow is created on
* demand, with the optimization that only one SourceTypeFlow is created ever for
* every distinct object (using, e.g., caching in a global IdentityHashMap).
*/
if (input instanceof ConstantNode && !typeFlows.contains((ConstantNode) input)) {
ConstantNode node = (ConstantNode) input;
if (node.asJavaConstant().isNull()) {
TypeFlowBuilder<SourceTypeFlow> sourceBuilder = TypeFlowBuilder.create(bb, node, SourceTypeFlow.class, () -> {
SourceTypeFlow constantSource = new SourceTypeFlow(node, TypeState.forNull());
methodFlow.addSource(constantSource);
return constantSource;
});
typeFlows.add(node, sourceBuilder);
} else if (node.asJavaConstant().getJavaKind() == JavaKind.Object) {
/*
* TODO a SubstrateObjectConstant wrapping a PrimitiveConstant has kind
* equals to Object. Do we care about the effective value of these primitive
* constants in the analysis?
*/
assert StampTool.isExactType(node);
AnalysisType type = (AnalysisType) StampTool.typeOrNull(node);
assert type.isInstantiated();
TypeFlowBuilder<SourceTypeFlow> sourceBuilder = TypeFlowBuilder.create(bb, node, SourceTypeFlow.class, () -> {
SourceTypeFlow constantSource = new SourceTypeFlow(node, TypeState.forConstant(this.bb, node.asJavaConstant(), type));
methodFlow.addSource(constantSource);
return constantSource;
});
typeFlows.add(node, sourceBuilder);
}
}
}
}
// Propagate the type flows through the method's graph
new NodeIterator(graph.start(), typeFlows).apply();
/* Prune the method graph. Eliminate nodes with no uses. */
typeFlowGraphBuilder.build();
/*
* Make sure that all existing InstanceOfNodes are registered even when only used as an
* input of a conditional.
*/
for (Node n : graph.getNodes()) {
if (n instanceof InstanceOfNode) {
InstanceOfNode instanceOf = (InstanceOfNode) n;
markFieldsUsedInComparison(instanceOf.getValue());
} else if (n instanceof ObjectEqualsNode) {
ObjectEqualsNode compareNode = (ObjectEqualsNode) n;
markFieldsUsedInComparison(compareNode.getX());
markFieldsUsedInComparison(compareNode.getY());
}
}
}
use of org.graalvm.compiler.nodes.ParameterNode in project graal by oracle.
the class GraalCompilerTest method getCanonicalGraphString.
protected static String getCanonicalGraphString(StructuredGraph graph, boolean excludeVirtual, boolean checkConstants) {
SchedulePhase schedule = new SchedulePhase(SchedulingStrategy.EARLIEST);
schedule.apply(graph);
ScheduleResult scheduleResult = graph.getLastSchedule();
NodeMap<Integer> canonicalId = graph.createNodeMap();
int nextId = 0;
List<String> constantsLines = new ArrayList<>();
StringBuilder result = new StringBuilder();
for (Block block : scheduleResult.getCFG().getBlocks()) {
result.append("Block ").append(block).append(' ');
if (block == scheduleResult.getCFG().getStartBlock()) {
result.append("* ");
}
result.append("-> ");
for (Block succ : block.getSuccessors()) {
result.append(succ).append(' ');
}
result.append('\n');
for (Node node : scheduleResult.getBlockToNodesMap().get(block)) {
if (node instanceof ValueNode && node.isAlive()) {
if (!excludeVirtual || !(node instanceof VirtualObjectNode || node instanceof ProxyNode || node instanceof FullInfopointNode || node instanceof ParameterNode)) {
if (node instanceof ConstantNode) {
String name = checkConstants ? node.toString(Verbosity.Name) : node.getClass().getSimpleName();
if (excludeVirtual) {
constantsLines.add(name);
} else {
constantsLines.add(name + " (" + filteredUsageCount(node) + ")");
}
} else {
int id;
if (canonicalId.get(node) != null) {
id = canonicalId.get(node);
} else {
id = nextId++;
canonicalId.set(node, id);
}
String name = node.getClass().getSimpleName();
result.append(" ").append(id).append('|').append(name);
if (node instanceof AccessFieldNode) {
result.append('#');
result.append(((AccessFieldNode) node).field());
}
if (!excludeVirtual) {
result.append(" (");
result.append(filteredUsageCount(node));
result.append(')');
}
result.append('\n');
}
}
}
}
}
StringBuilder constantsLinesResult = new StringBuilder();
constantsLinesResult.append(constantsLines.size()).append(" constants:\n");
Collections.sort(constantsLines);
for (String s : constantsLines) {
constantsLinesResult.append(s);
constantsLinesResult.append('\n');
}
return constantsLinesResult.toString() + result.toString();
}
use of org.graalvm.compiler.nodes.ParameterNode in project graal by oracle.
the class SwitchDyingLoopTest method test.
@Test
public void test() {
CanonicalizerPhase canonicalizerPhase = new CanonicalizerPhase();
HighTierContext highTierContext = getDefaultHighTierContext();
StructuredGraph graph = parseEager("snippet", StructuredGraph.AllowAssumptions.YES);
// there should be 1 loop and 1 switch
assertThat(graph.getNodes(LoopBeginNode.TYPE), hasCount(1));
assertThat(graph.getNodes(IntegerSwitchNode.TYPE), hasCount(1));
canonicalizerPhase.apply(graph, highTierContext);
// after canonicalization, the loop and switch should still be there
assertThat(graph.getNodes(LoopBeginNode.TYPE), hasCount(1));
assertThat(graph.getNodes(IntegerSwitchNode.TYPE), hasCount(1));
// add stamp to `a` so that paths leading to continue can be trimmed
ParameterNode parameter = graph.getParameter(0);
assertNotNull(parameter);
parameter.setStamp(StampFactory.forInteger(JavaKind.Int, 0, 255, 0, 0xf));
canonicalizerPhase.apply(graph, highTierContext);
// the loop should have disappeared and there should still be a switch
assertThat(graph.getNodes(LoopBeginNode.TYPE), isEmpty());
assertThat(graph.getNodes(IntegerSwitchNode.TYPE), hasCount(1));
}
use of org.graalvm.compiler.nodes.ParameterNode in project graal by oracle.
the class PushNodesThroughPiTest method test1.
@Ignore
@Test
@SuppressWarnings("try")
public void test1() {
final String snippet = "test1Snippet";
DebugContext debug = getDebugContext();
try (DebugContext.Scope s = debug.scope("PushThroughPi", new DebugDumpScope(snippet))) {
StructuredGraph graph = compileTestSnippet(snippet);
for (ReadNode rn : graph.getNodes().filter(ReadNode.class)) {
OffsetAddressNode address = (OffsetAddressNode) rn.getAddress();
long disp = address.getOffset().asJavaConstant().asLong();
ResolvedJavaType receiverType = StampTool.typeOrNull(address.getBase());
ResolvedJavaField field = receiverType.findInstanceFieldWithOffset(disp, rn.getStackKind());
assert field != null : "Node " + rn + " tries to access a field which doesn't exists for this type";
if (field.getName().equals("x")) {
Assert.assertTrue(address.getBase() instanceof ParameterNode);
} else {
Assert.assertTrue(address.getBase().toString(), address.getBase() instanceof PiNode);
}
}
Assert.assertTrue(graph.getNodes().filter(IsNullNode.class).count() == 1);
} catch (Throwable e) {
throw debug.handle(e);
}
}
Aggregations