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.ssa;
  24 
  25 import java.util.EnumSet;
  26 
  27 import org.graalvm.compiler.debug.Debug;
  28 import org.graalvm.compiler.lir.LIRInstruction;
  29 import org.graalvm.compiler.lir.LIRInstruction.OperandFlag;
  30 import org.graalvm.compiler.lir.LIRInstruction.OperandMode;
  31 import org.graalvm.compiler.lir.StandardOp.LabelOp;
  32 import org.graalvm.compiler.lir.ValueConsumer;
  33 import org.graalvm.compiler.lir.alloc.lsra.Interval;
  34 import org.graalvm.compiler.lir.alloc.lsra.Interval.RegisterPriority;
  35 import org.graalvm.compiler.lir.alloc.lsra.LinearScan;
  36 import org.graalvm.compiler.lir.alloc.lsra.LinearScanLifetimeAnalysisPhase;
  37 import org.graalvm.compiler.lir.ssa.SSAUtil;
  38 
  39 import jdk.vm.ci.meta.AllocatableValue;
  40 import jdk.vm.ci.meta.Value;
  41 
  42 public class SSALinearScanLifetimeAnalysisPhase extends LinearScanLifetimeAnalysisPhase {
  43 
  44     SSALinearScanLifetimeAnalysisPhase(LinearScan linearScan) {
  45         super(linearScan);
  46     }
  47 
  48     @Override
  49     protected void addRegisterHint(final LIRInstruction op, final Value targetValue, OperandMode mode, EnumSet<OperandFlag> flags, final boolean hintAtDef) {
  50         super.addRegisterHint(op, targetValue, mode, flags, hintAtDef);
  51 
  52         if (hintAtDef && op instanceof LabelOp) {
  53             LabelOp label = (LabelOp) op;
  54 
  55             Interval to = allocator.getOrCreateInterval((AllocatableValue) targetValue);
  56 
  57             SSAUtil.forEachPhiRegisterHint(allocator.getLIR(), allocator.blockForId(label.id()), label, targetValue, mode, (ValueConsumer) (registerHint, valueMode, valueFlags) -> {
  58                 if (LinearScan.isVariableOrRegister(registerHint)) {
  59                     Interval from = allocator.getOrCreateInterval((AllocatableValue) registerHint);
  60 
  61                     setHint(op, to, from);
  62                     setHint(op, from, to);
  63                 }
  64             });
  65         }
  66     }
  67 
  68     public static void setHint(final LIRInstruction op, Interval target, Interval source) {
  69         Interval currentHint = target.locationHint(false);
  70         if (currentHint == null || currentHint.from() > target.from()) {
  71             /*
  72              * Update hint if there was none or if the hint interval starts after the hinted
  73              * interval.
  74              */
  75             target.setLocationHint(source);
  76             if (Debug.isLogEnabled()) {
  77                 Debug.log("operation at opId %d: added hint from interval %d to %d", op.id(), source.operandNumber, target.operandNumber);
  78             }
  79         }
  80     }
  81 
  82     @Override
  83     protected RegisterPriority registerPriorityOfOutputOperand(LIRInstruction op) {
  84         if (op instanceof LabelOp) {
  85             LabelOp label = (LabelOp) op;
  86             if (label.isPhiIn()) {
  87                 return RegisterPriority.None;
  88             }
  89         }
  90         return super.registerPriorityOfOutputOperand(op);
  91     }
  92 }