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, livenessInfo, 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 GlobalLivenessInfo livenessInfo, 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 private final GlobalLivenessInfo livenessInfo; 85 86 private final ArrayList<AllocationStrategy> strategies; 87 88 public TraceRegisterAllocationPolicy(TargetDescription target, LIRGenerationResult lirGenRes, MoveFactory spillMoveFactory, RegisterAllocationConfig registerAllocationConfig, 89 AllocatableValue[] cachedStackSlots, TraceBuilderResult resultTraces, boolean neverSpillConstant, GlobalLivenessInfo livenessInfo) { 90 this.target = target; 91 this.lirGenRes = lirGenRes; 92 this.spillMoveFactory = spillMoveFactory; 93 this.registerAllocationConfig = registerAllocationConfig; 94 this.cachedStackSlots = cachedStackSlots; 95 this.resultTraces = resultTraces; 96 this.neverSpillConstants = neverSpillConstant; 97 this.livenessInfo = livenessInfo; 98 99 this.strategies = new ArrayList<>(3); 100 } 101 102 public void appendStrategy(AllocationStrategy strategy) { 103 strategies.add(strategy); 104 } 105 106 public TraceAllocationPhase<TraceAllocationContext> selectStrategy(Trace trace) { 107 for (AllocationStrategy strategy : strategies) { 108 if (strategy.shouldApplyTo(trace)) { 109 return strategy.getAllocator(); 110 } 111 } 112 throw JVMCIError.shouldNotReachHere("No Allocation Strategy found!"); 113 } 114 115 } | 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 import org.graalvm.compiler.options.OptionValues; 35 36 import jdk.vm.ci.code.TargetDescription; 37 import jdk.vm.ci.common.JVMCIError; 38 import jdk.vm.ci.meta.AllocatableValue; 39 40 /** 41 * Manages the selection of allocation strategies. 42 */ 43 public final class TraceRegisterAllocationPolicy { 44 45 protected abstract class AllocationStrategy { 46 TraceAllocationPhase<TraceAllocationContext> allocator; 47 48 public final TraceAllocationPhase<TraceAllocationContext> getAllocator() { 49 if (allocator == null) { 50 allocator = initAllocator(target, lirGenRes, spillMoveFactory, registerAllocationConfig, cachedStackSlots, resultTraces, neverSpillConstants, livenessInfo, strategies); 51 } 52 return allocator; 53 } 54 55 protected final LIR getLIR() { 56 return lirGenRes.getLIR(); 57 } 58 59 protected final LIRGenerationResult getLIRGenerationResult() { 60 return lirGenRes; 61 } 62 63 protected final TraceBuilderResult getTraceBuilderResult() { 64 return resultTraces; 65 } 66 67 protected final GlobalLivenessInfo getGlobalLivenessInfo() { 68 return livenessInfo; 69 } 70 71 protected final RegisterAllocationConfig getRegisterAllocationConfig() { 72 return registerAllocationConfig; 73 } 74 75 protected final TargetDescription getTarget() { 76 return target; 77 } 78 79 /** 80 * Returns {@code true} if the allocation strategy should be used for {@code trace}. 81 * 82 * This method must not alter any state of the strategy, nor rely on being called in a 83 * specific trace order. 84 */ 85 public abstract boolean shouldApplyTo(Trace trace); 86 87 @SuppressWarnings("hiding") 88 protected abstract TraceAllocationPhase<TraceAllocationContext> initAllocator(TargetDescription target, LIRGenerationResult lirGenRes, MoveFactory spillMoveFactory, 89 RegisterAllocationConfig registerAllocationConfig, AllocatableValue[] cachedStackSlots, TraceBuilderResult resultTraces, boolean neverSpillConstant, 90 GlobalLivenessInfo livenessInfo, ArrayList<AllocationStrategy> strategies); 91 } 92 93 private final TargetDescription target; 94 private final LIRGenerationResult lirGenRes; 95 private final MoveFactory spillMoveFactory; 96 private final RegisterAllocationConfig registerAllocationConfig; 97 private final AllocatableValue[] cachedStackSlots; 98 private final TraceBuilderResult resultTraces; 99 private final boolean neverSpillConstants; 100 private final GlobalLivenessInfo livenessInfo; 101 102 private final ArrayList<AllocationStrategy> strategies; 103 104 public TraceRegisterAllocationPolicy(TargetDescription target, LIRGenerationResult lirGenRes, MoveFactory spillMoveFactory, RegisterAllocationConfig registerAllocationConfig, 105 AllocatableValue[] cachedStackSlots, TraceBuilderResult resultTraces, boolean neverSpillConstant, GlobalLivenessInfo livenessInfo) { 106 this.target = target; 107 this.lirGenRes = lirGenRes; 108 this.spillMoveFactory = spillMoveFactory; 109 this.registerAllocationConfig = registerAllocationConfig; 110 this.cachedStackSlots = cachedStackSlots; 111 this.resultTraces = resultTraces; 112 this.neverSpillConstants = neverSpillConstant; 113 this.livenessInfo = livenessInfo; 114 115 this.strategies = new ArrayList<>(3); 116 } 117 118 protected OptionValues getOptions() { 119 return lirGenRes.getLIR().getOptions(); 120 } 121 122 public void appendStrategy(AllocationStrategy strategy) { 123 strategies.add(strategy); 124 } 125 126 public TraceAllocationPhase<TraceAllocationContext> selectStrategy(Trace trace) { 127 for (AllocationStrategy strategy : strategies) { 128 if (strategy.shouldApplyTo(trace)) { 129 return strategy.getAllocator(); 130 } 131 } 132 throw JVMCIError.shouldNotReachHere("No Allocation Strategy found!"); 133 } 134 135 } |