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 jdk.internal.jvmci.code.ValueUtil.*;
026
027import java.util.*;
028
029import com.oracle.graal.debug.*;
030
031import jdk.internal.jvmci.meta.*;
032
033import com.oracle.graal.compiler.common.cfg.*;
034import com.oracle.graal.lir.*;
035import com.oracle.graal.lir.alloc.lsra.*;
036import com.oracle.graal.lir.ssa.*;
037import com.oracle.graal.lir.ssa.SSAUtil.PhiValueVisitor;
038
039class SSALinarScanResolveDataFlowPhase extends LinearScanResolveDataFlowPhase {
040
041    private static final DebugMetric numPhiResolutionMoves = Debug.metric("SSA LSRA[numPhiResolutionMoves]");
042    private static final DebugMetric numStackToStackMoves = Debug.metric("SSA LSRA[numStackToStackMoves]");
043
044    SSALinarScanResolveDataFlowPhase(LinearScan allocator) {
045        super(allocator);
046    }
047
048    @Override
049    protected void resolveCollectMappings(AbstractBlockBase<?> fromBlock, AbstractBlockBase<?> toBlock, AbstractBlockBase<?> midBlock, MoveResolver moveResolver) {
050        super.resolveCollectMappings(fromBlock, toBlock, midBlock, moveResolver);
051
052        if (toBlock.getPredecessorCount() > 1) {
053            int toBlockFirstInstructionId = allocator.getFirstLirInstructionId(toBlock);
054            int fromBlockLastInstructionId = allocator.getLastLirInstructionId(fromBlock) + 1;
055
056            AbstractBlockBase<?> phiOutBlock = midBlock != null ? midBlock : fromBlock;
057            List<LIRInstruction> instructions = allocator.getLIR().getLIRforBlock(phiOutBlock);
058            int phiOutIdx = SSAUtil.phiOutIndex(allocator.getLIR(), phiOutBlock);
059            int phiOutId = midBlock != null ? fromBlockLastInstructionId : instructions.get(phiOutIdx).id();
060            assert phiOutId >= 0;
061
062            PhiValueVisitor visitor = new PhiValueVisitor() {
063
064                public void visit(Value phiIn, Value phiOut) {
065                    assert !isRegister(phiOut) : "phiOut is a register: " + phiOut;
066                    assert !isRegister(phiIn) : "phiIn is a register: " + phiIn;
067                    Interval toInterval = allocator.splitChildAtOpId(allocator.intervalFor(phiIn), toBlockFirstInstructionId, LIRInstruction.OperandMode.DEF);
068                    if (isConstant(phiOut)) {
069                        numPhiResolutionMoves.increment();
070                        moveResolver.addMapping(phiOut, toInterval);
071                    } else {
072                        Interval fromInterval = allocator.splitChildAtOpId(allocator.intervalFor(phiOut), phiOutId, LIRInstruction.OperandMode.DEF);
073                        if (fromInterval != toInterval && !fromInterval.location().equals(toInterval.location())) {
074                            numPhiResolutionMoves.increment();
075                            if (!(isStackSlotValue(toInterval.location()) && isStackSlotValue(fromInterval.location()))) {
076                                moveResolver.addMapping(fromInterval, toInterval);
077                            } else {
078                                numStackToStackMoves.increment();
079                                moveResolver.addMapping(fromInterval, toInterval);
080                            }
081                        }
082                    }
083                }
084            };
085
086            SSAUtil.forEachPhiValuePair(allocator.getLIR(), toBlock, phiOutBlock, visitor);
087            SSAUtil.removePhiOut(allocator.getLIR(), phiOutBlock);
088        }
089    }
090
091}