changeset 22551:f966e9701a33

TraceRA: merge LinearScanAssignLocationsPhase.
author Josef Eisl <josef.eisl@jku.at>
date Mon, 31 Aug 2015 13:33:08 +0200
parents 66d663de0de6
children bf3386380b45
files graal/com.oracle.graal.lir/src/com/oracle/graal/lir/alloc/trace/TraceLinearScan.java graal/com.oracle.graal.lir/src/com/oracle/graal/lir/alloc/trace/TraceLinearScanAssignLocationsPhase.java
diffstat 2 files changed, 187 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/alloc/trace/TraceLinearScan.java	Mon Aug 31 13:27:51 2015 +0200
+++ b/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/alloc/trace/TraceLinearScan.java	Mon Aug 31 13:33:08 2015 +0200
@@ -24,7 +24,6 @@
 
 import static com.oracle.graal.compiler.common.GraalOptions.*;
 import static com.oracle.graal.lir.LIRValueUtil.*;
-import static com.oracle.graal.lir.alloc.trace.TraceRegisterAllocationPhase.Options.*;
 import static jdk.internal.jvmci.code.CodeUtil.*;
 import static jdk.internal.jvmci.code.ValueUtil.*;
 
@@ -641,7 +640,7 @@
                 dataFlowPhase.apply(target, lirGenRes, codeEmittingOrder, linearScanOrder, context, false);
                 Debug.dump(TraceRegisterAllocationPhase.TRACE_DUMP_LEVEL, sortedBlocks(), "%s", dataFlowPhase.getName());
 
-                LinearScanAssignLocationsPhase assignPhase = createAssignLocationsPhase();
+                TraceLinearScanAssignLocationsPhase assignPhase = createAssignLocationsPhase();
                 assignPhase.apply(target, lirGenRes, codeEmittingOrder, linearScanOrder, context, false);
 
                 if (DetailedAsserts.getValue()) {
@@ -668,11 +667,8 @@
         return new TraceLinearScanEliminateSpillMovePhase(this);
     }
 
-    protected LinearScanAssignLocationsPhase createAssignLocationsPhase() {
-        if (TraceRAshareSpillInformation.getValue()) {
-            return new TraceLinearScanAssignLocationsPhase(this, traceBuilderResult);
-        }
-        return new LinearScanAssignLocationsPhase(this);
+    protected TraceLinearScanAssignLocationsPhase createAssignLocationsPhase() {
+        return new TraceLinearScanAssignLocationsPhase(this, traceBuilderResult);
     }
 
     protected void beforeSpillMoveElimination() {
--- a/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/alloc/trace/TraceLinearScanAssignLocationsPhase.java	Mon Aug 31 13:27:51 2015 +0200
+++ b/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/alloc/trace/TraceLinearScanAssignLocationsPhase.java	Mon Aug 31 13:33:08 2015 +0200
@@ -22,7 +22,9 @@
  */
 package com.oracle.graal.lir.alloc.trace;
 
+import static com.oracle.graal.compiler.common.GraalOptions.*;
 import static com.oracle.graal.lir.LIRValueUtil.*;
+import static com.oracle.graal.lir.alloc.trace.TraceRegisterAllocationPhase.Options.*;
 import static jdk.internal.jvmci.code.ValueUtil.*;
 
 import java.util.*;
@@ -30,33 +32,210 @@
 import jdk.internal.jvmci.code.*;
 import jdk.internal.jvmci.meta.*;
 
+import com.oracle.graal.compiler.common.alloc.*;
 import com.oracle.graal.compiler.common.alloc.TraceBuilder.TraceBuilderResult;
 import com.oracle.graal.compiler.common.cfg.*;
+import com.oracle.graal.debug.*;
 import com.oracle.graal.lir.*;
 import com.oracle.graal.lir.LIRInstruction.OperandFlag;
 import com.oracle.graal.lir.LIRInstruction.OperandMode;
 import com.oracle.graal.lir.StandardOp.BlockEndOp;
+import com.oracle.graal.lir.StandardOp.MoveOp;
+import com.oracle.graal.lir.StandardOp.ValueMoveOp;
+import com.oracle.graal.lir.alloc.lsra.*;
+import com.oracle.graal.lir.gen.*;
+import com.oracle.graal.lir.gen.LIRGeneratorTool.SpillMoveFactory;
+import com.oracle.graal.lir.phases.*;
 
 /**
  * Specialization of {@link LinearScanAssignLocationsPhase} that inserts
  * {@link ShadowedRegisterValue}s to describe {@link RegisterValue}s that are also available on the
  * {@link StackSlotValue stack}.
  */
-class TraceLinearScanAssignLocationsPhase extends LinearScanAssignLocationsPhase {
+public class TraceLinearScanAssignLocationsPhase extends AllocationPhase {
 
+    protected final TraceLinearScan allocator;
     private final TraceBuilderResult<?> traceBuilderResult;
 
-    TraceLinearScanAssignLocationsPhase(TraceLinearScan allocator, TraceBuilderResult<?> traceBuilderResult) {
-        super(allocator);
+    public TraceLinearScanAssignLocationsPhase(TraceLinearScan allocator, TraceBuilderResult<?> traceBuilderResult) {
+        this.allocator = allocator;
         this.traceBuilderResult = traceBuilderResult;
     }
 
     @Override
+    protected <B extends AbstractBlockBase<B>> void run(TargetDescription target, LIRGenerationResult lirGenRes, List<B> codeEmittingOrder, List<B> linearScanOrder, SpillMoveFactory spillMoveFactory,
+                    RegisterAllocationConfig registerAllocationConfig) {
+        assignLocations();
+    }
+
+    /**
+     * Assigns the allocated location for an LIR instruction operand back into the instruction.
+     *
+     * @param op current {@link LIRInstruction}
+     * @param operand an LIR instruction operand
+     * @param mode the usage mode for {@code operand} by the instruction
+     * @return the location assigned for the operand
+     */
+    protected Value colorLirOperand(LIRInstruction op, Variable operand, OperandMode mode) {
+        int opId = op.id();
+        Interval interval = allocator.intervalFor(operand);
+        assert interval != null : "interval must exist";
+
+        if (opId != -1) {
+            if (DetailedAsserts.getValue()) {
+                AbstractBlockBase<?> block = allocator.blockForId(opId);
+                if (block.getSuccessorCount() <= 1 && opId == allocator.getLastLirInstructionId(block)) {
+                    /*
+                     * Check if spill moves could have been appended at the end of this block, but
+                     * before the branch instruction. So the split child information for this branch
+                     * would be incorrect.
+                     */
+                    LIRInstruction instr = allocator.getLIR().getLIRforBlock(block).get(allocator.getLIR().getLIRforBlock(block).size() - 1);
+                    if (instr instanceof StandardOp.JumpOp) {
+                        if (allocator.getBlockData(block).liveOut.get(allocator.operandNumber(operand))) {
+                            assert false : String.format(
+                                            "can't get split child for the last branch of a block because the information would be incorrect (moves are inserted before the branch in resolveDataFlow) block=%s, instruction=%s, operand=%s",
+                                            block, instr, operand);
+                        }
+                    }
+                }
+            }
+
+            /*
+             * Operands are not changed when an interval is split during allocation, so search the
+             * right interval here.
+             */
+            interval = allocator.splitChildAtOpId(interval, opId, mode);
+        }
+
+        if (isIllegal(interval.location()) && interval.canMaterialize()) {
+            assert mode != OperandMode.DEF;
+            return interval.getMaterializedValue();
+        }
+        return interval.location();
+    }
+
+    /**
+     * @param op
+     * @param operand
+     * @param valueMode
+     * @param flags
+     * @see InstructionValueProcedure#doValue(LIRInstruction, Value, OperandMode, EnumSet)
+     */
+    private Value debugInfoProcedure(LIRInstruction op, Value operand, OperandMode valueMode, EnumSet<OperandFlag> flags) {
+        if (isVirtualStackSlot(operand)) {
+            return operand;
+        }
+        int tempOpId = op.id();
+        OperandMode mode = OperandMode.USE;
+        AbstractBlockBase<?> block = allocator.blockForId(tempOpId);
+        if (block.getSuccessorCount() == 1 && tempOpId == allocator.getLastLirInstructionId(block)) {
+            /*
+             * Generating debug information for the last instruction of a block. If this instruction
+             * is a branch, spill moves are inserted before this branch and so the wrong operand
+             * would be returned (spill moves at block boundaries are not considered in the live
+             * ranges of intervals).
+             * 
+             * Solution: use the first opId of the branch target block instead.
+             */
+            final LIRInstruction instr = allocator.getLIR().getLIRforBlock(block).get(allocator.getLIR().getLIRforBlock(block).size() - 1);
+            if (instr instanceof StandardOp.JumpOp) {
+                if (allocator.getBlockData(block).liveOut.get(allocator.operandNumber(operand))) {
+                    tempOpId = allocator.getFirstLirInstructionId(block.getSuccessors().iterator().next());
+                    mode = OperandMode.DEF;
+                }
+            }
+        }
+
+        /*
+         * Get current location of operand. The operand must be live because debug information is
+         * considered when building the intervals if the interval is not live, colorLirOperand will
+         * cause an assert on failure.
+         */
+        Value result = colorLirOperand(op, (Variable) operand, mode);
+        assert !allocator.hasCall(tempOpId) || isStackSlotValue(result) || isConstant(result) || !allocator.isCallerSave(result) : "cannot have caller-save register operands at calls";
+        return result;
+    }
+
+    private void computeDebugInfo(final LIRInstruction op, LIRFrameState info) {
+        info.forEachState(op, this::debugInfoProcedure);
+    }
+
+    private void assignLocations(List<LIRInstruction> 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.set(j, null);
+                hasDead = true;
+            }
+        }
+
+        if (hasDead) {
+            // Remove null values from the list.
+            instructions.removeAll(Collections.singleton(null));
+        }
+    }
+
+    /**
+     * Assigns the operand of an {@link LIRInstruction}.
+     *
+     * @param op The {@link LIRInstruction} that should be colored.
+     * @return {@code true} if the instruction should be deleted.
+     */
     protected boolean assignLocations(LIRInstruction op) {
-        if (isBlockEndWithEdgeToUnallocatedTrace(op)) {
+        assert op != null;
+        if (TraceRAshareSpillInformation.getValue() && isBlockEndWithEdgeToUnallocatedTrace(op)) {
             ((BlockEndOp) op).forEachOutgoingValue(colorOutgoingValues);
         }
-        return super.assignLocations(op);
+
+        InstructionValueProcedure assignProc = (inst, operand, mode, flags) -> isVariable(operand) ? colorLirOperand(inst, (Variable) operand, mode) : operand;
+        // remove useless moves
+        if (op instanceof MoveOp) {
+            AllocatableValue result = ((MoveOp) op).getResult();
+            if (isVariable(result) && allocator.isMaterialized(result, op.id(), OperandMode.DEF)) {
+                /*
+                 * This happens if a materializable interval is originally not spilled but then
+                 * kicked out in LinearScanWalker.splitForSpilling(). When kicking out such an
+                 * interval this move operation was already generated.
+                 */
+                return true;
+            }
+        }
+
+        op.forEachInput(assignProc);
+        op.forEachAlive(assignProc);
+        op.forEachTemp(assignProc);
+        op.forEachOutput(assignProc);
+
+        // compute reference map and debug information
+        op.forEachState((inst, state) -> computeDebugInfo(inst, state));
+
+        // remove useless moves
+        if (op instanceof ValueMoveOp) {
+            ValueMoveOp move = (ValueMoveOp) op;
+            if (move.getInput().equals(move.getResult())) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private void assignLocations() {
+        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));
+                }
+            }
+        }
     }
 
     private InstructionValueProcedure colorOutgoingValues = new InstructionValueProcedure() {