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.ssi;
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.*;
032import com.oracle.graal.compiler.common.cfg.*;
033import com.oracle.graal.debug.*;
034import com.oracle.graal.lir.*;
035import com.oracle.graal.lir.alloc.lsra.*;
036import com.oracle.graal.lir.ssa.SSAUtil.PhiValueVisitor;
037import com.oracle.graal.lir.ssi.*;
038
039public class SSILinearScanResolveDataFlowPhase extends LinearScanResolveDataFlowPhase {
040
041    private static final DebugMetric numSSIResolutionMoves = Debug.metric("SSI LSRA[numSSIResolutionMoves]");
042    private static final DebugMetric numStackToStackMoves = Debug.metric("SSI LSRA[numStackToStackMoves]");
043
044    public SSILinearScanResolveDataFlowPhase(LinearScan allocator) {
045        super(allocator);
046    }
047
048    @Override
049    protected void resolveDataFlow() {
050        super.resolveDataFlow();
051        /*
052         * Incoming Values are needed for the RegisterVerifier, otherwise SIGMAs/PHIs where the Out
053         * and In value matches (ie. there is no resolution move) are falsely detected as errors.
054         */
055        for (AbstractBlockBase<?> toBlock : allocator.sortedBlocks()) {
056            if (toBlock.getPredecessorCount() != 0) {
057                SSIUtil.removeIncoming(allocator.getLIR(), toBlock);
058            } else {
059                assert allocator.getLIR().getControlFlowGraph().getStartBlock().equals(toBlock);
060            }
061            SSIUtil.removeOutgoing(allocator.getLIR(), toBlock);
062        }
063    }
064
065    @Override
066    protected void resolveCollectMappings(AbstractBlockBase<?> fromBlock, AbstractBlockBase<?> toBlock, AbstractBlockBase<?> midBlock, MoveResolver moveResolver) {
067        super.resolveCollectMappings(fromBlock, toBlock, midBlock, moveResolver);
068
069        if (midBlock != null) {
070            HashMap<Value, Value> map = CollectionsFactory.newMap();
071            SSIUtil.forEachValuePair(allocator.getLIR(), midBlock, fromBlock, (to, from) -> map.put(to, from));
072
073            MyPhiValueVisitor visitor = new MyPhiValueVisitor(moveResolver, toBlock, fromBlock);
074            SSIUtil.forEachValuePair(allocator.getLIR(), toBlock, midBlock, (to, from) -> {
075                Value phiOut = isConstant(from) ? from : map.get(from);
076                assert phiOut != null : "No entry for " + from;
077                visitor.visit(to, phiOut);
078            });
079        } else {
080            // default case
081            SSIUtil.forEachValuePair(allocator.getLIR(), toBlock, fromBlock, new MyPhiValueVisitor(moveResolver, toBlock, fromBlock));
082        }
083
084    }
085
086    private class MyPhiValueVisitor implements PhiValueVisitor {
087        final MoveResolver moveResolver;
088        final int toId;
089        final int fromId;
090
091        public MyPhiValueVisitor(MoveResolver moveResolver, AbstractBlockBase<?> toBlock, AbstractBlockBase<?> fromBlock) {
092            this.moveResolver = moveResolver;
093            toId = allocator.getFirstLirInstructionId(toBlock);
094            fromId = allocator.getLastLirInstructionId(fromBlock);
095            assert fromId >= 0;
096        }
097
098        public void visit(Value phiIn, Value phiOut) {
099            assert !isRegister(phiOut) : "Out is a register: " + phiOut;
100            assert !isRegister(phiIn) : "In is a register: " + phiIn;
101            if (Value.ILLEGAL.equals(phiIn)) {
102                // The value not needed in this branch.
103                return;
104            }
105            if (isVirtualStackSlot(phiIn) && isVirtualStackSlot(phiOut) && phiIn.equals(phiOut)) {
106                // no need to handle virtual stack slots
107                return;
108            }
109            Interval toInterval = allocator.splitChildAtOpId(allocator.intervalFor(phiIn), toId, LIRInstruction.OperandMode.DEF);
110            if (isConstant(phiOut)) {
111                numSSIResolutionMoves.increment();
112                moveResolver.addMapping(phiOut, toInterval);
113            } else {
114                Interval fromInterval = allocator.splitChildAtOpId(allocator.intervalFor(phiOut), fromId, LIRInstruction.OperandMode.DEF);
115                if (fromInterval != toInterval) {
116                    numSSIResolutionMoves.increment();
117                    if (!(isStackSlotValue(toInterval.location()) && isStackSlotValue(fromInterval.location()))) {
118                        moveResolver.addMapping(fromInterval, toInterval);
119                    } else {
120                        numStackToStackMoves.increment();
121                        moveResolver.addMapping(fromInterval, toInterval);
122                    }
123                }
124            }
125        }
126    }
127
128}