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