001/*
002 * Copyright (c) 2013, 2013, 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.phases.common;
024
025import java.util.*;
026
027import com.oracle.graal.compiler.common.cfg.*;
028import com.oracle.graal.compiler.common.type.*;
029import com.oracle.graal.nodes.*;
030import com.oracle.graal.nodes.cfg.*;
031import com.oracle.graal.phases.*;
032import com.oracle.graal.phases.tiers.*;
033
034/**
035 * This phase tries to find {@link AbstractDeoptimizeNode DeoptimizeNodes} which use the same
036 * {@link FrameState} and merges them together.
037 */
038public class DeoptimizationGroupingPhase extends BasePhase<MidTierContext> {
039
040    @Override
041    protected void run(StructuredGraph graph, MidTierContext context) {
042        ControlFlowGraph cfg = null;
043        for (FrameState fs : graph.getNodes(FrameState.TYPE)) {
044            FixedNode target = null;
045            PhiNode reasonActionPhi = null;
046            PhiNode speculationPhi = null;
047            List<AbstractDeoptimizeNode> obsoletes = null;
048            for (AbstractDeoptimizeNode deopt : fs.usages().filter(AbstractDeoptimizeNode.class)) {
049                if (target == null) {
050                    target = deopt;
051                } else {
052                    if (cfg == null) {
053                        cfg = ControlFlowGraph.compute(graph, true, true, false, false);
054                    }
055                    AbstractMergeNode merge;
056                    if (target instanceof AbstractDeoptimizeNode) {
057                        merge = graph.add(new MergeNode());
058                        EndNode firstEnd = graph.add(new EndNode());
059                        ValueNode actionAndReason = ((AbstractDeoptimizeNode) target).getActionAndReason(context.getMetaAccess());
060                        ValueNode speculation = ((AbstractDeoptimizeNode) target).getSpeculation(context.getMetaAccess());
061                        reasonActionPhi = graph.addWithoutUnique(new ValuePhiNode(StampFactory.forKind(actionAndReason.getKind()), merge));
062                        speculationPhi = graph.addWithoutUnique(new ValuePhiNode(StampFactory.forKind(speculation.getKind()), merge));
063                        merge.addForwardEnd(firstEnd);
064                        reasonActionPhi.addInput(actionAndReason);
065                        speculationPhi.addInput(speculation);
066                        target.replaceAtPredecessor(firstEnd);
067
068                        exitLoops((AbstractDeoptimizeNode) target, firstEnd, cfg);
069
070                        merge.setNext(graph.add(new DynamicDeoptimizeNode(reasonActionPhi, speculationPhi)));
071                        obsoletes = new LinkedList<>();
072                        obsoletes.add((AbstractDeoptimizeNode) target);
073                        target = merge;
074                    } else {
075                        merge = (AbstractMergeNode) target;
076                    }
077                    EndNode newEnd = graph.add(new EndNode());
078                    merge.addForwardEnd(newEnd);
079                    reasonActionPhi.addInput(deopt.getActionAndReason(context.getMetaAccess()));
080                    speculationPhi.addInput(deopt.getSpeculation(context.getMetaAccess()));
081                    deopt.replaceAtPredecessor(newEnd);
082                    exitLoops(deopt, newEnd, cfg);
083                    obsoletes.add(deopt);
084                }
085            }
086            if (obsoletes != null) {
087                ((DynamicDeoptimizeNode) ((AbstractMergeNode) target).next()).setStateBefore(fs);
088                for (AbstractDeoptimizeNode obsolete : obsoletes) {
089                    obsolete.safeDelete();
090                }
091            }
092        }
093    }
094
095    private static void exitLoops(AbstractDeoptimizeNode deopt, EndNode end, ControlFlowGraph cfg) {
096        Block block = cfg.blockFor(deopt);
097        Loop<Block> loop = block.getLoop();
098        while (loop != null) {
099            end.graph().addBeforeFixed(end, end.graph().add(new LoopExitNode((LoopBeginNode) loop.getHeader().getBeginNode())));
100            loop = loop.getParent();
101        }
102    }
103}