view graal/com.oracle.graal.lir/src/com/oracle/graal/lir/alloc/trace/lsra/TraceLinearScanEliminateSpillMovePhase.java @ 23335:ef5ce69bdc21

TraceRA: outsource TraceBuilderResult.
author Josef Eisl <josef.eisl@jku.at>
date Tue, 19 Jan 2016 17:15:51 +0100
parents 7d8302d428bd
children
line wrap: on
line source

/*
 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.oracle.graal.lir.alloc.trace.lsra;

import static com.oracle.graal.compiler.common.GraalOptions.DetailedAsserts;
import static com.oracle.graal.lir.LIRValueUtil.isStackSlotValue;
import static com.oracle.graal.lir.LIRValueUtil.isVariable;
import static jdk.vm.ci.code.ValueUtil.isRegister;

import java.util.List;

import jdk.vm.ci.code.TargetDescription;
import jdk.vm.ci.meta.AllocatableValue;

import com.oracle.graal.compiler.common.alloc.TraceBuilderResult;
import com.oracle.graal.compiler.common.cfg.AbstractBlockBase;
import com.oracle.graal.debug.Debug;
import com.oracle.graal.debug.Indent;
import com.oracle.graal.lir.LIRInsertionBuffer;
import com.oracle.graal.lir.LIRInstruction;
import com.oracle.graal.lir.LIRInstruction.OperandMode;
import com.oracle.graal.lir.StandardOp.LoadConstantOp;
import com.oracle.graal.lir.StandardOp.MoveOp;
import com.oracle.graal.lir.StandardOp.ValueMoveOp;
import com.oracle.graal.lir.alloc.trace.lsra.TraceInterval.SpillState;
import com.oracle.graal.lir.alloc.trace.lsra.TraceLinearScan.IntervalPredicate;
import com.oracle.graal.lir.gen.LIRGenerationResult;

final class TraceLinearScanEliminateSpillMovePhase extends TraceLinearScanAllocationPhase {

    private static final IntervalPredicate spilledIntervals = new TraceLinearScan.IntervalPredicate() {

        @Override
        public boolean apply(TraceInterval i) {
            return i.isSplitParent() && SpillState.IN_MEMORY.contains(i.spillState());
        }
    };

    @Override
    protected <B extends AbstractBlockBase<B>> void run(TargetDescription target, LIRGenerationResult lirGenRes, List<B> codeEmittingOrder, List<B> linearScanOrder,
                    TraceLinearScanAllocationContext context) {
        TraceBuilderResult<?> traceBuilderResult = context.traceBuilderResult;
        TraceLinearScan allocator = context.allocator;
        boolean shouldEliminateSpillMoves = shouldEliminateSpillMoves(traceBuilderResult, allocator);
        eliminateSpillMoves(allocator, shouldEliminateSpillMoves, traceBuilderResult);
    }

    private static boolean shouldEliminateSpillMoves(TraceBuilderResult<?> traceBuilderResult, TraceLinearScan allocator) {
        return !traceBuilderResult.incomingSideEdges(traceBuilderResult.getTraceForBlock(allocator.sortedBlocks().get(0)));
    }

    // called once before assignment of register numbers
    @SuppressWarnings("try")
    private static void eliminateSpillMoves(TraceLinearScan allocator, boolean shouldEliminateSpillMoves, TraceBuilderResult<?> traceBuilderResult) {
        try (Indent indent = Debug.logAndIndent("Eliminating unnecessary spill moves: Trace%d", traceBuilderResult.getTraceForBlock(allocator.sortedBlocks().get(0)))) {
            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 (DetailedAsserts.getValue()) {
                checkIntervals(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)) {
                    List<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) 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 (move instanceof ValueMoveOp) {
                                            ValueMoveOp vmove = (ValueMoveOp) move;
                                            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) move;
                                            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 {
                                lastOpId = opId;
                                /*
                                 * Insert move from register to stack just after the beginning of
                                 * the interval.
                                 */
                                // assert interval == TraceInterval.EndMarker ||
                                // 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, allocator).location();
                                        AllocatableValue toLocation = TraceLinearScan.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);

                                            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;
                                }
                            }
                        }
                    } // end of instruction iteration

                    if (insertionBuffer.initialized()) {
                        insertionBuffer.finish();
                    }
                }
            } // end of block iteration

            assert interval == TraceInterval.EndMarker : "missed an interval";
        }
    }

    /**
     * @param allocator
     * @param block The block {@code move} is located in.
     * @param move Spill move.
     * @param lastOpId The id of last "normal" instruction before the spill move. (Spill moves have
     *            no valid opId but -1.)
     */
    private static boolean canEliminateSpillMove(TraceLinearScan allocator, AbstractBlockBase<?> block, MoveOp move, int lastOpId) {
        assert ((LIRInstruction) move).id() == -1 : "Not a spill move: " + move;
        assert isVariable(move.getResult()) : "LinearScan inserts only moves to variables: " + move;
        assert lastOpId >= 0 : "Invalid lastOpId: " + lastOpId;

        TraceInterval curInterval = allocator.intervalFor(move.getResult());

        if (!isRegister(curInterval.location()) && curInterval.inMemoryAt(lastOpId) && !isPhiResolutionMove(allocator, move)) {
            /* Phi resolution moves cannot be removed because they define the value. */
            // TODO (je) check if the comment is still valid!
            assert isStackSlotValue(curInterval.location()) : "Not a stack slot: " + curInterval.location();
            return true;
        }
        return false;
    }

    /**
     * Checks if a (spill or split) move is a Phi resolution move.
     *
     * A spill or split move connects a split parent or a split child with another split child.
     * Therefore the destination of the move is always a split child. Phi resolution moves look like
     * spill moves (i.e. {@link LIRInstruction#id() id} is {@code 0}, but they define a new
     * variable. As a result the destination interval is a split parent.
     */
    private static boolean isPhiResolutionMove(TraceLinearScan allocator, MoveOp move) {
        assert ((LIRInstruction) move).id() == -1 : "Not a spill move: " + move;
        TraceInterval curInterval = allocator.intervalFor(move.getResult());
        return curInterval.isSplitParent();
    }

    private static void checkIntervals(TraceInterval interval) {
        TraceInterval prev = null;
        TraceInterval temp = interval;
        while (temp != TraceInterval.EndMarker) {
            assert temp.spillDefinitionPos() >= 0 : "invalid spill definition pos";
            if (prev != null) {
                // assert temp.from() >= prev.from() : "intervals not sorted";
                assert temp.spillDefinitionPos() >= prev.spillDefinitionPos() : "when intervals are sorted by from :  then they must also be sorted by spillDefinitionPos";
            }

            assert temp.spillSlot() != null || temp.canMaterialize() : "interval has no spill slot assigned";
            assert temp.spillDefinitionPos() >= temp.from() : "invalid order";
            // assert temp.spillDefinitionPos() <= temp.from() + 2 :
            // "only intervals defined once at their start-pos can be optimized";

            if (Debug.isLogEnabled()) {
                Debug.log("interval %d (from %d to %d) must be stored at %d", temp.operandNumber, temp.from(), temp.to(), temp.spillDefinitionPos());
            }

            prev = temp;
            temp = temp.next;
        }
    }

}