1 /* 2 * Copyright (c) 2015, 2015, 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 package org.graalvm.compiler.lir.alloc.trace; 24 25 import org.graalvm.compiler.core.common.alloc.RegisterAllocationConfig; 26 import org.graalvm.compiler.core.common.alloc.Trace; 27 import org.graalvm.compiler.core.common.alloc.TraceBuilderResult; 28 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase; 29 import org.graalvm.compiler.debug.Debug; 30 import org.graalvm.compiler.debug.Debug.Scope; 31 import org.graalvm.compiler.debug.DebugCounter; 32 import org.graalvm.compiler.debug.Indent; 33 import org.graalvm.compiler.lir.LIR; 34 import org.graalvm.compiler.lir.alloc.trace.TraceAllocationPhase.TraceAllocationContext; 35 import org.graalvm.compiler.lir.gen.LIRGenerationResult; 36 import org.graalvm.compiler.lir.gen.LIRGeneratorTool.MoveFactory; 37 import org.graalvm.compiler.lir.phases.AllocationPhase; 38 import org.graalvm.compiler.lir.ssa.SSAUtil; 39 import org.graalvm.compiler.options.Option; 40 import org.graalvm.compiler.options.OptionKey; 41 import org.graalvm.compiler.options.OptionType; 42 43 import jdk.vm.ci.code.TargetDescription; 44 import jdk.vm.ci.meta.AllocatableValue; 45 46 /** 47 * Implements the Trace Register Allocation approach as described in 48 * <a href="http://dx.doi.org/10.1145/2972206.2972211">"Trace-based Register Allocation in a JIT 49 * Compiler"</a> by Josef Eisl et al. 50 */ 51 public final class TraceRegisterAllocationPhase extends AllocationPhase { 52 53 public static class Options { 54 // @formatter:off 55 @Option(help = "Use inter-trace register hints.", type = OptionType.Debug) 56 public static final OptionKey<Boolean> TraceRAuseInterTraceHints = new OptionKey<>(true); 57 @Option(help = "Share information about spilled values to other traces.", type = OptionType.Debug) 58 public static final OptionKey<Boolean> TraceRAshareSpillInformation = new OptionKey<>(true); 59 @Option(help = "Reuse spill slots for global move resolution cycle breaking.", type = OptionType.Debug) 60 public static final OptionKey<Boolean> TraceRAreuseStackSlotsForMoveResolutionCycleBreaking = new OptionKey<>(true); 61 @Option(help = "Cache stack slots globally (i.e. a variable always gets the same slot in every trace).", type = OptionType.Debug) 62 public static final OptionKey<Boolean> TraceRACacheStackSlots = new OptionKey<>(true); 63 // @formatter:on 64 } 65 66 private static final DebugCounter tracesCounter = Debug.counter("TraceRA[traces]"); 67 68 public static final DebugCounter globalStackSlots = Debug.counter("TraceRA[GlobalStackSlots]"); 69 public static final DebugCounter allocatedStackSlots = Debug.counter("TraceRA[AllocatedStackSlots]"); 70 71 @Override 72 @SuppressWarnings("try") 73 protected void run(TargetDescription target, LIRGenerationResult lirGenRes, AllocationContext context) { 74 MoveFactory spillMoveFactory = context.spillMoveFactory; 75 RegisterAllocationConfig registerAllocationConfig = context.registerAllocationConfig; 76 LIR lir = lirGenRes.getLIR(); 77 TraceBuilderResult resultTraces = context.contextLookup(TraceBuilderResult.class); 78 GlobalLivenessInfo livenessInfo = context.contextLookup(GlobalLivenessInfo.class); 79 assert livenessInfo != null; 80 TraceAllocationContext traceContext = new TraceAllocationContext(spillMoveFactory, registerAllocationConfig, resultTraces, livenessInfo); 81 AllocatableValue[] cachedStackSlots = Options.TraceRACacheStackSlots.getValue(lir.getOptions()) ? new AllocatableValue[lir.numVariables()] : null; 82 83 // currently this is not supported 84 boolean neverSpillConstant = false; 85 86 final TraceRegisterAllocationPolicy plan = DefaultTraceRegisterAllocationPolicy.allocationPolicy(target, lirGenRes, spillMoveFactory, registerAllocationConfig, cachedStackSlots, resultTraces, 87 neverSpillConstant, livenessInfo, lir.getOptions()); 88 89 try (Scope s0 = Debug.scope("AllocateTraces", resultTraces, livenessInfo)) { 90 for (Trace trace : resultTraces.getTraces()) { 91 tracesCounter.increment(); 92 TraceAllocationPhase<TraceAllocationContext> allocator = plan.selectStrategy(trace); 93 try (Indent i = Debug.logAndIndent("Allocating Trace%d: %s (%s)", trace.getId(), trace, allocator); Scope s = Debug.scope("AllocateTrace", trace)) { 94 allocator.apply(target, lirGenRes, trace, traceContext); 95 } 96 } 97 } catch (Throwable e) { 98 throw Debug.handle(e); 99 } 100 101 TraceGlobalMoveResolutionPhase.resolve(target, lirGenRes, traceContext); 102 deconstructSSAForm(lir); 103 } 104 105 /** 106 * Remove Phi In/Out. 107 */ 108 private static void deconstructSSAForm(LIR lir) { 109 for (AbstractBlockBase<?> block : lir.getControlFlowGraph().getBlocks()) { 110 if (SSAUtil.isMerge(block)) { 111 SSAUtil.phiIn(lir, block).clearIncomingValues(); 112 for (AbstractBlockBase<?> pred : block.getPredecessors()) { 113 SSAUtil.phiOut(lir, pred).clearOutgoingValues(); 114 } 115 } 116 } 117 } 118 119 }