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