1 /* 2 * Copyright (c) 2016, 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 java.util.ArrayList; 26 27 import org.graalvm.compiler.core.common.alloc.RegisterAllocationConfig; 28 import org.graalvm.compiler.core.common.alloc.Trace; 29 import org.graalvm.compiler.core.common.alloc.TraceBuilderResult; 30 import org.graalvm.compiler.lir.LIR; 31 import org.graalvm.compiler.lir.alloc.trace.TraceAllocationPhase.TraceAllocationContext; 32 import org.graalvm.compiler.lir.gen.LIRGenerationResult; 33 import org.graalvm.compiler.lir.gen.LIRGeneratorTool.MoveFactory; 34 35 import jdk.vm.ci.code.TargetDescription; 36 import jdk.vm.ci.common.JVMCIError; 37 import jdk.vm.ci.meta.AllocatableValue; 38 39 /** 40 * Manages the selection of allocation strategies. 41 */ 42 public final class TraceRegisterAllocationPolicy { 43 44 protected abstract class AllocationStrategy { 45 TraceAllocationPhase<TraceAllocationContext> allocator; 46 47 public final TraceAllocationPhase<TraceAllocationContext> getAllocator() { 48 if (allocator == null) { 49 allocator = initAllocator(target, lirGenRes, spillMoveFactory, registerAllocationConfig, cachedStackSlots, resultTraces, neverSpillConstants, strategies); 50 } 51 return allocator; 52 } 53 54 protected final LIR getLIR() { 55 return lirGenRes.getLIR(); 56 } 57 58 protected final LIRGenerationResult getLIRGenerationResult() { 59 return lirGenRes; 60 } 61 62 protected final TraceBuilderResult getTraceBuilderResult() { 63 return resultTraces; 64 } 65 66 /** 67 * Returns {@code true} if the allocation strategy should be used for {@code trace}. 68 */ 69 public abstract boolean shouldApplyTo(Trace trace); 70 71 @SuppressWarnings("hiding") 72 protected abstract TraceAllocationPhase<TraceAllocationContext> initAllocator(TargetDescription target, LIRGenerationResult lirGenRes, MoveFactory spillMoveFactory, 73 RegisterAllocationConfig registerAllocationConfig, AllocatableValue[] cachedStackSlots, TraceBuilderResult resultTraces, boolean neverSpillConstant, 74 ArrayList<AllocationStrategy> strategies); 75 } 76 77 private final TargetDescription target; 78 private final LIRGenerationResult lirGenRes; 79 private final MoveFactory spillMoveFactory; 80 private final RegisterAllocationConfig registerAllocationConfig; 81 private final AllocatableValue[] cachedStackSlots; 82 private final TraceBuilderResult resultTraces; 83 private final boolean neverSpillConstants; 84 85 private final ArrayList<AllocationStrategy> strategies; 86 87 public TraceRegisterAllocationPolicy(TargetDescription target, LIRGenerationResult lirGenRes, MoveFactory spillMoveFactory, RegisterAllocationConfig registerAllocationConfig, 88 AllocatableValue[] cachedStackSlots, TraceBuilderResult resultTraces, boolean neverSpillConstant) { 89 this.target = target; 90 this.lirGenRes = lirGenRes; 91 this.spillMoveFactory = spillMoveFactory; 92 this.registerAllocationConfig = registerAllocationConfig; 93 this.cachedStackSlots = cachedStackSlots; 94 this.resultTraces = resultTraces; 95 this.neverSpillConstants = neverSpillConstant; 96 97 this.strategies = new ArrayList<>(3); 98 } 99 100 public void appendStrategy(AllocationStrategy strategy) { 101 strategies.add(strategy); 102 } 103 104 public TraceAllocationPhase<TraceAllocationContext> selectStrategy(Trace trace) { 105 for (AllocationStrategy strategy : strategies) { 106 if (strategy.shouldApplyTo(trace)) { 107 return strategy.getAllocator(); 108 } 109 } 110 throw JVMCIError.shouldNotReachHere("No Allocation Strategy found!"); 111 } 112 113 }