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.trace;
024
025import static jdk.internal.jvmci.code.ValueUtil.*;
026
027import java.util.*;
028
029import jdk.internal.jvmci.meta.*;
030
031import com.oracle.graal.compiler.common.cfg.*;
032import com.oracle.graal.debug.*;
033import com.oracle.graal.lir.*;
034import com.oracle.graal.lir.alloc.lsra.*;
035import com.oracle.graal.lir.ssa.SSAUtil.PhiValueVisitor;
036import com.oracle.graal.lir.ssi.*;
037
038public class TraceLinearScanResolveDataFlowPhase extends LinearScanResolveDataFlowPhase {
039
040    private static final DebugMetric numSSIResolutionMoves = Debug.metric("SSI LSRA[numSSIResolutionMoves]");
041    private static final DebugMetric numStackToStackMoves = Debug.metric("SSI LSRA[numStackToStackMoves]");
042
043    public TraceLinearScanResolveDataFlowPhase(LinearScan allocator) {
044        super(allocator);
045    }
046
047    @Override
048    protected void optimizeEmptyBlocks(MoveResolver moveResolver, BitSet blockCompleted) {
049        // do not optimize
050    }
051
052    @Override
053    protected void resolveCollectMappings(AbstractBlockBase<?> fromBlock, AbstractBlockBase<?> toBlock, AbstractBlockBase<?> midBlock, MoveResolver moveResolver) {
054        assert midBlock == null;
055        if (containedInTrace(fromBlock) && containedInTrace(toBlock)) {
056            super.resolveCollectMappings(fromBlock, toBlock, midBlock, moveResolver);
057            SSIUtil.forEachValuePair(allocator.getLIR(), toBlock, fromBlock, new MyPhiValueVisitor(moveResolver, toBlock, fromBlock));
058        }
059
060    }
061
062    private boolean containedInTrace(AbstractBlockBase<?> block) {
063        return allocator.sortedBlocks().contains(block);
064    }
065
066    private class MyPhiValueVisitor implements PhiValueVisitor {
067        final MoveResolver moveResolver;
068        final int toId;
069        final int fromId;
070
071        public MyPhiValueVisitor(MoveResolver moveResolver, AbstractBlockBase<?> toBlock, AbstractBlockBase<?> fromBlock) {
072            this.moveResolver = moveResolver;
073            toId = allocator.getFirstLirInstructionId(toBlock);
074            fromId = allocator.getLastLirInstructionId(fromBlock);
075            assert fromId >= 0;
076        }
077
078        public void visit(Value phiIn, Value phiOut) {
079            assert !isRegister(phiOut) : "Out is a register: " + phiOut;
080            assert !isRegister(phiIn) : "In is a register: " + phiIn;
081            if (Value.ILLEGAL.equals(phiIn)) {
082                // The value not needed in this branch.
083                return;
084            }
085            if (isVirtualStackSlot(phiIn) && isVirtualStackSlot(phiOut) && phiIn.equals(phiOut)) {
086                // no need to handle virtual stack slots
087                return;
088            }
089            Interval toInterval = allocator.splitChildAtOpId(allocator.intervalFor(phiIn), toId, LIRInstruction.OperandMode.DEF);
090            if (isConstant(phiOut)) {
091                numSSIResolutionMoves.increment();
092                moveResolver.addMapping(phiOut, toInterval);
093            } else {
094                Interval fromInterval = allocator.splitChildAtOpId(allocator.intervalFor(phiOut), fromId, LIRInstruction.OperandMode.DEF);
095                if (fromInterval != toInterval) {
096                    numSSIResolutionMoves.increment();
097                    if (!(isStackSlotValue(toInterval.location()) && isStackSlotValue(fromInterval.location()))) {
098                        moveResolver.addMapping(fromInterval, toInterval);
099                    } else {
100                        numStackToStackMoves.increment();
101                        moveResolver.addMapping(fromInterval, toInterval);
102                    }
103                }
104            }
105        }
106    }
107
108}