src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core/src/org/graalvm/compiler/core/gen/LIRCompilerBackend.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58299 6df94ce3ab2f
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
     1
/*
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 55509
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
     4
 *
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
     8
 *
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    13
 * accompanied this code).
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    14
 *
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    18
 *
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    21
 * questions.
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    22
 */
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    23
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    24
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    25
package org.graalvm.compiler.core.gen;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    26
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    27
import java.util.Collection;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    28
import java.util.List;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    29
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    30
import jdk.internal.vm.compiler.collections.EconomicSet;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    31
import org.graalvm.compiler.code.CompilationResult;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    32
import org.graalvm.compiler.core.LIRGenerationPhase;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    33
import org.graalvm.compiler.core.LIRGenerationPhase.LIRGenerationContext;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    34
import org.graalvm.compiler.core.common.GraalOptions;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    35
import org.graalvm.compiler.core.common.alloc.ComputeBlockOrder;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    36
import org.graalvm.compiler.core.common.alloc.RegisterAllocationConfig;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    37
import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    38
import org.graalvm.compiler.core.target.Backend;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    39
import org.graalvm.compiler.debug.CounterKey;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    40
import org.graalvm.compiler.debug.DebugCloseable;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    41
import org.graalvm.compiler.debug.DebugContext;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    42
import org.graalvm.compiler.debug.GraalError;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    43
import org.graalvm.compiler.debug.TimerKey;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    44
import org.graalvm.compiler.lir.LIR;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    45
import org.graalvm.compiler.lir.alloc.OutOfRegistersException;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    46
import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    47
import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    48
import org.graalvm.compiler.lir.framemap.FrameMap;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    49
import org.graalvm.compiler.lir.gen.LIRGenerationResult;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    50
import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    51
import org.graalvm.compiler.lir.phases.AllocationPhase.AllocationContext;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    52
import org.graalvm.compiler.lir.phases.LIRSuites;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    53
import org.graalvm.compiler.lir.phases.PostAllocationOptimizationPhase.PostAllocationOptimizationContext;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    54
import org.graalvm.compiler.lir.phases.PreAllocationOptimizationPhase.PreAllocationOptimizationContext;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    55
import org.graalvm.compiler.nodes.StructuredGraph;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    56
import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    57
import org.graalvm.compiler.nodes.cfg.Block;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    58
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    59
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    60
import jdk.vm.ci.code.RegisterConfig;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    61
import jdk.vm.ci.code.TargetDescription;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    62
import jdk.vm.ci.code.site.ConstantReference;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    63
import jdk.vm.ci.code.site.DataPatch;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    64
import jdk.vm.ci.meta.Assumptions;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    65
import jdk.vm.ci.meta.JavaConstant;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    66
import jdk.vm.ci.meta.JavaKind;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    67
import jdk.vm.ci.meta.ResolvedJavaField;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    68
import jdk.vm.ci.meta.ResolvedJavaMethod;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    69
import jdk.vm.ci.meta.SpeculationLog;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    70
import jdk.vm.ci.meta.VMConstant;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    71
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    72
public class LIRCompilerBackend {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    73
    private static final TimerKey EmitLIR = DebugContext.timer("EmitLIR").doc("Time spent generating LIR from HIR.");
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    74
    private static final TimerKey EmitCode = DebugContext.timer("EmitCode").doc("Time spent generating machine code from LIR.");
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    75
    private static final TimerKey BackEnd = DebugContext.timer("BackEnd").doc("Time spent in EmitLIR and EmitCode.");
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    76
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    77
    @SuppressWarnings("try")
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    78
    public static <T extends CompilationResult> void emitBackEnd(StructuredGraph graph, Object stub, ResolvedJavaMethod installedCodeOwner, Backend backend, T compilationResult,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    79
                    CompilationResultBuilderFactory factory, RegisterConfig registerConfig, LIRSuites lirSuites) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    80
        DebugContext debug = graph.getDebug();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    81
        try (DebugContext.Scope s = debug.scope("BackEnd", graph.getLastSchedule()); DebugCloseable a = BackEnd.start(debug)) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    82
            LIRGenerationResult lirGen = null;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    83
            lirGen = emitLIR(backend, graph, stub, registerConfig, lirSuites);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    84
            try (DebugContext.Scope s2 = debug.scope("CodeGen", lirGen, lirGen.getLIR())) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    85
                int bytecodeSize = graph.method() == null ? 0 : graph.getBytecodeSize();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    86
                compilationResult.setHasUnsafeAccess(graph.hasUnsafeAccess());
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    87
                emitCode(backend,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    88
                                graph.getAssumptions(),
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    89
                                graph.method(),
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    90
                                graph.getMethods(),
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    91
                                graph.getFields(),
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    92
                                graph.getSpeculationLog(),
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    93
                                bytecodeSize,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    94
                                lirGen,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    95
                                compilationResult,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    96
                                installedCodeOwner,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    97
                                factory);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    98
            } catch (Throwable e) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
    99
                throw debug.handle(e);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   100
            }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   101
        } catch (Throwable e) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   102
            throw debug.handle(e);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   103
        } finally {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   104
            graph.checkCancellation();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   105
        }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   106
    }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   107
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   108
    @SuppressWarnings("try")
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   109
    public static LIRGenerationResult emitLIR(Backend backend, StructuredGraph graph, Object stub, RegisterConfig registerConfig, LIRSuites lirSuites) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   110
        String registerPressure = GraalOptions.RegisterPressure.getValue(graph.getOptions());
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   111
        String[] allocationRestrictedTo = registerPressure == null ? null : registerPressure.split(",");
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   112
        try {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   113
            return emitLIR0(backend, graph, stub, registerConfig, lirSuites, allocationRestrictedTo);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   114
        } catch (OutOfRegistersException e) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   115
            if (allocationRestrictedTo != null) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   116
                allocationRestrictedTo = null;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   117
                return emitLIR0(backend, graph, stub, registerConfig, lirSuites, allocationRestrictedTo);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   118
            }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   119
            /* If the re-execution fails we convert the exception into a "hard" failure */
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   120
            throw new GraalError(e);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   121
        } finally {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   122
            graph.checkCancellation();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   123
        }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   124
    }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   125
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   126
    @SuppressWarnings("try")
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54084
diff changeset
   127
    private static LIRGenerationResult emitLIR0(Backend backend,
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54084
diff changeset
   128
                    StructuredGraph graph,
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54084
diff changeset
   129
                    Object stub,
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54084
diff changeset
   130
                    RegisterConfig registerConfig,
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54084
diff changeset
   131
                    LIRSuites lirSuites,
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   132
                    String[] allocationRestrictedTo) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   133
        DebugContext debug = graph.getDebug();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   134
        try (DebugContext.Scope ds = debug.scope("EmitLIR"); DebugCloseable a = EmitLIR.start(debug)) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   135
            assert !graph.hasValueProxies();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   136
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   137
            ScheduleResult schedule = graph.getLastSchedule();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   138
            Block[] blocks = schedule.getCFG().getBlocks();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   139
            Block startBlock = schedule.getCFG().getStartBlock();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   140
            assert startBlock != null;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   141
            assert startBlock.getPredecessorCount() == 0;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   142
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   143
            AbstractBlockBase<?>[] codeEmittingOrder = ComputeBlockOrder.computeCodeEmittingOrder(blocks.length, startBlock);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   144
            AbstractBlockBase<?>[] linearScanOrder = ComputeBlockOrder.computeLinearScanOrder(blocks.length, startBlock);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   145
            LIR lir = new LIR(schedule.getCFG(), linearScanOrder, codeEmittingOrder, graph.getOptions(), graph.getDebug());
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   146
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   147
            LIRGenerationProvider lirBackend = (LIRGenerationProvider) backend;
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 55509
diff changeset
   148
            RegisterAllocationConfig registerAllocationConfig = backend.newRegisterAllocationConfig(registerConfig, allocationRestrictedTo);
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 55509
diff changeset
   149
            LIRGenerationResult lirGenRes = lirBackend.newLIRGenerationResult(graph.compilationId(), lir, registerAllocationConfig, graph, stub);
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   150
            LIRGeneratorTool lirGen = lirBackend.newLIRGenerator(lirGenRes);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   151
            NodeLIRBuilderTool nodeLirGen = lirBackend.newNodeLIRBuilder(graph, lirGen);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   152
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   153
            // LIR generation
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   154
            LIRGenerationContext context = new LIRGenerationContext(lirGen, nodeLirGen, graph, schedule);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   155
            new LIRGenerationPhase().apply(backend.getTarget(), lirGenRes, context);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   156
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   157
            try (DebugContext.Scope s = debug.scope("LIRStages", nodeLirGen, lirGenRes, lir)) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   158
                // Dump LIR along with HIR (the LIR is looked up from context)
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   159
                debug.dump(DebugContext.BASIC_LEVEL, graph.getLastSchedule(), "After LIR generation");
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 55509
diff changeset
   160
                LIRGenerationResult result = emitLowLevel(backend.getTarget(), lirGenRes, lirGen, lirSuites, registerAllocationConfig);
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   161
                return result;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   162
            } catch (Throwable e) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   163
                throw debug.handle(e);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   164
            }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   165
        } catch (Throwable e) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   166
            throw debug.handle(e);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   167
        } finally {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   168
            graph.checkCancellation();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   169
        }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   170
    }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   171
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   172
    private static LIRGenerationResult emitLowLevel(TargetDescription target, LIRGenerationResult lirGenRes, LIRGeneratorTool lirGen, LIRSuites lirSuites,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   173
                    RegisterAllocationConfig registerAllocationConfig) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   174
        DebugContext debug = lirGenRes.getLIR().getDebug();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   175
        PreAllocationOptimizationContext preAllocOptContext = new PreAllocationOptimizationContext(lirGen);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   176
        lirSuites.getPreAllocationOptimizationStage().apply(target, lirGenRes, preAllocOptContext);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   177
        debug.dump(DebugContext.BASIC_LEVEL, lirGenRes.getLIR(), "After PreAllocationOptimizationStage");
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   178
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   179
        AllocationContext allocContext = new AllocationContext(lirGen.getSpillMoveFactory(), registerAllocationConfig);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   180
        lirSuites.getAllocationStage().apply(target, lirGenRes, allocContext);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   181
        debug.dump(DebugContext.BASIC_LEVEL, lirGenRes.getLIR(), "After AllocationStage");
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   182
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   183
        PostAllocationOptimizationContext postAllocOptContext = new PostAllocationOptimizationContext(lirGen);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   184
        lirSuites.getPostAllocationOptimizationStage().apply(target, lirGenRes, postAllocOptContext);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   185
        debug.dump(DebugContext.BASIC_LEVEL, lirGenRes.getLIR(), "After PostAllocationOptimizationStage");
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   186
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   187
        return lirGenRes;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   188
    }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   189
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   190
    @SuppressWarnings("try")
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   191
    public static void emitCode(Backend backend,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   192
                    Assumptions assumptions,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   193
                    ResolvedJavaMethod rootMethod,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   194
                    Collection<ResolvedJavaMethod> inlinedMethods,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   195
                    EconomicSet<ResolvedJavaField> accessedFields,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   196
                    SpeculationLog speculationLog,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   197
                    int bytecodeSize,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   198
                    LIRGenerationResult lirGenRes,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   199
                    CompilationResult compilationResult,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   200
                    ResolvedJavaMethod installedCodeOwner,
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   201
                    CompilationResultBuilderFactory factory) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   202
        DebugContext debug = lirGenRes.getLIR().getDebug();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   203
        try (DebugCloseable a = EmitCode.start(debug)) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   204
            LIRGenerationProvider lirBackend = (LIRGenerationProvider) backend;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   205
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   206
            FrameMap frameMap = lirGenRes.getFrameMap();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   207
            CompilationResultBuilder crb = lirBackend.newCompilationResultBuilder(lirGenRes, frameMap, compilationResult, factory);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   208
            lirBackend.emitCode(crb, lirGenRes.getLIR(), installedCodeOwner);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   209
            if (assumptions != null && !assumptions.isEmpty()) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   210
                compilationResult.setAssumptions(assumptions.toArray());
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   211
            }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   212
            if (rootMethod != null) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   213
                compilationResult.setMethods(rootMethod, inlinedMethods);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   214
                compilationResult.setFields(accessedFields);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   215
                compilationResult.setBytecodeSize(bytecodeSize);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   216
            }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   217
            if (speculationLog != null) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   218
                compilationResult.setSpeculationLog(speculationLog);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   219
            }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   220
            crb.finish();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   221
            if (debug.isCountEnabled()) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   222
                List<DataPatch> ldp = compilationResult.getDataPatches();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   223
                JavaKind[] kindValues = JavaKind.values();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   224
                CounterKey[] dms = new CounterKey[kindValues.length];
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   225
                for (int i = 0; i < dms.length; i++) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   226
                    dms[i] = DebugContext.counter("DataPatches-%s", kindValues[i]);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   227
                }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   228
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   229
                for (DataPatch dp : ldp) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   230
                    JavaKind kind = JavaKind.Illegal;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   231
                    if (dp.reference instanceof ConstantReference) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   232
                        VMConstant constant = ((ConstantReference) dp.reference).getConstant();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   233
                        if (constant instanceof JavaConstant) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   234
                            kind = ((JavaConstant) constant).getJavaKind();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   235
                        }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   236
                    }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   237
                    dms[kind.ordinal()].add(debug, 1);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   238
                }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   239
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   240
                DebugContext.counter("CompilationResults").increment(debug);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   241
                DebugContext.counter("CodeBytesEmitted").add(debug, compilationResult.getTargetCodeSize());
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   242
                DebugContext.counter("InfopointsEmitted").add(debug, compilationResult.getInfopoints().size());
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   243
                DebugContext.counter("DataPatches").add(debug, ldp.size());
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   244
                DebugContext.counter("ExceptionHandlersEmitted").add(debug, compilationResult.getExceptionHandlers().size());
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   245
            }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   246
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   247
            debug.dump(DebugContext.BASIC_LEVEL, compilationResult, "After code generation");
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   248
        }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   249
    }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents:
diff changeset
   250
}