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) { | 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) { |