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  */
  23 package org.graalvm.compiler.core.test;
  24 
  25 import java.util.List;
  26 
  27 import org.junit.Test;
  28 
  29 import org.graalvm.compiler.core.common.cfg.BlockMap;
  30 import org.graalvm.compiler.debug.Debug;
  31 import org.graalvm.compiler.graph.Node;
  32 import org.graalvm.compiler.graph.NodeMap;
  33 import org.graalvm.compiler.nodes.BeginNode;
  34 import org.graalvm.compiler.nodes.DeoptimizingNode.DeoptDuring;
  35 import org.graalvm.compiler.nodes.FrameState;
  36 import org.graalvm.compiler.nodes.ReturnNode;
  37 import org.graalvm.compiler.nodes.StateSplit;
  38 import org.graalvm.compiler.nodes.StructuredGraph;
  39 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  40 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
  41 import org.graalvm.compiler.nodes.calc.AddNode;
  42 import org.graalvm.compiler.nodes.calc.BinaryArithmeticNode;
  43 import org.graalvm.compiler.nodes.cfg.Block;
  44 import org.graalvm.compiler.nodes.spi.LoweringTool;
  45 import org.graalvm.compiler.phases.OptimisticOptimizations;
  46 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  47 import org.graalvm.compiler.phases.common.FrameStateAssignmentPhase;
  48 import org.graalvm.compiler.phases.common.GuardLoweringPhase;
  49 import org.graalvm.compiler.phases.common.LoweringPhase;
  50 import org.graalvm.compiler.phases.schedule.SchedulePhase;
  51 import org.graalvm.compiler.phases.schedule.SchedulePhase.SchedulingStrategy;
  52 import org.graalvm.compiler.phases.tiers.MidTierContext;
  53 import org.graalvm.compiler.phases.tiers.PhaseContext;
  54 
  55 public 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 }