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.lsra;
  24 
  25 import org.graalvm.compiler.debug.Debug;
  26 import org.graalvm.compiler.debug.Indent;
  27 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
  28 import org.graalvm.compiler.lir.phases.AllocationPhase.AllocationContext;
  29 import org.graalvm.util.Pair;
  30 
  31 import jdk.vm.ci.code.TargetDescription;
  32 
  33 public final class LinearScanRegisterAllocationPhase extends LinearScanAllocationPhase {
  34 
  35     private final LinearScan allocator;
  36 
  37     LinearScanRegisterAllocationPhase(LinearScan allocator) {
  38         this.allocator = allocator;
  39     }
  40 
  41     @Override
  42     protected void run(TargetDescription target, LIRGenerationResult result, AllocationContext context) {
  43         allocator.printIntervals("Before register allocation");
  44         allocateRegisters();
  45         allocator.printIntervals("After register allocation");
  46     }
  47 
  48     @SuppressWarnings("try")
  49     void allocateRegisters() {
  50         try (Indent indent = Debug.logAndIndent("allocate registers")) {
  51             Interval precoloredIntervals;
  52             Interval notPrecoloredIntervals;
  53 
  54             Pair<Interval, Interval> result = allocator.createUnhandledLists(LinearScan.IS_PRECOLORED_INTERVAL, LinearScan.IS_VARIABLE_INTERVAL);
  55             precoloredIntervals = result.getLeft();
  56             notPrecoloredIntervals = result.getRight();
  57 
  58             // allocate cpu registers
  59             LinearScanWalker lsw;
  60             if (OptimizingLinearScanWalker.Options.LSRAOptimization.getValue(allocator.getOptions())) {
  61                 lsw = new OptimizingLinearScanWalker(allocator, precoloredIntervals, notPrecoloredIntervals);
  62             } else {
  63                 lsw = new LinearScanWalker(allocator, precoloredIntervals, notPrecoloredIntervals);
  64             }
  65             lsw.walk();
  66             lsw.finishAllocation();
  67         }
  68     }
  69 
  70 }