1 /*
2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
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 
26 package jdk.tools.jaotc;
27 
28 import java.util.ListIterator;
29 
30 import org.graalvm.compiler.code.CompilationResult;
31 import org.graalvm.compiler.core.GraalCompiler;
32 import org.graalvm.compiler.core.common.CompilationIdentifier;
33 import org.graalvm.compiler.debug.DebugContext;
34 import org.graalvm.compiler.hotspot.HotSpotBackend;
35 import org.graalvm.compiler.hotspot.HotSpotCompiledCodeBuilder;
36 import org.graalvm.compiler.hotspot.meta.HotSpotInvokeDynamicPlugin;
37 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
38 import org.graalvm.compiler.java.GraphBuilderPhase;
39 import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
40 import org.graalvm.compiler.lir.phases.LIRSuites;
41 import org.graalvm.compiler.nodes.StructuredGraph;
42 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
43 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
44 import org.graalvm.compiler.options.OptionValues;
45 import org.graalvm.compiler.phases.BasePhase;
46 import org.graalvm.compiler.phases.OptimisticOptimizations;
47 import org.graalvm.compiler.phases.PhaseSuite;
48 import org.graalvm.compiler.phases.tiers.HighTierContext;
49 import org.graalvm.compiler.phases.tiers.Suites;
50 
51 import jdk.vm.ci.code.InstalledCode;
52 import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
53 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
54 import jdk.vm.ci.meta.DefaultProfilingInfo;
55 import jdk.vm.ci.meta.ProfilingInfo;
56 import jdk.vm.ci.meta.ResolvedJavaMethod;
57 import jdk.vm.ci.meta.TriState;
58 
59 final class AOTBackend {
60     private final Main main;
61     private final OptionValues graalOptions;
62     private final HotSpotBackend backend;
63     private final HotSpotProviders providers;
64     private final HotSpotCodeCacheProvider codeCache;
65     private final PhaseSuite<HighTierContext> graphBuilderSuite;
66     private final HighTierContext highTierContext;
67 
AOTBackend(Main main, OptionValues graalOptions, HotSpotBackend backend, HotSpotInvokeDynamicPlugin inokeDynamicPlugin)68     AOTBackend(Main main, OptionValues graalOptions, HotSpotBackend backend, HotSpotInvokeDynamicPlugin inokeDynamicPlugin) {
69         this.main = main;
70         this.graalOptions = graalOptions;
71         this.backend = backend;
72         providers = backend.getProviders();
73         codeCache = providers.getCodeCache();
74         graphBuilderSuite = initGraphBuilderSuite(backend, main.options.compileWithAssertions, inokeDynamicPlugin);
75         highTierContext = new HighTierContext(providers, graphBuilderSuite, OptimisticOptimizations.ALL);
76     }
77 
getGraphBuilderSuite()78     PhaseSuite<HighTierContext> getGraphBuilderSuite() {
79         return graphBuilderSuite;
80     }
81 
getBackend()82     HotSpotBackend getBackend() {
83         return backend;
84     }
85 
getProviders()86     HotSpotProviders getProviders() {
87         return providers;
88     }
89 
getSuites()90     private Suites getSuites() {
91         // create suites every time, as we modify options for the compiler
92         return backend.getSuites().getDefaultSuites(graalOptions);
93     }
94 
getLirSuites()95     private LIRSuites getLirSuites() {
96         // create suites every time, as we modify options for the compiler
97         return backend.getSuites().getDefaultLIRSuites(graalOptions);
98     }
99 
100     @SuppressWarnings("try")
compileMethod(ResolvedJavaMethod resolvedMethod, DebugContext debug)101     CompilationResult compileMethod(ResolvedJavaMethod resolvedMethod, DebugContext debug) {
102         StructuredGraph graph = buildStructuredGraph(resolvedMethod, debug);
103         if (graph != null) {
104             return compileGraph(resolvedMethod, graph, debug);
105         }
106         return null;
107     }
108 
109     /**
110      * Build a structured graph for the member.
111      *
112      * @param javaMethod method for whose code the graph is to be created
113      * @param debug
114      * @return structured graph
115      */
116     @SuppressWarnings("try")
buildStructuredGraph(ResolvedJavaMethod javaMethod, DebugContext debug)117     private StructuredGraph buildStructuredGraph(ResolvedJavaMethod javaMethod, DebugContext debug) {
118         try (DebugContext.Scope s = debug.scope("AOTParseMethod")) {
119             StructuredGraph graph = new StructuredGraph.Builder(graalOptions, debug).method(javaMethod).useProfilingInfo(false).build();
120             graphBuilderSuite.apply(graph, highTierContext);
121             return graph;
122         } catch (Throwable e) {
123             main.handleError(javaMethod, e, " (building graph)");
124         }
125         return null;
126     }
127 
128     @SuppressWarnings("try")
compileGraph(ResolvedJavaMethod resolvedMethod, StructuredGraph graph, DebugContext debug)129     private CompilationResult compileGraph(ResolvedJavaMethod resolvedMethod, StructuredGraph graph, DebugContext debug) {
130         try (DebugContext.Scope s = debug.scope("AOTCompileMethod")) {
131             ProfilingInfo profilingInfo = DefaultProfilingInfo.get(TriState.FALSE);
132 
133             final boolean isImmutablePIC = true;
134             CompilationIdentifier id = new CompilationIdentifier() {
135                 @Override
136                 public String toString(Verbosity verbosity) {
137                     return resolvedMethod.getName();
138                 }
139             };
140             CompilationResult compilationResult = new CompilationResult(id, isImmutablePIC);
141 
142             return GraalCompiler.compileGraph(graph, resolvedMethod, providers, backend, graphBuilderSuite, OptimisticOptimizations.ALL, profilingInfo, getSuites(), getLirSuites(),
143                             compilationResult, CompilationResultBuilderFactory.Default, true);
144 
145         } catch (Throwable e) {
146             main.handleError(resolvedMethod, e, " (compiling graph)");
147         }
148         return null;
149     }
150 
initGraphBuilderSuite(HotSpotBackend backend, boolean compileWithAssertions, HotSpotInvokeDynamicPlugin inokeDynamicPlugin)151     private static PhaseSuite<HighTierContext> initGraphBuilderSuite(HotSpotBackend backend, boolean compileWithAssertions, HotSpotInvokeDynamicPlugin inokeDynamicPlugin) {
152         PhaseSuite<HighTierContext> graphBuilderSuite = backend.getSuites().getDefaultGraphBuilderSuite().copy();
153         ListIterator<BasePhase<? super HighTierContext>> iterator = graphBuilderSuite.findPhase(GraphBuilderPhase.class);
154         GraphBuilderConfiguration baseConfig = ((GraphBuilderPhase) iterator.previous()).getGraphBuilderConfig();
155 
156         // Use all default plugins.
157         Plugins plugins = baseConfig.getPlugins();
158         plugins.setInvokeDynamicPlugin(inokeDynamicPlugin);
159         GraphBuilderConfiguration aotConfig = GraphBuilderConfiguration.getDefault(plugins).withEagerResolving(true).withOmitAssertions(!compileWithAssertions);
160 
161         iterator.next();
162         iterator.remove();
163         iterator.add(new GraphBuilderPhase(aotConfig));
164 
165         return graphBuilderSuite;
166     }
167 
printCompiledMethod(HotSpotResolvedJavaMethod resolvedMethod, CompilationResult compResult)168     void printCompiledMethod(HotSpotResolvedJavaMethod resolvedMethod, CompilationResult compResult) {
169         // This is really not installing the method.
170         InstalledCode installedCode = codeCache.addCode(resolvedMethod, HotSpotCompiledCodeBuilder.createCompiledCode(codeCache, resolvedMethod, null, compResult, graalOptions), null, null);
171         String disassembly = codeCache.disassemble(installedCode);
172         if (disassembly != null) {
173             main.printer.printlnDebug(disassembly);
174         }
175     }
176 }
177