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