/* * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package org.graalvm.compiler.phases.common.inlining.info.elem; import static org.graalvm.compiler.phases.common.DeadCodeEliminationPhase.Optionality.Optional; import java.util.ArrayList; import java.util.List; import org.graalvm.compiler.core.common.type.Stamp; import org.graalvm.compiler.debug.DebugContext; import org.graalvm.compiler.graph.Node; import org.graalvm.compiler.graph.NodeInputList; import org.graalvm.compiler.nodes.ConstantNode; import org.graalvm.compiler.nodes.Invoke; import org.graalvm.compiler.nodes.NodeView; import org.graalvm.compiler.nodes.ParameterNode; import org.graalvm.compiler.nodes.StructuredGraph; import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; import org.graalvm.compiler.nodes.ValueNode; import org.graalvm.compiler.phases.common.CanonicalizerPhase; import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase; import org.graalvm.compiler.phases.common.inlining.InliningUtil; import org.graalvm.compiler.phases.graph.FixedNodeRelativeFrequencyCache; import org.graalvm.compiler.phases.tiers.HighTierContext; import jdk.vm.ci.meta.ResolvedJavaMethod; /** *
* Represents a feasible concrete target for inlining, whose graph has been copied already and thus * can be modified without affecting the original (usually cached) version. *
* ** Instances of this class don't make sense in isolation but as part of an * {@link org.graalvm.compiler.phases.common.inlining.info.InlineInfo InlineInfo}. *
* * @see org.graalvm.compiler.phases.common.inlining.walker.InliningData#moveForward() * @see org.graalvm.compiler.phases.common.inlining.walker.CallsiteHolderExplorable */ public class InlineableGraph implements Inlineable { private final StructuredGraph graph; private FixedNodeRelativeFrequencyCache probabilites = new FixedNodeRelativeFrequencyCache(); public InlineableGraph(final ResolvedJavaMethod method, final Invoke invoke, final HighTierContext context, CanonicalizerPhase canonicalizer, boolean trackNodeSourcePosition) { StructuredGraph original = context.getReplacements().getSubstitution(method, invoke.bci(), trackNodeSourcePosition, null, invoke.asNode().getOptions()); if (original == null) { original = parseBytecodes(method, context, canonicalizer, invoke.asNode().graph(), trackNodeSourcePosition); } else if (original.isFrozen()) { // Graph may be modified by specializeGraphToArguments so defensively // make a copy. We rely on the frozen state of a graph to denote // whether it is shared. original = (StructuredGraph) original.copy(invoke.asNode().getDebug()); } this.graph = original; specializeGraphToArguments(invoke, context, canonicalizer); } /** * @return true iff one or more parametersnewGraph
were specialized to account for
* a constant argument, or an argument with a more specific stamp.
*/
@SuppressWarnings("try")
private boolean specializeGraphToArguments(final Invoke invoke, final HighTierContext context, CanonicalizerPhase canonicalizer) {
DebugContext debug = graph.getDebug();
try (DebugContext.Scope s = debug.scope("InlineGraph", graph)) {
ArrayListinvoke
* The corresponding parameters are updated to reflect the above information. Before doing so,
* their usages are added to parameterUsages
for later incremental
* canonicalization.
*
method
and canonicalizes them.
* Provided profiling info is mature, the resulting graph is cached. The caller is responsible
* for cloning before modification.
*
*/
@SuppressWarnings("try")
private static StructuredGraph parseBytecodes(ResolvedJavaMethod method, HighTierContext context, CanonicalizerPhase canonicalizer, StructuredGraph caller, boolean trackNodeSourcePosition) {
DebugContext debug = caller.getDebug();
StructuredGraph newGraph = new StructuredGraph.Builder(caller.getOptions(), debug, AllowAssumptions.ifNonNull(caller.getAssumptions())).method(method).trackNodeSourcePosition(
trackNodeSourcePosition).useProfilingInfo(caller.useProfilingInfo()).build();
try (DebugContext.Scope s = debug.scope("InlineGraph", newGraph)) {
if (!caller.isUnsafeAccessTrackingEnabled()) {
newGraph.disableUnsafeAccessTracking();
}
if (context.getGraphBuilderSuite() != null) {
context.getGraphBuilderSuite().apply(newGraph, context);
}
assert newGraph.start().next() != null : "graph needs to be populated by the GraphBuilderSuite " + method + ", " + method.canBeInlined();
new DeadCodeEliminationPhase(Optional).apply(newGraph);
canonicalizer.apply(newGraph, context);
return newGraph;
} catch (Throwable e) {
throw debug.handle(e);
}
}
@Override
public int getNodeCount() {
return InliningUtil.getNodeCount(graph);
}
@Override
public Iterable