use of org.graalvm.compiler.nodes.ConstantNode in project graal by oracle.
the class CompiledMethodTest method test1.
/**
* Usages of the constant {@code " "} are replaced with the constant {@code "-"} and it is
* verified that executing the compiled code produces a result that the preserves the node
* replacement unless deoptimization occurs (e.g., due to -Xcomp causing profiles to be
* missing).
*/
@Test
public void test1() {
final ResolvedJavaMethod javaMethod = getResolvedJavaMethod("testMethod");
final StructuredGraph graph = parseEager(javaMethod, AllowAssumptions.NO);
new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
new DeadCodeEliminationPhase().apply(graph);
for (ConstantNode node : ConstantNode.getConstantNodes(graph)) {
if (node.getStackKind() == JavaKind.Object && " ".equals(getSnippetReflection().asObject(String.class, node.asJavaConstant()))) {
node.replace(graph, ConstantNode.forConstant(getSnippetReflection().forObject("-"), getMetaAccess(), graph));
}
}
InstalledCode compiledMethod = getCode(javaMethod, graph);
try {
Object result = compiledMethod.executeVarargs("1", "2", "3");
if (!"1-2-3".equals(result)) {
// Deoptimization probably occurred
Assert.assertEquals("interpreter", result);
}
} catch (InvalidInstalledCodeException t) {
Assert.fail("method invalidated");
}
}
use of org.graalvm.compiler.nodes.ConstantNode in project graal by oracle.
the class AddNode method create.
public static ValueNode create(ValueNode x, ValueNode y, NodeView view) {
BinaryOp<Add> op = ArithmeticOpTable.forStamp(x.stamp(view)).getAdd();
Stamp stamp = op.foldStamp(x.stamp(view), y.stamp(view));
ConstantNode tryConstantFold = tryConstantFold(op, x, y, stamp, view);
if (tryConstantFold != null) {
return tryConstantFold;
}
if (x.isConstant() && !y.isConstant()) {
return canonical(null, op, y, x, view);
} else {
return canonical(null, op, x, y, view);
}
}
use of org.graalvm.compiler.nodes.ConstantNode in project graal by oracle.
the class CanonicalStringGraphPrinter method writeCanonicalGraphExpressionString.
protected static void writeCanonicalGraphExpressionString(ValueNode node, boolean checkConstants, boolean removeIdentities, PrintWriter writer) {
writer.print(node.getClass().getSimpleName());
writer.print("(");
Fields properties = node.getNodeClass().getData();
for (int i = 0; i < properties.getCount(); i++) {
String dataStr = String.valueOf(properties.get(node, i));
if (removeIdentities) {
dataStr = removeIdentities(dataStr);
}
writer.print(dataStr);
if (i + 1 < properties.getCount() || node.inputPositions().iterator().hasNext()) {
writer.print(", ");
}
}
Iterator<Position> iterator = node.inputPositions().iterator();
while (iterator.hasNext()) {
Position position = iterator.next();
Node input = position.get(node);
if (checkConstants && input instanceof ConstantNode) {
ConstantNode constantNode = (ConstantNode) input;
String valueString = constantNode.getValue().toValueString();
if (removeIdentities) {
valueString = removeIdentities(valueString);
}
writer.print(valueString);
} else if (input instanceof ValueNode && !(input instanceof PhiNode) && !(input instanceof FixedNode)) {
writeCanonicalGraphExpressionString((ValueNode) input, checkConstants, removeIdentities, writer);
} else if (input == null) {
writer.print("null");
} else {
writer.print(input.getClass().getSimpleName());
}
if (iterator.hasNext()) {
writer.print(", ");
}
}
writer.print(")");
}
use of org.graalvm.compiler.nodes.ConstantNode in project graal by oracle.
the class TypeGuardInlineInfo method createGuard.
@SuppressWarnings("try")
private void createGuard(StructuredGraph graph, Providers providers) {
try (DebugCloseable context = invoke.asNode().withNodeSourcePosition()) {
ValueNode nonNullReceiver = InliningUtil.nonNullReceiver(invoke);
LoadHubNode receiverHub = graph.unique(new LoadHubNode(providers.getStampProvider(), nonNullReceiver));
ConstantNode typeHub = ConstantNode.forConstant(receiverHub.stamp(NodeView.DEFAULT), providers.getConstantReflection().asObjectHub(type), providers.getMetaAccess(), graph);
LogicNode typeCheck = CompareNode.createCompareNode(graph, CanonicalCondition.EQ, receiverHub, typeHub, providers.getConstantReflection(), NodeView.DEFAULT);
FixedGuardNode guard = graph.add(new FixedGuardNode(typeCheck, DeoptimizationReason.TypeCheckedInliningViolated, DeoptimizationAction.InvalidateReprofile));
assert invoke.predecessor() != null;
ValueNode anchoredReceiver = InliningUtil.createAnchoredReceiver(graph, guard, type, nonNullReceiver, true);
invoke.callTarget().replaceFirstInput(nonNullReceiver, anchoredReceiver);
graph.addBeforeFixed(invoke.asNode(), guard);
}
}
use of org.graalvm.compiler.nodes.ConstantNode in project graal by oracle.
the class InlineableGraph method replaceParamsWithMoreInformativeArguments.
/**
* This method detects:
* <ul>
* <li>constants among the arguments to the <code>invoke</code></li>
* <li>arguments with more precise type than that declared by the corresponding parameter</li>
* </ul>
*
* <p>
* The corresponding parameters are updated to reflect the above information. Before doing so,
* their usages are added to <code>parameterUsages</code> for later incremental
* canonicalization.
* </p>
*
* @return null if no incremental canonicalization is need, a list of nodes for such
* canonicalization otherwise.
*/
private ArrayList<Node> replaceParamsWithMoreInformativeArguments(final Invoke invoke, final HighTierContext context) {
NodeInputList<ValueNode> args = invoke.callTarget().arguments();
ArrayList<Node> parameterUsages = null;
List<ParameterNode> params = graph.getNodes(ParameterNode.TYPE).snapshot();
assert params.size() <= args.size();
/*
* param-nodes that aren't used (eg, as a result of canonicalization) don't occur in
* `params`. Thus, in general, the sizes of `params` and `args` don't always match. Still,
* it's always possible to pair a param-node with its corresponding arg-node using
* param.index() as index into `args`.
*/
for (ParameterNode param : params) {
if (param.usages().isNotEmpty()) {
ValueNode arg = args.get(param.index());
if (arg.isConstant()) {
ConstantNode constant = (ConstantNode) arg;
parameterUsages = trackParameterUsages(param, parameterUsages);
// collect param usages before replacing the param
param.replaceAtUsagesAndDelete(graph.unique(ConstantNode.forConstant(arg.stamp(NodeView.DEFAULT), constant.getValue(), constant.getStableDimension(), constant.isDefaultStable(), context.getMetaAccess())));
// param-node gone, leaving a gap in the sequence given by param.index()
} else {
Stamp impro = improvedStamp(arg, param);
if (impro != null) {
param.setStamp(impro);
parameterUsages = trackParameterUsages(param, parameterUsages);
} else {
assert !isArgMoreInformativeThanParam(arg, param);
}
}
}
}
assert (parameterUsages == null) || (!parameterUsages.isEmpty());
return parameterUsages;
}
Aggregations