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.compiler.test;
024
025import java.util.*;
026
027import com.oracle.graal.debug.*;
028
029import org.junit.*;
030
031import com.oracle.graal.compiler.common.cfg.*;
032import com.oracle.graal.graph.*;
033import com.oracle.graal.nodes.*;
034import com.oracle.graal.nodes.DeoptimizingNode.DeoptDuring;
035import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
036import com.oracle.graal.nodes.calc.*;
037import com.oracle.graal.nodes.cfg.*;
038import com.oracle.graal.nodes.spi.*;
039import com.oracle.graal.phases.*;
040import com.oracle.graal.phases.common.*;
041import com.oracle.graal.phases.schedule.*;
042import com.oracle.graal.phases.schedule.SchedulePhase.SchedulingStrategy;
043import com.oracle.graal.phases.tiers.*;
044
045public class SchedulingTest2 extends GraphScheduleTest {
046
047    public static int testSnippet() {
048        return test() + 2;
049    }
050
051    public static int test() {
052        return 40;
053    }
054
055    @Test
056    public void testValueProxyInputs() {
057        StructuredGraph graph = parseEager("testSnippet", AllowAssumptions.YES);
058        ReturnNode returnNode = graph.getNodes(ReturnNode.TYPE).first();
059        BeginNode beginNode = graph.add(new BeginNode());
060        returnNode.replaceAtPredecessor(beginNode);
061        beginNode.setNext(returnNode);
062        Debug.dump(graph, "Graph");
063        SchedulePhase schedule = new SchedulePhase(SchedulingStrategy.EARLIEST);
064        schedule.apply(graph);
065        BlockMap<List<Node>> blockToNodesMap = schedule.getBlockToNodesMap();
066        NodeMap<Block> nodeToBlock = schedule.getNodeToBlockMap();
067        assertDeepEquals(2, schedule.getCFG().getBlocks().size());
068        for (BinaryArithmeticNode<?> node : graph.getNodes().filter(BinaryArithmeticNode.class)) {
069            if (node instanceof AddNode) {
070                assertTrue(node.toString() + " expected: " + nodeToBlock.get(beginNode) + " but was: " + nodeToBlock.get(node), nodeToBlock.get(node) != nodeToBlock.get(beginNode));
071            }
072        }
073
074        for (FrameState fs : graph.getNodes(FrameState.TYPE)) {
075            Block block = nodeToBlock.get(fs);
076            assertTrue(fs.toString(), block == schedule.getCFG().getStartBlock());
077            for (Node usage : fs.usages()) {
078                if (usage instanceof StateSplit && ((StateSplit) usage).stateAfter() == fs) {
079                    assertTrue(usage.toString(), nodeToBlock.get(usage) == block);
080                    if (usage != block.getBeginNode()) {
081                        List<Node> map = blockToNodesMap.get(block);
082                        assertTrue(map.indexOf(fs) + " < " + map.indexOf(usage), map.indexOf(fs) < map.indexOf(usage));
083                    }
084                }
085            }
086        }
087
088        PhaseContext context = new PhaseContext(getProviders());
089        new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
090        new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, context);
091        MidTierContext midContext = new MidTierContext(getProviders(), getCodeCache().getTarget(), OptimisticOptimizations.ALL, graph.method().getProfilingInfo());
092
093        new GuardLoweringPhase().apply(graph, midContext);
094        FrameStateAssignmentPhase phase = new FrameStateAssignmentPhase();
095        phase.apply(graph);
096
097        schedule = new SchedulePhase(SchedulingStrategy.EARLIEST);
098        schedule.apply(graph);
099        blockToNodesMap = schedule.getBlockToNodesMap();
100        nodeToBlock = schedule.getNodeToBlockMap();
101        for (FrameState fs : graph.getNodes(FrameState.TYPE)) {
102            Block block = nodeToBlock.get(fs);
103            assertTrue(fs.toString(), block == schedule.getCFG().getStartBlock());
104            for (Node usage : fs.usages()) {
105                if ((usage instanceof StateSplit && ((StateSplit) usage).stateAfter() == fs) || (usage instanceof DeoptDuring && ((DeoptDuring) usage).stateDuring() == fs)) {
106                    assertTrue(usage.toString(), nodeToBlock.get(usage) == block);
107                    if (usage != block.getBeginNode()) {
108                        List<Node> map = blockToNodesMap.get(block);
109                        assertTrue(map.indexOf(fs) + " < " + map.indexOf(usage), map.indexOf(fs) < map.indexOf(usage));
110                    }
111                }
112            }
113        }
114    }
115}