Search in sources :

Example 1 with ValueMoveOp

use of org.graalvm.compiler.lir.StandardOp.ValueMoveOp in project graal by oracle.

the class LinearScanAssignLocationsPhase method assignLocations.

/**
 * 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) {
    assert op != null;
    // remove useless moves
    if (MoveOp.isMoveOp(op)) {
        AllocatableValue result = MoveOp.asMoveOp(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(debugInfoProc);
    // remove useless moves
    if (ValueMoveOp.isValueMoveOp(op)) {
        ValueMoveOp move = ValueMoveOp.asValueMoveOp(op);
        if (move.getInput().equals(move.getResult())) {
            return true;
        }
    }
    return false;
}
Also used : AllocatableValue(jdk.vm.ci.meta.AllocatableValue) ValueMoveOp(org.graalvm.compiler.lir.StandardOp.ValueMoveOp)

Example 2 with ValueMoveOp

use of org.graalvm.compiler.lir.StandardOp.ValueMoveOp in project graal by oracle.

the class LinearScanEliminateSpillMovePhase method eliminateSpillMoves.

// called once before assignment of register numbers
@SuppressWarnings("try")
void eliminateSpillMoves(LIRGenerationResult res) {
    DebugContext debug = allocator.getDebug();
    try (Indent indent = debug.logAndIndent("Eliminating unnecessary spill moves")) {
        /*
             * collect all intervals that must be stored after their definition. The list is sorted
             * by Interval.spillDefinitionPos.
             */
        Interval interval;
        interval = allocator.createUnhandledLists(mustStoreAtDefinition, null).getLeft();
        if (Assertions.detailedAssertionsEnabled(allocator.getOptions())) {
            checkIntervals(debug, interval);
        }
        LIRInsertionBuffer insertionBuffer = new LIRInsertionBuffer();
        for (AbstractBlockBase<?> block : allocator.sortedBlocks()) {
            try (Indent indent1 = debug.logAndIndent("Handle %s", block)) {
                ArrayList<LIRInstruction> instructions = allocator.getLIR().getLIRforBlock(block);
                int numInst = instructions.size();
                // iterate all instructions of the block.
                for (int j = firstInstructionOfInterest(); j < numInst; j++) {
                    LIRInstruction op = instructions.get(j);
                    int opId = op.id();
                    if (opId == -1) {
                        MoveOp move = MoveOp.asMoveOp(op);
                        /*
                             * Remove move from register to stack if the stack slot is guaranteed to
                             * be correct. Only moves that have been inserted by LinearScan can be
                             * removed.
                             */
                        if (Options.LIROptLSRAEliminateSpillMoves.getValue(allocator.getOptions()) && canEliminateSpillMove(block, move)) {
                            /*
                                 * Move target is a stack slot that is always correct, so eliminate
                                 * instruction.
                                 */
                            if (debug.isLogEnabled()) {
                                if (ValueMoveOp.isValueMoveOp(op)) {
                                    ValueMoveOp vmove = ValueMoveOp.asValueMoveOp(op);
                                    debug.log("eliminating move from interval %d (%s) to %d (%s) in block %s", allocator.operandNumber(vmove.getInput()), vmove.getInput(), allocator.operandNumber(vmove.getResult()), vmove.getResult(), block);
                                } else {
                                    LoadConstantOp load = LoadConstantOp.asLoadConstantOp(op);
                                    debug.log("eliminating constant load from %s to %d (%s) in block %s", load.getConstant(), allocator.operandNumber(load.getResult()), load.getResult(), block);
                                }
                            }
                            // null-instructions are deleted by assignRegNum
                            instructions.set(j, null);
                        }
                    } else {
                        /*
                             * Insert move from register to stack just after the beginning of the
                             * interval.
                             */
                        assert interval.isEndMarker() || interval.spillDefinitionPos() >= opId : "invalid order";
                        assert interval.isEndMarker() || (interval.isSplitParent() && interval.spillState() == SpillState.StoreAtDefinition) : "invalid interval";
                        while (!interval.isEndMarker() && interval.spillDefinitionPos() == opId) {
                            if (!interval.canMaterialize()) {
                                if (!insertionBuffer.initialized()) {
                                    /*
                                         * prepare insertion buffer (appended when all instructions
                                         * in the block are processed)
                                         */
                                    insertionBuffer.init(instructions);
                                }
                                AllocatableValue fromLocation = interval.location();
                                AllocatableValue toLocation = LinearScan.canonicalSpillOpr(interval);
                                if (!fromLocation.equals(toLocation)) {
                                    assert isRegister(fromLocation) : "from operand must be a register but is: " + fromLocation + " toLocation=" + toLocation + " spillState=" + interval.spillState();
                                    assert isStackSlotValue(toLocation) : "to operand must be a stack slot";
                                    LIRInstruction move = allocator.getSpillMoveFactory().createMove(toLocation, fromLocation);
                                    insertionBuffer.append(j + 1, move);
                                    move.setComment(res, "LSRAEliminateSpillMove: store at definition");
                                    if (debug.isLogEnabled()) {
                                        debug.log("inserting move after definition of interval %d to stack slot %s at opId %d", interval.operandNumber, interval.spillSlot(), opId);
                                    }
                                }
                            }
                            interval = interval.next;
                        }
                    }
                }
                if (insertionBuffer.initialized()) {
                    insertionBuffer.finish();
                }
            }
        }
        assert interval.isEndMarker() : "missed an interval";
    }
}
Also used : Indent(org.graalvm.compiler.debug.Indent) LIRInsertionBuffer(org.graalvm.compiler.lir.LIRInsertionBuffer) LIRInstruction(org.graalvm.compiler.lir.LIRInstruction) LoadConstantOp(org.graalvm.compiler.lir.StandardOp.LoadConstantOp) MoveOp(org.graalvm.compiler.lir.StandardOp.MoveOp) ValueMoveOp(org.graalvm.compiler.lir.StandardOp.ValueMoveOp) DebugContext(org.graalvm.compiler.debug.DebugContext) ValueMoveOp(org.graalvm.compiler.lir.StandardOp.ValueMoveOp) AllocatableValue(jdk.vm.ci.meta.AllocatableValue)

Example 3 with ValueMoveOp

use of org.graalvm.compiler.lir.StandardOp.ValueMoveOp in project graal by oracle.

the class LinearScanLifetimeAnalysisPhase method handleMethodArguments.

/**
 * Optimizes moves related to incoming stack based arguments. The interval for the destination
 * of such moves is assigned the stack slot (which is in the caller's frame) as its spill slot.
 */
protected void handleMethodArguments(LIRInstruction op) {
    if (ValueMoveOp.isValueMoveOp(op)) {
        ValueMoveOp move = ValueMoveOp.asValueMoveOp(op);
        if (optimizeMethodArgument(move.getInput())) {
            StackSlot slot = asStackSlot(move.getInput());
            if (Assertions.detailedAssertionsEnabled(allocator.getOptions())) {
                assert op.id() > 0 : "invalid id";
                assert allocator.blockForId(op.id()).getPredecessorCount() == 0 : "move from stack must be in first block";
                assert isVariable(move.getResult()) : "result of move must be a variable";
                if (debug.isLogEnabled()) {
                    debug.log("found move from stack slot %s to %s", slot, move.getResult());
                }
            }
            Interval interval = allocator.intervalFor(move.getResult());
            interval.setSpillSlot(slot);
            interval.assignLocation(slot);
        }
    }
}
Also used : ValueUtil.asStackSlot(jdk.vm.ci.code.ValueUtil.asStackSlot) ValueUtil.isStackSlot(jdk.vm.ci.code.ValueUtil.isStackSlot) StackSlot(jdk.vm.ci.code.StackSlot) ValueMoveOp(org.graalvm.compiler.lir.StandardOp.ValueMoveOp)

Example 4 with ValueMoveOp

use of org.graalvm.compiler.lir.StandardOp.ValueMoveOp in project graal by oracle.

the class TraceLinearScanEliminateSpillMovePhase method eliminateSpillMoves.

// called once before assignment of register numbers
@SuppressWarnings("try")
private static void eliminateSpillMoves(TraceLinearScan allocator, boolean shouldEliminateSpillMoves, TraceBuilderResult traceBuilderResult, LIRGenerationResult res) {
    DebugContext debug = allocator.getDebug();
    try (Indent indent = debug.logAndIndent("Eliminating unnecessary spill moves: Trace%d", traceBuilderResult.getTraceForBlock(allocator.blockAt(0)).getId())) {
        allocator.sortIntervalsBySpillPos();
        /*
             * collect all intervals that must be stored after their definition. The list is sorted
             * by Interval.spillDefinitionPos.
             */
        TraceInterval interval = allocator.createUnhandledListBySpillPos(spilledIntervals);
        if (Assertions.detailedAssertionsEnabled(allocator.getOptions())) {
            checkIntervals(debug, interval);
        }
        if (debug.isLogEnabled()) {
            try (Indent indent2 = debug.logAndIndent("Sorted intervals")) {
                for (TraceInterval i = interval; i != null; i = i.next) {
                    debug.log("%5d: %s", i.spillDefinitionPos(), i);
                }
            }
        }
        LIRInsertionBuffer insertionBuffer = new LIRInsertionBuffer();
        for (AbstractBlockBase<?> block : allocator.sortedBlocks()) {
            try (Indent indent1 = debug.logAndIndent("Handle %s", block)) {
                ArrayList<LIRInstruction> instructions = allocator.getLIR().getLIRforBlock(block);
                int numInst = instructions.size();
                int lastOpId = -1;
                // iterate all instructions of the block.
                for (int j = 0; j < numInst; j++) {
                    LIRInstruction op = instructions.get(j);
                    int opId = op.id();
                    try (Indent indent2 = debug.logAndIndent("%5d %s", opId, op)) {
                        if (opId == -1) {
                            MoveOp move = MoveOp.asMoveOp(op);
                            /*
                                 * Remove move from register to stack if the stack slot is
                                 * guaranteed to be correct. Only moves that have been inserted by
                                 * LinearScan can be removed.
                                 */
                            if (shouldEliminateSpillMoves && canEliminateSpillMove(allocator, block, move, lastOpId)) {
                                /*
                                     * Move target is a stack slot that is always correct, so
                                     * eliminate instruction.
                                     */
                                if (debug.isLogEnabled()) {
                                    if (ValueMoveOp.isValueMoveOp(op)) {
                                        ValueMoveOp vmove = ValueMoveOp.asValueMoveOp(op);
                                        debug.log("eliminating move from interval %s to %s in block %s", vmove.getInput(), vmove.getResult(), block);
                                    } else {
                                        LoadConstantOp load = LoadConstantOp.asLoadConstantOp(op);
                                        debug.log("eliminating constant load from %s to %s in block %s", load.getConstant(), load.getResult(), block);
                                    }
                                }
                                // null-instructions are deleted by assignRegNum
                                instructions.set(j, null);
                            }
                        } else {
                            lastOpId = opId;
                            // interval.spillDefinitionPos() >= opId : "invalid order";
                            assert interval == TraceInterval.EndMarker || (interval.isSplitParent() && SpillState.IN_MEMORY.contains(interval.spillState())) : "invalid interval";
                            while (interval != TraceInterval.EndMarker && interval.spillDefinitionPos() == opId) {
                                debug.log("handle %s", interval);
                                if (!interval.canMaterialize() && interval.spillState() != SpillState.StartInMemory) {
                                    AllocatableValue fromLocation = interval.getSplitChildAtOpId(opId, OperandMode.DEF).location();
                                    AllocatableValue toLocation = allocator.canonicalSpillOpr(interval);
                                    if (!fromLocation.equals(toLocation)) {
                                        if (!insertionBuffer.initialized()) {
                                            /*
                                                 * prepare insertion buffer (appended when all
                                                 * instructions in the block are processed)
                                                 */
                                            insertionBuffer.init(instructions);
                                        }
                                        assert isRegister(fromLocation) : "from operand must be a register but is: " + fromLocation + " toLocation=" + toLocation + " spillState=" + interval.spillState();
                                        assert isStackSlotValue(toLocation) : "to operand must be a stack slot";
                                        LIRInstruction move = allocator.getSpillMoveFactory().createMove(toLocation, fromLocation);
                                        insertionBuffer.append(j + 1, move);
                                        move.setComment(res, "TraceLSRAEliminateSpillMove: spill def pos");
                                        if (debug.isLogEnabled()) {
                                            debug.log("inserting move after definition of interval %d to stack slot %s at opId %d", interval.operandNumber, interval.spillSlot(), opId);
                                        }
                                    }
                                }
                                interval = interval.next;
                            }
                        }
                    }
                }
                if (insertionBuffer.initialized()) {
                    insertionBuffer.finish();
                }
            }
        }
        assert interval == TraceInterval.EndMarker : "missed an interval";
    }
}
Also used : Indent(org.graalvm.compiler.debug.Indent) LIRInsertionBuffer(org.graalvm.compiler.lir.LIRInsertionBuffer) LIRInstruction(org.graalvm.compiler.lir.LIRInstruction) LoadConstantOp(org.graalvm.compiler.lir.StandardOp.LoadConstantOp) MoveOp(org.graalvm.compiler.lir.StandardOp.MoveOp) ValueMoveOp(org.graalvm.compiler.lir.StandardOp.ValueMoveOp) DebugContext(org.graalvm.compiler.debug.DebugContext) ValueMoveOp(org.graalvm.compiler.lir.StandardOp.ValueMoveOp) AllocatableValue(jdk.vm.ci.meta.AllocatableValue)

Aggregations

ValueMoveOp (org.graalvm.compiler.lir.StandardOp.ValueMoveOp)4 AllocatableValue (jdk.vm.ci.meta.AllocatableValue)3 DebugContext (org.graalvm.compiler.debug.DebugContext)2 Indent (org.graalvm.compiler.debug.Indent)2 LIRInsertionBuffer (org.graalvm.compiler.lir.LIRInsertionBuffer)2 LIRInstruction (org.graalvm.compiler.lir.LIRInstruction)2 LoadConstantOp (org.graalvm.compiler.lir.StandardOp.LoadConstantOp)2 MoveOp (org.graalvm.compiler.lir.StandardOp.MoveOp)2 StackSlot (jdk.vm.ci.code.StackSlot)1 ValueUtil.asStackSlot (jdk.vm.ci.code.ValueUtil.asStackSlot)1 ValueUtil.isStackSlot (jdk.vm.ci.code.ValueUtil.isStackSlot)1