--- /dev/null 2017-01-22 10:16:57.869617664 -0800 +++ new/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.phases.common/src/org/graalvm/compiler/phases/common/inlining/info/elem/InlineableGraph.java 2017-02-15 17:08:18.122987793 -0800 @@ -0,0 +1,240 @@ +/* + * Copyright (c) 2012, 2016, 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.core.common.CompilationIdentifier.INVALID_COMPILATION_ID; +import static org.graalvm.compiler.core.common.GraalOptions.UseGraalInstrumentation; +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.Debug; +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.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.common.instrumentation.ExtractInstrumentationPhase; +import org.graalvm.compiler.phases.graph.FixedNodeProbabilityCache; +import org.graalvm.compiler.phases.tiers.HighTierContext; + +import jdk.vm.ci.meta.Constant; +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 FixedNodeProbabilityCache probabilites = new FixedNodeProbabilityCache(); + + public InlineableGraph(final ResolvedJavaMethod method, final Invoke invoke, final HighTierContext context, CanonicalizerPhase canonicalizer) { + StructuredGraph original = getOriginalGraph(method, context, canonicalizer, invoke.asNode().graph(), invoke.bci()); + // TODO copying the graph is only necessary if it is modified or if it contains any invokes + this.graph = (StructuredGraph) original.copy(); + specializeGraphToArguments(invoke, context, canonicalizer); + } + + /** + * This method looks up in a cache the graph for the argument, if not found bytecode is parsed. + * The graph thus obtained is returned, ie the caller is responsible for cloning before + * modification. + */ + private static StructuredGraph getOriginalGraph(final ResolvedJavaMethod method, final HighTierContext context, CanonicalizerPhase canonicalizer, StructuredGraph caller, int callerBci) { + StructuredGraph result = InliningUtil.getIntrinsicGraph(context.getReplacements(), method, callerBci); + if (result != null) { + return result; + } + return parseBytecodes(method, context, canonicalizer, caller); + } + + /** + * @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) {
+ try (Debug.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) {
+ StructuredGraph newGraph = new StructuredGraph(method, AllowAssumptions.from(caller.getAssumptions() != null), INVALID_COMPILATION_ID);
+ try (Debug.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();
+
+ if (UseGraalInstrumentation.getValue()) {
+ new ExtractInstrumentationPhase().apply(newGraph, context);
+ }
+ 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