--- old/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.lir/src/org/graalvm/compiler/lir/alloc/trace/lsra/TraceLinearScanAssignLocationsPhase.java 2017-03-20 17:39:58.000000000 -0700 +++ new/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.lir/src/org/graalvm/compiler/lir/alloc/trace/lsra/TraceLinearScanAssignLocationsPhase.java 2017-03-20 17:39:58.000000000 -0700 @@ -22,17 +22,16 @@ */ package org.graalvm.compiler.lir.alloc.trace.lsra; +import static jdk.vm.ci.code.ValueUtil.isIllegal; +import static jdk.vm.ci.code.ValueUtil.isRegister; import static org.graalvm.compiler.lir.LIRValueUtil.isConstantValue; import static org.graalvm.compiler.lir.LIRValueUtil.isStackSlotValue; import static org.graalvm.compiler.lir.LIRValueUtil.isVariable; import static org.graalvm.compiler.lir.LIRValueUtil.isVirtualStackSlot; -import static org.graalvm.compiler.lir.alloc.trace.TraceRegisterAllocationPhase.Options.TraceRAshareSpillInformation; -import static jdk.vm.ci.code.ValueUtil.isIllegal; -import static jdk.vm.ci.code.ValueUtil.isRegister; +import java.util.ArrayList; import java.util.Collections; import java.util.EnumSet; -import java.util.List; import org.graalvm.compiler.core.common.alloc.Trace; import org.graalvm.compiler.core.common.cfg.AbstractBlockBase; @@ -46,11 +45,11 @@ import org.graalvm.compiler.lir.LIRInstruction.OperandFlag; import org.graalvm.compiler.lir.LIRInstruction.OperandMode; import org.graalvm.compiler.lir.StandardOp; -import org.graalvm.compiler.lir.StandardOp.BlockEndOp; import org.graalvm.compiler.lir.StandardOp.LabelOp; import org.graalvm.compiler.lir.StandardOp.MoveOp; import org.graalvm.compiler.lir.StandardOp.ValueMoveOp; import org.graalvm.compiler.lir.Variable; +import org.graalvm.compiler.lir.alloc.trace.GlobalLivenessInfo; import org.graalvm.compiler.lir.alloc.trace.ShadowedRegisterValue; import org.graalvm.compiler.lir.alloc.trace.lsra.TraceLinearScanPhase.TraceLinearScan; import org.graalvm.compiler.lir.gen.LIRGenerationResult; @@ -73,7 +72,7 @@ protected void run(TargetDescription target, LIRGenerationResult lirGenRes, Trace trace, TraceLinearScanAllocationContext context) { TraceLinearScan allocator = context.allocator; MoveFactory spillMoveFactory = context.spillMoveFactory; - new Assigner(allocator, spillMoveFactory).assignLocations(); + new Assigner(allocator, spillMoveFactory).assign(); } private static final class Assigner { @@ -106,6 +105,10 @@ interval = allocator.splitChildAtOpId(interval, opId, mode); } + return getLocation(op, interval, mode); + } + + private static Value getLocation(LIRInstruction op, TraceInterval interval, OperandMode mode) { if (isIllegal(interval.location()) && interval.canMaterialize()) { if (op instanceof LabelOp) { /* @@ -163,26 +166,87 @@ info.forEachState(op, this::debugInfoProcedure); } - private void assignLocations(List instructions) { - int numInst = instructions.size(); - boolean hasDead = false; - - for (int j = 0; j < numInst; j++) { - final LIRInstruction op = instructions.get(j); - if (op == null) { - /* - * this can happen when spill-moves are removed in eliminateSpillMoves - */ - hasDead = true; - } else if (assignLocations(op, instructions, j)) { - hasDead = true; + @SuppressWarnings("try") + private void assignBlock(AbstractBlockBase block) { + try (Indent indent2 = Debug.logAndIndent("assign locations in block B%d", block.getId())) { + ArrayList instructions = allocator.getLIR().getLIRforBlock(block); + handleBlockBegin(block, instructions); + int numInst = instructions.size(); + boolean hasDead = false; + + for (int j = 0; j < numInst; j++) { + final LIRInstruction op = instructions.get(j); + if (op == null) { + /* + * this can happen when spill-moves are removed in eliminateSpillMoves + */ + hasDead = true; + } else if (assignLocations(op, instructions, j)) { + hasDead = true; + } } + handleBlockEnd(block, instructions); + + if (hasDead) { + // Remove null values from the list. + instructions.removeAll(Collections.singleton(null)); + } + } + } + + private void handleBlockBegin(AbstractBlockBase block, ArrayList instructions) { + if (allocator.hasInterTracePredecessor(block)) { + /* Only materialize the locations array if there is an incoming inter-trace edge. */ + assert instructions.equals(allocator.getLIR().getLIRforBlock(block)); + GlobalLivenessInfo li = allocator.getGlobalLivenessInfo(); + LIRInstruction instruction = instructions.get(0); + OperandMode mode = OperandMode.DEF; + int[] live = li.getBlockIn(block); + Value[] values = calculateBlockBoundaryValues(instruction, live, mode); + li.setInLocations(block, values); + } + } + + private void handleBlockEnd(AbstractBlockBase block, ArrayList instructions) { + if (allocator.hasInterTraceSuccessor(block)) { + /* Only materialize the locations array if there is an outgoing inter-trace edge. */ + assert instructions.equals(allocator.getLIR().getLIRforBlock(block)); + GlobalLivenessInfo li = allocator.getGlobalLivenessInfo(); + LIRInstruction instruction = instructions.get(instructions.size() - 1); + OperandMode mode = OperandMode.USE; + int[] live = li.getBlockOut(block); + Value[] values = calculateBlockBoundaryValues(instruction, live, mode); + li.setOutLocations(block, values); } + } - if (hasDead) { - // Remove null values from the list. - instructions.removeAll(Collections.singleton(null)); + private Value[] calculateBlockBoundaryValues(LIRInstruction instruction, int[] live, OperandMode mode) { + Value[] values = new Value[live.length]; + for (int i = 0; i < live.length; i++) { + TraceInterval interval = allocator.intervalFor(live[i]); + Value val = valueAtBlockBoundary(instruction, interval, mode); + values[i] = val; } + return values; + } + + private static Value valueAtBlockBoundary(LIRInstruction instruction, TraceInterval interval, OperandMode mode) { + if (mode == OperandMode.DEF && interval == null) { + // not needed in this trace + return Value.ILLEGAL; + } + assert interval != null : "interval must exist"; + TraceInterval splitInterval = interval.getSplitChildAtOpIdOrNull(instruction.id(), mode); + if (splitInterval == null) { + assert mode == OperandMode.DEF : String.format("Not split child at %d for interval %s", instruction.id(), interval); + // not needed in this branch + return Value.ILLEGAL; + } + + if (splitInterval.inMemoryAt(instruction.id()) && isRegister(splitInterval.location())) { + return new ShadowedRegisterValue((RegisterValue) splitInterval.location(), splitInterval.spillSlot()); + } + return getLocation(instruction, splitInterval, mode); } private final InstructionValueProcedure assignProc = new InstructionValueProcedure() { @@ -203,15 +267,8 @@ * @param instructions The instructions of the current block. * @return {@code true} if the instruction was deleted. */ - private boolean assignLocations(LIRInstruction op, List instructions, int j) { + private boolean assignLocations(LIRInstruction op, ArrayList instructions, int j) { assert op != null && instructions.get(j) == op; - if (TraceRAshareSpillInformation.getValue()) { - if (op instanceof BlockEndOp) { - ((BlockEndOp) op).forEachOutgoingValue(colorOutgoingIncomingValues); - } else if (op instanceof LabelOp) { - ((LabelOp) op).forEachIncomingValue(colorOutgoingIncomingValues); - } - } // remove useless moves if (op instanceof MoveOp) { @@ -245,38 +302,18 @@ if (isStackSlotValue(move.getInput()) && isStackSlotValue(move.getResult())) { // rewrite stack to stack moves instructions.set(j, spillMoveFactory.createStackMove(move.getResult(), move.getInput())); - return true; } } return false; } @SuppressWarnings("try") - private void assignLocations() { + private void assign() { try (Indent indent = Debug.logAndIndent("assign locations")) { for (AbstractBlockBase block : allocator.sortedBlocks()) { - try (Indent indent2 = Debug.logAndIndent("assign locations in block B%d", block.getId())) { - assignLocations(allocator.getLIR().getLIRforBlock(block)); - } + assignBlock(block); } } } - - private final InstructionValueProcedure colorOutgoingIncomingValues = new InstructionValueProcedure() { - - @Override - public Value doValue(LIRInstruction instruction, Value value, OperandMode mode, EnumSet flags) { - if (isVariable(value)) { - TraceInterval interval = allocator.intervalFor(value); - assert interval != null : "interval must exist"; - interval = allocator.splitChildAtOpId(interval, instruction.id(), mode); - - if (interval.inMemoryAt(instruction.id()) && isRegister(interval.location())) { - return new ShadowedRegisterValue((RegisterValue) interval.location(), interval.spillSlot()); - } - } - return value; - } - }; } }