001/*
002 * Copyright (c) 2012, 2012, 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.loop;
024
025import com.oracle.graal.compiler.common.cfg.*;
026import com.oracle.graal.graph.*;
027import com.oracle.graal.graph.Graph.DuplicationReplacement;
028import com.oracle.graal.nodes.*;
029import com.oracle.graal.nodes.cfg.*;
030
031public class LoopFragmentWhole extends LoopFragment {
032
033    public LoopFragmentWhole(LoopEx loop) {
034        super(loop);
035    }
036
037    public LoopFragmentWhole(LoopFragmentWhole original) {
038        super(null, original);
039    }
040
041    @Override
042    public LoopFragmentWhole duplicate() {
043        LoopFragmentWhole loopFragmentWhole = new LoopFragmentWhole(this);
044        loopFragmentWhole.reify();
045        return loopFragmentWhole;
046    }
047
048    private void reify() {
049        assert this.isDuplicate();
050
051        patchNodes(null);
052
053        mergeEarlyExits();
054    }
055
056    @Override
057    public NodeBitMap nodes() {
058        if (nodes == null) {
059            Loop<Block> loop = loop().loop();
060            nodes = LoopFragment.computeNodes(graph(), LoopFragment.toHirBlocks(loop.getBlocks()), LoopFragment.toHirExits(loop.getExits()));
061        }
062        return nodes;
063    }
064
065    @Override
066    protected ValueNode prim(ValueNode b) {
067        return getDuplicatedNode(b);
068    }
069
070    @Override
071    protected DuplicationReplacement getDuplicationReplacement() {
072        final FixedNode entry = loop().entryPoint();
073        final Graph graph = this.graph();
074        return new DuplicationReplacement() {
075
076            private EndNode endNode;
077
078            @Override
079            public Node replacement(Node o) {
080                if (o == entry) {
081                    if (endNode == null) {
082                        endNode = graph.add(new EndNode());
083                    }
084                    return endNode;
085                }
086                return o;
087            }
088        };
089    }
090
091    public FixedNode entryPoint() {
092        if (isDuplicate()) {
093            LoopBeginNode newLoopBegin = getDuplicatedNode(original().loop().loopBegin());
094            return newLoopBegin.forwardEnd();
095        }
096        return loop().entryPoint();
097    }
098
099    @Override
100    protected void finishDuplication() {
101        // TODO (gd) ?
102    }
103
104    @Override
105    public void insertBefore(LoopEx loop) {
106        // TODO Auto-generated method stub
107
108    }
109}