7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 package org.graalvm.compiler.replacements;
26
27 import static org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.INLINE_AFTER_PARSING;
28
29 import java.lang.reflect.Method;
30 import java.lang.reflect.Modifier;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import org.graalvm.compiler.core.common.CompilationIdentifier;
35 import org.graalvm.compiler.core.common.spi.ConstantFieldProvider;
36 import org.graalvm.compiler.core.common.type.StampFactory;
37 import org.graalvm.compiler.core.common.type.StampPair;
38 import org.graalvm.compiler.debug.DebugCloseable;
39 import org.graalvm.compiler.debug.DebugContext;
40 import org.graalvm.compiler.debug.GraalError;
41 import org.graalvm.compiler.graph.Graph;
42 import org.graalvm.compiler.graph.Node.ValueNumberable;
43 import org.graalvm.compiler.graph.NodeSourcePosition;
44 import org.graalvm.compiler.java.FrameStateBuilder;
45 import org.graalvm.compiler.java.GraphBuilderPhase;
46 import org.graalvm.compiler.nodes.AbstractBeginNode;
341 for (InvokeNode invoke : graph.getNodes().filter(InvokeNode.class).snapshot()) {
342 inline(invoke, reason, phase);
343 }
344 }
345
346 // Clean up all code that is now dead after inlining.
347 new DeadCodeEliminationPhase().apply(graph);
348 }
349
350 /**
351 * Inlines a given invocation to a method. The graph of the inlined method is processed in the
352 * same manner as for snippets and method substitutions.
353 */
354 public void inline(InvokeNode invoke, String reason, String phase) {
355 ResolvedJavaMethod method = ((MethodCallTargetNode) invoke.callTarget()).targetMethod();
356
357 MetaAccessProvider metaAccess = providers.getMetaAccess();
358 Plugins plugins = new Plugins(graphBuilderPlugins);
359 GraphBuilderConfiguration config = GraphBuilderConfiguration.getSnippetDefault(plugins);
360
361 StructuredGraph calleeGraph = new StructuredGraph.Builder(invoke.getOptions(), invoke.getDebug()).method(method).trackNodeSourcePosition(
362 invoke.graph().trackNodeSourcePosition()).setIsSubstitution(true).build();
363 IntrinsicContext initialReplacementContext = new IntrinsicContext(method, method, providers.getReplacements().getDefaultReplacementBytecodeProvider(), INLINE_AFTER_PARSING);
364 GraphBuilderPhase.Instance instance = createGraphBuilderInstance(metaAccess, providers.getStampProvider(), providers.getConstantReflection(), providers.getConstantFieldProvider(), config,
365 OptimisticOptimizations.NONE,
366 initialReplacementContext);
367 instance.apply(calleeGraph);
368
369 // Remove all frame states from inlinee
370 calleeGraph.clearAllStateAfter();
371 new DeadCodeEliminationPhase(Optionality.Required).apply(calleeGraph);
372
373 InliningUtil.inline(invoke, calleeGraph, false, method, reason, phase);
374 }
375
376 protected GraphBuilderPhase.Instance createGraphBuilderInstance(MetaAccessProvider metaAccess, StampProvider stampProvider, ConstantReflectionProvider constantReflection,
377 ConstantFieldProvider constantFieldProvider, GraphBuilderConfiguration graphBuilderConfig, OptimisticOptimizations optimisticOpts, IntrinsicContext initialIntrinsicContext) {
378 return new GraphBuilderPhase.Instance(metaAccess, stampProvider, constantReflection, constantFieldProvider, graphBuilderConfig, optimisticOpts, initialIntrinsicContext);
379 }
380
381 protected void pushStructure(Structure structure) {
382 structures.add(structure);
383 }
384
385 protected <T extends Structure> T getTopStructure(Class<T> expectedClass) {
386 return expectedClass.cast(structures.get(structures.size() - 1));
387 }
|
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 package org.graalvm.compiler.replacements;
26
27 import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
28 import static org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.INLINE_AFTER_PARSING;
29
30 import java.lang.reflect.Method;
31 import java.lang.reflect.Modifier;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import org.graalvm.compiler.core.common.CompilationIdentifier;
36 import org.graalvm.compiler.core.common.spi.ConstantFieldProvider;
37 import org.graalvm.compiler.core.common.type.StampFactory;
38 import org.graalvm.compiler.core.common.type.StampPair;
39 import org.graalvm.compiler.debug.DebugCloseable;
40 import org.graalvm.compiler.debug.DebugContext;
41 import org.graalvm.compiler.debug.GraalError;
42 import org.graalvm.compiler.graph.Graph;
43 import org.graalvm.compiler.graph.Node.ValueNumberable;
44 import org.graalvm.compiler.graph.NodeSourcePosition;
45 import org.graalvm.compiler.java.FrameStateBuilder;
46 import org.graalvm.compiler.java.GraphBuilderPhase;
47 import org.graalvm.compiler.nodes.AbstractBeginNode;
342 for (InvokeNode invoke : graph.getNodes().filter(InvokeNode.class).snapshot()) {
343 inline(invoke, reason, phase);
344 }
345 }
346
347 // Clean up all code that is now dead after inlining.
348 new DeadCodeEliminationPhase().apply(graph);
349 }
350
351 /**
352 * Inlines a given invocation to a method. The graph of the inlined method is processed in the
353 * same manner as for snippets and method substitutions.
354 */
355 public void inline(InvokeNode invoke, String reason, String phase) {
356 ResolvedJavaMethod method = ((MethodCallTargetNode) invoke.callTarget()).targetMethod();
357
358 MetaAccessProvider metaAccess = providers.getMetaAccess();
359 Plugins plugins = new Plugins(graphBuilderPlugins);
360 GraphBuilderConfiguration config = GraphBuilderConfiguration.getSnippetDefault(plugins);
361
362 StructuredGraph calleeGraph;
363 if (IS_IN_NATIVE_IMAGE) {
364 calleeGraph = providers.getReplacements().getSnippet(method, null, false, null);
365 } else {
366 calleeGraph = new StructuredGraph.Builder(invoke.getOptions(), invoke.getDebug()).method(method).trackNodeSourcePosition(invoke.graph().trackNodeSourcePosition()).setIsSubstitution(
367 true).build();
368 IntrinsicContext initialReplacementContext = new IntrinsicContext(method, method, providers.getReplacements().getDefaultReplacementBytecodeProvider(), INLINE_AFTER_PARSING);
369 GraphBuilderPhase.Instance instance = createGraphBuilderInstance(metaAccess, providers.getStampProvider(), providers.getConstantReflection(), providers.getConstantFieldProvider(), config,
370 OptimisticOptimizations.NONE,
371 initialReplacementContext);
372 instance.apply(calleeGraph);
373 }
374
375 // Remove all frame states from inlinee
376 calleeGraph.clearAllStateAfter();
377 new DeadCodeEliminationPhase(Optionality.Required).apply(calleeGraph);
378
379 InliningUtil.inline(invoke, calleeGraph, false, method, reason, phase);
380 }
381
382 protected GraphBuilderPhase.Instance createGraphBuilderInstance(MetaAccessProvider metaAccess, StampProvider stampProvider, ConstantReflectionProvider constantReflection,
383 ConstantFieldProvider constantFieldProvider, GraphBuilderConfiguration graphBuilderConfig, OptimisticOptimizations optimisticOpts, IntrinsicContext initialIntrinsicContext) {
384 return new GraphBuilderPhase.Instance(metaAccess, stampProvider, constantReflection, constantFieldProvider, graphBuilderConfig, optimisticOpts, initialIntrinsicContext);
385 }
386
387 protected void pushStructure(Structure structure) {
388 structures.add(structure);
389 }
390
391 protected <T extends Structure> T getTopStructure(Class<T> expectedClass) {
392 return expectedClass.cast(structures.get(structures.size() - 1));
393 }
|