SchedulingTest2.java revision 12651:6ef01bd40ce2
1/*
2 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.core.test;
24
25import java.util.List;
26
27import org.junit.Test;
28
29import org.graalvm.compiler.core.common.cfg.BlockMap;
30import org.graalvm.compiler.debug.Debug;
31import org.graalvm.compiler.graph.Node;
32import org.graalvm.compiler.graph.NodeMap;
33import org.graalvm.compiler.nodes.BeginNode;
34import org.graalvm.compiler.nodes.DeoptimizingNode.DeoptDuring;
35import org.graalvm.compiler.nodes.FrameState;
36import org.graalvm.compiler.nodes.ReturnNode;
37import org.graalvm.compiler.nodes.StateSplit;
38import org.graalvm.compiler.nodes.StructuredGraph;
39import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
40import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
41import org.graalvm.compiler.nodes.calc.AddNode;
42import org.graalvm.compiler.nodes.calc.BinaryArithmeticNode;
43import org.graalvm.compiler.nodes.cfg.Block;
44import org.graalvm.compiler.nodes.spi.LoweringTool;
45import org.graalvm.compiler.phases.OptimisticOptimizations;
46import org.graalvm.compiler.phases.common.CanonicalizerPhase;
47import org.graalvm.compiler.phases.common.FrameStateAssignmentPhase;
48import org.graalvm.compiler.phases.common.GuardLoweringPhase;
49import org.graalvm.compiler.phases.common.LoweringPhase;
50import org.graalvm.compiler.phases.schedule.SchedulePhase;
51import org.graalvm.compiler.phases.schedule.SchedulePhase.SchedulingStrategy;
52import org.graalvm.compiler.phases.tiers.MidTierContext;
53import org.graalvm.compiler.phases.tiers.PhaseContext;
54
55public class SchedulingTest2 extends GraphScheduleTest {
56
57    public static int testSnippet() {
58        return test() + 2;
59    }
60
61    public static int test() {
62        return 40;
63    }
64
65    @Test
66    public void testValueProxyInputs() {
67        StructuredGraph graph = parseEager("testSnippet", AllowAssumptions.YES);
68        ReturnNode returnNode = graph.getNodes(ReturnNode.TYPE).first();
69        BeginNode beginNode = graph.add(new BeginNode());
70        returnNode.replaceAtPredecessor(beginNode);
71        beginNode.setNext(returnNode);
72        Debug.dump(Debug.BASIC_LOG_LEVEL, graph, "Graph");
73        SchedulePhase schedulePhase = new SchedulePhase(SchedulingStrategy.EARLIEST);
74        schedulePhase.apply(graph);
75        ScheduleResult schedule = graph.getLastSchedule();
76        BlockMap<List<Node>> blockToNodesMap = schedule.getBlockToNodesMap();
77        NodeMap<Block> nodeToBlock = schedule.getNodeToBlockMap();
78        assertDeepEquals(2, schedule.getCFG().getBlocks().length);
79        for (BinaryArithmeticNode<?> node : graph.getNodes().filter(BinaryArithmeticNode.class)) {
80            if (node instanceof AddNode) {
81                assertTrue(node.toString() + " expected: " + nodeToBlock.get(beginNode) + " but was: " + nodeToBlock.get(node), nodeToBlock.get(node) != nodeToBlock.get(beginNode));
82            }
83        }
84
85        for (FrameState fs : graph.getNodes(FrameState.TYPE)) {
86            Block block = nodeToBlock.get(fs);
87            assertTrue(fs.toString(), block == schedule.getCFG().getStartBlock());
88            for (Node usage : fs.usages()) {
89                if (usage instanceof StateSplit && ((StateSplit) usage).stateAfter() == fs) {
90                    assertTrue(usage.toString(), nodeToBlock.get(usage) == block);
91                    if (usage != block.getBeginNode()) {
92                        List<Node> map = blockToNodesMap.get(block);
93                        assertTrue(map.indexOf(fs) + " < " + map.indexOf(usage), map.indexOf(fs) < map.indexOf(usage));
94                    }
95                }
96            }
97        }
98
99        PhaseContext context = new PhaseContext(getProviders());
100        new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
101        new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, context);
102        MidTierContext midContext = new MidTierContext(getProviders(), getTargetProvider(), OptimisticOptimizations.ALL, graph.getProfilingInfo());
103
104        new GuardLoweringPhase().apply(graph, midContext);
105        FrameStateAssignmentPhase phase = new FrameStateAssignmentPhase();
106        phase.apply(graph);
107
108        schedulePhase.apply(graph);
109        schedule = graph.getLastSchedule();
110        blockToNodesMap = schedule.getBlockToNodesMap();
111        nodeToBlock = schedule.getNodeToBlockMap();
112        for (FrameState fs : graph.getNodes(FrameState.TYPE)) {
113            Block block = nodeToBlock.get(fs);
114            assertTrue(fs.toString(), block == schedule.getCFG().getStartBlock());
115            for (Node usage : fs.usages()) {
116                if ((usage instanceof StateSplit && ((StateSplit) usage).stateAfter() == fs) || (usage instanceof DeoptDuring && ((DeoptDuring) usage).stateDuring() == fs)) {
117                    assertTrue(usage.toString(), nodeToBlock.get(usage) == block);
118                    if (usage != block.getBeginNode()) {
119                        List<Node> map = blockToNodesMap.get(block);
120                        assertTrue(map.indexOf(fs) + " < " + map.indexOf(usage), map.indexOf(fs) < map.indexOf(usage));
121                    }
122                }
123            }
124        }
125    }
126}
127