1 /*
   2  * Copyright (c) 2015, 2019, 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.BitSet;
  28 import java.util.EnumSet;
  29 
  30 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
  31 import org.graalvm.compiler.debug.DebugContext;
  32 import org.graalvm.compiler.lir.LIR;
  33 import org.graalvm.compiler.lir.LIRInstruction;
  34 import org.graalvm.compiler.lir.LIRInstruction.OperandFlag;
  35 import org.graalvm.compiler.lir.LIRInstruction.OperandMode;
  36 import org.graalvm.compiler.lir.StandardOp;
  37 import org.graalvm.compiler.lir.StandardOp.LabelOp;
  38 import org.graalvm.compiler.lir.alloc.lsra.Interval;
  39 import org.graalvm.compiler.lir.alloc.lsra.Interval.RegisterPriority;
  40 import org.graalvm.compiler.lir.alloc.lsra.LinearScan;
  41 import org.graalvm.compiler.lir.alloc.lsra.LinearScanLifetimeAnalysisPhase;
  42 import org.graalvm.compiler.lir.ssa.SSAUtil;
  43 
  44 import jdk.vm.ci.meta.AllocatableValue;
  45 import jdk.vm.ci.meta.Value;
  46 
  47 public class SSALinearScanLifetimeAnalysisPhase extends LinearScanLifetimeAnalysisPhase {
  48 
  49     SSALinearScanLifetimeAnalysisPhase(LinearScan linearScan) {
  50         super(linearScan);
  51     }
  52 
  53     @Override
  54     protected void addRegisterHint(final LIRInstruction op, final Value targetValue, OperandMode mode, EnumSet<OperandFlag> flags, final boolean hintAtDef) {
  55         super.addRegisterHint(op, targetValue, mode, flags, hintAtDef);
  56 
  57         if (hintAtDef && op instanceof LabelOp) {
  58             LabelOp label = (LabelOp) op;
  59             if (!label.isPhiIn()) {
  60                 return;
  61             }
  62 
  63             Interval to = allocator.getOrCreateInterval((AllocatableValue) targetValue);
  64 
  65             LIR lir = allocator.getLIR();
  66             AbstractBlockBase<?> block = allocator.blockForId(label.id());
  67             assert mode == OperandMode.DEF : "Wrong operand mode: " + mode;
  68             assert lir.getLIRforBlock(block).get(0).equals(label) : String.format("Block %s and Label %s do not match!", block, label);
  69 
  70             int idx = SSAUtil.indexOfValue(label, targetValue);
  71             assert idx >= 0 : String.format("Value %s not in label %s", targetValue, label);
  72 
  73             BitSet blockLiveIn = allocator.getBlockData(block).liveIn;
  74 
  75             AbstractBlockBase<?> selectedPredecessor = null;
  76             AllocatableValue selectedSource = null;
  77             for (AbstractBlockBase<?> pred : block.getPredecessors()) {
  78                 if (selectedPredecessor == null || pred.getRelativeFrequency() > selectedPredecessor.getRelativeFrequency()) {
  79                     StandardOp.JumpOp jump = SSAUtil.phiOut(lir, pred);
  80                     Value sourceValue = jump.getOutgoingValue(idx);
  81                     if (LinearScan.isVariableOrRegister(sourceValue) && !blockLiveIn.get(getOperandNumber(sourceValue))) {
  82                         selectedSource = (AllocatableValue) sourceValue;
  83                         selectedPredecessor = pred;
  84                     }
  85                 }
  86             }
  87             if (selectedSource != null) {
  88                 Interval from = allocator.getOrCreateInterval(selectedSource);
  89                 setHint(debug, op, to, from);
  90                 setHint(debug, op, from, to);
  91             }
  92         }
  93     }
  94 
  95     public static void setHint(DebugContext debug, final LIRInstruction op, Interval target, Interval source) {
  96         Interval currentHint = target.locationHint(false);
  97         if (currentHint == null || currentHint.from() > target.from()) {
  98             /*
  99              * Update hint if there was none or if the hint interval starts after the hinted
 100              * interval.
 101              */
 102             target.setLocationHint(source);
 103             if (debug.isLogEnabled()) {
 104                 debug.log("operation at opId %d: added hint from interval %d to %d", op.id(), source.operandNumber, target.operandNumber);
 105             }
 106         }
 107     }
 108 
 109     @Override
 110     protected RegisterPriority registerPriorityOfOutputOperand(LIRInstruction op) {
 111         if (op instanceof LabelOp) {
 112             LabelOp label = (LabelOp) op;
 113             if (label.isPhiIn()) {
 114                 return RegisterPriority.None;
 115             }
 116         }
 117         return super.registerPriorityOfOutputOperand(op);
 118     }
 119 }