001/*
002 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.lir.alloc.lsra.ssa;
024
025import static com.oracle.graal.compiler.common.BackendOptions.*;
026import static com.oracle.graal.lir.LIRValueUtil.*;
027import static jdk.internal.jvmci.code.ValueUtil.*;
028
029import com.oracle.graal.debug.*;
030import com.oracle.graal.compiler.common.cfg.*;
031import com.oracle.graal.lir.*;
032import com.oracle.graal.lir.StandardOp.LabelOp;
033import com.oracle.graal.lir.StandardOp.MoveOp;
034import com.oracle.graal.lir.alloc.lsra.*;
035
036public class SSALinearScanEliminateSpillMovePhase extends LinearScanEliminateSpillMovePhase {
037
038    SSALinearScanEliminateSpillMovePhase(LinearScan allocator) {
039        super(allocator);
040    }
041
042    @Override
043    protected int firstInstructionOfInterest() {
044        // also look at Labels as they define PHI values
045        return 0;
046    }
047
048    @Override
049    protected boolean canEliminateSpillMove(AbstractBlockBase<?> block, MoveOp move) {
050        assert isVariable(move.getResult()) || LinearScanVariant.getValue() == LSRAVariant.SSA_LSRA : "Move should not be produced in a non-SSA compilation: " + move;
051
052        if (super.canEliminateSpillMove(block, move)) {
053            // SSA Linear Scan might introduce moves to stack slots
054            Interval curInterval = allocator.intervalFor(move.getResult());
055            assert !isRegister(curInterval.location()) && curInterval.alwaysInMemory();
056            if (!isPhiResolutionMove(block, move, curInterval)) {
057                assert isStackSlotValue(curInterval.location()) : "Not a stack slot: " + curInterval.location();
058                return true;
059            }
060        }
061        return false;
062    }
063
064    private boolean isPhiResolutionMove(AbstractBlockBase<?> block, MoveOp move, Interval toInterval) {
065        assert LinearScanVariant.getValue() == LSRAVariant.SSA_LSRA;
066        if (!toInterval.isSplitParent()) {
067            return false;
068        }
069        if ((toInterval.from() & 1) == 1) {
070            // phi intervals start at even positions.
071            return false;
072        }
073        if (block.getSuccessorCount() != 1) {
074            return false;
075        }
076        LIRInstruction op = allocator.instructionForId(toInterval.from());
077        if (!(op instanceof LabelOp)) {
078            return false;
079        }
080        AbstractBlockBase<?> intStartBlock = allocator.blockForId(toInterval.from());
081        assert allocator.getLIR().getLIRforBlock(intStartBlock).get(0).equals(op);
082        if (!block.getSuccessors().get(0).equals(intStartBlock)) {
083            return false;
084        }
085        try (Indent indet = Debug.indent()) {
086            Debug.log("Is a move (%s) to phi interval %s", move, toInterval);
087        }
088        return true;
089    }
090}